blob: 4185b2f2c588c8c56faca649c1b458a48006fefd [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 {
Roland Levillaine53bd812016-02-24 14:54:18 +00002834 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2835 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002836 }
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;
Roland Levillain9867bc72015-08-05 10:21:34 +01002839 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2840 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002841 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2842 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002843 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002844 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2845 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002846
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002847 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002848 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002849 HConstant* GetConstantRight() const;
2850
2851 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002852 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002853 HInstruction* GetLeastConstantLeft() const;
2854
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002855 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002856
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002857 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002858 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2859};
2860
Mark Mendellc4701932015-04-10 13:18:51 -04002861// The comparison bias applies for floating point operations and indicates how NaN
2862// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002863enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002864 kNoBias, // bias is not applicable (i.e. for long operation)
2865 kGtBias, // return 1 for NaN comparisons
2866 kLtBias, // return -1 for NaN comparisons
2867};
2868
Roland Levillain31dd3d62016-02-16 12:21:02 +00002869std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2870
Dave Allison20dfc792014-06-16 20:44:29 -07002871class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002872 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002873 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2874 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002875 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002876
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002877 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002878 // `instruction`, and disregard moves in between.
2879 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002880
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002881 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002882
2883 virtual IfCondition GetCondition() const = 0;
2884
Mark Mendellc4701932015-04-10 13:18:51 -04002885 virtual IfCondition GetOppositeCondition() const = 0;
2886
Roland Levillain4fa13f62015-07-06 18:11:54 +01002887 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002888 ComparisonBias GetBias() const { return bias_; }
Mark Mendellc4701932015-04-10 13:18:51 -04002889 void SetBias(ComparisonBias bias) { bias_ = bias; }
2890
2891 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2892 return bias_ == other->AsCondition()->bias_;
2893 }
2894
Roland Levillain4fa13f62015-07-06 18:11:54 +01002895 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002896 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002897 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002898 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002899 }
2900
2901 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002902 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002903 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002904 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002905 }
2906
Roland Levillain31dd3d62016-02-16 12:21:02 +00002907 protected:
2908 template <typename T>
2909 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
2910
2911 template <typename T>
2912 int32_t CompareFP(T x, T y) const {
2913 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
2914 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
2915 // Handle the bias.
2916 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
2917 }
2918
2919 // Return an integer constant containing the result of a condition evaluated at compile time.
2920 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
2921 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
2922 }
2923
Dave Allison20dfc792014-06-16 20:44:29 -07002924 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002925 // Needed if we merge a HCompare into a HCondition.
2926 ComparisonBias bias_;
2927
Dave Allison20dfc792014-06-16 20:44:29 -07002928 DISALLOW_COPY_AND_ASSIGN(HCondition);
2929};
2930
2931// Instruction to check if two inputs are equal to each other.
2932class HEqual : public HCondition {
2933 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002934 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2935 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002936
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002937 bool IsCommutative() const OVERRIDE { return true; }
2938
Vladimir Marko9e23df52015-11-10 17:14:35 +00002939 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2940 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002941 return MakeConstantCondition(true, GetDexPc());
2942 }
2943 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2944 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
2945 }
2946 // In the following Evaluate methods, a HCompare instruction has
2947 // been merged into this HEqual instruction; evaluate it as
2948 // `Compare(x, y) == 0`.
2949 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2950 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
2951 GetDexPc());
2952 }
2953 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
2954 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
2955 }
2956 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
2957 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00002958 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002959
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002960 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002961
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002962 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002963 return kCondEQ;
2964 }
2965
Mark Mendellc4701932015-04-10 13:18:51 -04002966 IfCondition GetOppositeCondition() const OVERRIDE {
2967 return kCondNE;
2968 }
2969
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002970 private:
Aart Bike9f37602015-10-09 11:15:55 -07002971 template <typename T> bool Compute(T x, T y) const { return x == y; }
2972
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002973 DISALLOW_COPY_AND_ASSIGN(HEqual);
2974};
2975
Dave Allison20dfc792014-06-16 20:44:29 -07002976class HNotEqual : public HCondition {
2977 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002978 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2979 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002980
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002981 bool IsCommutative() const OVERRIDE { return true; }
2982
Vladimir Marko9e23df52015-11-10 17:14:35 +00002983 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2984 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002985 return MakeConstantCondition(false, GetDexPc());
2986 }
2987 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2988 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
2989 }
2990 // In the following Evaluate methods, a HCompare instruction has
2991 // been merged into this HNotEqual instruction; evaluate it as
2992 // `Compare(x, y) != 0`.
2993 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2994 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
2995 }
2996 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
2997 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
2998 }
2999 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3000 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003001 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003002
Dave Allison20dfc792014-06-16 20:44:29 -07003003 DECLARE_INSTRUCTION(NotEqual);
3004
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003005 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003006 return kCondNE;
3007 }
3008
Mark Mendellc4701932015-04-10 13:18:51 -04003009 IfCondition GetOppositeCondition() const OVERRIDE {
3010 return kCondEQ;
3011 }
3012
Dave Allison20dfc792014-06-16 20:44:29 -07003013 private:
Aart Bike9f37602015-10-09 11:15:55 -07003014 template <typename T> bool Compute(T x, T y) const { return x != y; }
3015
Dave Allison20dfc792014-06-16 20:44:29 -07003016 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3017};
3018
3019class HLessThan : public HCondition {
3020 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003021 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3022 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003023
Roland Levillain9867bc72015-08-05 10:21:34 +01003024 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003025 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003026 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 // In the following Evaluate methods, a HCompare instruction has
3028 // been merged into this HLessThan instruction; evaluate it as
3029 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003030 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003031 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3032 }
3033 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3034 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3035 }
3036 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3037 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003038 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003039
Dave Allison20dfc792014-06-16 20:44:29 -07003040 DECLARE_INSTRUCTION(LessThan);
3041
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003042 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003043 return kCondLT;
3044 }
3045
Mark Mendellc4701932015-04-10 13:18:51 -04003046 IfCondition GetOppositeCondition() const OVERRIDE {
3047 return kCondGE;
3048 }
3049
Dave Allison20dfc792014-06-16 20:44:29 -07003050 private:
Aart Bike9f37602015-10-09 11:15:55 -07003051 template <typename T> bool Compute(T x, T y) const { return x < y; }
3052
Dave Allison20dfc792014-06-16 20:44:29 -07003053 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3054};
3055
3056class HLessThanOrEqual : public HCondition {
3057 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003058 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3059 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003060
Roland Levillain9867bc72015-08-05 10:21:34 +01003061 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003062 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003063 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003064 // In the following Evaluate methods, a HCompare instruction has
3065 // been merged into this HLessThanOrEqual instruction; evaluate it as
3066 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003067 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3069 }
3070 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3071 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3072 }
3073 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3074 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003075 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003076
Dave Allison20dfc792014-06-16 20:44:29 -07003077 DECLARE_INSTRUCTION(LessThanOrEqual);
3078
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003079 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003080 return kCondLE;
3081 }
3082
Mark Mendellc4701932015-04-10 13:18:51 -04003083 IfCondition GetOppositeCondition() const OVERRIDE {
3084 return kCondGT;
3085 }
3086
Dave Allison20dfc792014-06-16 20:44:29 -07003087 private:
Aart Bike9f37602015-10-09 11:15:55 -07003088 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3089
Dave Allison20dfc792014-06-16 20:44:29 -07003090 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3091};
3092
3093class HGreaterThan : public HCondition {
3094 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003095 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3096 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003097
Roland Levillain9867bc72015-08-05 10:21:34 +01003098 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003099 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003100 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003101 // In the following Evaluate methods, a HCompare instruction has
3102 // been merged into this HGreaterThan instruction; evaluate it as
3103 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003104 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3106 }
3107 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3108 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3109 }
3110 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3111 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003112 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003113
Dave Allison20dfc792014-06-16 20:44:29 -07003114 DECLARE_INSTRUCTION(GreaterThan);
3115
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003116 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003117 return kCondGT;
3118 }
3119
Mark Mendellc4701932015-04-10 13:18:51 -04003120 IfCondition GetOppositeCondition() const OVERRIDE {
3121 return kCondLE;
3122 }
3123
Dave Allison20dfc792014-06-16 20:44:29 -07003124 private:
Aart Bike9f37602015-10-09 11:15:55 -07003125 template <typename T> bool Compute(T x, T y) const { return x > y; }
3126
Dave Allison20dfc792014-06-16 20:44:29 -07003127 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3128};
3129
3130class HGreaterThanOrEqual : public HCondition {
3131 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003132 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3133 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003134
Roland Levillain9867bc72015-08-05 10:21:34 +01003135 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003136 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003137 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003138 // In the following Evaluate methods, a HCompare instruction has
3139 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3140 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003141 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003142 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3143 }
3144 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3145 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3146 }
3147 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3148 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003149 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003150
Dave Allison20dfc792014-06-16 20:44:29 -07003151 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3152
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003153 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003154 return kCondGE;
3155 }
3156
Mark Mendellc4701932015-04-10 13:18:51 -04003157 IfCondition GetOppositeCondition() const OVERRIDE {
3158 return kCondLT;
3159 }
3160
Dave Allison20dfc792014-06-16 20:44:29 -07003161 private:
Aart Bike9f37602015-10-09 11:15:55 -07003162 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3163
Dave Allison20dfc792014-06-16 20:44:29 -07003164 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3165};
3166
Aart Bike9f37602015-10-09 11:15:55 -07003167class HBelow : public HCondition {
3168 public:
3169 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3170 : HCondition(first, second, dex_pc) {}
3171
3172 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003173 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003174 }
3175 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003176 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3177 }
3178 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3179 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3180 LOG(FATAL) << DebugName() << " is not defined for float values";
3181 UNREACHABLE();
3182 }
3183 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3184 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3185 LOG(FATAL) << DebugName() << " is not defined for double values";
3186 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003187 }
3188
3189 DECLARE_INSTRUCTION(Below);
3190
3191 IfCondition GetCondition() const OVERRIDE {
3192 return kCondB;
3193 }
3194
3195 IfCondition GetOppositeCondition() const OVERRIDE {
3196 return kCondAE;
3197 }
3198
3199 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003200 template <typename T> bool Compute(T x, T y) const {
3201 return MakeUnsigned(x) < MakeUnsigned(y);
3202 }
Aart Bike9f37602015-10-09 11:15:55 -07003203
3204 DISALLOW_COPY_AND_ASSIGN(HBelow);
3205};
3206
3207class HBelowOrEqual : public HCondition {
3208 public:
3209 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3210 : HCondition(first, second, dex_pc) {}
3211
3212 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003213 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003214 }
3215 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003216 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3217 }
3218 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3219 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3220 LOG(FATAL) << DebugName() << " is not defined for float values";
3221 UNREACHABLE();
3222 }
3223 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3224 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3225 LOG(FATAL) << DebugName() << " is not defined for double values";
3226 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003227 }
3228
3229 DECLARE_INSTRUCTION(BelowOrEqual);
3230
3231 IfCondition GetCondition() const OVERRIDE {
3232 return kCondBE;
3233 }
3234
3235 IfCondition GetOppositeCondition() const OVERRIDE {
3236 return kCondA;
3237 }
3238
3239 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003240 template <typename T> bool Compute(T x, T y) const {
3241 return MakeUnsigned(x) <= MakeUnsigned(y);
3242 }
Aart Bike9f37602015-10-09 11:15:55 -07003243
3244 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3245};
3246
3247class HAbove : public HCondition {
3248 public:
3249 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3250 : HCondition(first, second, dex_pc) {}
3251
3252 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003253 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003254 }
3255 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003256 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3257 }
3258 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3259 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3260 LOG(FATAL) << DebugName() << " is not defined for float values";
3261 UNREACHABLE();
3262 }
3263 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3264 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3265 LOG(FATAL) << DebugName() << " is not defined for double values";
3266 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003267 }
3268
3269 DECLARE_INSTRUCTION(Above);
3270
3271 IfCondition GetCondition() const OVERRIDE {
3272 return kCondA;
3273 }
3274
3275 IfCondition GetOppositeCondition() const OVERRIDE {
3276 return kCondBE;
3277 }
3278
3279 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003280 template <typename T> bool Compute(T x, T y) const {
3281 return MakeUnsigned(x) > MakeUnsigned(y);
3282 }
Aart Bike9f37602015-10-09 11:15:55 -07003283
3284 DISALLOW_COPY_AND_ASSIGN(HAbove);
3285};
3286
3287class HAboveOrEqual : public HCondition {
3288 public:
3289 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3290 : HCondition(first, second, dex_pc) {}
3291
3292 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003293 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003294 }
3295 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003296 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3297 }
3298 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3299 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3300 LOG(FATAL) << DebugName() << " is not defined for float values";
3301 UNREACHABLE();
3302 }
3303 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3304 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3305 LOG(FATAL) << DebugName() << " is not defined for double values";
3306 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003307 }
3308
3309 DECLARE_INSTRUCTION(AboveOrEqual);
3310
3311 IfCondition GetCondition() const OVERRIDE {
3312 return kCondAE;
3313 }
3314
3315 IfCondition GetOppositeCondition() const OVERRIDE {
3316 return kCondB;
3317 }
3318
3319 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003320 template <typename T> bool Compute(T x, T y) const {
3321 return MakeUnsigned(x) >= MakeUnsigned(y);
3322 }
Aart Bike9f37602015-10-09 11:15:55 -07003323
3324 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3325};
Dave Allison20dfc792014-06-16 20:44:29 -07003326
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003327// Instruction to check how two inputs compare to each other.
3328// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3329class HCompare : public HBinaryOperation {
3330 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003331 HCompare(Primitive::Type type,
3332 HInstruction* first,
3333 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003334 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003335 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003336 : HBinaryOperation(Primitive::kPrimInt,
3337 first,
3338 second,
3339 SideEffectsForArchRuntimeCalls(type),
3340 dex_pc),
3341 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003342 DCHECK_EQ(type, first->GetType());
3343 DCHECK_EQ(type, second->GetType());
3344 }
3345
Roland Levillain9867bc72015-08-05 10:21:34 +01003346 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003347 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3348
3349 template <typename T>
3350 int32_t ComputeFP(T x, T y) const {
3351 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3352 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3353 // Handle the bias.
3354 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3355 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003356
Roland Levillain9867bc72015-08-05 10:21:34 +01003357 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003358 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3359 // reach this code path when processing a freshly built HIR
3360 // graph. However HCompare integer instructions can be synthesized
3361 // by the instruction simplifier to implement IntegerCompare and
3362 // IntegerSignum intrinsics, so we have to handle this case.
3363 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003364 }
3365 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003366 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3367 }
3368 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3369 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3370 }
3371 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3372 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003373 }
3374
Calin Juravleddb7df22014-11-25 20:56:51 +00003375 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3376 return bias_ == other->AsCompare()->bias_;
3377 }
3378
Mark Mendellc4701932015-04-10 13:18:51 -04003379 ComparisonBias GetBias() const { return bias_; }
3380
Roland Levillain31dd3d62016-02-16 12:21:02 +00003381 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
3382 // Only meaninfgul for floating-point comparisons.
3383 bool IsGtBias() const {
3384 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3385 return bias_ == ComparisonBias::kGtBias;
3386 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003387
3388 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3389 // MIPS64 uses a runtime call for FP comparisons.
3390 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3391 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003392
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003393 DECLARE_INSTRUCTION(Compare);
3394
Roland Levillain31dd3d62016-02-16 12:21:02 +00003395 protected:
3396 // Return an integer constant containing the result of a comparison evaluated at compile time.
3397 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3398 DCHECK(value == -1 || value == 0 || value == 1) << value;
3399 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3400 }
3401
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003402 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003403 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003404
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003405 DISALLOW_COPY_AND_ASSIGN(HCompare);
3406};
3407
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003408// A local in the graph. Corresponds to a Dex register.
3409class HLocal : public HTemplateInstruction<0> {
3410 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003411 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003412 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003413
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003414 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003415
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003416 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003417
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003418 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003419 // The Dex register number.
3420 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003421
3422 DISALLOW_COPY_AND_ASSIGN(HLocal);
3423};
3424
3425// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003426class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003427 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003428 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3429 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003430 SetRawInputAt(0, local);
3431 }
3432
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003433 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3434
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003435 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003436
3437 private:
3438 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3439};
3440
3441// Store a value in a given local. This instruction has two inputs: the value
3442// and the local.
3443class HStoreLocal : public HTemplateInstruction<2> {
3444 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003445 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3446 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003447 SetRawInputAt(0, local);
3448 SetRawInputAt(1, value);
3449 }
3450
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003451 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3452
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003453 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003454
3455 private:
3456 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3457};
3458
David Brazdil6de19382016-01-08 17:37:10 +00003459class HNewInstance : public HExpression<2> {
3460 public:
3461 HNewInstance(HInstruction* cls,
3462 HCurrentMethod* current_method,
3463 uint32_t dex_pc,
3464 uint16_t type_index,
3465 const DexFile& dex_file,
3466 bool can_throw,
3467 bool finalizable,
3468 QuickEntrypointEnum entrypoint)
3469 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3470 type_index_(type_index),
3471 dex_file_(dex_file),
3472 can_throw_(can_throw),
3473 finalizable_(finalizable),
3474 entrypoint_(entrypoint) {
3475 SetRawInputAt(0, cls);
3476 SetRawInputAt(1, current_method);
3477 }
3478
3479 uint16_t GetTypeIndex() const { return type_index_; }
3480 const DexFile& GetDexFile() const { return dex_file_; }
3481
3482 // Calls runtime so needs an environment.
3483 bool NeedsEnvironment() const OVERRIDE { return true; }
3484
3485 // It may throw when called on type that's not instantiable/accessible.
3486 // It can throw OOME.
3487 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3488 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3489
3490 bool IsFinalizable() const { return finalizable_; }
3491
3492 bool CanBeNull() const OVERRIDE { return false; }
3493
3494 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3495
3496 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3497 entrypoint_ = entrypoint;
3498 }
3499
3500 bool IsStringAlloc() const;
3501
3502 DECLARE_INSTRUCTION(NewInstance);
3503
3504 private:
3505 const uint16_t type_index_;
3506 const DexFile& dex_file_;
3507 const bool can_throw_;
3508 const bool finalizable_;
3509 QuickEntrypointEnum entrypoint_;
3510
3511 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3512};
3513
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003514enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003515#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3516 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003517#include "intrinsics_list.h"
3518 kNone,
3519 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3520#undef INTRINSICS_LIST
3521#undef OPTIMIZING_INTRINSICS
3522};
3523std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3524
Agi Csaki05f20562015-08-19 14:58:14 -07003525enum IntrinsicNeedsEnvironmentOrCache {
3526 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3527 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003528};
3529
Aart Bik5d75afe2015-12-14 11:57:01 -08003530enum IntrinsicSideEffects {
3531 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3532 kReadSideEffects, // Intrinsic may read heap memory.
3533 kWriteSideEffects, // Intrinsic may write heap memory.
3534 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3535};
3536
3537enum IntrinsicExceptions {
3538 kNoThrow, // Intrinsic does not throw any exceptions.
3539 kCanThrow // Intrinsic may throw exceptions.
3540};
3541
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003542class HInvoke : public HInstruction {
3543 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003544 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003545
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003546 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003547
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003548 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003549 SetRawInputAt(index, argument);
3550 }
3551
Roland Levillain3e3d7332015-04-28 11:00:54 +01003552 // Return the number of arguments. This number can be lower than
3553 // the number of inputs returned by InputCount(), as some invoke
3554 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3555 // inputs at the end of their list of inputs.
3556 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3557
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003558 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003559
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003560 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003561 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003562
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003563 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3564
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003565 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003566 return intrinsic_;
3567 }
3568
Aart Bik5d75afe2015-12-14 11:57:01 -08003569 void SetIntrinsic(Intrinsics intrinsic,
3570 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3571 IntrinsicSideEffects side_effects,
3572 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003573
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003574 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003575 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003576 }
3577
Aart Bik5d75afe2015-12-14 11:57:01 -08003578 bool CanThrow() const OVERRIDE { return can_throw_; }
3579
3580 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3581
3582 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3583 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3584 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003585
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003586 uint32_t* GetIntrinsicOptimizations() {
3587 return &intrinsic_optimizations_;
3588 }
3589
3590 const uint32_t* GetIntrinsicOptimizations() const {
3591 return &intrinsic_optimizations_;
3592 }
3593
3594 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3595
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003596 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003597
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003598 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003599 HInvoke(ArenaAllocator* arena,
3600 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003601 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003602 Primitive::Type return_type,
3603 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003604 uint32_t dex_method_index,
3605 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003606 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003607 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003608 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003609 inputs_(number_of_arguments + number_of_other_inputs,
3610 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003611 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003612 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003613 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003614 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003615 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003616 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003617 }
3618
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003619 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003620 return inputs_[index];
3621 }
3622
David Brazdil1abb4192015-02-17 18:33:36 +00003623 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003624 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003625 }
3626
Aart Bik5d75afe2015-12-14 11:57:01 -08003627 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3628
Roland Levillain3e3d7332015-04-28 11:00:54 +01003629 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003630 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003631 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003632 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003633 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003634 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003635 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003636
3637 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3638 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003639
3640 private:
3641 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3642};
3643
Calin Juravle175dc732015-08-25 15:42:32 +01003644class HInvokeUnresolved : public HInvoke {
3645 public:
3646 HInvokeUnresolved(ArenaAllocator* arena,
3647 uint32_t number_of_arguments,
3648 Primitive::Type return_type,
3649 uint32_t dex_pc,
3650 uint32_t dex_method_index,
3651 InvokeType invoke_type)
3652 : HInvoke(arena,
3653 number_of_arguments,
3654 0u /* number_of_other_inputs */,
3655 return_type,
3656 dex_pc,
3657 dex_method_index,
3658 invoke_type) {
3659 }
3660
3661 DECLARE_INSTRUCTION(InvokeUnresolved);
3662
3663 private:
3664 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3665};
3666
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003667class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003668 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003669 // Requirements of this method call regarding the class
3670 // initialization (clinit) check of its declaring class.
3671 enum class ClinitCheckRequirement {
3672 kNone, // Class already initialized.
3673 kExplicit, // Static call having explicit clinit check as last input.
3674 kImplicit, // Static call implicitly requiring a clinit check.
3675 };
3676
Vladimir Marko58155012015-08-19 12:49:41 +00003677 // Determines how to load the target ArtMethod*.
3678 enum class MethodLoadKind {
3679 // Use a String init ArtMethod* loaded from Thread entrypoints.
3680 kStringInit,
3681
3682 // Use the method's own ArtMethod* loaded by the register allocator.
3683 kRecursive,
3684
3685 // Use ArtMethod* at a known address, embed the direct address in the code.
3686 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3687 kDirectAddress,
3688
3689 // Use ArtMethod* at an address that will be known at link time, embed the direct
3690 // address in the code. If the image is relocatable, emit .patch_oat entry.
3691 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3692 // the image relocatable or not.
3693 kDirectAddressWithFixup,
3694
3695 // Load from resoved methods array in the dex cache using a PC-relative load.
3696 // Used when we need to use the dex cache, for example for invoke-static that
3697 // may cause class initialization (the entry may point to a resolution method),
3698 // and we know that we can access the dex cache arrays using a PC-relative load.
3699 kDexCachePcRelative,
3700
3701 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3702 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3703 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3704 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3705 kDexCacheViaMethod,
3706 };
3707
3708 // Determines the location of the code pointer.
3709 enum class CodePtrLocation {
3710 // Recursive call, use local PC-relative call instruction.
3711 kCallSelf,
3712
3713 // Use PC-relative call instruction patched at link time.
3714 // Used for calls within an oat file, boot->boot or app->app.
3715 kCallPCRelative,
3716
3717 // Call to a known target address, embed the direct address in code.
3718 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3719 kCallDirect,
3720
3721 // Call to a target address that will be known at link time, embed the direct
3722 // address in code. If the image is relocatable, emit .patch_oat entry.
3723 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3724 // the image relocatable or not.
3725 kCallDirectWithFixup,
3726
3727 // Use code pointer from the ArtMethod*.
3728 // Used when we don't know the target code. This is also the last-resort-kind used when
3729 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3730 kCallArtMethod,
3731 };
3732
3733 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003734 MethodLoadKind method_load_kind;
3735 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003736 // The method load data holds
3737 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3738 // Note that there are multiple string init methods, each having its own offset.
3739 // - the method address for kDirectAddress
3740 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003741 uint64_t method_load_data;
3742 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003743 };
3744
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003745 HInvokeStaticOrDirect(ArenaAllocator* arena,
3746 uint32_t number_of_arguments,
3747 Primitive::Type return_type,
3748 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003749 uint32_t method_index,
3750 MethodReference target_method,
3751 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003752 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003753 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003754 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003755 : HInvoke(arena,
3756 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003757 // There is potentially one extra argument for the HCurrentMethod node, and
3758 // potentially one other if the clinit check is explicit, and potentially
3759 // one other if the method is a string factory.
3760 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3761 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3762 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003763 return_type,
3764 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003765 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003766 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003767 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003768 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003769 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003770 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003771
Vladimir Markodc151b22015-10-15 18:02:30 +01003772 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003773 bool had_current_method_input = HasCurrentMethodInput();
3774 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3775
3776 // Using the current method is the default and once we find a better
3777 // method load kind, we should not go back to using the current method.
3778 DCHECK(had_current_method_input || !needs_current_method_input);
3779
3780 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003781 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3782 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003783 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003784 dispatch_info_ = dispatch_info;
3785 }
3786
Vladimir Markoc53c0792015-11-19 15:48:33 +00003787 void AddSpecialInput(HInstruction* input) {
3788 // We allow only one special input.
3789 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3790 DCHECK(InputCount() == GetSpecialInputIndex() ||
3791 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3792 InsertInputAt(GetSpecialInputIndex(), input);
3793 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003794
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003795 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003796 // We access the method via the dex cache so we can't do an implicit null check.
3797 // TODO: for intrinsics we can generate implicit null checks.
3798 return false;
3799 }
3800
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003801 bool CanBeNull() const OVERRIDE {
3802 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3803 }
3804
Vladimir Markoc53c0792015-11-19 15:48:33 +00003805 // Get the index of the special input, if any.
3806 //
David Brazdil6de19382016-01-08 17:37:10 +00003807 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3808 // method pointer; otherwise there may be one platform-specific special input,
3809 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003810 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003811 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003812
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003813 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3814 void SetOptimizedInvokeType(InvokeType invoke_type) {
3815 optimized_invoke_type_ = invoke_type;
3816 }
3817
Vladimir Marko58155012015-08-19 12:49:41 +00003818 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3819 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3820 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003821 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003822 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003823 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003824 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003825 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3826 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003827 bool HasCurrentMethodInput() const {
3828 // This function can be called only after the invoke has been fully initialized by the builder.
3829 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003830 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003831 return true;
3832 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003833 DCHECK(InputCount() == GetSpecialInputIndex() ||
3834 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003835 return false;
3836 }
3837 }
Vladimir Marko58155012015-08-19 12:49:41 +00003838 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3839 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003840 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003841
3842 int32_t GetStringInitOffset() const {
3843 DCHECK(IsStringInit());
3844 return dispatch_info_.method_load_data;
3845 }
3846
3847 uint64_t GetMethodAddress() const {
3848 DCHECK(HasMethodAddress());
3849 return dispatch_info_.method_load_data;
3850 }
3851
3852 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003853 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003854 return dispatch_info_.method_load_data;
3855 }
3856
3857 uint64_t GetDirectCodePtr() const {
3858 DCHECK(HasDirectCodePtr());
3859 return dispatch_info_.direct_code_ptr;
3860 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003861
Calin Juravle68ad6492015-08-18 17:08:12 +01003862 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3863
Roland Levillain4c0eb422015-04-24 16:43:49 +01003864 // Is this instruction a call to a static method?
3865 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003866 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003867 }
3868
Vladimir Markofbb184a2015-11-13 14:47:00 +00003869 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3870 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3871 // instruction; only relevant for static calls with explicit clinit check.
3872 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003873 DCHECK(IsStaticWithExplicitClinitCheck());
3874 size_t last_input_index = InputCount() - 1;
3875 HInstruction* last_input = InputAt(last_input_index);
3876 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003877 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003878 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003879 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003880 clinit_check_requirement_ = new_requirement;
3881 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003882 }
3883
David Brazdilbc9ab162016-01-20 14:50:19 +00003884 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003885 DCHECK(IsStringInit());
3886 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003887 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003888 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003889 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003890 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003891 }
3892
Roland Levillain4c0eb422015-04-24 16:43:49 +01003893 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003894 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003895 bool IsStaticWithExplicitClinitCheck() const {
3896 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3897 }
3898
3899 // Is this a call to a static method whose declaring class has an
3900 // implicit intialization check requirement?
3901 bool IsStaticWithImplicitClinitCheck() const {
3902 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3903 }
3904
Vladimir Markob554b5a2015-11-06 12:57:55 +00003905 // Does this method load kind need the current method as an input?
3906 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3907 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3908 }
3909
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003910 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003911
Roland Levillain4c0eb422015-04-24 16:43:49 +01003912 protected:
3913 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3914 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3915 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3916 HInstruction* input = input_record.GetInstruction();
3917 // `input` is the last input of a static invoke marked as having
3918 // an explicit clinit check. It must either be:
3919 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3920 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3921 DCHECK(input != nullptr);
3922 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3923 }
3924 return input_record;
3925 }
3926
Vladimir Markoc53c0792015-11-19 15:48:33 +00003927 void InsertInputAt(size_t index, HInstruction* input);
3928 void RemoveInputAt(size_t index);
3929
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003930 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003931 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003932 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003933 // The target method may refer to different dex file or method index than the original
3934 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3935 // in derived class to increase visibility.
3936 MethodReference target_method_;
3937 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003938
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003939 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003940};
Vladimir Markof64242a2015-12-01 14:58:23 +00003941std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003942std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003943
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003944class HInvokeVirtual : public HInvoke {
3945 public:
3946 HInvokeVirtual(ArenaAllocator* arena,
3947 uint32_t number_of_arguments,
3948 Primitive::Type return_type,
3949 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003950 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003951 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003952 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003953 vtable_index_(vtable_index) {}
3954
Calin Juravle641547a2015-04-21 22:08:51 +01003955 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003956 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003957 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003958 }
3959
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003960 uint32_t GetVTableIndex() const { return vtable_index_; }
3961
3962 DECLARE_INSTRUCTION(InvokeVirtual);
3963
3964 private:
3965 const uint32_t vtable_index_;
3966
3967 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3968};
3969
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003970class HInvokeInterface : public HInvoke {
3971 public:
3972 HInvokeInterface(ArenaAllocator* arena,
3973 uint32_t number_of_arguments,
3974 Primitive::Type return_type,
3975 uint32_t dex_pc,
3976 uint32_t dex_method_index,
3977 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003978 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003979 imt_index_(imt_index) {}
3980
Calin Juravle641547a2015-04-21 22:08:51 +01003981 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003982 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003983 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003984 }
3985
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003986 uint32_t GetImtIndex() const { return imt_index_; }
3987 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3988
3989 DECLARE_INSTRUCTION(InvokeInterface);
3990
3991 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003992 const uint32_t imt_index_;
3993
3994 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3995};
3996
Roland Levillain88cb1752014-10-20 16:36:47 +01003997class HNeg : public HUnaryOperation {
3998 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003999 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4000 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004001
Roland Levillain9867bc72015-08-05 10:21:34 +01004002 template <typename T> T Compute(T x) const { return -x; }
4003
4004 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004005 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004006 }
4007 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004008 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004009 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004010 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4011 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4012 }
4013 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4014 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4015 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004016
Roland Levillain88cb1752014-10-20 16:36:47 +01004017 DECLARE_INSTRUCTION(Neg);
4018
4019 private:
4020 DISALLOW_COPY_AND_ASSIGN(HNeg);
4021};
4022
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004023class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004024 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004025 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004026 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004027 uint32_t dex_pc,
4028 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004029 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004030 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004031 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004032 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004033 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004034 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004035 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004036 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004037 }
4038
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004039 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004040 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004041
4042 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004043 bool NeedsEnvironment() const OVERRIDE { return true; }
4044
Mingyao Yang0c365e62015-03-31 15:09:29 -07004045 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4046 bool CanThrow() const OVERRIDE { return true; }
4047
Calin Juravle10e244f2015-01-26 18:54:32 +00004048 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004049
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004050 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4051
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004052 DECLARE_INSTRUCTION(NewArray);
4053
4054 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004055 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004056 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004057 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004058
4059 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4060};
4061
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004062class HAdd : public HBinaryOperation {
4063 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004064 HAdd(Primitive::Type result_type,
4065 HInstruction* left,
4066 HInstruction* right,
4067 uint32_t dex_pc = kNoDexPc)
4068 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004069
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004070 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004071
Roland Levillain9867bc72015-08-05 10:21:34 +01004072 template <typename T> T Compute(T x, T y) const { return x + y; }
4073
4074 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004075 return GetBlock()->GetGraph()->GetIntConstant(
4076 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004077 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004078 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004079 return GetBlock()->GetGraph()->GetLongConstant(
4080 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004081 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004082 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4083 return GetBlock()->GetGraph()->GetFloatConstant(
4084 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4085 }
4086 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4087 return GetBlock()->GetGraph()->GetDoubleConstant(
4088 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4089 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004090
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004091 DECLARE_INSTRUCTION(Add);
4092
4093 private:
4094 DISALLOW_COPY_AND_ASSIGN(HAdd);
4095};
4096
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004097class HSub : public HBinaryOperation {
4098 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004099 HSub(Primitive::Type result_type,
4100 HInstruction* left,
4101 HInstruction* right,
4102 uint32_t dex_pc = kNoDexPc)
4103 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004104
Roland Levillain9867bc72015-08-05 10:21:34 +01004105 template <typename T> T Compute(T x, T y) const { return x - y; }
4106
4107 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004108 return GetBlock()->GetGraph()->GetIntConstant(
4109 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004110 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004111 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004112 return GetBlock()->GetGraph()->GetLongConstant(
4113 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004114 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004115 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4116 return GetBlock()->GetGraph()->GetFloatConstant(
4117 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4118 }
4119 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4120 return GetBlock()->GetGraph()->GetDoubleConstant(
4121 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4122 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004123
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004124 DECLARE_INSTRUCTION(Sub);
4125
4126 private:
4127 DISALLOW_COPY_AND_ASSIGN(HSub);
4128};
4129
Calin Juravle34bacdf2014-10-07 20:23:36 +01004130class HMul : public HBinaryOperation {
4131 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004132 HMul(Primitive::Type result_type,
4133 HInstruction* left,
4134 HInstruction* right,
4135 uint32_t dex_pc = kNoDexPc)
4136 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004137
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004138 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004139
Roland Levillain9867bc72015-08-05 10:21:34 +01004140 template <typename T> T Compute(T x, T y) const { return x * y; }
4141
4142 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 return GetBlock()->GetGraph()->GetIntConstant(
4144 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004145 }
4146 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 return GetBlock()->GetGraph()->GetLongConstant(
4148 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004149 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004150 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4151 return GetBlock()->GetGraph()->GetFloatConstant(
4152 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4153 }
4154 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4155 return GetBlock()->GetGraph()->GetDoubleConstant(
4156 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4157 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004158
4159 DECLARE_INSTRUCTION(Mul);
4160
4161 private:
4162 DISALLOW_COPY_AND_ASSIGN(HMul);
4163};
4164
Calin Juravle7c4954d2014-10-28 16:57:40 +00004165class HDiv : public HBinaryOperation {
4166 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004167 HDiv(Primitive::Type result_type,
4168 HInstruction* left,
4169 HInstruction* right,
4170 uint32_t dex_pc)
4171 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004172
Roland Levillain9867bc72015-08-05 10:21:34 +01004173 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004174 T ComputeIntegral(T x, T y) const {
4175 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004176 // Our graph structure ensures we never have 0 for `y` during
4177 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004178 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004179 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004180 return (y == -1) ? -x : x / y;
4181 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004182
Roland Levillain31dd3d62016-02-16 12:21:02 +00004183 template <typename T>
4184 T ComputeFP(T x, T y) const {
4185 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4186 return x / y;
4187 }
4188
Roland Levillain9867bc72015-08-05 10:21:34 +01004189 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004190 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004191 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004192 }
4193 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004194 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004195 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4196 }
4197 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4198 return GetBlock()->GetGraph()->GetFloatConstant(
4199 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4200 }
4201 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4202 return GetBlock()->GetGraph()->GetDoubleConstant(
4203 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004204 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004205
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004206 static SideEffects SideEffectsForArchRuntimeCalls() {
4207 // The generated code can use a runtime call.
4208 return SideEffects::CanTriggerGC();
4209 }
4210
Calin Juravle7c4954d2014-10-28 16:57:40 +00004211 DECLARE_INSTRUCTION(Div);
4212
4213 private:
4214 DISALLOW_COPY_AND_ASSIGN(HDiv);
4215};
4216
Calin Juravlebacfec32014-11-14 15:54:36 +00004217class HRem : public HBinaryOperation {
4218 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004219 HRem(Primitive::Type result_type,
4220 HInstruction* left,
4221 HInstruction* right,
4222 uint32_t dex_pc)
4223 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004224
Roland Levillain9867bc72015-08-05 10:21:34 +01004225 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004226 T ComputeIntegral(T x, T y) const {
4227 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004228 // Our graph structure ensures we never have 0 for `y` during
4229 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004230 DCHECK_NE(y, 0);
4231 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4232 return (y == -1) ? 0 : x % y;
4233 }
4234
Roland Levillain31dd3d62016-02-16 12:21:02 +00004235 template <typename T>
4236 T ComputeFP(T x, T y) const {
4237 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4238 return std::fmod(x, y);
4239 }
4240
Roland Levillain9867bc72015-08-05 10:21:34 +01004241 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004242 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004243 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004244 }
4245 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004246 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004247 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004248 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004249 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4250 return GetBlock()->GetGraph()->GetFloatConstant(
4251 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4252 }
4253 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4254 return GetBlock()->GetGraph()->GetDoubleConstant(
4255 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4256 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004257
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004258 static SideEffects SideEffectsForArchRuntimeCalls() {
4259 return SideEffects::CanTriggerGC();
4260 }
4261
Calin Juravlebacfec32014-11-14 15:54:36 +00004262 DECLARE_INSTRUCTION(Rem);
4263
4264 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004265 DISALLOW_COPY_AND_ASSIGN(HRem);
4266};
4267
Calin Juravled0d48522014-11-04 16:40:20 +00004268class HDivZeroCheck : public HExpression<1> {
4269 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004270 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4271 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004272 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004273 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004274 SetRawInputAt(0, value);
4275 }
4276
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004277 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4278
Calin Juravled0d48522014-11-04 16:40:20 +00004279 bool CanBeMoved() const OVERRIDE { return true; }
4280
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004281 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004282 return true;
4283 }
4284
4285 bool NeedsEnvironment() const OVERRIDE { return true; }
4286 bool CanThrow() const OVERRIDE { return true; }
4287
Calin Juravled0d48522014-11-04 16:40:20 +00004288 DECLARE_INSTRUCTION(DivZeroCheck);
4289
4290 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004291 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4292};
4293
Calin Juravle9aec02f2014-11-18 23:06:35 +00004294class HShl : public HBinaryOperation {
4295 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004296 HShl(Primitive::Type result_type,
4297 HInstruction* left,
4298 HInstruction* right,
4299 uint32_t dex_pc = kNoDexPc)
4300 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004301
Roland Levillain9867bc72015-08-05 10:21:34 +01004302 template <typename T, typename U, typename V>
4303 T Compute(T x, U y, V max_shift_value) const {
4304 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4305 "V is not the unsigned integer type corresponding to T");
4306 return x << (y & max_shift_value);
4307 }
4308
4309 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4310 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004312 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004313 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4314 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004315 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004316 }
4317 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4318 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004319 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004320 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004321 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4322 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4323 LOG(FATAL) << DebugName() << " is not defined for float values";
4324 UNREACHABLE();
4325 }
4326 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4327 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4328 LOG(FATAL) << DebugName() << " is not defined for double values";
4329 UNREACHABLE();
4330 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004331
4332 DECLARE_INSTRUCTION(Shl);
4333
4334 private:
4335 DISALLOW_COPY_AND_ASSIGN(HShl);
4336};
4337
4338class HShr : public HBinaryOperation {
4339 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004340 HShr(Primitive::Type result_type,
4341 HInstruction* left,
4342 HInstruction* right,
4343 uint32_t dex_pc = kNoDexPc)
4344 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004345
Roland Levillain9867bc72015-08-05 10:21:34 +01004346 template <typename T, typename U, typename V>
4347 T Compute(T x, U y, V max_shift_value) const {
4348 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4349 "V is not the unsigned integer type corresponding to T");
4350 return x >> (y & max_shift_value);
4351 }
4352
4353 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4354 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004356 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004357 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4358 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004359 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004360 }
4361 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4362 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004363 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004364 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004365 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4366 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4367 LOG(FATAL) << DebugName() << " is not defined for float values";
4368 UNREACHABLE();
4369 }
4370 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4371 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4372 LOG(FATAL) << DebugName() << " is not defined for double values";
4373 UNREACHABLE();
4374 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004375
4376 DECLARE_INSTRUCTION(Shr);
4377
4378 private:
4379 DISALLOW_COPY_AND_ASSIGN(HShr);
4380};
4381
4382class HUShr : public HBinaryOperation {
4383 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004384 HUShr(Primitive::Type result_type,
4385 HInstruction* left,
4386 HInstruction* right,
4387 uint32_t dex_pc = kNoDexPc)
4388 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004389
Roland Levillain9867bc72015-08-05 10:21:34 +01004390 template <typename T, typename U, typename V>
4391 T Compute(T x, U y, V max_shift_value) const {
4392 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4393 "V is not the unsigned integer type corresponding to T");
4394 V ux = static_cast<V>(x);
4395 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004396 }
4397
Roland Levillain9867bc72015-08-05 10:21:34 +01004398 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4399 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004400 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004401 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004402 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4403 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004404 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004405 }
4406 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4407 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004408 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004409 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004410 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4411 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4412 LOG(FATAL) << DebugName() << " is not defined for float values";
4413 UNREACHABLE();
4414 }
4415 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4416 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4417 LOG(FATAL) << DebugName() << " is not defined for double values";
4418 UNREACHABLE();
4419 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004420
4421 DECLARE_INSTRUCTION(UShr);
4422
4423 private:
4424 DISALLOW_COPY_AND_ASSIGN(HUShr);
4425};
4426
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004427class HAnd : public HBinaryOperation {
4428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004429 HAnd(Primitive::Type result_type,
4430 HInstruction* left,
4431 HInstruction* right,
4432 uint32_t dex_pc = kNoDexPc)
4433 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004434
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004435 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004436
Roland Levillaine53bd812016-02-24 14:54:18 +00004437 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004438
4439 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004440 return GetBlock()->GetGraph()->GetIntConstant(
4441 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004442 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004443 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004444 return GetBlock()->GetGraph()->GetLongConstant(
4445 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004446 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004447 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4448 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4449 LOG(FATAL) << DebugName() << " is not defined for float values";
4450 UNREACHABLE();
4451 }
4452 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4453 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4454 LOG(FATAL) << DebugName() << " is not defined for double values";
4455 UNREACHABLE();
4456 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004457
4458 DECLARE_INSTRUCTION(And);
4459
4460 private:
4461 DISALLOW_COPY_AND_ASSIGN(HAnd);
4462};
4463
4464class HOr : public HBinaryOperation {
4465 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004466 HOr(Primitive::Type result_type,
4467 HInstruction* left,
4468 HInstruction* right,
4469 uint32_t dex_pc = kNoDexPc)
4470 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004471
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004472 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004473
Roland Levillaine53bd812016-02-24 14:54:18 +00004474 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004475
4476 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004477 return GetBlock()->GetGraph()->GetIntConstant(
4478 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004479 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004480 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004481 return GetBlock()->GetGraph()->GetLongConstant(
4482 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004483 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004484 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4485 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4486 LOG(FATAL) << DebugName() << " is not defined for float values";
4487 UNREACHABLE();
4488 }
4489 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4490 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4491 LOG(FATAL) << DebugName() << " is not defined for double values";
4492 UNREACHABLE();
4493 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004494
4495 DECLARE_INSTRUCTION(Or);
4496
4497 private:
4498 DISALLOW_COPY_AND_ASSIGN(HOr);
4499};
4500
4501class HXor : public HBinaryOperation {
4502 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004503 HXor(Primitive::Type result_type,
4504 HInstruction* left,
4505 HInstruction* right,
4506 uint32_t dex_pc = kNoDexPc)
4507 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004508
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004509 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004510
Roland Levillaine53bd812016-02-24 14:54:18 +00004511 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004512
4513 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004514 return GetBlock()->GetGraph()->GetIntConstant(
4515 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004517 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004518 return GetBlock()->GetGraph()->GetLongConstant(
4519 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004520 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004521 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4522 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4523 LOG(FATAL) << DebugName() << " is not defined for float values";
4524 UNREACHABLE();
4525 }
4526 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4527 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4528 LOG(FATAL) << DebugName() << " is not defined for double values";
4529 UNREACHABLE();
4530 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004531
4532 DECLARE_INSTRUCTION(Xor);
4533
4534 private:
4535 DISALLOW_COPY_AND_ASSIGN(HXor);
4536};
4537
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004538class HRor : public HBinaryOperation {
4539 public:
4540 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4541 : HBinaryOperation(result_type, value, distance) {}
4542
4543 template <typename T, typename U, typename V>
4544 T Compute(T x, U y, V max_shift_value) const {
4545 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4546 "V is not the unsigned integer type corresponding to T");
4547 V ux = static_cast<V>(x);
4548 if ((y & max_shift_value) == 0) {
4549 return static_cast<T>(ux);
4550 } else {
4551 const V reg_bits = sizeof(T) * 8;
4552 return static_cast<T>(ux >> (y & max_shift_value)) |
4553 (x << (reg_bits - (y & max_shift_value)));
4554 }
4555 }
4556
4557 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4558 return GetBlock()->GetGraph()->GetIntConstant(
4559 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4560 }
4561 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4562 return GetBlock()->GetGraph()->GetLongConstant(
4563 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4564 }
4565 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4566 return GetBlock()->GetGraph()->GetLongConstant(
4567 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4568 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004569 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4570 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4571 LOG(FATAL) << DebugName() << " is not defined for float values";
4572 UNREACHABLE();
4573 }
4574 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4575 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4576 LOG(FATAL) << DebugName() << " is not defined for double values";
4577 UNREACHABLE();
4578 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004579
4580 DECLARE_INSTRUCTION(Ror);
4581
4582 private:
4583 DISALLOW_COPY_AND_ASSIGN(HRor);
4584};
4585
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004586// The value of a parameter in this method. Its location depends on
4587// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004588class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004589 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004590 HParameterValue(const DexFile& dex_file,
4591 uint16_t type_index,
4592 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004593 Primitive::Type parameter_type,
4594 bool is_this = false)
4595 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004596 dex_file_(dex_file),
4597 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004598 index_(index),
4599 is_this_(is_this),
4600 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004601
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004602 const DexFile& GetDexFile() const { return dex_file_; }
4603 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004604 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004605 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004606
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004607 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4608 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004609
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004610 DECLARE_INSTRUCTION(ParameterValue);
4611
4612 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004613 const DexFile& dex_file_;
4614 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004615 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004616 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004617 const uint8_t index_;
4618
Calin Juravle10e244f2015-01-26 18:54:32 +00004619 // Whether or not the parameter value corresponds to 'this' argument.
4620 const bool is_this_;
4621
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004622 bool can_be_null_;
4623
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004624 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4625};
4626
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004627class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004628 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004629 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4630 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004631
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004632 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004633 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004634 return true;
4635 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004636
Roland Levillain9867bc72015-08-05 10:21:34 +01004637 template <typename T> T Compute(T x) const { return ~x; }
4638
4639 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004640 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004641 }
4642 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004643 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004644 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004645 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4646 LOG(FATAL) << DebugName() << " is not defined for float values";
4647 UNREACHABLE();
4648 }
4649 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4650 LOG(FATAL) << DebugName() << " is not defined for double values";
4651 UNREACHABLE();
4652 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004653
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004654 DECLARE_INSTRUCTION(Not);
4655
4656 private:
4657 DISALLOW_COPY_AND_ASSIGN(HNot);
4658};
4659
David Brazdil66d126e2015-04-03 16:02:44 +01004660class HBooleanNot : public HUnaryOperation {
4661 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004662 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4663 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004664
4665 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004666 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004667 return true;
4668 }
4669
Roland Levillain9867bc72015-08-05 10:21:34 +01004670 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004671 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004672 return !x;
4673 }
4674
Roland Levillain9867bc72015-08-05 10:21:34 +01004675 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004676 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004677 }
4678 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4679 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004680 UNREACHABLE();
4681 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004682 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4683 LOG(FATAL) << DebugName() << " is not defined for float values";
4684 UNREACHABLE();
4685 }
4686 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4687 LOG(FATAL) << DebugName() << " is not defined for double values";
4688 UNREACHABLE();
4689 }
David Brazdil66d126e2015-04-03 16:02:44 +01004690
4691 DECLARE_INSTRUCTION(BooleanNot);
4692
4693 private:
4694 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4695};
4696
Roland Levillaindff1f282014-11-05 14:15:05 +00004697class HTypeConversion : public HExpression<1> {
4698 public:
4699 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004700 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004701 : HExpression(result_type,
4702 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4703 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004704 SetRawInputAt(0, input);
4705 DCHECK_NE(input->GetType(), result_type);
4706 }
4707
4708 HInstruction* GetInput() const { return InputAt(0); }
4709 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4710 Primitive::Type GetResultType() const { return GetType(); }
4711
4712 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004713 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004714
Mark Mendelle82549b2015-05-06 10:55:34 -04004715 // Try to statically evaluate the conversion and return a HConstant
4716 // containing the result. If the input cannot be converted, return nullptr.
4717 HConstant* TryStaticEvaluation() const;
4718
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004719 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4720 Primitive::Type result_type) {
4721 // Some architectures may not require the 'GC' side effects, but at this point
4722 // in the compilation process we do not know what architecture we will
4723 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004724 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4725 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004726 return SideEffects::CanTriggerGC();
4727 }
4728 return SideEffects::None();
4729 }
4730
Roland Levillaindff1f282014-11-05 14:15:05 +00004731 DECLARE_INSTRUCTION(TypeConversion);
4732
4733 private:
4734 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4735};
4736
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004737static constexpr uint32_t kNoRegNumber = -1;
4738
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004739class HPhi : public HInstruction {
4740 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004741 HPhi(ArenaAllocator* arena,
4742 uint32_t reg_number,
4743 size_t number_of_inputs,
4744 Primitive::Type type,
4745 uint32_t dex_pc = kNoDexPc)
4746 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004747 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004748 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004749 type_(ToPhiType(type)),
4750 // Phis are constructed live and marked dead if conflicting or unused.
4751 // Individual steps of SsaBuilder should assume that if a phi has been
4752 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4753 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004754 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004755 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004756 }
4757
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004758 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4759 static Primitive::Type ToPhiType(Primitive::Type type) {
4760 switch (type) {
4761 case Primitive::kPrimBoolean:
4762 case Primitive::kPrimByte:
4763 case Primitive::kPrimShort:
4764 case Primitive::kPrimChar:
4765 return Primitive::kPrimInt;
4766 default:
4767 return type;
4768 }
4769 }
4770
David Brazdilffee3d32015-07-06 11:48:53 +01004771 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4772
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004773 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004774
4775 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004776 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004777
Calin Juravle10e244f2015-01-26 18:54:32 +00004778 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004779 void SetType(Primitive::Type new_type) {
4780 // Make sure that only valid type changes occur. The following are allowed:
4781 // (1) int -> float/ref (primitive type propagation),
4782 // (2) long -> double (primitive type propagation).
4783 DCHECK(type_ == new_type ||
4784 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4785 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4786 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4787 type_ = new_type;
4788 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004789
Calin Juravle10e244f2015-01-26 18:54:32 +00004790 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4791 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4792
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004793 uint32_t GetRegNumber() const { return reg_number_; }
4794
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004795 void SetDead() { is_live_ = false; }
4796 void SetLive() { is_live_ = true; }
4797 bool IsDead() const { return !is_live_; }
4798 bool IsLive() const { return is_live_; }
4799
David Brazdil77a48ae2015-09-15 12:34:04 +00004800 bool IsVRegEquivalentOf(HInstruction* other) const {
4801 return other != nullptr
4802 && other->IsPhi()
4803 && other->AsPhi()->GetBlock() == GetBlock()
4804 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4805 }
4806
Calin Juravlea4f88312015-04-16 12:57:19 +01004807 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4808 // An equivalent phi is a phi having the same dex register and type.
4809 // It assumes that phis with the same dex register are adjacent.
4810 HPhi* GetNextEquivalentPhiWithSameType() {
4811 HInstruction* next = GetNext();
4812 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4813 if (next->GetType() == GetType()) {
4814 return next->AsPhi();
4815 }
4816 next = next->GetNext();
4817 }
4818 return nullptr;
4819 }
4820
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004821 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004822
David Brazdil1abb4192015-02-17 18:33:36 +00004823 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004824 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004825 return inputs_[index];
4826 }
David Brazdil1abb4192015-02-17 18:33:36 +00004827
4828 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004829 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004830 }
4831
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004832 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004833 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004834 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004835 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004836 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004837 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004838
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004839 DISALLOW_COPY_AND_ASSIGN(HPhi);
4840};
4841
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004842class HNullCheck : public HExpression<1> {
4843 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004844 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4845 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004846 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004847 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004848 SetRawInputAt(0, value);
4849 }
4850
Calin Juravle10e244f2015-01-26 18:54:32 +00004851 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004852 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004853 return true;
4854 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004855
Calin Juravle10e244f2015-01-26 18:54:32 +00004856 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004857
Calin Juravle10e244f2015-01-26 18:54:32 +00004858 bool CanThrow() const OVERRIDE { return true; }
4859
4860 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004861
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004862
4863 DECLARE_INSTRUCTION(NullCheck);
4864
4865 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004866 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4867};
4868
4869class FieldInfo : public ValueObject {
4870 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004871 FieldInfo(MemberOffset field_offset,
4872 Primitive::Type field_type,
4873 bool is_volatile,
4874 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004875 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004876 const DexFile& dex_file,
4877 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004878 : field_offset_(field_offset),
4879 field_type_(field_type),
4880 is_volatile_(is_volatile),
4881 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004882 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004883 dex_file_(dex_file),
4884 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004885
4886 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004887 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004888 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004889 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004890 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004891 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004892 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004893
4894 private:
4895 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004896 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004897 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004898 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004899 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004900 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004901 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004902};
4903
4904class HInstanceFieldGet : public HExpression<1> {
4905 public:
4906 HInstanceFieldGet(HInstruction* value,
4907 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004908 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004909 bool is_volatile,
4910 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004911 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004912 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004913 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004914 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004915 : HExpression(field_type,
4916 SideEffects::FieldReadOfType(field_type, is_volatile),
4917 dex_pc),
4918 field_info_(field_offset,
4919 field_type,
4920 is_volatile,
4921 field_idx,
4922 declaring_class_def_index,
4923 dex_file,
4924 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004925 SetRawInputAt(0, value);
4926 }
4927
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004928 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004929
4930 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4931 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4932 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004933 }
4934
Calin Juravle641547a2015-04-21 22:08:51 +01004935 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4936 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004937 }
4938
4939 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004940 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4941 }
4942
Calin Juravle52c48962014-12-16 17:02:57 +00004943 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004944 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004945 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004946 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004947
4948 DECLARE_INSTRUCTION(InstanceFieldGet);
4949
4950 private:
4951 const FieldInfo field_info_;
4952
4953 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4954};
4955
4956class HInstanceFieldSet : public HTemplateInstruction<2> {
4957 public:
4958 HInstanceFieldSet(HInstruction* object,
4959 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004960 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004961 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004962 bool is_volatile,
4963 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004964 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004965 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004966 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004967 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004968 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4969 dex_pc),
4970 field_info_(field_offset,
4971 field_type,
4972 is_volatile,
4973 field_idx,
4974 declaring_class_def_index,
4975 dex_file,
4976 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004977 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978 SetRawInputAt(0, object);
4979 SetRawInputAt(1, value);
4980 }
4981
Calin Juravle641547a2015-04-21 22:08:51 +01004982 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4983 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004984 }
4985
Calin Juravle52c48962014-12-16 17:02:57 +00004986 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004987 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004988 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004989 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004990 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004991 bool GetValueCanBeNull() const { return value_can_be_null_; }
4992 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004993
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004994 DECLARE_INSTRUCTION(InstanceFieldSet);
4995
4996 private:
4997 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004998 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004999
5000 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5001};
5002
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005003class HArrayGet : public HExpression<2> {
5004 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005005 HArrayGet(HInstruction* array,
5006 HInstruction* index,
5007 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005008 uint32_t dex_pc,
5009 SideEffects additional_side_effects = SideEffects::None())
5010 : HExpression(type,
5011 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005012 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005013 SetRawInputAt(0, array);
5014 SetRawInputAt(1, index);
5015 }
5016
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005017 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005018 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005019 return true;
5020 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005021 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005022 // TODO: We can be smarter here.
5023 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5024 // which generates the implicit null check. There are cases when these can be removed
5025 // to produce better code. If we ever add optimizations to do so we should allow an
5026 // implicit check here (as long as the address falls in the first page).
5027 return false;
5028 }
5029
David Brazdil4833f5a2015-12-16 10:37:39 +00005030 bool IsEquivalentOf(HArrayGet* other) const {
5031 bool result = (GetDexPc() == other->GetDexPc());
5032 if (kIsDebugBuild && result) {
5033 DCHECK_EQ(GetBlock(), other->GetBlock());
5034 DCHECK_EQ(GetArray(), other->GetArray());
5035 DCHECK_EQ(GetIndex(), other->GetIndex());
5036 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005037 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005038 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005039 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5040 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005041 }
5042 }
5043 return result;
5044 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005045
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005046 HInstruction* GetArray() const { return InputAt(0); }
5047 HInstruction* GetIndex() const { return InputAt(1); }
5048
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005049 DECLARE_INSTRUCTION(ArrayGet);
5050
5051 private:
5052 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5053};
5054
5055class HArraySet : public HTemplateInstruction<3> {
5056 public:
5057 HArraySet(HInstruction* array,
5058 HInstruction* index,
5059 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005060 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005061 uint32_t dex_pc,
5062 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005063 : HTemplateInstruction(
5064 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005065 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5066 additional_side_effects),
5067 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005068 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005069 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005070 value_can_be_null_(true),
5071 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005072 SetRawInputAt(0, array);
5073 SetRawInputAt(1, index);
5074 SetRawInputAt(2, value);
5075 }
5076
Calin Juravle77520bc2015-01-12 18:45:46 +00005077 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005078 // We call a runtime method to throw ArrayStoreException.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005079 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005080 }
5081
Mingyao Yang81014cb2015-06-02 03:16:27 -07005082 // Can throw ArrayStoreException.
5083 bool CanThrow() const OVERRIDE { return needs_type_check_; }
5084
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005085 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005086 // TODO: Same as for ArrayGet.
5087 return false;
5088 }
5089
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005090 void ClearNeedsTypeCheck() {
5091 needs_type_check_ = false;
5092 }
5093
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005094 void ClearValueCanBeNull() {
5095 value_can_be_null_ = false;
5096 }
5097
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005098 void SetStaticTypeOfArrayIsObjectArray() {
5099 static_type_of_array_is_object_array_ = true;
5100 }
5101
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005102 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005103 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005104 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005105
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005106 HInstruction* GetArray() const { return InputAt(0); }
5107 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005108 HInstruction* GetValue() const { return InputAt(2); }
5109
5110 Primitive::Type GetComponentType() const {
5111 // The Dex format does not type floating point index operations. Since the
5112 // `expected_component_type_` is set during building and can therefore not
5113 // be correct, we also check what is the value type. If it is a floating
5114 // point type, we must use that type.
5115 Primitive::Type value_type = GetValue()->GetType();
5116 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5117 ? value_type
5118 : expected_component_type_;
5119 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005120
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005121 Primitive::Type GetRawExpectedComponentType() const {
5122 return expected_component_type_;
5123 }
5124
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005125 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5126 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5127 }
5128
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005129 DECLARE_INSTRUCTION(ArraySet);
5130
5131 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005132 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005133 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005134 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005135 // Cached information for the reference_type_info_ so that codegen
5136 // does not need to inspect the static type.
5137 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138
5139 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5140};
5141
5142class HArrayLength : public HExpression<1> {
5143 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005144 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005145 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005146 // Note that arrays do not change length, so the instruction does not
5147 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 SetRawInputAt(0, array);
5149 }
5150
Calin Juravle77520bc2015-01-12 18:45:46 +00005151 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005152 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005153 return true;
5154 }
Calin Juravle641547a2015-04-21 22:08:51 +01005155 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5156 return obj == InputAt(0);
5157 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005158
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 DECLARE_INSTRUCTION(ArrayLength);
5160
5161 private:
5162 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5163};
5164
5165class HBoundsCheck : public HExpression<2> {
5166 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005167 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5168 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005169 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005170 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005171 DCHECK(index->GetType() == Primitive::kPrimInt);
5172 SetRawInputAt(0, index);
5173 SetRawInputAt(1, length);
5174 }
5175
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005176 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005177 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005178 return true;
5179 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005180
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005181 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005182
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005183 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005184
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005185 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005186
5187 DECLARE_INSTRUCTION(BoundsCheck);
5188
5189 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005190 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5191};
5192
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005193class HSuspendCheck : public HTemplateInstruction<0> {
5194 public:
5195 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005196 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005197
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005198 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005199 return true;
5200 }
5201
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005202 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5203 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005204
5205 DECLARE_INSTRUCTION(SuspendCheck);
5206
5207 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005208 // Only used for code generation, in order to share the same slow path between back edges
5209 // of a same loop.
5210 SlowPathCode* slow_path_;
5211
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005212 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5213};
5214
David Srbecky0cf44932015-12-09 14:09:59 +00005215// Pseudo-instruction which provides the native debugger with mapping information.
5216// It ensures that we can generate line number and local variables at this point.
5217class HNativeDebugInfo : public HTemplateInstruction<0> {
5218 public:
5219 explicit HNativeDebugInfo(uint32_t dex_pc)
5220 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5221
5222 bool NeedsEnvironment() const OVERRIDE {
5223 return true;
5224 }
5225
5226 DECLARE_INSTRUCTION(NativeDebugInfo);
5227
5228 private:
5229 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5230};
5231
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005232/**
5233 * Instruction to load a Class object.
5234 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005235class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005236 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005237 HLoadClass(HCurrentMethod* current_method,
5238 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005239 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005240 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005241 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005242 bool needs_access_check,
5243 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005244 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005245 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005246 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005247 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00005248 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01005249 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005250 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00005251 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005252 // Referrers class should not need access check. We never inline unverified
5253 // methods so we can't possibly end up in this situation.
5254 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005255 SetRawInputAt(0, current_method);
5256 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005257
5258 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005259
5260 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005261 // Note that we don't need to test for generate_clinit_check_.
5262 // Whether or not we need to generate the clinit check is processed in
5263 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005264 return other->AsLoadClass()->type_index_ == type_index_ &&
5265 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005266 }
5267
5268 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5269
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005270 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005271 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005272 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005273
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005274 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005275 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005276 }
5277
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005278 bool MustGenerateClinitCheck() const {
5279 return generate_clinit_check_;
5280 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005281
Calin Juravle0ba218d2015-05-19 18:46:01 +01005282 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005283 // The entrypoint the code generator is going to call does not do
5284 // clinit of the class.
5285 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005286 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005287 }
5288
5289 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005290 return MustGenerateClinitCheck() ||
5291 (!is_referrers_class_ && !is_in_dex_cache_) ||
5292 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005293 }
5294
5295 bool NeedsAccessCheck() const {
5296 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005297 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005298
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005299 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005300 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005301 }
5302
Calin Juravleacf735c2015-02-12 15:25:22 +00005303 ReferenceTypeInfo GetLoadedClassRTI() {
5304 return loaded_class_rti_;
5305 }
5306
5307 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5308 // Make sure we only set exact types (the loaded class should never be merged).
5309 DCHECK(rti.IsExact());
5310 loaded_class_rti_ = rti;
5311 }
5312
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005313 const DexFile& GetDexFile() { return dex_file_; }
5314
Vladimir Markodc151b22015-10-15 18:02:30 +01005315 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005316
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005317 static SideEffects SideEffectsForArchRuntimeCalls() {
5318 return SideEffects::CanTriggerGC();
5319 }
5320
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005321 bool IsInDexCache() const { return is_in_dex_cache_; }
5322
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005323 DECLARE_INSTRUCTION(LoadClass);
5324
5325 private:
5326 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005327 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005328 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005329 // Whether this instruction must generate the initialization check.
5330 // Used for code generation.
5331 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005332 const bool needs_access_check_;
5333 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005334
Calin Juravleacf735c2015-02-12 15:25:22 +00005335 ReferenceTypeInfo loaded_class_rti_;
5336
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005337 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5338};
5339
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005340class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005341 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005342 HLoadString(HCurrentMethod* current_method,
5343 uint32_t string_index,
5344 uint32_t dex_pc,
5345 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005346 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005347 string_index_(string_index),
5348 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005349 SetRawInputAt(0, current_method);
5350 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005351
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005352 bool CanBeMoved() const OVERRIDE { return true; }
5353
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005354 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5355 return other->AsLoadString()->string_index_ == string_index_;
5356 }
5357
5358 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5359
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005360 uint32_t GetStringIndex() const { return string_index_; }
5361
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005362 // Will call the runtime if the string is not already in the dex cache.
5363 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5364
Vladimir Markodc151b22015-10-15 18:02:30 +01005365 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005366 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005367 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005368 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005369
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005370 static SideEffects SideEffectsForArchRuntimeCalls() {
5371 return SideEffects::CanTriggerGC();
5372 }
5373
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005374 DECLARE_INSTRUCTION(LoadString);
5375
5376 private:
5377 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005378 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005379
5380 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5381};
5382
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005383/**
5384 * Performs an initialization check on its Class object input.
5385 */
5386class HClinitCheck : public HExpression<1> {
5387 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005388 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005389 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005390 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005391 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5392 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005393 SetRawInputAt(0, constant);
5394 }
5395
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005396 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005397 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005398 return true;
5399 }
5400
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005401 bool NeedsEnvironment() const OVERRIDE {
5402 // May call runtime to initialize the class.
5403 return true;
5404 }
5405
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005406 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005407
5408 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5409
5410 DECLARE_INSTRUCTION(ClinitCheck);
5411
5412 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005413 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5414};
5415
5416class HStaticFieldGet : public HExpression<1> {
5417 public:
5418 HStaticFieldGet(HInstruction* cls,
5419 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005420 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005421 bool is_volatile,
5422 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005423 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005424 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005425 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005426 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005427 : HExpression(field_type,
5428 SideEffects::FieldReadOfType(field_type, is_volatile),
5429 dex_pc),
5430 field_info_(field_offset,
5431 field_type,
5432 is_volatile,
5433 field_idx,
5434 declaring_class_def_index,
5435 dex_file,
5436 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005437 SetRawInputAt(0, cls);
5438 }
5439
Calin Juravle52c48962014-12-16 17:02:57 +00005440
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005441 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005442
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005443 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005444 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5445 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005446 }
5447
5448 size_t ComputeHashCode() const OVERRIDE {
5449 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5450 }
5451
Calin Juravle52c48962014-12-16 17:02:57 +00005452 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005453 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5454 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005455 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005456
5457 DECLARE_INSTRUCTION(StaticFieldGet);
5458
5459 private:
5460 const FieldInfo field_info_;
5461
5462 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5463};
5464
5465class HStaticFieldSet : public HTemplateInstruction<2> {
5466 public:
5467 HStaticFieldSet(HInstruction* cls,
5468 HInstruction* value,
5469 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005470 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005471 bool is_volatile,
5472 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005473 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005474 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005475 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005476 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005477 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5478 dex_pc),
5479 field_info_(field_offset,
5480 field_type,
5481 is_volatile,
5482 field_idx,
5483 declaring_class_def_index,
5484 dex_file,
5485 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005486 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005487 SetRawInputAt(0, cls);
5488 SetRawInputAt(1, value);
5489 }
5490
Calin Juravle52c48962014-12-16 17:02:57 +00005491 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005492 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5493 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005494 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005495
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005496 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005497 bool GetValueCanBeNull() const { return value_can_be_null_; }
5498 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005499
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005500 DECLARE_INSTRUCTION(StaticFieldSet);
5501
5502 private:
5503 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005504 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005505
5506 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5507};
5508
Calin Juravlee460d1d2015-09-29 04:52:17 +01005509class HUnresolvedInstanceFieldGet : public HExpression<1> {
5510 public:
5511 HUnresolvedInstanceFieldGet(HInstruction* obj,
5512 Primitive::Type field_type,
5513 uint32_t field_index,
5514 uint32_t dex_pc)
5515 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5516 field_index_(field_index) {
5517 SetRawInputAt(0, obj);
5518 }
5519
5520 bool NeedsEnvironment() const OVERRIDE { return true; }
5521 bool CanThrow() const OVERRIDE { return true; }
5522
5523 Primitive::Type GetFieldType() const { return GetType(); }
5524 uint32_t GetFieldIndex() const { return field_index_; }
5525
5526 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5527
5528 private:
5529 const uint32_t field_index_;
5530
5531 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5532};
5533
5534class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5535 public:
5536 HUnresolvedInstanceFieldSet(HInstruction* obj,
5537 HInstruction* value,
5538 Primitive::Type field_type,
5539 uint32_t field_index,
5540 uint32_t dex_pc)
5541 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5542 field_type_(field_type),
5543 field_index_(field_index) {
5544 DCHECK_EQ(field_type, value->GetType());
5545 SetRawInputAt(0, obj);
5546 SetRawInputAt(1, value);
5547 }
5548
5549 bool NeedsEnvironment() const OVERRIDE { return true; }
5550 bool CanThrow() const OVERRIDE { return true; }
5551
5552 Primitive::Type GetFieldType() const { return field_type_; }
5553 uint32_t GetFieldIndex() const { return field_index_; }
5554
5555 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5556
5557 private:
5558 const Primitive::Type field_type_;
5559 const uint32_t field_index_;
5560
5561 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5562};
5563
5564class HUnresolvedStaticFieldGet : public HExpression<0> {
5565 public:
5566 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5567 uint32_t field_index,
5568 uint32_t dex_pc)
5569 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5570 field_index_(field_index) {
5571 }
5572
5573 bool NeedsEnvironment() const OVERRIDE { return true; }
5574 bool CanThrow() const OVERRIDE { return true; }
5575
5576 Primitive::Type GetFieldType() const { return GetType(); }
5577 uint32_t GetFieldIndex() const { return field_index_; }
5578
5579 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5580
5581 private:
5582 const uint32_t field_index_;
5583
5584 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5585};
5586
5587class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5588 public:
5589 HUnresolvedStaticFieldSet(HInstruction* value,
5590 Primitive::Type field_type,
5591 uint32_t field_index,
5592 uint32_t dex_pc)
5593 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5594 field_type_(field_type),
5595 field_index_(field_index) {
5596 DCHECK_EQ(field_type, value->GetType());
5597 SetRawInputAt(0, value);
5598 }
5599
5600 bool NeedsEnvironment() const OVERRIDE { return true; }
5601 bool CanThrow() const OVERRIDE { return true; }
5602
5603 Primitive::Type GetFieldType() const { return field_type_; }
5604 uint32_t GetFieldIndex() const { return field_index_; }
5605
5606 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5607
5608 private:
5609 const Primitive::Type field_type_;
5610 const uint32_t field_index_;
5611
5612 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5613};
5614
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005615// Implement the move-exception DEX instruction.
5616class HLoadException : public HExpression<0> {
5617 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005618 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5619 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005620
David Brazdilbbd733e2015-08-18 17:48:17 +01005621 bool CanBeNull() const OVERRIDE { return false; }
5622
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005623 DECLARE_INSTRUCTION(LoadException);
5624
5625 private:
5626 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5627};
5628
David Brazdilcb1c0552015-08-04 16:22:25 +01005629// Implicit part of move-exception which clears thread-local exception storage.
5630// Must not be removed because the runtime expects the TLS to get cleared.
5631class HClearException : public HTemplateInstruction<0> {
5632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005633 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5634 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005635
5636 DECLARE_INSTRUCTION(ClearException);
5637
5638 private:
5639 DISALLOW_COPY_AND_ASSIGN(HClearException);
5640};
5641
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005642class HThrow : public HTemplateInstruction<1> {
5643 public:
5644 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005645 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005646 SetRawInputAt(0, exception);
5647 }
5648
5649 bool IsControlFlow() const OVERRIDE { return true; }
5650
5651 bool NeedsEnvironment() const OVERRIDE { return true; }
5652
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005653 bool CanThrow() const OVERRIDE { return true; }
5654
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005655
5656 DECLARE_INSTRUCTION(Throw);
5657
5658 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005659 DISALLOW_COPY_AND_ASSIGN(HThrow);
5660};
5661
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005662/**
5663 * Implementation strategies for the code generator of a HInstanceOf
5664 * or `HCheckCast`.
5665 */
5666enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005667 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005668 kExactCheck, // Can do a single class compare.
5669 kClassHierarchyCheck, // Can just walk the super class chain.
5670 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5671 kInterfaceCheck, // No optimization yet when checking against an interface.
5672 kArrayObjectCheck, // Can just check if the array is not primitive.
5673 kArrayCheck // No optimization yet when checking against a generic array.
5674};
5675
Roland Levillain86503782016-02-11 19:07:30 +00005676std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5677
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005678class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005679 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005680 HInstanceOf(HInstruction* object,
5681 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005682 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005683 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005684 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005685 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005686 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005687 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005688 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005689 SetRawInputAt(0, object);
5690 SetRawInputAt(1, constant);
5691 }
5692
5693 bool CanBeMoved() const OVERRIDE { return true; }
5694
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005695 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005696 return true;
5697 }
5698
5699 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005700 return CanCallRuntime(check_kind_);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005701 }
5702
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005703 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5704
5705 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005706
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005707 // Used only in code generation.
5708 bool MustDoNullCheck() const { return must_do_null_check_; }
5709 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5710
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005711 static bool CanCallRuntime(TypeCheckKind check_kind) {
5712 // Mips currently does runtime calls for any other checks.
5713 return check_kind != TypeCheckKind::kExactCheck;
5714 }
5715
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005716 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005717 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005718 }
5719
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005720 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005721
5722 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005723 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005724 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005725
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005726 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5727};
5728
Calin Juravleb1498f62015-02-16 13:13:29 +00005729class HBoundType : public HExpression<1> {
5730 public:
David Brazdilf5552582015-12-27 13:36:12 +00005731 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005732 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005733 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5734 upper_can_be_null_(true),
5735 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005736 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005737 SetRawInputAt(0, input);
5738 }
5739
David Brazdilf5552582015-12-27 13:36:12 +00005740 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005741 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5742 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005743 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005744
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005745 void SetCanBeNull(bool can_be_null) {
5746 DCHECK(upper_can_be_null_ || !can_be_null);
5747 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005748 }
5749
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005750 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5751
Calin Juravleb1498f62015-02-16 13:13:29 +00005752 DECLARE_INSTRUCTION(BoundType);
5753
5754 private:
5755 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005756 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5757 // It is used to bound the type in cases like:
5758 // if (x instanceof ClassX) {
5759 // // uper_bound_ will be ClassX
5760 // }
David Brazdilf5552582015-12-27 13:36:12 +00005761 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005762 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5763 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005764 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005765 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005766
5767 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5768};
5769
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005770class HCheckCast : public HTemplateInstruction<2> {
5771 public:
5772 HCheckCast(HInstruction* object,
5773 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005774 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005775 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005776 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005777 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005778 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005779 SetRawInputAt(0, object);
5780 SetRawInputAt(1, constant);
5781 }
5782
5783 bool CanBeMoved() const OVERRIDE { return true; }
5784
5785 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5786 return true;
5787 }
5788
5789 bool NeedsEnvironment() const OVERRIDE {
5790 // Instruction may throw a CheckCastError.
5791 return true;
5792 }
5793
5794 bool CanThrow() const OVERRIDE { return true; }
5795
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005796 bool MustDoNullCheck() const { return must_do_null_check_; }
5797 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005798 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005799
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005800 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005801
5802 DECLARE_INSTRUCTION(CheckCast);
5803
5804 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005805 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005806 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005807
5808 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005809};
5810
Calin Juravle27df7582015-04-17 19:12:31 +01005811class HMemoryBarrier : public HTemplateInstruction<0> {
5812 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005813 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005814 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005815 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005816 barrier_kind_(barrier_kind) {}
5817
5818 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5819
5820 DECLARE_INSTRUCTION(MemoryBarrier);
5821
5822 private:
5823 const MemBarrierKind barrier_kind_;
5824
5825 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5826};
5827
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005828class HMonitorOperation : public HTemplateInstruction<1> {
5829 public:
5830 enum OperationKind {
5831 kEnter,
5832 kExit,
5833 };
5834
5835 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005836 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005837 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5838 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005839 SetRawInputAt(0, object);
5840 }
5841
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005842 // Instruction may go into runtime, so we need an environment.
5843 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00005844
5845 bool CanThrow() const OVERRIDE {
5846 // Verifier guarantees that monitor-exit cannot throw.
5847 // This is important because it allows the HGraphBuilder to remove
5848 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5849 return IsEnter();
5850 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005851
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005852
5853 bool IsEnter() const { return kind_ == kEnter; }
5854
5855 DECLARE_INSTRUCTION(MonitorOperation);
5856
Calin Juravle52c48962014-12-16 17:02:57 +00005857 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005858 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005859
5860 private:
5861 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5862};
5863
David Brazdil74eb1b22015-12-14 11:44:01 +00005864class HSelect : public HExpression<3> {
5865 public:
5866 HSelect(HInstruction* condition,
5867 HInstruction* true_value,
5868 HInstruction* false_value,
5869 uint32_t dex_pc)
5870 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
5871 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
5872
5873 // First input must be `true_value` or `false_value` to allow codegens to
5874 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
5875 // that architectures which implement HSelect as a conditional move also
5876 // will not need to invert the condition.
5877 SetRawInputAt(0, false_value);
5878 SetRawInputAt(1, true_value);
5879 SetRawInputAt(2, condition);
5880 }
5881
5882 HInstruction* GetFalseValue() const { return InputAt(0); }
5883 HInstruction* GetTrueValue() const { return InputAt(1); }
5884 HInstruction* GetCondition() const { return InputAt(2); }
5885
5886 bool CanBeMoved() const OVERRIDE { return true; }
5887 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
5888
5889 bool CanBeNull() const OVERRIDE {
5890 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
5891 }
5892
5893 DECLARE_INSTRUCTION(Select);
5894
5895 private:
5896 DISALLOW_COPY_AND_ASSIGN(HSelect);
5897};
5898
Vladimir Markof9f64412015-09-02 14:05:49 +01005899class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005900 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005901 MoveOperands(Location source,
5902 Location destination,
5903 Primitive::Type type,
5904 HInstruction* instruction)
5905 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005906
5907 Location GetSource() const { return source_; }
5908 Location GetDestination() const { return destination_; }
5909
5910 void SetSource(Location value) { source_ = value; }
5911 void SetDestination(Location value) { destination_ = value; }
5912
5913 // The parallel move resolver marks moves as "in-progress" by clearing the
5914 // destination (but not the source).
5915 Location MarkPending() {
5916 DCHECK(!IsPending());
5917 Location dest = destination_;
5918 destination_ = Location::NoLocation();
5919 return dest;
5920 }
5921
5922 void ClearPending(Location dest) {
5923 DCHECK(IsPending());
5924 destination_ = dest;
5925 }
5926
5927 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005928 DCHECK(source_.IsValid() || destination_.IsInvalid());
5929 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005930 }
5931
5932 // True if this blocks a move from the given location.
5933 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005934 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005935 }
5936
5937 // A move is redundant if it's been eliminated, if its source and
5938 // destination are the same, or if its destination is unneeded.
5939 bool IsRedundant() const {
5940 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5941 }
5942
5943 // We clear both operands to indicate move that's been eliminated.
5944 void Eliminate() {
5945 source_ = destination_ = Location::NoLocation();
5946 }
5947
5948 bool IsEliminated() const {
5949 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5950 return source_.IsInvalid();
5951 }
5952
Alexey Frunze4dda3372015-06-01 18:31:49 -07005953 Primitive::Type GetType() const { return type_; }
5954
Nicolas Geoffray90218252015-04-15 11:56:51 +01005955 bool Is64BitMove() const {
5956 return Primitive::Is64BitType(type_);
5957 }
5958
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005959 HInstruction* GetInstruction() const { return instruction_; }
5960
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005961 private:
5962 Location source_;
5963 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005964 // The type this move is for.
5965 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005966 // The instruction this move is assocatied with. Null when this move is
5967 // for moving an input in the expected locations of user (including a phi user).
5968 // This is only used in debug mode, to ensure we do not connect interval siblings
5969 // in the same parallel move.
5970 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005971};
5972
Roland Levillainc9285912015-12-18 10:38:42 +00005973std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5974
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005975static constexpr size_t kDefaultNumberOfMoves = 4;
5976
5977class HParallelMove : public HTemplateInstruction<0> {
5978 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005979 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005980 : HTemplateInstruction(SideEffects::None(), dex_pc),
5981 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5982 moves_.reserve(kDefaultNumberOfMoves);
5983 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005984
Nicolas Geoffray90218252015-04-15 11:56:51 +01005985 void AddMove(Location source,
5986 Location destination,
5987 Primitive::Type type,
5988 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005989 DCHECK(source.IsValid());
5990 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005991 if (kIsDebugBuild) {
5992 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005993 for (const MoveOperands& move : moves_) {
5994 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005995 // Special case the situation where the move is for the spill slot
5996 // of the instruction.
5997 if ((GetPrevious() == instruction)
5998 || ((GetPrevious() == nullptr)
5999 && instruction->IsPhi()
6000 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006001 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006002 << "Doing parallel moves for the same instruction.";
6003 } else {
6004 DCHECK(false) << "Doing parallel moves for the same instruction.";
6005 }
6006 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006007 }
6008 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006009 for (const MoveOperands& move : moves_) {
6010 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006011 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006012 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006013 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006014 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006015 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006016 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006017 }
6018
Vladimir Marko225b6462015-09-28 12:17:40 +01006019 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006020 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006021 }
6022
Vladimir Marko225b6462015-09-28 12:17:40 +01006023 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006024
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006025 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006026
6027 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006028 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006029
6030 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6031};
6032
Mark Mendell0616ae02015-04-17 12:49:27 -04006033} // namespace art
6034
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006035#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6036#include "nodes_shared.h"
6037#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006038#ifdef ART_ENABLE_CODEGEN_arm
6039#include "nodes_arm.h"
6040#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006041#ifdef ART_ENABLE_CODEGEN_arm64
6042#include "nodes_arm64.h"
6043#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006044#ifdef ART_ENABLE_CODEGEN_x86
6045#include "nodes_x86.h"
6046#endif
6047
6048namespace art {
6049
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006050class HGraphVisitor : public ValueObject {
6051 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006052 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6053 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006054
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006055 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006056 virtual void VisitBasicBlock(HBasicBlock* block);
6057
Roland Levillain633021e2014-10-01 14:12:25 +01006058 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006059 void VisitInsertionOrder();
6060
Roland Levillain633021e2014-10-01 14:12:25 +01006061 // Visit the graph following dominator tree reverse post-order.
6062 void VisitReversePostOrder();
6063
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006064 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006065
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006066 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006067#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006068 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6069
6070 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6071
6072#undef DECLARE_VISIT_INSTRUCTION
6073
6074 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006075 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006076
6077 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6078};
6079
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006080class HGraphDelegateVisitor : public HGraphVisitor {
6081 public:
6082 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6083 virtual ~HGraphDelegateVisitor() {}
6084
6085 // Visit functions that delegate to to super class.
6086#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006087 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006088
6089 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6090
6091#undef DECLARE_VISIT_INSTRUCTION
6092
6093 private:
6094 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6095};
6096
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006097class HInsertionOrderIterator : public ValueObject {
6098 public:
6099 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6100
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006101 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006102 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006103 void Advance() { ++index_; }
6104
6105 private:
6106 const HGraph& graph_;
6107 size_t index_;
6108
6109 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6110};
6111
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006112class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006113 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006114 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6115 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006116 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006117 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006118
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006119 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6120 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006121 void Advance() { ++index_; }
6122
6123 private:
6124 const HGraph& graph_;
6125 size_t index_;
6126
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006127 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006128};
6129
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006130class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006131 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006132 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006133 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006134 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006135 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006136 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006137
6138 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006139 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006140 void Advance() { --index_; }
6141
6142 private:
6143 const HGraph& graph_;
6144 size_t index_;
6145
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006146 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006147};
6148
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006149class HLinearPostOrderIterator : public ValueObject {
6150 public:
6151 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006152 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006153
6154 bool Done() const { return index_ == 0; }
6155
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006156 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006157
6158 void Advance() {
6159 --index_;
6160 DCHECK_GE(index_, 0U);
6161 }
6162
6163 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006164 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006165 size_t index_;
6166
6167 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6168};
6169
6170class HLinearOrderIterator : public ValueObject {
6171 public:
6172 explicit HLinearOrderIterator(const HGraph& graph)
6173 : order_(graph.GetLinearOrder()), index_(0) {}
6174
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006175 bool Done() const { return index_ == order_.size(); }
6176 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006177 void Advance() { ++index_; }
6178
6179 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006180 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006181 size_t index_;
6182
6183 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6184};
6185
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006186// Iterator over the blocks that art part of the loop. Includes blocks part
6187// of an inner loop. The order in which the blocks are iterated is on their
6188// block id.
6189class HBlocksInLoopIterator : public ValueObject {
6190 public:
6191 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6192 : blocks_in_loop_(info.GetBlocks()),
6193 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6194 index_(0) {
6195 if (!blocks_in_loop_.IsBitSet(index_)) {
6196 Advance();
6197 }
6198 }
6199
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006200 bool Done() const { return index_ == blocks_.size(); }
6201 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006202 void Advance() {
6203 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006204 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006205 if (blocks_in_loop_.IsBitSet(index_)) {
6206 break;
6207 }
6208 }
6209 }
6210
6211 private:
6212 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006213 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006214 size_t index_;
6215
6216 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6217};
6218
Mingyao Yang3584bce2015-05-19 16:01:59 -07006219// Iterator over the blocks that art part of the loop. Includes blocks part
6220// of an inner loop. The order in which the blocks are iterated is reverse
6221// post order.
6222class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6223 public:
6224 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6225 : blocks_in_loop_(info.GetBlocks()),
6226 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6227 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006228 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006229 Advance();
6230 }
6231 }
6232
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006233 bool Done() const { return index_ == blocks_.size(); }
6234 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006235 void Advance() {
6236 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006237 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6238 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006239 break;
6240 }
6241 }
6242 }
6243
6244 private:
6245 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006246 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006247 size_t index_;
6248
6249 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6250};
6251
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006252inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006253 if (constant->IsIntConstant()) {
6254 return constant->AsIntConstant()->GetValue();
6255 } else if (constant->IsLongConstant()) {
6256 return constant->AsLongConstant()->GetValue();
6257 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006258 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006259 return 0;
6260 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006261}
6262
Vladimir Marko58155012015-08-19 12:49:41 +00006263inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6264 // For the purposes of the compiler, the dex files must actually be the same object
6265 // if we want to safely treat them as the same. This is especially important for JIT
6266 // as custom class loaders can open the same underlying file (or memory) multiple
6267 // times and provide different class resolution but no two class loaders should ever
6268 // use the same DexFile object - doing so is an unsupported hack that can lead to
6269 // all sorts of weird failures.
6270 return &lhs == &rhs;
6271}
6272
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006273#define INSTRUCTION_TYPE_CHECK(type, super) \
6274 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6275 inline const H##type* HInstruction::As##type() const { \
6276 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6277 } \
6278 inline H##type* HInstruction::As##type() { \
6279 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6280 }
6281
6282 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6283#undef INSTRUCTION_TYPE_CHECK
6284
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006285class SwitchTable : public ValueObject {
6286 public:
6287 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6288 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6289 int32_t table_offset = instruction.VRegB_31t();
6290 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6291 if (sparse) {
6292 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6293 } else {
6294 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6295 }
6296 num_entries_ = table[1];
6297 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6298 }
6299
6300 uint16_t GetNumEntries() const {
6301 return num_entries_;
6302 }
6303
6304 void CheckIndex(size_t index) const {
6305 if (sparse_) {
6306 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6307 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6308 } else {
6309 // In a packed table, we have the starting key and num_entries_ values.
6310 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6311 }
6312 }
6313
6314 int32_t GetEntryAt(size_t index) const {
6315 CheckIndex(index);
6316 return values_[index];
6317 }
6318
6319 uint32_t GetDexPcForIndex(size_t index) const {
6320 CheckIndex(index);
6321 return dex_pc_ +
6322 (reinterpret_cast<const int16_t*>(values_ + index) -
6323 reinterpret_cast<const int16_t*>(&instruction_));
6324 }
6325
6326 // Index of the first value in the table.
6327 size_t GetFirstValueIndex() const {
6328 if (sparse_) {
6329 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6330 return num_entries_;
6331 } else {
6332 // In a packed table, we have the starting key and num_entries_ values.
6333 return 1;
6334 }
6335 }
6336
6337 private:
6338 const Instruction& instruction_;
6339 const uint32_t dex_pc_;
6340
6341 // Whether this is a sparse-switch table (or a packed-switch one).
6342 const bool sparse_;
6343
6344 // This can't be const as it needs to be computed off of the given instruction, and complicated
6345 // expressions in the initializer list seemed very ugly.
6346 uint16_t num_entries_;
6347
6348 const int32_t* values_;
6349
6350 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6351};
6352
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006353// Create space in `blocks` for adding `number_of_new_blocks` entries
6354// starting at location `at`. Blocks after `at` are moved accordingly.
6355inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6356 size_t number_of_new_blocks,
6357 size_t after) {
6358 DCHECK_LT(after, blocks->size());
6359 size_t old_size = blocks->size();
6360 size_t new_size = old_size + number_of_new_blocks;
6361 blocks->resize(new_size);
6362 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6363}
6364
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006365} // namespace art
6366
6367#endif // ART_COMPILER_OPTIMIZING_NODES_H_