blob: c4764ccbb41fbab066808d3bb90f0540917c2b9a [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) \
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001271 M(Arm64BitwiseNegatedRight, Instruction) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001272 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001273 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001274#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001275
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001276#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1277
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001278#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1279
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001280#ifndef ART_ENABLE_CODEGEN_x86
1281#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1282#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001283#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1284 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001285 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001286 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001287 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001288#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001289
1290#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1291
1292#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1293 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001294 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001295 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1296 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001298 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001299 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1300 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1301
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001302#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1303 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001304 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001305 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001306 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001307 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001308
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001309#define FOR_EACH_INSTRUCTION(M) \
1310 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1311 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1312
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001313#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001314FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1315#undef FORWARD_DECLARATION
1316
Roland Levillainccc07a92014-09-16 14:48:16 +01001317#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001318 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001319 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001320 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001321 return other->Is##type(); \
1322 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001323 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001324
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001325#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1326 bool Is##type() const { return As##type() != nullptr; } \
1327 const H##type* As##type() const { return this; } \
1328 H##type* As##type() { return this; }
1329
David Brazdiled596192015-01-23 10:39:45 +00001330template <typename T> class HUseList;
1331
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001332template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001333class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001334 public:
David Brazdiled596192015-01-23 10:39:45 +00001335 HUseListNode* GetPrevious() const { return prev_; }
1336 HUseListNode* GetNext() const { return next_; }
1337 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001338 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001339 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001340
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001341 private:
David Brazdiled596192015-01-23 10:39:45 +00001342 HUseListNode(T user, size_t index)
1343 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1344
1345 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001346 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001347 HUseListNode<T>* prev_;
1348 HUseListNode<T>* next_;
1349
1350 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001351
1352 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1353};
1354
David Brazdiled596192015-01-23 10:39:45 +00001355template <typename T>
1356class HUseList : public ValueObject {
1357 public:
1358 HUseList() : first_(nullptr) {}
1359
1360 void Clear() {
1361 first_ = nullptr;
1362 }
1363
1364 // Adds a new entry at the beginning of the use list and returns
1365 // the newly created node.
1366 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001367 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001368 if (IsEmpty()) {
1369 first_ = new_node;
1370 } else {
1371 first_->prev_ = new_node;
1372 new_node->next_ = first_;
1373 first_ = new_node;
1374 }
1375 return new_node;
1376 }
1377
1378 HUseListNode<T>* GetFirst() const {
1379 return first_;
1380 }
1381
1382 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001383 DCHECK(node != nullptr);
1384 DCHECK(Contains(node));
1385
David Brazdiled596192015-01-23 10:39:45 +00001386 if (node->prev_ != nullptr) {
1387 node->prev_->next_ = node->next_;
1388 }
1389 if (node->next_ != nullptr) {
1390 node->next_->prev_ = node->prev_;
1391 }
1392 if (node == first_) {
1393 first_ = node->next_;
1394 }
1395 }
1396
David Brazdil1abb4192015-02-17 18:33:36 +00001397 bool Contains(const HUseListNode<T>* node) const {
1398 if (node == nullptr) {
1399 return false;
1400 }
1401 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1402 if (current == node) {
1403 return true;
1404 }
1405 }
1406 return false;
1407 }
1408
David Brazdiled596192015-01-23 10:39:45 +00001409 bool IsEmpty() const {
1410 return first_ == nullptr;
1411 }
1412
1413 bool HasOnlyOneUse() const {
1414 return first_ != nullptr && first_->next_ == nullptr;
1415 }
1416
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001417 size_t SizeSlow() const {
1418 size_t count = 0;
1419 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1420 ++count;
1421 }
1422 return count;
1423 }
1424
David Brazdiled596192015-01-23 10:39:45 +00001425 private:
1426 HUseListNode<T>* first_;
1427};
1428
1429template<typename T>
1430class HUseIterator : public ValueObject {
1431 public:
1432 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1433
1434 bool Done() const { return current_ == nullptr; }
1435
1436 void Advance() {
1437 DCHECK(!Done());
1438 current_ = current_->GetNext();
1439 }
1440
1441 HUseListNode<T>* Current() const {
1442 DCHECK(!Done());
1443 return current_;
1444 }
1445
1446 private:
1447 HUseListNode<T>* current_;
1448
1449 friend class HValue;
1450};
1451
David Brazdil1abb4192015-02-17 18:33:36 +00001452// This class is used by HEnvironment and HInstruction classes to record the
1453// instructions they use and pointers to the corresponding HUseListNodes kept
1454// by the used instructions.
1455template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001456class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001457 public:
1458 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1459 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1460
1461 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1462 : instruction_(old_record.instruction_), use_node_(use_node) {
1463 DCHECK(instruction_ != nullptr);
1464 DCHECK(use_node_ != nullptr);
1465 DCHECK(old_record.use_node_ == nullptr);
1466 }
1467
1468 HInstruction* GetInstruction() const { return instruction_; }
1469 HUseListNode<T>* GetUseNode() const { return use_node_; }
1470
1471 private:
1472 // Instruction used by the user.
1473 HInstruction* instruction_;
1474
1475 // Corresponding entry in the use list kept by 'instruction_'.
1476 HUseListNode<T>* use_node_;
1477};
1478
Aart Bik854a02b2015-07-14 16:07:00 -07001479/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001480 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001481 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001482 * For write/read dependences on fields/arrays, the dependence analysis uses
1483 * type disambiguation (e.g. a float field write cannot modify the value of an
1484 * integer field read) and the access type (e.g. a reference array write cannot
1485 * modify the value of a reference field read [although it may modify the
1486 * reference fetch prior to reading the field, which is represented by its own
1487 * write/read dependence]). The analysis makes conservative points-to
1488 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1489 * the same, and any reference read depends on any reference read without
1490 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001491 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001492 * The internal representation uses 38-bit and is described in the table below.
1493 * The first line indicates the side effect, and for field/array accesses the
1494 * second line indicates the type of the access (in the order of the
1495 * Primitive::Type enum).
1496 * The two numbered lines below indicate the bit position in the bitfield (read
1497 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001498 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001499 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1500 * +-------------+---------+---------+--------------+---------+---------+
1501 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1502 * | 3 |333333322|222222221| 1 |111111110|000000000|
1503 * | 7 |654321098|765432109| 8 |765432109|876543210|
1504 *
1505 * Note that, to ease the implementation, 'changes' bits are least significant
1506 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001507 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001508class SideEffects : public ValueObject {
1509 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001510 SideEffects() : flags_(0) {}
1511
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001512 static SideEffects None() {
1513 return SideEffects(0);
1514 }
1515
1516 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001517 return SideEffects(kAllChangeBits | kAllDependOnBits);
1518 }
1519
1520 static SideEffects AllChanges() {
1521 return SideEffects(kAllChangeBits);
1522 }
1523
1524 static SideEffects AllDependencies() {
1525 return SideEffects(kAllDependOnBits);
1526 }
1527
1528 static SideEffects AllExceptGCDependency() {
1529 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1530 }
1531
1532 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001533 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001534 }
1535
Aart Bik34c3ba92015-07-20 14:08:59 -07001536 static SideEffects AllWrites() {
1537 return SideEffects(kAllWrites);
1538 }
1539
1540 static SideEffects AllReads() {
1541 return SideEffects(kAllReads);
1542 }
1543
1544 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1545 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001546 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001547 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001548 }
1549
Aart Bik854a02b2015-07-14 16:07:00 -07001550 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1551 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001552 }
1553
Aart Bik34c3ba92015-07-20 14:08:59 -07001554 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1555 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001556 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001557 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001558 }
1559
1560 static SideEffects ArrayReadOfType(Primitive::Type type) {
1561 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1562 }
1563
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001564 static SideEffects CanTriggerGC() {
1565 return SideEffects(1ULL << kCanTriggerGCBit);
1566 }
1567
1568 static SideEffects DependsOnGC() {
1569 return SideEffects(1ULL << kDependsOnGCBit);
1570 }
1571
Aart Bik854a02b2015-07-14 16:07:00 -07001572 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001573 SideEffects Union(SideEffects other) const {
1574 return SideEffects(flags_ | other.flags_);
1575 }
1576
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001577 SideEffects Exclusion(SideEffects other) const {
1578 return SideEffects(flags_ & ~other.flags_);
1579 }
1580
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001581 void Add(SideEffects other) {
1582 flags_ |= other.flags_;
1583 }
1584
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001585 bool Includes(SideEffects other) const {
1586 return (other.flags_ & flags_) == other.flags_;
1587 }
1588
1589 bool HasSideEffects() const {
1590 return (flags_ & kAllChangeBits);
1591 }
1592
1593 bool HasDependencies() const {
1594 return (flags_ & kAllDependOnBits);
1595 }
1596
1597 // Returns true if there are no side effects or dependencies.
1598 bool DoesNothing() const {
1599 return flags_ == 0;
1600 }
1601
Aart Bik854a02b2015-07-14 16:07:00 -07001602 // Returns true if something is written.
1603 bool DoesAnyWrite() const {
1604 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001605 }
1606
Aart Bik854a02b2015-07-14 16:07:00 -07001607 // Returns true if something is read.
1608 bool DoesAnyRead() const {
1609 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001610 }
1611
Aart Bik854a02b2015-07-14 16:07:00 -07001612 // Returns true if potentially everything is written and read
1613 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001614 bool DoesAllReadWrite() const {
1615 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1616 }
1617
Aart Bik854a02b2015-07-14 16:07:00 -07001618 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001619 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001620 }
1621
Roland Levillain0d5a2812015-11-13 10:07:31 +00001622 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001623 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001624 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1625 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001626 }
1627
1628 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001630 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001631 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 for (int s = kLastBit; s >= 0; s--) {
1633 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1634 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1635 // This is a bit for the GC side effect.
1636 if (current_bit_is_set) {
1637 flags += "GC";
1638 }
Aart Bik854a02b2015-07-14 16:07:00 -07001639 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001640 } else {
1641 // This is a bit for the array/field analysis.
1642 // The underscore character stands for the 'can trigger GC' bit.
1643 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1644 if (current_bit_is_set) {
1645 flags += kDebug[s];
1646 }
1647 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1648 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1649 flags += "|";
1650 }
1651 }
Aart Bik854a02b2015-07-14 16:07:00 -07001652 }
1653 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001654 }
1655
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001656 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001657
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001658 private:
1659 static constexpr int kFieldArrayAnalysisBits = 9;
1660
1661 static constexpr int kFieldWriteOffset = 0;
1662 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1663 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1664 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1665
1666 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1667
1668 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1669 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1670 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1671 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1672
1673 static constexpr int kLastBit = kDependsOnGCBit;
1674 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1675
1676 // Aliases.
1677
1678 static_assert(kChangeBits == kDependOnBits,
1679 "the 'change' bits should match the 'depend on' bits.");
1680
1681 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1682 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1683 static constexpr uint64_t kAllWrites =
1684 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1685 static constexpr uint64_t kAllReads =
1686 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001687
Aart Bik854a02b2015-07-14 16:07:00 -07001688 // Work around the fact that HIR aliases I/F and J/D.
1689 // TODO: remove this interceptor once HIR types are clean
1690 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1691 switch (type) {
1692 case Primitive::kPrimInt:
1693 case Primitive::kPrimFloat:
1694 return TypeFlag(Primitive::kPrimInt, offset) |
1695 TypeFlag(Primitive::kPrimFloat, offset);
1696 case Primitive::kPrimLong:
1697 case Primitive::kPrimDouble:
1698 return TypeFlag(Primitive::kPrimLong, offset) |
1699 TypeFlag(Primitive::kPrimDouble, offset);
1700 default:
1701 return TypeFlag(type, offset);
1702 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001703 }
1704
Aart Bik854a02b2015-07-14 16:07:00 -07001705 // Translates type to bit flag.
1706 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1707 CHECK_NE(type, Primitive::kPrimVoid);
1708 const uint64_t one = 1;
1709 const int shift = type; // 0-based consecutive enum
1710 DCHECK_LE(kFieldWriteOffset, shift);
1711 DCHECK_LT(shift, kArrayWriteOffset);
1712 return one << (type + offset);
1713 }
1714
1715 // Private constructor on direct flags value.
1716 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1717
1718 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001719};
1720
David Brazdiled596192015-01-23 10:39:45 +00001721// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001722class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001723 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001724 HEnvironment(ArenaAllocator* arena,
1725 size_t number_of_vregs,
1726 const DexFile& dex_file,
1727 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001728 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001729 InvokeType invoke_type,
1730 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001731 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1732 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001733 parent_(nullptr),
1734 dex_file_(dex_file),
1735 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001736 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001737 invoke_type_(invoke_type),
1738 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001739 }
1740
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001741 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001742 : HEnvironment(arena,
1743 to_copy.Size(),
1744 to_copy.GetDexFile(),
1745 to_copy.GetMethodIdx(),
1746 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001747 to_copy.GetInvokeType(),
1748 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001749
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001750 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001751 if (parent_ != nullptr) {
1752 parent_->SetAndCopyParentChain(allocator, parent);
1753 } else {
1754 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1755 parent_->CopyFrom(parent);
1756 if (parent->GetParent() != nullptr) {
1757 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1758 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001759 }
David Brazdiled596192015-01-23 10:39:45 +00001760 }
1761
Vladimir Marko71bf8092015-09-15 15:33:14 +01001762 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001763 void CopyFrom(HEnvironment* environment);
1764
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001765 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1766 // input to the loop phi instead. This is for inserting instructions that
1767 // require an environment (like HDeoptimization) in the loop pre-header.
1768 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001769
1770 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001771 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001772 }
1773
1774 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001775 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001776 }
1777
David Brazdil1abb4192015-02-17 18:33:36 +00001778 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001779
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001780 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001781
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001782 HEnvironment* GetParent() const { return parent_; }
1783
1784 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001785 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001786 }
1787
1788 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001789 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001790 }
1791
1792 uint32_t GetDexPc() const {
1793 return dex_pc_;
1794 }
1795
1796 uint32_t GetMethodIdx() const {
1797 return method_idx_;
1798 }
1799
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001800 InvokeType GetInvokeType() const {
1801 return invoke_type_;
1802 }
1803
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001804 const DexFile& GetDexFile() const {
1805 return dex_file_;
1806 }
1807
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001808 HInstruction* GetHolder() const {
1809 return holder_;
1810 }
1811
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001812
1813 bool IsFromInlinedInvoke() const {
1814 return GetParent() != nullptr;
1815 }
1816
David Brazdiled596192015-01-23 10:39:45 +00001817 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001818 // Record instructions' use entries of this environment for constant-time removal.
1819 // It should only be called by HInstruction when a new environment use is added.
1820 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1821 DCHECK(env_use->GetUser() == this);
1822 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001823 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001824 }
David Brazdiled596192015-01-23 10:39:45 +00001825
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001826 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1827 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001828 HEnvironment* parent_;
1829 const DexFile& dex_file_;
1830 const uint32_t method_idx_;
1831 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001832 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001833
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001834 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001835 HInstruction* const holder_;
1836
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001837 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001838
1839 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1840};
1841
Vladimir Markof9f64412015-09-02 14:05:49 +01001842class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001843 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001844 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001845 : previous_(nullptr),
1846 next_(nullptr),
1847 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001848 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001849 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001850 ssa_index_(-1),
David Brazdilb3e773e2016-01-26 11:28:37 +00001851 emitted_at_use_site_(false),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001852 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001853 locations_(nullptr),
1854 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001855 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001856 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001857 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001858
Dave Allison20dfc792014-06-16 20:44:29 -07001859 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001860
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001861#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001862 enum InstructionKind {
1863 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1864 };
1865#undef DECLARE_KIND
1866
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001867 HInstruction* GetNext() const { return next_; }
1868 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001869
Calin Juravle77520bc2015-01-12 18:45:46 +00001870 HInstruction* GetNextDisregardingMoves() const;
1871 HInstruction* GetPreviousDisregardingMoves() const;
1872
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001873 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001874 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001875 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001876 bool IsInBlock() const { return block_ != nullptr; }
1877 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001878 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1879 bool IsIrreducibleLoopHeaderPhi() const {
1880 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1881 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001882
Roland Levillain6b879dd2014-09-22 17:13:44 +01001883 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001884 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001885
1886 virtual void Accept(HGraphVisitor* visitor) = 0;
1887 virtual const char* DebugName() const = 0;
1888
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001890 void SetRawInputAt(size_t index, HInstruction* input) {
1891 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1892 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001893
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001894 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001895
1896 uint32_t GetDexPc() const { return dex_pc_; }
1897
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001898 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001899
Roland Levillaine161a2a2014-10-03 12:45:18 +01001900 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001901 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001902
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001903 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001904 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001905
Calin Juravle10e244f2015-01-26 18:54:32 +00001906 // Does not apply for all instructions, but having this at top level greatly
1907 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001908 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001909 virtual bool CanBeNull() const {
1910 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1911 return true;
1912 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001913
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001914 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001915 return false;
1916 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001917
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001918 virtual bool IsActualObject() const {
1919 return GetType() == Primitive::kPrimNot;
1920 }
1921
Calin Juravle2e768302015-07-28 14:41:11 +00001922 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001923
Calin Juravle61d544b2015-02-23 16:46:57 +00001924 ReferenceTypeInfo GetReferenceTypeInfo() const {
1925 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1926 return reference_type_info_;
1927 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001928
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001929 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001930 DCHECK(user != nullptr);
1931 HUseListNode<HInstruction*>* use =
1932 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1933 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001934 }
1935
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001936 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001937 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001938 HUseListNode<HEnvironment*>* env_use =
1939 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1940 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001941 }
1942
David Brazdil1abb4192015-02-17 18:33:36 +00001943 void RemoveAsUserOfInput(size_t input) {
1944 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1945 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1946 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001947
David Brazdil1abb4192015-02-17 18:33:36 +00001948 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1949 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001950
David Brazdiled596192015-01-23 10:39:45 +00001951 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1952 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001953 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001954 bool HasOnlyOneNonEnvironmentUse() const {
1955 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1956 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001957
Roland Levillain6c82d402014-10-13 16:10:27 +01001958 // Does this instruction strictly dominate `other_instruction`?
1959 // Returns false if this instruction and `other_instruction` are the same.
1960 // Aborts if this instruction and `other_instruction` are both phis.
1961 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001962
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001963 int GetId() const { return id_; }
1964 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001965
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001966 int GetSsaIndex() const { return ssa_index_; }
1967 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1968 bool HasSsaIndex() const { return ssa_index_ != -1; }
1969
1970 bool HasEnvironment() const { return environment_ != nullptr; }
1971 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001972 // Set the `environment_` field. Raw because this method does not
1973 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001974 void SetRawEnvironment(HEnvironment* environment) {
1975 DCHECK(environment_ == nullptr);
1976 DCHECK_EQ(environment->GetHolder(), this);
1977 environment_ = environment;
1978 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001979
1980 // Set the environment of this instruction, copying it from `environment`. While
1981 // copying, the uses lists are being updated.
1982 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001983 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001984 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001985 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001986 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001987 if (environment->GetParent() != nullptr) {
1988 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1989 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001990 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001991
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001992 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1993 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001994 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001995 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001996 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001997 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001998 if (environment->GetParent() != nullptr) {
1999 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2000 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002001 }
2002
Nicolas Geoffray39468442014-09-02 15:17:15 +01002003 // Returns the number of entries in the environment. Typically, that is the
2004 // number of dex registers in a method. It could be more in case of inlining.
2005 size_t EnvironmentSize() const;
2006
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002007 LocationSummary* GetLocations() const { return locations_; }
2008 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002009
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002010 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002011 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002012
Alexandre Rames188d4312015-04-09 18:30:21 +01002013 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2014 // uses of this instruction by `other` are *not* updated.
2015 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2016 ReplaceWith(other);
2017 other->ReplaceInput(this, use_index);
2018 }
2019
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002020 // Move `this` instruction before `cursor`.
2021 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002022
Vladimir Markofb337ea2015-11-25 15:25:10 +00002023 // Move `this` before its first user and out of any loops. If there is no
2024 // out-of-loop user that dominates all other users, move the instruction
2025 // to the end of the out-of-loop common dominator of the user's blocks.
2026 //
2027 // This can be used only on non-throwing instructions with no side effects that
2028 // have at least one use but no environment uses.
2029 void MoveBeforeFirstUserAndOutOfLoops();
2030
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002031#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002032 bool Is##type() const; \
2033 const H##type* As##type() const; \
2034 H##type* As##type();
2035
2036 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2037#undef INSTRUCTION_TYPE_CHECK
2038
2039#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002040 bool Is##type() const { return (As##type() != nullptr); } \
2041 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002042 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002043 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002044#undef INSTRUCTION_TYPE_CHECK
2045
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002046 // Returns whether the instruction can be moved within the graph.
2047 virtual bool CanBeMoved() const { return false; }
2048
2049 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002050 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002051 return false;
2052 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002053
2054 // Returns whether any data encoded in the two instructions is equal.
2055 // This method does not look at the inputs. Both instructions must be
2056 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002057 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002058 return false;
2059 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002060
2061 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002062 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002063 // 2) Their inputs are identical.
2064 bool Equals(HInstruction* other) const;
2065
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002066 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2067 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2068 // the virtual function because the __attribute__((__pure__)) doesn't really
2069 // apply the strong requirement for virtual functions, preventing optimizations.
2070 InstructionKind GetKind() const PURE;
2071 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002072
2073 virtual size_t ComputeHashCode() const {
2074 size_t result = GetKind();
2075 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2076 result = (result * 31) + InputAt(i)->GetId();
2077 }
2078 return result;
2079 }
2080
2081 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002082 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002083 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002084
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002085 size_t GetLifetimePosition() const { return lifetime_position_; }
2086 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2087 LiveInterval* GetLiveInterval() const { return live_interval_; }
2088 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2089 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2090
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002091 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2092
2093 // Returns whether the code generation of the instruction will require to have access
2094 // to the current method. Such instructions are:
2095 // (1): Instructions that require an environment, as calling the runtime requires
2096 // to walk the stack and have the current method stored at a specific stack address.
2097 // (2): Object literals like classes and strings, that are loaded from the dex cache
2098 // fields of the current method.
2099 bool NeedsCurrentMethod() const {
2100 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2101 }
2102
Vladimir Markodc151b22015-10-15 18:02:30 +01002103 // Returns whether the code generation of the instruction will require to have access
2104 // to the dex cache of the current method's declaring class via the current method.
2105 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002106
Mark Mendellc4701932015-04-10 13:18:51 -04002107 // Does this instruction have any use in an environment before
2108 // control flow hits 'other'?
2109 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2110
2111 // Remove all references to environment uses of this instruction.
2112 // The caller must ensure that this is safe to do.
2113 void RemoveEnvironmentUsers();
2114
David Brazdilb3e773e2016-01-26 11:28:37 +00002115 bool IsEmittedAtUseSite() const { return emitted_at_use_site_; }
2116 void MarkEmittedAtUseSite() { emitted_at_use_site_ = true; }
2117
David Brazdil1abb4192015-02-17 18:33:36 +00002118 protected:
2119 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2120 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2121
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002123 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2124
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002125 HInstruction* previous_;
2126 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002127 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002128 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002129
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002130 // An instruction gets an id when it is added to the graph.
2131 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002132 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002133 int id_;
2134
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002135 // When doing liveness analysis, instructions that have uses get an SSA index.
2136 int ssa_index_;
2137
David Brazdilb3e773e2016-01-26 11:28:37 +00002138 // If set, the machine code for this instruction is assumed to be generated by
2139 // its users. Used by liveness analysis to compute use positions accordingly.
2140 bool emitted_at_use_site_;
2141
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002142 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002143 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002144
2145 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002146 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002147
Nicolas Geoffray39468442014-09-02 15:17:15 +01002148 // The environment associated with this instruction. Not null if the instruction
2149 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002150 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002151
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002152 // Set by the code generator.
2153 LocationSummary* locations_;
2154
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002155 // Set by the liveness analysis.
2156 LiveInterval* live_interval_;
2157
2158 // Set by the liveness analysis, this is the position in a linear
2159 // order of blocks where this instruction's live interval start.
2160 size_t lifetime_position_;
2161
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002162 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002163
Calin Juravleacf735c2015-02-12 15:25:22 +00002164 // TODO: for primitive types this should be marked as invalid.
2165 ReferenceTypeInfo reference_type_info_;
2166
David Brazdil1abb4192015-02-17 18:33:36 +00002167 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002168 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002169 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002170 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002171 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002172
2173 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2174};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002175std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002177class HInputIterator : public ValueObject {
2178 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002179 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002180
2181 bool Done() const { return index_ == instruction_->InputCount(); }
2182 HInstruction* Current() const { return instruction_->InputAt(index_); }
2183 void Advance() { index_++; }
2184
2185 private:
2186 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002188
2189 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2190};
2191
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002192class HInstructionIterator : public ValueObject {
2193 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002194 explicit HInstructionIterator(const HInstructionList& instructions)
2195 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002196 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002197 }
2198
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002199 bool Done() const { return instruction_ == nullptr; }
2200 HInstruction* Current() const { return instruction_; }
2201 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002202 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002203 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002204 }
2205
2206 private:
2207 HInstruction* instruction_;
2208 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002209
2210 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002211};
2212
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002213class HBackwardInstructionIterator : public ValueObject {
2214 public:
2215 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2216 : instruction_(instructions.last_instruction_) {
2217 next_ = Done() ? nullptr : instruction_->GetPrevious();
2218 }
2219
2220 bool Done() const { return instruction_ == nullptr; }
2221 HInstruction* Current() const { return instruction_; }
2222 void Advance() {
2223 instruction_ = next_;
2224 next_ = Done() ? nullptr : instruction_->GetPrevious();
2225 }
2226
2227 private:
2228 HInstruction* instruction_;
2229 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002230
2231 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002232};
2233
Vladimir Markof9f64412015-09-02 14:05:49 +01002234template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002235class HTemplateInstruction: public HInstruction {
2236 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002237 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002238 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002239 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002240
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002241 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002243 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002244 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2245 DCHECK_LT(i, N);
2246 return inputs_[i];
2247 }
David Brazdil1abb4192015-02-17 18:33:36 +00002248
2249 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002250 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002251 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002252 }
2253
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002254 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002255 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002256
2257 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002258};
2259
Vladimir Markof9f64412015-09-02 14:05:49 +01002260// HTemplateInstruction specialization for N=0.
2261template<>
2262class HTemplateInstruction<0>: public HInstruction {
2263 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002264 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002265 : HInstruction(side_effects, dex_pc) {}
2266
Vladimir Markof9f64412015-09-02 14:05:49 +01002267 virtual ~HTemplateInstruction() {}
2268
2269 size_t InputCount() const OVERRIDE { return 0; }
2270
2271 protected:
2272 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2273 LOG(FATAL) << "Unreachable";
2274 UNREACHABLE();
2275 }
2276
2277 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2278 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2279 LOG(FATAL) << "Unreachable";
2280 UNREACHABLE();
2281 }
2282
2283 private:
2284 friend class SsaBuilder;
2285};
2286
Dave Allison20dfc792014-06-16 20:44:29 -07002287template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002288class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002289 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002290 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002291 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002292 virtual ~HExpression() {}
2293
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002294 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002295
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002296 protected:
2297 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002298};
2299
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002300// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2301// instruction that branches to the exit block.
2302class HReturnVoid : public HTemplateInstruction<0> {
2303 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002304 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2305 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002306
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002307 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002308
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002309 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002310
2311 private:
2312 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2313};
2314
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002315// Represents dex's RETURN opcodes. A HReturn is a control flow
2316// instruction that branches to the exit block.
2317class HReturn : public HTemplateInstruction<1> {
2318 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002319 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2320 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002321 SetRawInputAt(0, value);
2322 }
2323
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002324 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002325
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002326 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002327
2328 private:
2329 DISALLOW_COPY_AND_ASSIGN(HReturn);
2330};
2331
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002332// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002333// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002334// exit block.
2335class HExit : public HTemplateInstruction<0> {
2336 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002337 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002338
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002339 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002340
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002341 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002342
2343 private:
2344 DISALLOW_COPY_AND_ASSIGN(HExit);
2345};
2346
2347// Jumps from one block to another.
2348class HGoto : public HTemplateInstruction<0> {
2349 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002350 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002351
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002352 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002353
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002354 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002355 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002356 }
2357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002358 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002359
2360 private:
2361 DISALLOW_COPY_AND_ASSIGN(HGoto);
2362};
2363
Roland Levillain9867bc72015-08-05 10:21:34 +01002364class HConstant : public HExpression<0> {
2365 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002366 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2367 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002368
2369 bool CanBeMoved() const OVERRIDE { return true; }
2370
2371 virtual bool IsMinusOne() const { return false; }
2372 virtual bool IsZero() const { return false; }
2373 virtual bool IsOne() const { return false; }
2374
David Brazdil77a48ae2015-09-15 12:34:04 +00002375 virtual uint64_t GetValueAsUint64() const = 0;
2376
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002377 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002378
2379 private:
2380 DISALLOW_COPY_AND_ASSIGN(HConstant);
2381};
2382
2383class HNullConstant : public HConstant {
2384 public:
2385 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2386 return true;
2387 }
2388
David Brazdil77a48ae2015-09-15 12:34:04 +00002389 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2390
Roland Levillain9867bc72015-08-05 10:21:34 +01002391 size_t ComputeHashCode() const OVERRIDE { return 0; }
2392
2393 DECLARE_INSTRUCTION(NullConstant);
2394
2395 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002396 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002397
2398 friend class HGraph;
2399 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2400};
2401
2402// Constants of the type int. Those can be from Dex instructions, or
2403// synthesized (for example with the if-eqz instruction).
2404class HIntConstant : public HConstant {
2405 public:
2406 int32_t GetValue() const { return value_; }
2407
David Brazdil9f389d42015-10-01 14:32:56 +01002408 uint64_t GetValueAsUint64() const OVERRIDE {
2409 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2410 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002411
Roland Levillain9867bc72015-08-05 10:21:34 +01002412 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002413 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002414 return other->AsIntConstant()->value_ == value_;
2415 }
2416
2417 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2418
2419 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2420 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2421 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2422
2423 DECLARE_INSTRUCTION(IntConstant);
2424
2425 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002426 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2427 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2428 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2429 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002430
2431 const int32_t value_;
2432
2433 friend class HGraph;
2434 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2435 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2436 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2437};
2438
2439class HLongConstant : public HConstant {
2440 public:
2441 int64_t GetValue() const { return value_; }
2442
David Brazdil77a48ae2015-09-15 12:34:04 +00002443 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2444
Roland Levillain9867bc72015-08-05 10:21:34 +01002445 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002446 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002447 return other->AsLongConstant()->value_ == value_;
2448 }
2449
2450 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2451
2452 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2453 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2454 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2455
2456 DECLARE_INSTRUCTION(LongConstant);
2457
2458 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002459 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2460 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002461
2462 const int64_t value_;
2463
2464 friend class HGraph;
2465 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2466};
Dave Allison20dfc792014-06-16 20:44:29 -07002467
Roland Levillain31dd3d62016-02-16 12:21:02 +00002468class HFloatConstant : public HConstant {
2469 public:
2470 float GetValue() const { return value_; }
2471
2472 uint64_t GetValueAsUint64() const OVERRIDE {
2473 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2474 }
2475
2476 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2477 DCHECK(other->IsFloatConstant()) << other->DebugName();
2478 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2479 }
2480
2481 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2482
2483 bool IsMinusOne() const OVERRIDE {
2484 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2485 }
2486 bool IsZero() const OVERRIDE {
2487 return value_ == 0.0f;
2488 }
2489 bool IsOne() const OVERRIDE {
2490 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2491 }
2492 bool IsNaN() const {
2493 return std::isnan(value_);
2494 }
2495
2496 DECLARE_INSTRUCTION(FloatConstant);
2497
2498 private:
2499 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2500 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2501 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2502 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2503
2504 const float value_;
2505
2506 // Only the SsaBuilder and HGraph can create floating-point constants.
2507 friend class SsaBuilder;
2508 friend class HGraph;
2509 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2510};
2511
2512class HDoubleConstant : public HConstant {
2513 public:
2514 double GetValue() const { return value_; }
2515
2516 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2517
2518 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2519 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2520 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2521 }
2522
2523 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2524
2525 bool IsMinusOne() const OVERRIDE {
2526 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2527 }
2528 bool IsZero() const OVERRIDE {
2529 return value_ == 0.0;
2530 }
2531 bool IsOne() const OVERRIDE {
2532 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2533 }
2534 bool IsNaN() const {
2535 return std::isnan(value_);
2536 }
2537
2538 DECLARE_INSTRUCTION(DoubleConstant);
2539
2540 private:
2541 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2542 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2543 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2544 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2545
2546 const double value_;
2547
2548 // Only the SsaBuilder and HGraph can create floating-point constants.
2549 friend class SsaBuilder;
2550 friend class HGraph;
2551 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2552};
2553
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002554// Conditional branch. A block ending with an HIf instruction must have
2555// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002556class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002557 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002558 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2559 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002560 SetRawInputAt(0, input);
2561 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002562
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002563 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002564
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002565 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002566 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002567 }
2568
2569 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002570 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002571 }
2572
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002573 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002574
2575 private:
2576 DISALLOW_COPY_AND_ASSIGN(HIf);
2577};
2578
David Brazdilfc6a86a2015-06-26 10:33:45 +00002579
2580// Abstract instruction which marks the beginning and/or end of a try block and
2581// links it to the respective exception handlers. Behaves the same as a Goto in
2582// non-exceptional control flow.
2583// Normal-flow successor is stored at index zero, exception handlers under
2584// higher indices in no particular order.
2585class HTryBoundary : public HTemplateInstruction<0> {
2586 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002587 enum BoundaryKind {
2588 kEntry,
2589 kExit,
2590 };
2591
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002592 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2593 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002594
2595 bool IsControlFlow() const OVERRIDE { return true; }
2596
2597 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002598 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002599
David Brazdild26a4112015-11-10 11:07:31 +00002600 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2601 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2602 }
2603
David Brazdilfc6a86a2015-06-26 10:33:45 +00002604 // Returns whether `handler` is among its exception handlers (non-zero index
2605 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002606 bool HasExceptionHandler(const HBasicBlock& handler) const {
2607 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002608 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002609 }
2610
2611 // If not present already, adds `handler` to its block's list of exception
2612 // handlers.
2613 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002614 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002615 GetBlock()->AddSuccessor(handler);
2616 }
2617 }
2618
David Brazdil56e1acc2015-06-30 15:41:36 +01002619 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002620
David Brazdilffee3d32015-07-06 11:48:53 +01002621 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2622
David Brazdilfc6a86a2015-06-26 10:33:45 +00002623 DECLARE_INSTRUCTION(TryBoundary);
2624
2625 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002626 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002627
2628 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2629};
2630
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002631// Deoptimize to interpreter, upon checking a condition.
2632class HDeoptimize : public HTemplateInstruction<1> {
2633 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002634 // We set CanTriggerGC to prevent any intermediate address to be live
2635 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002636 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002637 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002638 SetRawInputAt(0, cond);
2639 }
2640
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002641 bool CanBeMoved() const OVERRIDE { return true; }
2642 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2643 return true;
2644 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002645 bool NeedsEnvironment() const OVERRIDE { return true; }
2646 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002647
2648 DECLARE_INSTRUCTION(Deoptimize);
2649
2650 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002651 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2652};
2653
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002654// Represents the ArtMethod that was passed as a first argument to
2655// the method. It is used by instructions that depend on it, like
2656// instructions that work with the dex cache.
2657class HCurrentMethod : public HExpression<0> {
2658 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002659 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2660 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002661
2662 DECLARE_INSTRUCTION(CurrentMethod);
2663
2664 private:
2665 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2666};
2667
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002668// Fetches an ArtMethod from the virtual table or the interface method table
2669// of a class.
2670class HClassTableGet : public HExpression<1> {
2671 public:
2672 enum TableKind {
2673 kVTable,
2674 kIMTable,
2675 };
2676 HClassTableGet(HInstruction* cls,
2677 Primitive::Type type,
2678 TableKind kind,
2679 size_t index,
2680 uint32_t dex_pc)
2681 : HExpression(type, SideEffects::None(), dex_pc),
2682 index_(index),
2683 table_kind_(kind) {
2684 SetRawInputAt(0, cls);
2685 }
2686
2687 bool CanBeMoved() const OVERRIDE { return true; }
2688 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2689 return other->AsClassTableGet()->GetIndex() == index_ &&
2690 other->AsClassTableGet()->GetTableKind() == table_kind_;
2691 }
2692
2693 TableKind GetTableKind() const { return table_kind_; }
2694 size_t GetIndex() const { return index_; }
2695
2696 DECLARE_INSTRUCTION(ClassTableGet);
2697
2698 private:
2699 // The index of the ArtMethod in the table.
2700 const size_t index_;
2701 const TableKind table_kind_;
2702
2703 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2704};
2705
Mark Mendellfe57faa2015-09-18 09:26:15 -04002706// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2707// have one successor for each entry in the switch table, and the final successor
2708// will be the block containing the next Dex opcode.
2709class HPackedSwitch : public HTemplateInstruction<1> {
2710 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002711 HPackedSwitch(int32_t start_value,
2712 uint32_t num_entries,
2713 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002714 uint32_t dex_pc = kNoDexPc)
2715 : HTemplateInstruction(SideEffects::None(), dex_pc),
2716 start_value_(start_value),
2717 num_entries_(num_entries) {
2718 SetRawInputAt(0, input);
2719 }
2720
2721 bool IsControlFlow() const OVERRIDE { return true; }
2722
2723 int32_t GetStartValue() const { return start_value_; }
2724
Vladimir Marko211c2112015-09-24 16:52:33 +01002725 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002726
2727 HBasicBlock* GetDefaultBlock() const {
2728 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002729 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002730 }
2731 DECLARE_INSTRUCTION(PackedSwitch);
2732
2733 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002734 const int32_t start_value_;
2735 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002736
2737 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2738};
2739
Roland Levillain88cb1752014-10-20 16:36:47 +01002740class HUnaryOperation : public HExpression<1> {
2741 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002742 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2743 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002744 SetRawInputAt(0, input);
2745 }
2746
2747 HInstruction* GetInput() const { return InputAt(0); }
2748 Primitive::Type GetResultType() const { return GetType(); }
2749
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002750 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002751 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002752 return true;
2753 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002754
Roland Levillain31dd3d62016-02-16 12:21:02 +00002755 // Try to statically evaluate `this` and return a HConstant
2756 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002757 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002758 HConstant* TryStaticEvaluation() const;
2759
2760 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002761 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2762 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002763 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2764 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002765
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002766 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002767
2768 private:
2769 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2770};
2771
Dave Allison20dfc792014-06-16 20:44:29 -07002772class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002773 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002774 HBinaryOperation(Primitive::Type result_type,
2775 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002776 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002777 SideEffects side_effects = SideEffects::None(),
2778 uint32_t dex_pc = kNoDexPc)
2779 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002780 SetRawInputAt(0, left);
2781 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002782 }
2783
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002784 HInstruction* GetLeft() const { return InputAt(0); }
2785 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002786 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002787
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002788 virtual bool IsCommutative() const { return false; }
2789
2790 // Put constant on the right.
2791 // Returns whether order is changed.
2792 bool OrderInputsWithConstantOnTheRight() {
2793 HInstruction* left = InputAt(0);
2794 HInstruction* right = InputAt(1);
2795 if (left->IsConstant() && !right->IsConstant()) {
2796 ReplaceInput(right, 0);
2797 ReplaceInput(left, 1);
2798 return true;
2799 }
2800 return false;
2801 }
2802
2803 // Order inputs by instruction id, but favor constant on the right side.
2804 // This helps GVN for commutative ops.
2805 void OrderInputs() {
2806 DCHECK(IsCommutative());
2807 HInstruction* left = InputAt(0);
2808 HInstruction* right = InputAt(1);
2809 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2810 return;
2811 }
2812 if (OrderInputsWithConstantOnTheRight()) {
2813 return;
2814 }
2815 // Order according to instruction id.
2816 if (left->GetId() > right->GetId()) {
2817 ReplaceInput(right, 0);
2818 ReplaceInput(left, 1);
2819 }
2820 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002822 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002823 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002824 return true;
2825 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002826
Roland Levillain31dd3d62016-02-16 12:21:02 +00002827 // Try to statically evaluate `this` and return a HConstant
2828 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002829 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002830 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002831
2832 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002833 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2834 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002835 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2836 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002837 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002838 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2839 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002840 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2841 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002842 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2843 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002844 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002845 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2846 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002847
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002848 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002849 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002850 HConstant* GetConstantRight() const;
2851
2852 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002853 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002854 HInstruction* GetLeastConstantLeft() const;
2855
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002856 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002857
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002858 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002859 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2860};
2861
Mark Mendellc4701932015-04-10 13:18:51 -04002862// The comparison bias applies for floating point operations and indicates how NaN
2863// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002864enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002865 kNoBias, // bias is not applicable (i.e. for long operation)
2866 kGtBias, // return 1 for NaN comparisons
2867 kLtBias, // return -1 for NaN comparisons
2868};
2869
Roland Levillain31dd3d62016-02-16 12:21:02 +00002870std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2871
Dave Allison20dfc792014-06-16 20:44:29 -07002872class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002873 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002874 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2875 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002876 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002877
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002878 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002879 // `instruction`, and disregard moves in between.
2880 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002881
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002882 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002883
2884 virtual IfCondition GetCondition() const = 0;
2885
Mark Mendellc4701932015-04-10 13:18:51 -04002886 virtual IfCondition GetOppositeCondition() const = 0;
2887
Roland Levillain4fa13f62015-07-06 18:11:54 +01002888 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002889 ComparisonBias GetBias() const { return bias_; }
Mark Mendellc4701932015-04-10 13:18:51 -04002890 void SetBias(ComparisonBias bias) { bias_ = bias; }
2891
2892 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2893 return bias_ == other->AsCondition()->bias_;
2894 }
2895
Roland Levillain4fa13f62015-07-06 18:11:54 +01002896 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002897 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002898 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002899 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002900 }
2901
2902 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002903 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002904 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002905 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002906 }
2907
Roland Levillain31dd3d62016-02-16 12:21:02 +00002908 protected:
2909 template <typename T>
2910 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
2911
2912 template <typename T>
2913 int32_t CompareFP(T x, T y) const {
2914 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
2915 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
2916 // Handle the bias.
2917 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
2918 }
2919
2920 // Return an integer constant containing the result of a condition evaluated at compile time.
2921 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
2922 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
2923 }
2924
Dave Allison20dfc792014-06-16 20:44:29 -07002925 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002926 // Needed if we merge a HCompare into a HCondition.
2927 ComparisonBias bias_;
2928
Dave Allison20dfc792014-06-16 20:44:29 -07002929 DISALLOW_COPY_AND_ASSIGN(HCondition);
2930};
2931
2932// Instruction to check if two inputs are equal to each other.
2933class HEqual : public HCondition {
2934 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002935 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2936 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002937
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002938 bool IsCommutative() const OVERRIDE { return true; }
2939
Vladimir Marko9e23df52015-11-10 17:14:35 +00002940 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2941 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002942 return MakeConstantCondition(true, GetDexPc());
2943 }
2944 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2945 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
2946 }
2947 // In the following Evaluate methods, a HCompare instruction has
2948 // been merged into this HEqual instruction; evaluate it as
2949 // `Compare(x, y) == 0`.
2950 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2951 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
2952 GetDexPc());
2953 }
2954 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
2955 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
2956 }
2957 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
2958 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00002959 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002960
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002961 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002962
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002963 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002964 return kCondEQ;
2965 }
2966
Mark Mendellc4701932015-04-10 13:18:51 -04002967 IfCondition GetOppositeCondition() const OVERRIDE {
2968 return kCondNE;
2969 }
2970
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002971 private:
Aart Bike9f37602015-10-09 11:15:55 -07002972 template <typename T> bool Compute(T x, T y) const { return x == y; }
2973
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002974 DISALLOW_COPY_AND_ASSIGN(HEqual);
2975};
2976
Dave Allison20dfc792014-06-16 20:44:29 -07002977class HNotEqual : public HCondition {
2978 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002979 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2980 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002981
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002982 bool IsCommutative() const OVERRIDE { return true; }
2983
Vladimir Marko9e23df52015-11-10 17:14:35 +00002984 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2985 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002986 return MakeConstantCondition(false, GetDexPc());
2987 }
2988 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2989 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
2990 }
2991 // In the following Evaluate methods, a HCompare instruction has
2992 // been merged into this HNotEqual instruction; evaluate it as
2993 // `Compare(x, y) != 0`.
2994 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2995 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
2996 }
2997 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
2998 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
2999 }
3000 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3001 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003002 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003003
Dave Allison20dfc792014-06-16 20:44:29 -07003004 DECLARE_INSTRUCTION(NotEqual);
3005
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003006 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003007 return kCondNE;
3008 }
3009
Mark Mendellc4701932015-04-10 13:18:51 -04003010 IfCondition GetOppositeCondition() const OVERRIDE {
3011 return kCondEQ;
3012 }
3013
Dave Allison20dfc792014-06-16 20:44:29 -07003014 private:
Aart Bike9f37602015-10-09 11:15:55 -07003015 template <typename T> bool Compute(T x, T y) const { return x != y; }
3016
Dave Allison20dfc792014-06-16 20:44:29 -07003017 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3018};
3019
3020class HLessThan : public HCondition {
3021 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003022 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3023 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003024
Roland Levillain9867bc72015-08-05 10:21:34 +01003025 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003026 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003027 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028 // In the following Evaluate methods, a HCompare instruction has
3029 // been merged into this HLessThan instruction; evaluate it as
3030 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003031 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003032 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3033 }
3034 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3035 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3036 }
3037 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3038 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003039 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003040
Dave Allison20dfc792014-06-16 20:44:29 -07003041 DECLARE_INSTRUCTION(LessThan);
3042
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003043 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003044 return kCondLT;
3045 }
3046
Mark Mendellc4701932015-04-10 13:18:51 -04003047 IfCondition GetOppositeCondition() const OVERRIDE {
3048 return kCondGE;
3049 }
3050
Dave Allison20dfc792014-06-16 20:44:29 -07003051 private:
Aart Bike9f37602015-10-09 11:15:55 -07003052 template <typename T> bool Compute(T x, T y) const { return x < y; }
3053
Dave Allison20dfc792014-06-16 20:44:29 -07003054 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3055};
3056
3057class HLessThanOrEqual : public HCondition {
3058 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003059 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3060 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003061
Roland Levillain9867bc72015-08-05 10:21:34 +01003062 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003064 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003065 // In the following Evaluate methods, a HCompare instruction has
3066 // been merged into this HLessThanOrEqual instruction; evaluate it as
3067 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003068 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3070 }
3071 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3072 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3073 }
3074 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3075 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003076 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003077
Dave Allison20dfc792014-06-16 20:44:29 -07003078 DECLARE_INSTRUCTION(LessThanOrEqual);
3079
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003080 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003081 return kCondLE;
3082 }
3083
Mark Mendellc4701932015-04-10 13:18:51 -04003084 IfCondition GetOppositeCondition() const OVERRIDE {
3085 return kCondGT;
3086 }
3087
Dave Allison20dfc792014-06-16 20:44:29 -07003088 private:
Aart Bike9f37602015-10-09 11:15:55 -07003089 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3090
Dave Allison20dfc792014-06-16 20:44:29 -07003091 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3092};
3093
3094class HGreaterThan : public HCondition {
3095 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003096 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3097 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003098
Roland Levillain9867bc72015-08-05 10:21:34 +01003099 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003100 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003101 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003102 // In the following Evaluate methods, a HCompare instruction has
3103 // been merged into this HGreaterThan instruction; evaluate it as
3104 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003105 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003106 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3107 }
3108 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3109 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3110 }
3111 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3112 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003113 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003114
Dave Allison20dfc792014-06-16 20:44:29 -07003115 DECLARE_INSTRUCTION(GreaterThan);
3116
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003117 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003118 return kCondGT;
3119 }
3120
Mark Mendellc4701932015-04-10 13:18:51 -04003121 IfCondition GetOppositeCondition() const OVERRIDE {
3122 return kCondLE;
3123 }
3124
Dave Allison20dfc792014-06-16 20:44:29 -07003125 private:
Aart Bike9f37602015-10-09 11:15:55 -07003126 template <typename T> bool Compute(T x, T y) const { return x > y; }
3127
Dave Allison20dfc792014-06-16 20:44:29 -07003128 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3129};
3130
3131class HGreaterThanOrEqual : public HCondition {
3132 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003133 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3134 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003135
Roland Levillain9867bc72015-08-05 10:21:34 +01003136 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003137 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003138 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003139 // In the following Evaluate methods, a HCompare instruction has
3140 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3141 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003142 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003143 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3144 }
3145 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3146 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3147 }
3148 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3149 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003150 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003151
Dave Allison20dfc792014-06-16 20:44:29 -07003152 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3153
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003154 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003155 return kCondGE;
3156 }
3157
Mark Mendellc4701932015-04-10 13:18:51 -04003158 IfCondition GetOppositeCondition() const OVERRIDE {
3159 return kCondLT;
3160 }
3161
Dave Allison20dfc792014-06-16 20:44:29 -07003162 private:
Aart Bike9f37602015-10-09 11:15:55 -07003163 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3164
Dave Allison20dfc792014-06-16 20:44:29 -07003165 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3166};
3167
Aart Bike9f37602015-10-09 11:15:55 -07003168class HBelow : public HCondition {
3169 public:
3170 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3171 : HCondition(first, second, dex_pc) {}
3172
3173 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003174 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003175 }
3176 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003177 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3178 }
3179 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3180 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3181 LOG(FATAL) << DebugName() << " is not defined for float values";
3182 UNREACHABLE();
3183 }
3184 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3185 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3186 LOG(FATAL) << DebugName() << " is not defined for double values";
3187 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003188 }
3189
3190 DECLARE_INSTRUCTION(Below);
3191
3192 IfCondition GetCondition() const OVERRIDE {
3193 return kCondB;
3194 }
3195
3196 IfCondition GetOppositeCondition() const OVERRIDE {
3197 return kCondAE;
3198 }
3199
3200 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003201 template <typename T> bool Compute(T x, T y) const {
3202 return MakeUnsigned(x) < MakeUnsigned(y);
3203 }
Aart Bike9f37602015-10-09 11:15:55 -07003204
3205 DISALLOW_COPY_AND_ASSIGN(HBelow);
3206};
3207
3208class HBelowOrEqual : public HCondition {
3209 public:
3210 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3211 : HCondition(first, second, dex_pc) {}
3212
3213 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003214 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003215 }
3216 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003217 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3218 }
3219 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3220 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3221 LOG(FATAL) << DebugName() << " is not defined for float values";
3222 UNREACHABLE();
3223 }
3224 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3225 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3226 LOG(FATAL) << DebugName() << " is not defined for double values";
3227 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003228 }
3229
3230 DECLARE_INSTRUCTION(BelowOrEqual);
3231
3232 IfCondition GetCondition() const OVERRIDE {
3233 return kCondBE;
3234 }
3235
3236 IfCondition GetOppositeCondition() const OVERRIDE {
3237 return kCondA;
3238 }
3239
3240 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003241 template <typename T> bool Compute(T x, T y) const {
3242 return MakeUnsigned(x) <= MakeUnsigned(y);
3243 }
Aart Bike9f37602015-10-09 11:15:55 -07003244
3245 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3246};
3247
3248class HAbove : public HCondition {
3249 public:
3250 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3251 : HCondition(first, second, dex_pc) {}
3252
3253 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003254 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003255 }
3256 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003257 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3258 }
3259 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3260 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3261 LOG(FATAL) << DebugName() << " is not defined for float values";
3262 UNREACHABLE();
3263 }
3264 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3265 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3266 LOG(FATAL) << DebugName() << " is not defined for double values";
3267 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003268 }
3269
3270 DECLARE_INSTRUCTION(Above);
3271
3272 IfCondition GetCondition() const OVERRIDE {
3273 return kCondA;
3274 }
3275
3276 IfCondition GetOppositeCondition() const OVERRIDE {
3277 return kCondBE;
3278 }
3279
3280 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003281 template <typename T> bool Compute(T x, T y) const {
3282 return MakeUnsigned(x) > MakeUnsigned(y);
3283 }
Aart Bike9f37602015-10-09 11:15:55 -07003284
3285 DISALLOW_COPY_AND_ASSIGN(HAbove);
3286};
3287
3288class HAboveOrEqual : public HCondition {
3289 public:
3290 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3291 : HCondition(first, second, dex_pc) {}
3292
3293 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003294 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003295 }
3296 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003297 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3298 }
3299 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3300 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3301 LOG(FATAL) << DebugName() << " is not defined for float values";
3302 UNREACHABLE();
3303 }
3304 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3305 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3306 LOG(FATAL) << DebugName() << " is not defined for double values";
3307 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003308 }
3309
3310 DECLARE_INSTRUCTION(AboveOrEqual);
3311
3312 IfCondition GetCondition() const OVERRIDE {
3313 return kCondAE;
3314 }
3315
3316 IfCondition GetOppositeCondition() const OVERRIDE {
3317 return kCondB;
3318 }
3319
3320 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003321 template <typename T> bool Compute(T x, T y) const {
3322 return MakeUnsigned(x) >= MakeUnsigned(y);
3323 }
Aart Bike9f37602015-10-09 11:15:55 -07003324
3325 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3326};
Dave Allison20dfc792014-06-16 20:44:29 -07003327
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003328// Instruction to check how two inputs compare to each other.
3329// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3330class HCompare : public HBinaryOperation {
3331 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003332 HCompare(Primitive::Type type,
3333 HInstruction* first,
3334 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003335 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003336 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003337 : HBinaryOperation(Primitive::kPrimInt,
3338 first,
3339 second,
3340 SideEffectsForArchRuntimeCalls(type),
3341 dex_pc),
3342 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003343 DCHECK_EQ(type, first->GetType());
3344 DCHECK_EQ(type, second->GetType());
3345 }
3346
Roland Levillain9867bc72015-08-05 10:21:34 +01003347 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003348 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3349
3350 template <typename T>
3351 int32_t ComputeFP(T x, T y) const {
3352 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3353 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3354 // Handle the bias.
3355 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3356 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003357
Roland Levillain9867bc72015-08-05 10:21:34 +01003358 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003359 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3360 // reach this code path when processing a freshly built HIR
3361 // graph. However HCompare integer instructions can be synthesized
3362 // by the instruction simplifier to implement IntegerCompare and
3363 // IntegerSignum intrinsics, so we have to handle this case.
3364 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003365 }
3366 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003367 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3368 }
3369 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3370 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3371 }
3372 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3373 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003374 }
3375
Calin Juravleddb7df22014-11-25 20:56:51 +00003376 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3377 return bias_ == other->AsCompare()->bias_;
3378 }
3379
Mark Mendellc4701932015-04-10 13:18:51 -04003380 ComparisonBias GetBias() const { return bias_; }
3381
Roland Levillain31dd3d62016-02-16 12:21:02 +00003382 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
3383 // Only meaninfgul for floating-point comparisons.
3384 bool IsGtBias() const {
3385 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3386 return bias_ == ComparisonBias::kGtBias;
3387 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003388
3389 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3390 // MIPS64 uses a runtime call for FP comparisons.
3391 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3392 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003393
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003394 DECLARE_INSTRUCTION(Compare);
3395
Roland Levillain31dd3d62016-02-16 12:21:02 +00003396 protected:
3397 // Return an integer constant containing the result of a comparison evaluated at compile time.
3398 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3399 DCHECK(value == -1 || value == 0 || value == 1) << value;
3400 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3401 }
3402
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003403 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003404 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003405
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003406 DISALLOW_COPY_AND_ASSIGN(HCompare);
3407};
3408
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003409// A local in the graph. Corresponds to a Dex register.
3410class HLocal : public HTemplateInstruction<0> {
3411 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003412 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003413 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003414
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003415 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003416
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003417 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003418
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003419 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003420 // The Dex register number.
3421 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003422
3423 DISALLOW_COPY_AND_ASSIGN(HLocal);
3424};
3425
3426// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003427class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003429 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3430 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003431 SetRawInputAt(0, local);
3432 }
3433
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003434 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3435
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003436 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003437
3438 private:
3439 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3440};
3441
3442// Store a value in a given local. This instruction has two inputs: the value
3443// and the local.
3444class HStoreLocal : public HTemplateInstruction<2> {
3445 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003446 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3447 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003448 SetRawInputAt(0, local);
3449 SetRawInputAt(1, value);
3450 }
3451
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003452 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3453
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003454 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003455
3456 private:
3457 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3458};
3459
David Brazdil6de19382016-01-08 17:37:10 +00003460class HNewInstance : public HExpression<2> {
3461 public:
3462 HNewInstance(HInstruction* cls,
3463 HCurrentMethod* current_method,
3464 uint32_t dex_pc,
3465 uint16_t type_index,
3466 const DexFile& dex_file,
3467 bool can_throw,
3468 bool finalizable,
3469 QuickEntrypointEnum entrypoint)
3470 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3471 type_index_(type_index),
3472 dex_file_(dex_file),
3473 can_throw_(can_throw),
3474 finalizable_(finalizable),
3475 entrypoint_(entrypoint) {
3476 SetRawInputAt(0, cls);
3477 SetRawInputAt(1, current_method);
3478 }
3479
3480 uint16_t GetTypeIndex() const { return type_index_; }
3481 const DexFile& GetDexFile() const { return dex_file_; }
3482
3483 // Calls runtime so needs an environment.
3484 bool NeedsEnvironment() const OVERRIDE { return true; }
3485
3486 // It may throw when called on type that's not instantiable/accessible.
3487 // It can throw OOME.
3488 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3489 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3490
3491 bool IsFinalizable() const { return finalizable_; }
3492
3493 bool CanBeNull() const OVERRIDE { return false; }
3494
3495 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3496
3497 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3498 entrypoint_ = entrypoint;
3499 }
3500
3501 bool IsStringAlloc() const;
3502
3503 DECLARE_INSTRUCTION(NewInstance);
3504
3505 private:
3506 const uint16_t type_index_;
3507 const DexFile& dex_file_;
3508 const bool can_throw_;
3509 const bool finalizable_;
3510 QuickEntrypointEnum entrypoint_;
3511
3512 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3513};
3514
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003515enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003516#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3517 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003518#include "intrinsics_list.h"
3519 kNone,
3520 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3521#undef INTRINSICS_LIST
3522#undef OPTIMIZING_INTRINSICS
3523};
3524std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3525
Agi Csaki05f20562015-08-19 14:58:14 -07003526enum IntrinsicNeedsEnvironmentOrCache {
3527 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3528 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003529};
3530
Aart Bik5d75afe2015-12-14 11:57:01 -08003531enum IntrinsicSideEffects {
3532 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3533 kReadSideEffects, // Intrinsic may read heap memory.
3534 kWriteSideEffects, // Intrinsic may write heap memory.
3535 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3536};
3537
3538enum IntrinsicExceptions {
3539 kNoThrow, // Intrinsic does not throw any exceptions.
3540 kCanThrow // Intrinsic may throw exceptions.
3541};
3542
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003543class HInvoke : public HInstruction {
3544 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003545 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003546
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003547 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003548
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003549 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003550 SetRawInputAt(index, argument);
3551 }
3552
Roland Levillain3e3d7332015-04-28 11:00:54 +01003553 // Return the number of arguments. This number can be lower than
3554 // the number of inputs returned by InputCount(), as some invoke
3555 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3556 // inputs at the end of their list of inputs.
3557 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3558
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003559 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003560
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003561 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003562 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003563
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003564 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3565
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003566 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003567 return intrinsic_;
3568 }
3569
Aart Bik5d75afe2015-12-14 11:57:01 -08003570 void SetIntrinsic(Intrinsics intrinsic,
3571 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3572 IntrinsicSideEffects side_effects,
3573 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003574
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003575 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003576 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003577 }
3578
Aart Bik5d75afe2015-12-14 11:57:01 -08003579 bool CanThrow() const OVERRIDE { return can_throw_; }
3580
3581 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3582
3583 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3584 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3585 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003586
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003587 uint32_t* GetIntrinsicOptimizations() {
3588 return &intrinsic_optimizations_;
3589 }
3590
3591 const uint32_t* GetIntrinsicOptimizations() const {
3592 return &intrinsic_optimizations_;
3593 }
3594
3595 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3596
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003597 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003598
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003599 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003600 HInvoke(ArenaAllocator* arena,
3601 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003602 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003603 Primitive::Type return_type,
3604 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003605 uint32_t dex_method_index,
3606 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003607 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003608 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003609 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003610 inputs_(number_of_arguments + number_of_other_inputs,
3611 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003612 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003613 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003614 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003615 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003616 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003617 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003618 }
3619
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003620 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003621 return inputs_[index];
3622 }
3623
David Brazdil1abb4192015-02-17 18:33:36 +00003624 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003625 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003626 }
3627
Aart Bik5d75afe2015-12-14 11:57:01 -08003628 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3629
Roland Levillain3e3d7332015-04-28 11:00:54 +01003630 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003631 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003632 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003633 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003634 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003635 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003636 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003637
3638 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3639 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003640
3641 private:
3642 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3643};
3644
Calin Juravle175dc732015-08-25 15:42:32 +01003645class HInvokeUnresolved : public HInvoke {
3646 public:
3647 HInvokeUnresolved(ArenaAllocator* arena,
3648 uint32_t number_of_arguments,
3649 Primitive::Type return_type,
3650 uint32_t dex_pc,
3651 uint32_t dex_method_index,
3652 InvokeType invoke_type)
3653 : HInvoke(arena,
3654 number_of_arguments,
3655 0u /* number_of_other_inputs */,
3656 return_type,
3657 dex_pc,
3658 dex_method_index,
3659 invoke_type) {
3660 }
3661
3662 DECLARE_INSTRUCTION(InvokeUnresolved);
3663
3664 private:
3665 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3666};
3667
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003668class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003669 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003670 // Requirements of this method call regarding the class
3671 // initialization (clinit) check of its declaring class.
3672 enum class ClinitCheckRequirement {
3673 kNone, // Class already initialized.
3674 kExplicit, // Static call having explicit clinit check as last input.
3675 kImplicit, // Static call implicitly requiring a clinit check.
3676 };
3677
Vladimir Marko58155012015-08-19 12:49:41 +00003678 // Determines how to load the target ArtMethod*.
3679 enum class MethodLoadKind {
3680 // Use a String init ArtMethod* loaded from Thread entrypoints.
3681 kStringInit,
3682
3683 // Use the method's own ArtMethod* loaded by the register allocator.
3684 kRecursive,
3685
3686 // Use ArtMethod* at a known address, embed the direct address in the code.
3687 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3688 kDirectAddress,
3689
3690 // Use ArtMethod* at an address that will be known at link time, embed the direct
3691 // address in the code. If the image is relocatable, emit .patch_oat entry.
3692 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3693 // the image relocatable or not.
3694 kDirectAddressWithFixup,
3695
3696 // Load from resoved methods array in the dex cache using a PC-relative load.
3697 // Used when we need to use the dex cache, for example for invoke-static that
3698 // may cause class initialization (the entry may point to a resolution method),
3699 // and we know that we can access the dex cache arrays using a PC-relative load.
3700 kDexCachePcRelative,
3701
3702 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3703 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3704 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3705 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3706 kDexCacheViaMethod,
3707 };
3708
3709 // Determines the location of the code pointer.
3710 enum class CodePtrLocation {
3711 // Recursive call, use local PC-relative call instruction.
3712 kCallSelf,
3713
3714 // Use PC-relative call instruction patched at link time.
3715 // Used for calls within an oat file, boot->boot or app->app.
3716 kCallPCRelative,
3717
3718 // Call to a known target address, embed the direct address in code.
3719 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3720 kCallDirect,
3721
3722 // Call to a target address that will be known at link time, embed the direct
3723 // address in code. If the image is relocatable, emit .patch_oat entry.
3724 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3725 // the image relocatable or not.
3726 kCallDirectWithFixup,
3727
3728 // Use code pointer from the ArtMethod*.
3729 // Used when we don't know the target code. This is also the last-resort-kind used when
3730 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3731 kCallArtMethod,
3732 };
3733
3734 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003735 MethodLoadKind method_load_kind;
3736 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003737 // The method load data holds
3738 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3739 // Note that there are multiple string init methods, each having its own offset.
3740 // - the method address for kDirectAddress
3741 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003742 uint64_t method_load_data;
3743 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003744 };
3745
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003746 HInvokeStaticOrDirect(ArenaAllocator* arena,
3747 uint32_t number_of_arguments,
3748 Primitive::Type return_type,
3749 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003750 uint32_t method_index,
3751 MethodReference target_method,
3752 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003753 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003754 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003755 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003756 : HInvoke(arena,
3757 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003758 // There is potentially one extra argument for the HCurrentMethod node, and
3759 // potentially one other if the clinit check is explicit, and potentially
3760 // one other if the method is a string factory.
3761 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3762 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3763 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003764 return_type,
3765 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003766 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003767 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003768 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003769 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003770 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003771 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003772
Vladimir Markodc151b22015-10-15 18:02:30 +01003773 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003774 bool had_current_method_input = HasCurrentMethodInput();
3775 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3776
3777 // Using the current method is the default and once we find a better
3778 // method load kind, we should not go back to using the current method.
3779 DCHECK(had_current_method_input || !needs_current_method_input);
3780
3781 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003782 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3783 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003784 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003785 dispatch_info_ = dispatch_info;
3786 }
3787
Vladimir Markoc53c0792015-11-19 15:48:33 +00003788 void AddSpecialInput(HInstruction* input) {
3789 // We allow only one special input.
3790 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3791 DCHECK(InputCount() == GetSpecialInputIndex() ||
3792 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3793 InsertInputAt(GetSpecialInputIndex(), input);
3794 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003795
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003796 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003797 // We access the method via the dex cache so we can't do an implicit null check.
3798 // TODO: for intrinsics we can generate implicit null checks.
3799 return false;
3800 }
3801
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003802 bool CanBeNull() const OVERRIDE {
3803 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3804 }
3805
Vladimir Markoc53c0792015-11-19 15:48:33 +00003806 // Get the index of the special input, if any.
3807 //
David Brazdil6de19382016-01-08 17:37:10 +00003808 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3809 // method pointer; otherwise there may be one platform-specific special input,
3810 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003811 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003812 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003813
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003814 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3815 void SetOptimizedInvokeType(InvokeType invoke_type) {
3816 optimized_invoke_type_ = invoke_type;
3817 }
3818
Vladimir Marko58155012015-08-19 12:49:41 +00003819 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3820 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3821 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003822 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003823 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003824 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003825 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003826 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3827 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003828 bool HasCurrentMethodInput() const {
3829 // This function can be called only after the invoke has been fully initialized by the builder.
3830 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003831 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003832 return true;
3833 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003834 DCHECK(InputCount() == GetSpecialInputIndex() ||
3835 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003836 return false;
3837 }
3838 }
Vladimir Marko58155012015-08-19 12:49:41 +00003839 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3840 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003841 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003842
3843 int32_t GetStringInitOffset() const {
3844 DCHECK(IsStringInit());
3845 return dispatch_info_.method_load_data;
3846 }
3847
3848 uint64_t GetMethodAddress() const {
3849 DCHECK(HasMethodAddress());
3850 return dispatch_info_.method_load_data;
3851 }
3852
3853 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003854 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003855 return dispatch_info_.method_load_data;
3856 }
3857
3858 uint64_t GetDirectCodePtr() const {
3859 DCHECK(HasDirectCodePtr());
3860 return dispatch_info_.direct_code_ptr;
3861 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003862
Calin Juravle68ad6492015-08-18 17:08:12 +01003863 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3864
Roland Levillain4c0eb422015-04-24 16:43:49 +01003865 // Is this instruction a call to a static method?
3866 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003867 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003868 }
3869
Vladimir Markofbb184a2015-11-13 14:47:00 +00003870 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3871 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3872 // instruction; only relevant for static calls with explicit clinit check.
3873 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003874 DCHECK(IsStaticWithExplicitClinitCheck());
3875 size_t last_input_index = InputCount() - 1;
3876 HInstruction* last_input = InputAt(last_input_index);
3877 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003878 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003879 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003880 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003881 clinit_check_requirement_ = new_requirement;
3882 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003883 }
3884
David Brazdilbc9ab162016-01-20 14:50:19 +00003885 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003886 DCHECK(IsStringInit());
3887 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003888 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003889 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003890 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003891 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003892 }
3893
Roland Levillain4c0eb422015-04-24 16:43:49 +01003894 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003895 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003896 bool IsStaticWithExplicitClinitCheck() const {
3897 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3898 }
3899
3900 // Is this a call to a static method whose declaring class has an
3901 // implicit intialization check requirement?
3902 bool IsStaticWithImplicitClinitCheck() const {
3903 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3904 }
3905
Vladimir Markob554b5a2015-11-06 12:57:55 +00003906 // Does this method load kind need the current method as an input?
3907 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3908 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3909 }
3910
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003911 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003912
Roland Levillain4c0eb422015-04-24 16:43:49 +01003913 protected:
3914 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3915 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3916 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3917 HInstruction* input = input_record.GetInstruction();
3918 // `input` is the last input of a static invoke marked as having
3919 // an explicit clinit check. It must either be:
3920 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3921 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3922 DCHECK(input != nullptr);
3923 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3924 }
3925 return input_record;
3926 }
3927
Vladimir Markoc53c0792015-11-19 15:48:33 +00003928 void InsertInputAt(size_t index, HInstruction* input);
3929 void RemoveInputAt(size_t index);
3930
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003931 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003932 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003933 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003934 // The target method may refer to different dex file or method index than the original
3935 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3936 // in derived class to increase visibility.
3937 MethodReference target_method_;
3938 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003939
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003940 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003941};
Vladimir Markof64242a2015-12-01 14:58:23 +00003942std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003943std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003944
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003945class HInvokeVirtual : public HInvoke {
3946 public:
3947 HInvokeVirtual(ArenaAllocator* arena,
3948 uint32_t number_of_arguments,
3949 Primitive::Type return_type,
3950 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003951 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003952 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003953 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003954 vtable_index_(vtable_index) {}
3955
Calin Juravle641547a2015-04-21 22:08:51 +01003956 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003957 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003958 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003959 }
3960
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003961 uint32_t GetVTableIndex() const { return vtable_index_; }
3962
3963 DECLARE_INSTRUCTION(InvokeVirtual);
3964
3965 private:
3966 const uint32_t vtable_index_;
3967
3968 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3969};
3970
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003971class HInvokeInterface : public HInvoke {
3972 public:
3973 HInvokeInterface(ArenaAllocator* arena,
3974 uint32_t number_of_arguments,
3975 Primitive::Type return_type,
3976 uint32_t dex_pc,
3977 uint32_t dex_method_index,
3978 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003979 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003980 imt_index_(imt_index) {}
3981
Calin Juravle641547a2015-04-21 22:08:51 +01003982 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003983 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003984 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003985 }
3986
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003987 uint32_t GetImtIndex() const { return imt_index_; }
3988 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3989
3990 DECLARE_INSTRUCTION(InvokeInterface);
3991
3992 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003993 const uint32_t imt_index_;
3994
3995 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3996};
3997
Roland Levillain88cb1752014-10-20 16:36:47 +01003998class HNeg : public HUnaryOperation {
3999 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004000 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4001 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004002
Roland Levillain9867bc72015-08-05 10:21:34 +01004003 template <typename T> T Compute(T x) const { return -x; }
4004
4005 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004006 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004007 }
4008 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004009 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004010 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004011 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4012 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4013 }
4014 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4015 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4016 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004017
Roland Levillain88cb1752014-10-20 16:36:47 +01004018 DECLARE_INSTRUCTION(Neg);
4019
4020 private:
4021 DISALLOW_COPY_AND_ASSIGN(HNeg);
4022};
4023
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004024class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004025 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004026 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004027 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004028 uint32_t dex_pc,
4029 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004030 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004031 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004032 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004033 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004034 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004035 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004036 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004037 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004038 }
4039
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004040 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004041 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004042
4043 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004044 bool NeedsEnvironment() const OVERRIDE { return true; }
4045
Mingyao Yang0c365e62015-03-31 15:09:29 -07004046 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4047 bool CanThrow() const OVERRIDE { return true; }
4048
Calin Juravle10e244f2015-01-26 18:54:32 +00004049 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004050
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004051 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4052
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004053 DECLARE_INSTRUCTION(NewArray);
4054
4055 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004056 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004057 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004058 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004059
4060 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4061};
4062
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004063class HAdd : public HBinaryOperation {
4064 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004065 HAdd(Primitive::Type result_type,
4066 HInstruction* left,
4067 HInstruction* right,
4068 uint32_t dex_pc = kNoDexPc)
4069 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004070
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004071 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004072
Roland Levillain9867bc72015-08-05 10:21:34 +01004073 template <typename T> T Compute(T x, T y) const { return x + y; }
4074
4075 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004076 return GetBlock()->GetGraph()->GetIntConstant(
4077 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004078 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004079 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004080 return GetBlock()->GetGraph()->GetLongConstant(
4081 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004082 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004083 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4084 return GetBlock()->GetGraph()->GetFloatConstant(
4085 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4086 }
4087 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4088 return GetBlock()->GetGraph()->GetDoubleConstant(
4089 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4090 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004091
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004092 DECLARE_INSTRUCTION(Add);
4093
4094 private:
4095 DISALLOW_COPY_AND_ASSIGN(HAdd);
4096};
4097
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004098class HSub : public HBinaryOperation {
4099 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004100 HSub(Primitive::Type result_type,
4101 HInstruction* left,
4102 HInstruction* right,
4103 uint32_t dex_pc = kNoDexPc)
4104 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004105
Roland Levillain9867bc72015-08-05 10:21:34 +01004106 template <typename T> T Compute(T x, T y) const { return x - y; }
4107
4108 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004109 return GetBlock()->GetGraph()->GetIntConstant(
4110 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004111 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004112 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004113 return GetBlock()->GetGraph()->GetLongConstant(
4114 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004115 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004116 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4117 return GetBlock()->GetGraph()->GetFloatConstant(
4118 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4119 }
4120 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4121 return GetBlock()->GetGraph()->GetDoubleConstant(
4122 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4123 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004124
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004125 DECLARE_INSTRUCTION(Sub);
4126
4127 private:
4128 DISALLOW_COPY_AND_ASSIGN(HSub);
4129};
4130
Calin Juravle34bacdf2014-10-07 20:23:36 +01004131class HMul : public HBinaryOperation {
4132 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004133 HMul(Primitive::Type result_type,
4134 HInstruction* left,
4135 HInstruction* right,
4136 uint32_t dex_pc = kNoDexPc)
4137 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004138
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004139 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004140
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 template <typename T> T Compute(T x, T y) const { return x * y; }
4142
4143 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004144 return GetBlock()->GetGraph()->GetIntConstant(
4145 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004146 }
4147 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004148 return GetBlock()->GetGraph()->GetLongConstant(
4149 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004150 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004151 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4152 return GetBlock()->GetGraph()->GetFloatConstant(
4153 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4154 }
4155 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4156 return GetBlock()->GetGraph()->GetDoubleConstant(
4157 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4158 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004159
4160 DECLARE_INSTRUCTION(Mul);
4161
4162 private:
4163 DISALLOW_COPY_AND_ASSIGN(HMul);
4164};
4165
Calin Juravle7c4954d2014-10-28 16:57:40 +00004166class HDiv : public HBinaryOperation {
4167 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004168 HDiv(Primitive::Type result_type,
4169 HInstruction* left,
4170 HInstruction* right,
4171 uint32_t dex_pc)
4172 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004173
Roland Levillain9867bc72015-08-05 10:21:34 +01004174 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004175 T ComputeIntegral(T x, T y) const {
4176 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004177 // Our graph structure ensures we never have 0 for `y` during
4178 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004179 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004180 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004181 return (y == -1) ? -x : x / y;
4182 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004183
Roland Levillain31dd3d62016-02-16 12:21:02 +00004184 template <typename T>
4185 T ComputeFP(T x, T y) const {
4186 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4187 return x / y;
4188 }
4189
Roland Levillain9867bc72015-08-05 10:21:34 +01004190 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004191 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004192 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004193 }
4194 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004196 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4197 }
4198 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4199 return GetBlock()->GetGraph()->GetFloatConstant(
4200 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4201 }
4202 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4203 return GetBlock()->GetGraph()->GetDoubleConstant(
4204 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004205 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004206
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004207 static SideEffects SideEffectsForArchRuntimeCalls() {
4208 // The generated code can use a runtime call.
4209 return SideEffects::CanTriggerGC();
4210 }
4211
Calin Juravle7c4954d2014-10-28 16:57:40 +00004212 DECLARE_INSTRUCTION(Div);
4213
4214 private:
4215 DISALLOW_COPY_AND_ASSIGN(HDiv);
4216};
4217
Calin Juravlebacfec32014-11-14 15:54:36 +00004218class HRem : public HBinaryOperation {
4219 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004220 HRem(Primitive::Type result_type,
4221 HInstruction* left,
4222 HInstruction* right,
4223 uint32_t dex_pc)
4224 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004225
Roland Levillain9867bc72015-08-05 10:21:34 +01004226 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004227 T ComputeIntegral(T x, T y) const {
4228 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004229 // Our graph structure ensures we never have 0 for `y` during
4230 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004231 DCHECK_NE(y, 0);
4232 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4233 return (y == -1) ? 0 : x % y;
4234 }
4235
Roland Levillain31dd3d62016-02-16 12:21:02 +00004236 template <typename T>
4237 T ComputeFP(T x, T y) const {
4238 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4239 return std::fmod(x, y);
4240 }
4241
Roland Levillain9867bc72015-08-05 10:21:34 +01004242 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004244 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004245 }
4246 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004247 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004248 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004249 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004250 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4251 return GetBlock()->GetGraph()->GetFloatConstant(
4252 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4253 }
4254 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4255 return GetBlock()->GetGraph()->GetDoubleConstant(
4256 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4257 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004258
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004259 static SideEffects SideEffectsForArchRuntimeCalls() {
4260 return SideEffects::CanTriggerGC();
4261 }
4262
Calin Juravlebacfec32014-11-14 15:54:36 +00004263 DECLARE_INSTRUCTION(Rem);
4264
4265 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004266 DISALLOW_COPY_AND_ASSIGN(HRem);
4267};
4268
Calin Juravled0d48522014-11-04 16:40:20 +00004269class HDivZeroCheck : public HExpression<1> {
4270 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004271 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4272 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004273 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004274 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004275 SetRawInputAt(0, value);
4276 }
4277
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004278 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4279
Calin Juravled0d48522014-11-04 16:40:20 +00004280 bool CanBeMoved() const OVERRIDE { return true; }
4281
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004282 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004283 return true;
4284 }
4285
4286 bool NeedsEnvironment() const OVERRIDE { return true; }
4287 bool CanThrow() const OVERRIDE { return true; }
4288
Calin Juravled0d48522014-11-04 16:40:20 +00004289 DECLARE_INSTRUCTION(DivZeroCheck);
4290
4291 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004292 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4293};
4294
Calin Juravle9aec02f2014-11-18 23:06:35 +00004295class HShl : public HBinaryOperation {
4296 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004297 HShl(Primitive::Type result_type,
4298 HInstruction* left,
4299 HInstruction* right,
4300 uint32_t dex_pc = kNoDexPc)
4301 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004302
Roland Levillain9867bc72015-08-05 10:21:34 +01004303 template <typename T, typename U, typename V>
4304 T Compute(T x, U y, V max_shift_value) const {
4305 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4306 "V is not the unsigned integer type corresponding to T");
4307 return x << (y & max_shift_value);
4308 }
4309
4310 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4311 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004312 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004313 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004314 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4315 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004316 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004317 }
4318 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4319 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004320 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004321 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004322 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4323 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4324 LOG(FATAL) << DebugName() << " is not defined for float values";
4325 UNREACHABLE();
4326 }
4327 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4328 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4329 LOG(FATAL) << DebugName() << " is not defined for double values";
4330 UNREACHABLE();
4331 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004332
4333 DECLARE_INSTRUCTION(Shl);
4334
4335 private:
4336 DISALLOW_COPY_AND_ASSIGN(HShl);
4337};
4338
4339class HShr : public HBinaryOperation {
4340 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004341 HShr(Primitive::Type result_type,
4342 HInstruction* left,
4343 HInstruction* right,
4344 uint32_t dex_pc = kNoDexPc)
4345 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004346
Roland Levillain9867bc72015-08-05 10:21:34 +01004347 template <typename T, typename U, typename V>
4348 T Compute(T x, U y, V max_shift_value) const {
4349 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4350 "V is not the unsigned integer type corresponding to T");
4351 return x >> (y & max_shift_value);
4352 }
4353
4354 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4355 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004356 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004357 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004358 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4359 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004360 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004361 }
4362 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4363 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004364 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004365 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004366 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4367 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4368 LOG(FATAL) << DebugName() << " is not defined for float values";
4369 UNREACHABLE();
4370 }
4371 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4372 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4373 LOG(FATAL) << DebugName() << " is not defined for double values";
4374 UNREACHABLE();
4375 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004376
4377 DECLARE_INSTRUCTION(Shr);
4378
4379 private:
4380 DISALLOW_COPY_AND_ASSIGN(HShr);
4381};
4382
4383class HUShr : public HBinaryOperation {
4384 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004385 HUShr(Primitive::Type result_type,
4386 HInstruction* left,
4387 HInstruction* right,
4388 uint32_t dex_pc = kNoDexPc)
4389 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004390
Roland Levillain9867bc72015-08-05 10:21:34 +01004391 template <typename T, typename U, typename V>
4392 T Compute(T x, U y, V max_shift_value) const {
4393 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4394 "V is not the unsigned integer type corresponding to T");
4395 V ux = static_cast<V>(x);
4396 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004397 }
4398
Roland Levillain9867bc72015-08-05 10:21:34 +01004399 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4400 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004401 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004402 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004403 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4404 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004405 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004406 }
4407 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4408 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004409 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004410 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004411 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4412 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4413 LOG(FATAL) << DebugName() << " is not defined for float values";
4414 UNREACHABLE();
4415 }
4416 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4417 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4418 LOG(FATAL) << DebugName() << " is not defined for double values";
4419 UNREACHABLE();
4420 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004421
4422 DECLARE_INSTRUCTION(UShr);
4423
4424 private:
4425 DISALLOW_COPY_AND_ASSIGN(HUShr);
4426};
4427
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004428class HAnd : public HBinaryOperation {
4429 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004430 HAnd(Primitive::Type result_type,
4431 HInstruction* left,
4432 HInstruction* right,
4433 uint32_t dex_pc = kNoDexPc)
4434 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004435
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004436 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004437
Roland Levillaine53bd812016-02-24 14:54:18 +00004438 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004439
4440 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004441 return GetBlock()->GetGraph()->GetIntConstant(
4442 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004443 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004444 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004445 return GetBlock()->GetGraph()->GetLongConstant(
4446 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004447 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004448 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4449 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4450 LOG(FATAL) << DebugName() << " is not defined for float values";
4451 UNREACHABLE();
4452 }
4453 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4454 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4455 LOG(FATAL) << DebugName() << " is not defined for double values";
4456 UNREACHABLE();
4457 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004458
4459 DECLARE_INSTRUCTION(And);
4460
4461 private:
4462 DISALLOW_COPY_AND_ASSIGN(HAnd);
4463};
4464
4465class HOr : public HBinaryOperation {
4466 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004467 HOr(Primitive::Type result_type,
4468 HInstruction* left,
4469 HInstruction* right,
4470 uint32_t dex_pc = kNoDexPc)
4471 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004472
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004473 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004474
Roland Levillaine53bd812016-02-24 14:54:18 +00004475 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004476
4477 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004478 return GetBlock()->GetGraph()->GetIntConstant(
4479 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004480 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004481 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 return GetBlock()->GetGraph()->GetLongConstant(
4483 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004484 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004485 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4486 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4487 LOG(FATAL) << DebugName() << " is not defined for float values";
4488 UNREACHABLE();
4489 }
4490 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4491 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4492 LOG(FATAL) << DebugName() << " is not defined for double values";
4493 UNREACHABLE();
4494 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004495
4496 DECLARE_INSTRUCTION(Or);
4497
4498 private:
4499 DISALLOW_COPY_AND_ASSIGN(HOr);
4500};
4501
4502class HXor : public HBinaryOperation {
4503 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004504 HXor(Primitive::Type result_type,
4505 HInstruction* left,
4506 HInstruction* right,
4507 uint32_t dex_pc = kNoDexPc)
4508 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004509
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004510 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004511
Roland Levillaine53bd812016-02-24 14:54:18 +00004512 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004513
4514 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004515 return GetBlock()->GetGraph()->GetIntConstant(
4516 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004517 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004519 return GetBlock()->GetGraph()->GetLongConstant(
4520 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004521 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004522 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4523 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4524 LOG(FATAL) << DebugName() << " is not defined for float values";
4525 UNREACHABLE();
4526 }
4527 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4528 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4529 LOG(FATAL) << DebugName() << " is not defined for double values";
4530 UNREACHABLE();
4531 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004532
4533 DECLARE_INSTRUCTION(Xor);
4534
4535 private:
4536 DISALLOW_COPY_AND_ASSIGN(HXor);
4537};
4538
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004539class HRor : public HBinaryOperation {
4540 public:
4541 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4542 : HBinaryOperation(result_type, value, distance) {}
4543
4544 template <typename T, typename U, typename V>
4545 T Compute(T x, U y, V max_shift_value) const {
4546 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4547 "V is not the unsigned integer type corresponding to T");
4548 V ux = static_cast<V>(x);
4549 if ((y & max_shift_value) == 0) {
4550 return static_cast<T>(ux);
4551 } else {
4552 const V reg_bits = sizeof(T) * 8;
4553 return static_cast<T>(ux >> (y & max_shift_value)) |
4554 (x << (reg_bits - (y & max_shift_value)));
4555 }
4556 }
4557
4558 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4559 return GetBlock()->GetGraph()->GetIntConstant(
4560 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4561 }
4562 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4563 return GetBlock()->GetGraph()->GetLongConstant(
4564 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4565 }
4566 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4567 return GetBlock()->GetGraph()->GetLongConstant(
4568 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4569 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004570 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4571 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4572 LOG(FATAL) << DebugName() << " is not defined for float values";
4573 UNREACHABLE();
4574 }
4575 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4576 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4577 LOG(FATAL) << DebugName() << " is not defined for double values";
4578 UNREACHABLE();
4579 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004580
4581 DECLARE_INSTRUCTION(Ror);
4582
4583 private:
4584 DISALLOW_COPY_AND_ASSIGN(HRor);
4585};
4586
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004587// The value of a parameter in this method. Its location depends on
4588// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004589class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004590 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004591 HParameterValue(const DexFile& dex_file,
4592 uint16_t type_index,
4593 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004594 Primitive::Type parameter_type,
4595 bool is_this = false)
4596 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004597 dex_file_(dex_file),
4598 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004599 index_(index),
4600 is_this_(is_this),
4601 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004602
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004603 const DexFile& GetDexFile() const { return dex_file_; }
4604 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004605 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004606 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004607
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004608 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4609 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004610
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004611 DECLARE_INSTRUCTION(ParameterValue);
4612
4613 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004614 const DexFile& dex_file_;
4615 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004616 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004617 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004618 const uint8_t index_;
4619
Calin Juravle10e244f2015-01-26 18:54:32 +00004620 // Whether or not the parameter value corresponds to 'this' argument.
4621 const bool is_this_;
4622
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004623 bool can_be_null_;
4624
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004625 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4626};
4627
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004628class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004629 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004630 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4631 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004632
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004633 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004634 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004635 return true;
4636 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004637
Roland Levillain9867bc72015-08-05 10:21:34 +01004638 template <typename T> T Compute(T x) const { return ~x; }
4639
4640 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004641 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004642 }
4643 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004644 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004646 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4647 LOG(FATAL) << DebugName() << " is not defined for float values";
4648 UNREACHABLE();
4649 }
4650 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4651 LOG(FATAL) << DebugName() << " is not defined for double values";
4652 UNREACHABLE();
4653 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004654
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004655 DECLARE_INSTRUCTION(Not);
4656
4657 private:
4658 DISALLOW_COPY_AND_ASSIGN(HNot);
4659};
4660
David Brazdil66d126e2015-04-03 16:02:44 +01004661class HBooleanNot : public HUnaryOperation {
4662 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4664 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004665
4666 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004667 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004668 return true;
4669 }
4670
Roland Levillain9867bc72015-08-05 10:21:34 +01004671 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004672 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004673 return !x;
4674 }
4675
Roland Levillain9867bc72015-08-05 10:21:34 +01004676 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004677 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004678 }
4679 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4680 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004681 UNREACHABLE();
4682 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004683 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4684 LOG(FATAL) << DebugName() << " is not defined for float values";
4685 UNREACHABLE();
4686 }
4687 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4688 LOG(FATAL) << DebugName() << " is not defined for double values";
4689 UNREACHABLE();
4690 }
David Brazdil66d126e2015-04-03 16:02:44 +01004691
4692 DECLARE_INSTRUCTION(BooleanNot);
4693
4694 private:
4695 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4696};
4697
Roland Levillaindff1f282014-11-05 14:15:05 +00004698class HTypeConversion : public HExpression<1> {
4699 public:
4700 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004701 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004702 : HExpression(result_type,
4703 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4704 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004705 SetRawInputAt(0, input);
4706 DCHECK_NE(input->GetType(), result_type);
4707 }
4708
4709 HInstruction* GetInput() const { return InputAt(0); }
4710 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4711 Primitive::Type GetResultType() const { return GetType(); }
4712
4713 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004714 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004715
Mark Mendelle82549b2015-05-06 10:55:34 -04004716 // Try to statically evaluate the conversion and return a HConstant
4717 // containing the result. If the input cannot be converted, return nullptr.
4718 HConstant* TryStaticEvaluation() const;
4719
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004720 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4721 Primitive::Type result_type) {
4722 // Some architectures may not require the 'GC' side effects, but at this point
4723 // in the compilation process we do not know what architecture we will
4724 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004725 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4726 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004727 return SideEffects::CanTriggerGC();
4728 }
4729 return SideEffects::None();
4730 }
4731
Roland Levillaindff1f282014-11-05 14:15:05 +00004732 DECLARE_INSTRUCTION(TypeConversion);
4733
4734 private:
4735 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4736};
4737
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004738static constexpr uint32_t kNoRegNumber = -1;
4739
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004740class HPhi : public HInstruction {
4741 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004742 HPhi(ArenaAllocator* arena,
4743 uint32_t reg_number,
4744 size_t number_of_inputs,
4745 Primitive::Type type,
4746 uint32_t dex_pc = kNoDexPc)
4747 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004748 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004749 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004750 type_(ToPhiType(type)),
4751 // Phis are constructed live and marked dead if conflicting or unused.
4752 // Individual steps of SsaBuilder should assume that if a phi has been
4753 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4754 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004755 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004756 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004757 }
4758
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004759 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4760 static Primitive::Type ToPhiType(Primitive::Type type) {
4761 switch (type) {
4762 case Primitive::kPrimBoolean:
4763 case Primitive::kPrimByte:
4764 case Primitive::kPrimShort:
4765 case Primitive::kPrimChar:
4766 return Primitive::kPrimInt;
4767 default:
4768 return type;
4769 }
4770 }
4771
David Brazdilffee3d32015-07-06 11:48:53 +01004772 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4773
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004774 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004775
4776 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004777 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004778
Calin Juravle10e244f2015-01-26 18:54:32 +00004779 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004780 void SetType(Primitive::Type new_type) {
4781 // Make sure that only valid type changes occur. The following are allowed:
4782 // (1) int -> float/ref (primitive type propagation),
4783 // (2) long -> double (primitive type propagation).
4784 DCHECK(type_ == new_type ||
4785 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4786 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4787 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4788 type_ = new_type;
4789 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004790
Calin Juravle10e244f2015-01-26 18:54:32 +00004791 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4792 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4793
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004794 uint32_t GetRegNumber() const { return reg_number_; }
4795
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004796 void SetDead() { is_live_ = false; }
4797 void SetLive() { is_live_ = true; }
4798 bool IsDead() const { return !is_live_; }
4799 bool IsLive() const { return is_live_; }
4800
David Brazdil77a48ae2015-09-15 12:34:04 +00004801 bool IsVRegEquivalentOf(HInstruction* other) const {
4802 return other != nullptr
4803 && other->IsPhi()
4804 && other->AsPhi()->GetBlock() == GetBlock()
4805 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4806 }
4807
Calin Juravlea4f88312015-04-16 12:57:19 +01004808 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4809 // An equivalent phi is a phi having the same dex register and type.
4810 // It assumes that phis with the same dex register are adjacent.
4811 HPhi* GetNextEquivalentPhiWithSameType() {
4812 HInstruction* next = GetNext();
4813 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4814 if (next->GetType() == GetType()) {
4815 return next->AsPhi();
4816 }
4817 next = next->GetNext();
4818 }
4819 return nullptr;
4820 }
4821
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004822 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004823
David Brazdil1abb4192015-02-17 18:33:36 +00004824 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004825 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004826 return inputs_[index];
4827 }
David Brazdil1abb4192015-02-17 18:33:36 +00004828
4829 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004830 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004831 }
4832
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004833 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004834 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004835 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004836 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004837 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004838 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004839
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004840 DISALLOW_COPY_AND_ASSIGN(HPhi);
4841};
4842
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004843class HNullCheck : public HExpression<1> {
4844 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004845 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4846 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004847 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004848 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004849 SetRawInputAt(0, value);
4850 }
4851
Calin Juravle10e244f2015-01-26 18:54:32 +00004852 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004853 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004854 return true;
4855 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004856
Calin Juravle10e244f2015-01-26 18:54:32 +00004857 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004858
Calin Juravle10e244f2015-01-26 18:54:32 +00004859 bool CanThrow() const OVERRIDE { return true; }
4860
4861 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004862
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004863
4864 DECLARE_INSTRUCTION(NullCheck);
4865
4866 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004867 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4868};
4869
4870class FieldInfo : public ValueObject {
4871 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004872 FieldInfo(MemberOffset field_offset,
4873 Primitive::Type field_type,
4874 bool is_volatile,
4875 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004876 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004877 const DexFile& dex_file,
4878 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004879 : field_offset_(field_offset),
4880 field_type_(field_type),
4881 is_volatile_(is_volatile),
4882 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004883 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004884 dex_file_(dex_file),
4885 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004886
4887 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004888 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004889 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004890 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004891 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004892 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004893 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004894
4895 private:
4896 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004897 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004898 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004899 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004900 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004901 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004902 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004903};
4904
4905class HInstanceFieldGet : public HExpression<1> {
4906 public:
4907 HInstanceFieldGet(HInstruction* value,
4908 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004909 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004910 bool is_volatile,
4911 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004912 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004913 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004914 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004915 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004916 : HExpression(field_type,
4917 SideEffects::FieldReadOfType(field_type, is_volatile),
4918 dex_pc),
4919 field_info_(field_offset,
4920 field_type,
4921 is_volatile,
4922 field_idx,
4923 declaring_class_def_index,
4924 dex_file,
4925 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004926 SetRawInputAt(0, value);
4927 }
4928
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004929 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004930
4931 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4932 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4933 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004934 }
4935
Calin Juravle641547a2015-04-21 22:08:51 +01004936 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4937 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004938 }
4939
4940 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004941 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4942 }
4943
Calin Juravle52c48962014-12-16 17:02:57 +00004944 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004945 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004946 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004947 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004948
4949 DECLARE_INSTRUCTION(InstanceFieldGet);
4950
4951 private:
4952 const FieldInfo field_info_;
4953
4954 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4955};
4956
4957class HInstanceFieldSet : public HTemplateInstruction<2> {
4958 public:
4959 HInstanceFieldSet(HInstruction* object,
4960 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004961 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004962 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004963 bool is_volatile,
4964 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004965 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004966 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004967 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004968 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004969 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4970 dex_pc),
4971 field_info_(field_offset,
4972 field_type,
4973 is_volatile,
4974 field_idx,
4975 declaring_class_def_index,
4976 dex_file,
4977 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004978 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004979 SetRawInputAt(0, object);
4980 SetRawInputAt(1, value);
4981 }
4982
Calin Juravle641547a2015-04-21 22:08:51 +01004983 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4984 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004985 }
4986
Calin Juravle52c48962014-12-16 17:02:57 +00004987 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004989 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004990 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004991 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004992 bool GetValueCanBeNull() const { return value_can_be_null_; }
4993 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004994
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004995 DECLARE_INSTRUCTION(InstanceFieldSet);
4996
4997 private:
4998 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004999 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005000
5001 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5002};
5003
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005004class HArrayGet : public HExpression<2> {
5005 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005006 HArrayGet(HInstruction* array,
5007 HInstruction* index,
5008 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005009 uint32_t dex_pc,
5010 SideEffects additional_side_effects = SideEffects::None())
5011 : HExpression(type,
5012 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005013 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005014 SetRawInputAt(0, array);
5015 SetRawInputAt(1, index);
5016 }
5017
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005018 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005019 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005020 return true;
5021 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005022 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005023 // TODO: We can be smarter here.
5024 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5025 // which generates the implicit null check. There are cases when these can be removed
5026 // to produce better code. If we ever add optimizations to do so we should allow an
5027 // implicit check here (as long as the address falls in the first page).
5028 return false;
5029 }
5030
David Brazdil4833f5a2015-12-16 10:37:39 +00005031 bool IsEquivalentOf(HArrayGet* other) const {
5032 bool result = (GetDexPc() == other->GetDexPc());
5033 if (kIsDebugBuild && result) {
5034 DCHECK_EQ(GetBlock(), other->GetBlock());
5035 DCHECK_EQ(GetArray(), other->GetArray());
5036 DCHECK_EQ(GetIndex(), other->GetIndex());
5037 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005038 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005039 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005040 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5041 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005042 }
5043 }
5044 return result;
5045 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005046
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005047 HInstruction* GetArray() const { return InputAt(0); }
5048 HInstruction* GetIndex() const { return InputAt(1); }
5049
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005050 DECLARE_INSTRUCTION(ArrayGet);
5051
5052 private:
5053 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5054};
5055
5056class HArraySet : public HTemplateInstruction<3> {
5057 public:
5058 HArraySet(HInstruction* array,
5059 HInstruction* index,
5060 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005061 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005062 uint32_t dex_pc,
5063 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005064 : HTemplateInstruction(
5065 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005066 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5067 additional_side_effects),
5068 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005069 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005070 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005071 value_can_be_null_(true),
5072 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005073 SetRawInputAt(0, array);
5074 SetRawInputAt(1, index);
5075 SetRawInputAt(2, value);
5076 }
5077
Calin Juravle77520bc2015-01-12 18:45:46 +00005078 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005079 // We call a runtime method to throw ArrayStoreException.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005080 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 }
5082
Mingyao Yang81014cb2015-06-02 03:16:27 -07005083 // Can throw ArrayStoreException.
5084 bool CanThrow() const OVERRIDE { return needs_type_check_; }
5085
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005086 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005087 // TODO: Same as for ArrayGet.
5088 return false;
5089 }
5090
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005091 void ClearNeedsTypeCheck() {
5092 needs_type_check_ = false;
5093 }
5094
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005095 void ClearValueCanBeNull() {
5096 value_can_be_null_ = false;
5097 }
5098
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005099 void SetStaticTypeOfArrayIsObjectArray() {
5100 static_type_of_array_is_object_array_ = true;
5101 }
5102
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005103 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005104 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005105 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005106
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005107 HInstruction* GetArray() const { return InputAt(0); }
5108 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005109 HInstruction* GetValue() const { return InputAt(2); }
5110
5111 Primitive::Type GetComponentType() const {
5112 // The Dex format does not type floating point index operations. Since the
5113 // `expected_component_type_` is set during building and can therefore not
5114 // be correct, we also check what is the value type. If it is a floating
5115 // point type, we must use that type.
5116 Primitive::Type value_type = GetValue()->GetType();
5117 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5118 ? value_type
5119 : expected_component_type_;
5120 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005121
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005122 Primitive::Type GetRawExpectedComponentType() const {
5123 return expected_component_type_;
5124 }
5125
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005126 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5127 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5128 }
5129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005130 DECLARE_INSTRUCTION(ArraySet);
5131
5132 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005133 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005134 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005135 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005136 // Cached information for the reference_type_info_ so that codegen
5137 // does not need to inspect the static type.
5138 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005139
5140 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5141};
5142
5143class HArrayLength : public HExpression<1> {
5144 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005145 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005146 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005147 // Note that arrays do not change length, so the instruction does not
5148 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 SetRawInputAt(0, array);
5150 }
5151
Calin Juravle77520bc2015-01-12 18:45:46 +00005152 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005153 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005154 return true;
5155 }
Calin Juravle641547a2015-04-21 22:08:51 +01005156 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5157 return obj == InputAt(0);
5158 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 DECLARE_INSTRUCTION(ArrayLength);
5161
5162 private:
5163 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5164};
5165
5166class HBoundsCheck : public HExpression<2> {
5167 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005168 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5169 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005170 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005171 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172 DCHECK(index->GetType() == Primitive::kPrimInt);
5173 SetRawInputAt(0, index);
5174 SetRawInputAt(1, length);
5175 }
5176
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005177 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005178 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005179 return true;
5180 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005181
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005182 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005183
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005184 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005185
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005186 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187
5188 DECLARE_INSTRUCTION(BoundsCheck);
5189
5190 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005191 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5192};
5193
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005194class HSuspendCheck : public HTemplateInstruction<0> {
5195 public:
5196 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005197 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005198
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005199 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005200 return true;
5201 }
5202
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005203 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5204 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005205
5206 DECLARE_INSTRUCTION(SuspendCheck);
5207
5208 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005209 // Only used for code generation, in order to share the same slow path between back edges
5210 // of a same loop.
5211 SlowPathCode* slow_path_;
5212
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005213 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5214};
5215
David Srbecky0cf44932015-12-09 14:09:59 +00005216// Pseudo-instruction which provides the native debugger with mapping information.
5217// It ensures that we can generate line number and local variables at this point.
5218class HNativeDebugInfo : public HTemplateInstruction<0> {
5219 public:
5220 explicit HNativeDebugInfo(uint32_t dex_pc)
5221 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5222
5223 bool NeedsEnvironment() const OVERRIDE {
5224 return true;
5225 }
5226
5227 DECLARE_INSTRUCTION(NativeDebugInfo);
5228
5229 private:
5230 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5231};
5232
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005233/**
5234 * Instruction to load a Class object.
5235 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005236class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005237 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005238 HLoadClass(HCurrentMethod* current_method,
5239 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005240 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005241 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005242 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005243 bool needs_access_check,
5244 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005245 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005246 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005247 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005248 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00005249 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01005250 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005251 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00005252 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005253 // Referrers class should not need access check. We never inline unverified
5254 // methods so we can't possibly end up in this situation.
5255 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005256 SetRawInputAt(0, current_method);
5257 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005258
5259 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005260
5261 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005262 // Note that we don't need to test for generate_clinit_check_.
5263 // Whether or not we need to generate the clinit check is processed in
5264 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005265 return other->AsLoadClass()->type_index_ == type_index_ &&
5266 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005267 }
5268
5269 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5270
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005271 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005272 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005273 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005274
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005275 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005276 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005277 }
5278
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005279 bool MustGenerateClinitCheck() const {
5280 return generate_clinit_check_;
5281 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005282
Calin Juravle0ba218d2015-05-19 18:46:01 +01005283 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005284 // The entrypoint the code generator is going to call does not do
5285 // clinit of the class.
5286 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005287 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005288 }
5289
5290 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005291 return MustGenerateClinitCheck() ||
5292 (!is_referrers_class_ && !is_in_dex_cache_) ||
5293 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005294 }
5295
5296 bool NeedsAccessCheck() const {
5297 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005298 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005299
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005300 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005301 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005302 }
5303
Calin Juravleacf735c2015-02-12 15:25:22 +00005304 ReferenceTypeInfo GetLoadedClassRTI() {
5305 return loaded_class_rti_;
5306 }
5307
5308 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5309 // Make sure we only set exact types (the loaded class should never be merged).
5310 DCHECK(rti.IsExact());
5311 loaded_class_rti_ = rti;
5312 }
5313
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005314 const DexFile& GetDexFile() { return dex_file_; }
5315
Vladimir Markodc151b22015-10-15 18:02:30 +01005316 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005317
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005318 static SideEffects SideEffectsForArchRuntimeCalls() {
5319 return SideEffects::CanTriggerGC();
5320 }
5321
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005322 bool IsInDexCache() const { return is_in_dex_cache_; }
5323
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005324 DECLARE_INSTRUCTION(LoadClass);
5325
5326 private:
5327 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005328 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005329 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005330 // Whether this instruction must generate the initialization check.
5331 // Used for code generation.
5332 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005333 const bool needs_access_check_;
5334 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005335
Calin Juravleacf735c2015-02-12 15:25:22 +00005336 ReferenceTypeInfo loaded_class_rti_;
5337
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005338 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5339};
5340
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005341class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005342 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005343 HLoadString(HCurrentMethod* current_method,
5344 uint32_t string_index,
5345 uint32_t dex_pc,
5346 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005347 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005348 string_index_(string_index),
5349 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005350 SetRawInputAt(0, current_method);
5351 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005352
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005353 bool CanBeMoved() const OVERRIDE { return true; }
5354
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005355 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5356 return other->AsLoadString()->string_index_ == string_index_;
5357 }
5358
5359 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5360
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005361 uint32_t GetStringIndex() const { return string_index_; }
5362
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005363 // Will call the runtime if the string is not already in the dex cache.
5364 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5365
Vladimir Markodc151b22015-10-15 18:02:30 +01005366 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005367 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005368 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005369 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005370
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005371 static SideEffects SideEffectsForArchRuntimeCalls() {
5372 return SideEffects::CanTriggerGC();
5373 }
5374
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005375 DECLARE_INSTRUCTION(LoadString);
5376
5377 private:
5378 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005379 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005380
5381 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5382};
5383
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005384/**
5385 * Performs an initialization check on its Class object input.
5386 */
5387class HClinitCheck : public HExpression<1> {
5388 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005389 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005390 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005391 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005392 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5393 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005394 SetRawInputAt(0, constant);
5395 }
5396
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005397 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005398 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005399 return true;
5400 }
5401
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005402 bool NeedsEnvironment() const OVERRIDE {
5403 // May call runtime to initialize the class.
5404 return true;
5405 }
5406
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005407 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005408
5409 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5410
5411 DECLARE_INSTRUCTION(ClinitCheck);
5412
5413 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005414 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5415};
5416
5417class HStaticFieldGet : public HExpression<1> {
5418 public:
5419 HStaticFieldGet(HInstruction* cls,
5420 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005421 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005422 bool is_volatile,
5423 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005424 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005425 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005426 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005427 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005428 : HExpression(field_type,
5429 SideEffects::FieldReadOfType(field_type, is_volatile),
5430 dex_pc),
5431 field_info_(field_offset,
5432 field_type,
5433 is_volatile,
5434 field_idx,
5435 declaring_class_def_index,
5436 dex_file,
5437 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005438 SetRawInputAt(0, cls);
5439 }
5440
Calin Juravle52c48962014-12-16 17:02:57 +00005441
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005442 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005443
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005444 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005445 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5446 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005447 }
5448
5449 size_t ComputeHashCode() const OVERRIDE {
5450 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5451 }
5452
Calin Juravle52c48962014-12-16 17:02:57 +00005453 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005454 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5455 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005456 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005457
5458 DECLARE_INSTRUCTION(StaticFieldGet);
5459
5460 private:
5461 const FieldInfo field_info_;
5462
5463 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5464};
5465
5466class HStaticFieldSet : public HTemplateInstruction<2> {
5467 public:
5468 HStaticFieldSet(HInstruction* cls,
5469 HInstruction* value,
5470 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005471 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005472 bool is_volatile,
5473 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005474 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005475 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005476 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005477 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005478 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5479 dex_pc),
5480 field_info_(field_offset,
5481 field_type,
5482 is_volatile,
5483 field_idx,
5484 declaring_class_def_index,
5485 dex_file,
5486 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005487 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005488 SetRawInputAt(0, cls);
5489 SetRawInputAt(1, value);
5490 }
5491
Calin Juravle52c48962014-12-16 17:02:57 +00005492 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005493 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5494 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005495 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005496
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005497 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005498 bool GetValueCanBeNull() const { return value_can_be_null_; }
5499 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005500
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005501 DECLARE_INSTRUCTION(StaticFieldSet);
5502
5503 private:
5504 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005505 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005506
5507 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5508};
5509
Calin Juravlee460d1d2015-09-29 04:52:17 +01005510class HUnresolvedInstanceFieldGet : public HExpression<1> {
5511 public:
5512 HUnresolvedInstanceFieldGet(HInstruction* obj,
5513 Primitive::Type field_type,
5514 uint32_t field_index,
5515 uint32_t dex_pc)
5516 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5517 field_index_(field_index) {
5518 SetRawInputAt(0, obj);
5519 }
5520
5521 bool NeedsEnvironment() const OVERRIDE { return true; }
5522 bool CanThrow() const OVERRIDE { return true; }
5523
5524 Primitive::Type GetFieldType() const { return GetType(); }
5525 uint32_t GetFieldIndex() const { return field_index_; }
5526
5527 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5528
5529 private:
5530 const uint32_t field_index_;
5531
5532 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5533};
5534
5535class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5536 public:
5537 HUnresolvedInstanceFieldSet(HInstruction* obj,
5538 HInstruction* value,
5539 Primitive::Type field_type,
5540 uint32_t field_index,
5541 uint32_t dex_pc)
5542 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5543 field_type_(field_type),
5544 field_index_(field_index) {
5545 DCHECK_EQ(field_type, value->GetType());
5546 SetRawInputAt(0, obj);
5547 SetRawInputAt(1, value);
5548 }
5549
5550 bool NeedsEnvironment() const OVERRIDE { return true; }
5551 bool CanThrow() const OVERRIDE { return true; }
5552
5553 Primitive::Type GetFieldType() const { return field_type_; }
5554 uint32_t GetFieldIndex() const { return field_index_; }
5555
5556 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5557
5558 private:
5559 const Primitive::Type field_type_;
5560 const uint32_t field_index_;
5561
5562 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5563};
5564
5565class HUnresolvedStaticFieldGet : public HExpression<0> {
5566 public:
5567 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5568 uint32_t field_index,
5569 uint32_t dex_pc)
5570 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5571 field_index_(field_index) {
5572 }
5573
5574 bool NeedsEnvironment() const OVERRIDE { return true; }
5575 bool CanThrow() const OVERRIDE { return true; }
5576
5577 Primitive::Type GetFieldType() const { return GetType(); }
5578 uint32_t GetFieldIndex() const { return field_index_; }
5579
5580 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5581
5582 private:
5583 const uint32_t field_index_;
5584
5585 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5586};
5587
5588class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5589 public:
5590 HUnresolvedStaticFieldSet(HInstruction* value,
5591 Primitive::Type field_type,
5592 uint32_t field_index,
5593 uint32_t dex_pc)
5594 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5595 field_type_(field_type),
5596 field_index_(field_index) {
5597 DCHECK_EQ(field_type, value->GetType());
5598 SetRawInputAt(0, value);
5599 }
5600
5601 bool NeedsEnvironment() const OVERRIDE { return true; }
5602 bool CanThrow() const OVERRIDE { return true; }
5603
5604 Primitive::Type GetFieldType() const { return field_type_; }
5605 uint32_t GetFieldIndex() const { return field_index_; }
5606
5607 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5608
5609 private:
5610 const Primitive::Type field_type_;
5611 const uint32_t field_index_;
5612
5613 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5614};
5615
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005616// Implement the move-exception DEX instruction.
5617class HLoadException : public HExpression<0> {
5618 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005619 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5620 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005621
David Brazdilbbd733e2015-08-18 17:48:17 +01005622 bool CanBeNull() const OVERRIDE { return false; }
5623
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005624 DECLARE_INSTRUCTION(LoadException);
5625
5626 private:
5627 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5628};
5629
David Brazdilcb1c0552015-08-04 16:22:25 +01005630// Implicit part of move-exception which clears thread-local exception storage.
5631// Must not be removed because the runtime expects the TLS to get cleared.
5632class HClearException : public HTemplateInstruction<0> {
5633 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005634 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5635 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005636
5637 DECLARE_INSTRUCTION(ClearException);
5638
5639 private:
5640 DISALLOW_COPY_AND_ASSIGN(HClearException);
5641};
5642
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005643class HThrow : public HTemplateInstruction<1> {
5644 public:
5645 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005646 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005647 SetRawInputAt(0, exception);
5648 }
5649
5650 bool IsControlFlow() const OVERRIDE { return true; }
5651
5652 bool NeedsEnvironment() const OVERRIDE { return true; }
5653
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005654 bool CanThrow() const OVERRIDE { return true; }
5655
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005656
5657 DECLARE_INSTRUCTION(Throw);
5658
5659 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005660 DISALLOW_COPY_AND_ASSIGN(HThrow);
5661};
5662
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005663/**
5664 * Implementation strategies for the code generator of a HInstanceOf
5665 * or `HCheckCast`.
5666 */
5667enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005668 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005669 kExactCheck, // Can do a single class compare.
5670 kClassHierarchyCheck, // Can just walk the super class chain.
5671 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5672 kInterfaceCheck, // No optimization yet when checking against an interface.
5673 kArrayObjectCheck, // Can just check if the array is not primitive.
5674 kArrayCheck // No optimization yet when checking against a generic array.
5675};
5676
Roland Levillain86503782016-02-11 19:07:30 +00005677std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5678
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005679class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005680 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005681 HInstanceOf(HInstruction* object,
5682 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005683 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005684 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005685 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005686 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005687 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005688 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005689 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005690 SetRawInputAt(0, object);
5691 SetRawInputAt(1, constant);
5692 }
5693
5694 bool CanBeMoved() const OVERRIDE { return true; }
5695
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005696 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005697 return true;
5698 }
5699
5700 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005701 return CanCallRuntime(check_kind_);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005702 }
5703
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005704 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5705
5706 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005707
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005708 // Used only in code generation.
5709 bool MustDoNullCheck() const { return must_do_null_check_; }
5710 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5711
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005712 static bool CanCallRuntime(TypeCheckKind check_kind) {
5713 // Mips currently does runtime calls for any other checks.
5714 return check_kind != TypeCheckKind::kExactCheck;
5715 }
5716
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005717 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005718 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005719 }
5720
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005721 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005722
5723 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005724 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005725 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005726
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005727 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5728};
5729
Calin Juravleb1498f62015-02-16 13:13:29 +00005730class HBoundType : public HExpression<1> {
5731 public:
David Brazdilf5552582015-12-27 13:36:12 +00005732 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005733 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005734 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5735 upper_can_be_null_(true),
5736 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005737 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005738 SetRawInputAt(0, input);
5739 }
5740
David Brazdilf5552582015-12-27 13:36:12 +00005741 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005742 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5743 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005744 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005745
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005746 void SetCanBeNull(bool can_be_null) {
5747 DCHECK(upper_can_be_null_ || !can_be_null);
5748 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005749 }
5750
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005751 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5752
Calin Juravleb1498f62015-02-16 13:13:29 +00005753 DECLARE_INSTRUCTION(BoundType);
5754
5755 private:
5756 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005757 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5758 // It is used to bound the type in cases like:
5759 // if (x instanceof ClassX) {
5760 // // uper_bound_ will be ClassX
5761 // }
David Brazdilf5552582015-12-27 13:36:12 +00005762 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005763 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5764 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005765 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005766 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005767
5768 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5769};
5770
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005771class HCheckCast : public HTemplateInstruction<2> {
5772 public:
5773 HCheckCast(HInstruction* object,
5774 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005775 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005776 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005777 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005778 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005779 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005780 SetRawInputAt(0, object);
5781 SetRawInputAt(1, constant);
5782 }
5783
5784 bool CanBeMoved() const OVERRIDE { return true; }
5785
5786 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5787 return true;
5788 }
5789
5790 bool NeedsEnvironment() const OVERRIDE {
5791 // Instruction may throw a CheckCastError.
5792 return true;
5793 }
5794
5795 bool CanThrow() const OVERRIDE { return true; }
5796
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005797 bool MustDoNullCheck() const { return must_do_null_check_; }
5798 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005799 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005800
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005801 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005802
5803 DECLARE_INSTRUCTION(CheckCast);
5804
5805 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005806 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005807 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005808
5809 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005810};
5811
Calin Juravle27df7582015-04-17 19:12:31 +01005812class HMemoryBarrier : public HTemplateInstruction<0> {
5813 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005814 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005815 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005816 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005817 barrier_kind_(barrier_kind) {}
5818
5819 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5820
5821 DECLARE_INSTRUCTION(MemoryBarrier);
5822
5823 private:
5824 const MemBarrierKind barrier_kind_;
5825
5826 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5827};
5828
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005829class HMonitorOperation : public HTemplateInstruction<1> {
5830 public:
5831 enum OperationKind {
5832 kEnter,
5833 kExit,
5834 };
5835
5836 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005837 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005838 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5839 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005840 SetRawInputAt(0, object);
5841 }
5842
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005843 // Instruction may go into runtime, so we need an environment.
5844 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00005845
5846 bool CanThrow() const OVERRIDE {
5847 // Verifier guarantees that monitor-exit cannot throw.
5848 // This is important because it allows the HGraphBuilder to remove
5849 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5850 return IsEnter();
5851 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005852
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005853
5854 bool IsEnter() const { return kind_ == kEnter; }
5855
5856 DECLARE_INSTRUCTION(MonitorOperation);
5857
Calin Juravle52c48962014-12-16 17:02:57 +00005858 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005859 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005860
5861 private:
5862 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5863};
5864
David Brazdil74eb1b22015-12-14 11:44:01 +00005865class HSelect : public HExpression<3> {
5866 public:
5867 HSelect(HInstruction* condition,
5868 HInstruction* true_value,
5869 HInstruction* false_value,
5870 uint32_t dex_pc)
5871 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
5872 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
5873
5874 // First input must be `true_value` or `false_value` to allow codegens to
5875 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
5876 // that architectures which implement HSelect as a conditional move also
5877 // will not need to invert the condition.
5878 SetRawInputAt(0, false_value);
5879 SetRawInputAt(1, true_value);
5880 SetRawInputAt(2, condition);
5881 }
5882
5883 HInstruction* GetFalseValue() const { return InputAt(0); }
5884 HInstruction* GetTrueValue() const { return InputAt(1); }
5885 HInstruction* GetCondition() const { return InputAt(2); }
5886
5887 bool CanBeMoved() const OVERRIDE { return true; }
5888 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
5889
5890 bool CanBeNull() const OVERRIDE {
5891 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
5892 }
5893
5894 DECLARE_INSTRUCTION(Select);
5895
5896 private:
5897 DISALLOW_COPY_AND_ASSIGN(HSelect);
5898};
5899
Vladimir Markof9f64412015-09-02 14:05:49 +01005900class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005901 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005902 MoveOperands(Location source,
5903 Location destination,
5904 Primitive::Type type,
5905 HInstruction* instruction)
5906 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005907
5908 Location GetSource() const { return source_; }
5909 Location GetDestination() const { return destination_; }
5910
5911 void SetSource(Location value) { source_ = value; }
5912 void SetDestination(Location value) { destination_ = value; }
5913
5914 // The parallel move resolver marks moves as "in-progress" by clearing the
5915 // destination (but not the source).
5916 Location MarkPending() {
5917 DCHECK(!IsPending());
5918 Location dest = destination_;
5919 destination_ = Location::NoLocation();
5920 return dest;
5921 }
5922
5923 void ClearPending(Location dest) {
5924 DCHECK(IsPending());
5925 destination_ = dest;
5926 }
5927
5928 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005929 DCHECK(source_.IsValid() || destination_.IsInvalid());
5930 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005931 }
5932
5933 // True if this blocks a move from the given location.
5934 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005935 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005936 }
5937
5938 // A move is redundant if it's been eliminated, if its source and
5939 // destination are the same, or if its destination is unneeded.
5940 bool IsRedundant() const {
5941 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5942 }
5943
5944 // We clear both operands to indicate move that's been eliminated.
5945 void Eliminate() {
5946 source_ = destination_ = Location::NoLocation();
5947 }
5948
5949 bool IsEliminated() const {
5950 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5951 return source_.IsInvalid();
5952 }
5953
Alexey Frunze4dda3372015-06-01 18:31:49 -07005954 Primitive::Type GetType() const { return type_; }
5955
Nicolas Geoffray90218252015-04-15 11:56:51 +01005956 bool Is64BitMove() const {
5957 return Primitive::Is64BitType(type_);
5958 }
5959
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005960 HInstruction* GetInstruction() const { return instruction_; }
5961
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005962 private:
5963 Location source_;
5964 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005965 // The type this move is for.
5966 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005967 // The instruction this move is assocatied with. Null when this move is
5968 // for moving an input in the expected locations of user (including a phi user).
5969 // This is only used in debug mode, to ensure we do not connect interval siblings
5970 // in the same parallel move.
5971 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005972};
5973
Roland Levillainc9285912015-12-18 10:38:42 +00005974std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5975
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005976static constexpr size_t kDefaultNumberOfMoves = 4;
5977
5978class HParallelMove : public HTemplateInstruction<0> {
5979 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005980 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005981 : HTemplateInstruction(SideEffects::None(), dex_pc),
5982 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5983 moves_.reserve(kDefaultNumberOfMoves);
5984 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005985
Nicolas Geoffray90218252015-04-15 11:56:51 +01005986 void AddMove(Location source,
5987 Location destination,
5988 Primitive::Type type,
5989 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005990 DCHECK(source.IsValid());
5991 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005992 if (kIsDebugBuild) {
5993 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005994 for (const MoveOperands& move : moves_) {
5995 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005996 // Special case the situation where the move is for the spill slot
5997 // of the instruction.
5998 if ((GetPrevious() == instruction)
5999 || ((GetPrevious() == nullptr)
6000 && instruction->IsPhi()
6001 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006002 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006003 << "Doing parallel moves for the same instruction.";
6004 } else {
6005 DCHECK(false) << "Doing parallel moves for the same instruction.";
6006 }
6007 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006008 }
6009 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006010 for (const MoveOperands& move : moves_) {
6011 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006012 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006013 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006014 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006015 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006016 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006017 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006018 }
6019
Vladimir Marko225b6462015-09-28 12:17:40 +01006020 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006021 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006022 }
6023
Vladimir Marko225b6462015-09-28 12:17:40 +01006024 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006025
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006026 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006027
6028 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006029 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006030
6031 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6032};
6033
Mark Mendell0616ae02015-04-17 12:49:27 -04006034} // namespace art
6035
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006036#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6037#include "nodes_shared.h"
6038#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006039#ifdef ART_ENABLE_CODEGEN_arm
6040#include "nodes_arm.h"
6041#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006042#ifdef ART_ENABLE_CODEGEN_arm64
6043#include "nodes_arm64.h"
6044#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006045#ifdef ART_ENABLE_CODEGEN_x86
6046#include "nodes_x86.h"
6047#endif
6048
6049namespace art {
6050
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006051class HGraphVisitor : public ValueObject {
6052 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006053 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6054 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006055
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006056 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006057 virtual void VisitBasicBlock(HBasicBlock* block);
6058
Roland Levillain633021e2014-10-01 14:12:25 +01006059 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006060 void VisitInsertionOrder();
6061
Roland Levillain633021e2014-10-01 14:12:25 +01006062 // Visit the graph following dominator tree reverse post-order.
6063 void VisitReversePostOrder();
6064
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006065 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006066
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006067 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006068#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006069 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6070
6071 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6072
6073#undef DECLARE_VISIT_INSTRUCTION
6074
6075 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006076 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006077
6078 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6079};
6080
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006081class HGraphDelegateVisitor : public HGraphVisitor {
6082 public:
6083 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6084 virtual ~HGraphDelegateVisitor() {}
6085
6086 // Visit functions that delegate to to super class.
6087#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006088 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006089
6090 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6091
6092#undef DECLARE_VISIT_INSTRUCTION
6093
6094 private:
6095 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6096};
6097
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006098class HInsertionOrderIterator : public ValueObject {
6099 public:
6100 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6101
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006102 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006103 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006104 void Advance() { ++index_; }
6105
6106 private:
6107 const HGraph& graph_;
6108 size_t index_;
6109
6110 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6111};
6112
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006113class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006114 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006115 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6116 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006117 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006118 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006119
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006120 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6121 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006122 void Advance() { ++index_; }
6123
6124 private:
6125 const HGraph& graph_;
6126 size_t index_;
6127
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006128 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006129};
6130
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006131class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006132 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006133 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006134 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006135 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006136 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006137 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006138
6139 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006140 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006141 void Advance() { --index_; }
6142
6143 private:
6144 const HGraph& graph_;
6145 size_t index_;
6146
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006147 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006148};
6149
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006150class HLinearPostOrderIterator : public ValueObject {
6151 public:
6152 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006153 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006154
6155 bool Done() const { return index_ == 0; }
6156
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006157 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006158
6159 void Advance() {
6160 --index_;
6161 DCHECK_GE(index_, 0U);
6162 }
6163
6164 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006165 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006166 size_t index_;
6167
6168 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6169};
6170
6171class HLinearOrderIterator : public ValueObject {
6172 public:
6173 explicit HLinearOrderIterator(const HGraph& graph)
6174 : order_(graph.GetLinearOrder()), index_(0) {}
6175
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006176 bool Done() const { return index_ == order_.size(); }
6177 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006178 void Advance() { ++index_; }
6179
6180 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006181 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006182 size_t index_;
6183
6184 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6185};
6186
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006187// Iterator over the blocks that art part of the loop. Includes blocks part
6188// of an inner loop. The order in which the blocks are iterated is on their
6189// block id.
6190class HBlocksInLoopIterator : public ValueObject {
6191 public:
6192 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6193 : blocks_in_loop_(info.GetBlocks()),
6194 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6195 index_(0) {
6196 if (!blocks_in_loop_.IsBitSet(index_)) {
6197 Advance();
6198 }
6199 }
6200
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006201 bool Done() const { return index_ == blocks_.size(); }
6202 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006203 void Advance() {
6204 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006205 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006206 if (blocks_in_loop_.IsBitSet(index_)) {
6207 break;
6208 }
6209 }
6210 }
6211
6212 private:
6213 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006214 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006215 size_t index_;
6216
6217 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6218};
6219
Mingyao Yang3584bce2015-05-19 16:01:59 -07006220// Iterator over the blocks that art part of the loop. Includes blocks part
6221// of an inner loop. The order in which the blocks are iterated is reverse
6222// post order.
6223class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6224 public:
6225 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6226 : blocks_in_loop_(info.GetBlocks()),
6227 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6228 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006229 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006230 Advance();
6231 }
6232 }
6233
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006234 bool Done() const { return index_ == blocks_.size(); }
6235 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006236 void Advance() {
6237 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006238 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6239 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006240 break;
6241 }
6242 }
6243 }
6244
6245 private:
6246 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006247 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006248 size_t index_;
6249
6250 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6251};
6252
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006253inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006254 if (constant->IsIntConstant()) {
6255 return constant->AsIntConstant()->GetValue();
6256 } else if (constant->IsLongConstant()) {
6257 return constant->AsLongConstant()->GetValue();
6258 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006259 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006260 return 0;
6261 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006262}
6263
Vladimir Marko58155012015-08-19 12:49:41 +00006264inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6265 // For the purposes of the compiler, the dex files must actually be the same object
6266 // if we want to safely treat them as the same. This is especially important for JIT
6267 // as custom class loaders can open the same underlying file (or memory) multiple
6268 // times and provide different class resolution but no two class loaders should ever
6269 // use the same DexFile object - doing so is an unsupported hack that can lead to
6270 // all sorts of weird failures.
6271 return &lhs == &rhs;
6272}
6273
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006274#define INSTRUCTION_TYPE_CHECK(type, super) \
6275 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6276 inline const H##type* HInstruction::As##type() const { \
6277 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6278 } \
6279 inline H##type* HInstruction::As##type() { \
6280 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6281 }
6282
6283 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6284#undef INSTRUCTION_TYPE_CHECK
6285
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006286class SwitchTable : public ValueObject {
6287 public:
6288 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6289 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6290 int32_t table_offset = instruction.VRegB_31t();
6291 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6292 if (sparse) {
6293 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6294 } else {
6295 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6296 }
6297 num_entries_ = table[1];
6298 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6299 }
6300
6301 uint16_t GetNumEntries() const {
6302 return num_entries_;
6303 }
6304
6305 void CheckIndex(size_t index) const {
6306 if (sparse_) {
6307 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6308 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6309 } else {
6310 // In a packed table, we have the starting key and num_entries_ values.
6311 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6312 }
6313 }
6314
6315 int32_t GetEntryAt(size_t index) const {
6316 CheckIndex(index);
6317 return values_[index];
6318 }
6319
6320 uint32_t GetDexPcForIndex(size_t index) const {
6321 CheckIndex(index);
6322 return dex_pc_ +
6323 (reinterpret_cast<const int16_t*>(values_ + index) -
6324 reinterpret_cast<const int16_t*>(&instruction_));
6325 }
6326
6327 // Index of the first value in the table.
6328 size_t GetFirstValueIndex() const {
6329 if (sparse_) {
6330 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6331 return num_entries_;
6332 } else {
6333 // In a packed table, we have the starting key and num_entries_ values.
6334 return 1;
6335 }
6336 }
6337
6338 private:
6339 const Instruction& instruction_;
6340 const uint32_t dex_pc_;
6341
6342 // Whether this is a sparse-switch table (or a packed-switch one).
6343 const bool sparse_;
6344
6345 // This can't be const as it needs to be computed off of the given instruction, and complicated
6346 // expressions in the initializer list seemed very ugly.
6347 uint16_t num_entries_;
6348
6349 const int32_t* values_;
6350
6351 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6352};
6353
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006354// Create space in `blocks` for adding `number_of_new_blocks` entries
6355// starting at location `at`. Blocks after `at` are moved accordingly.
6356inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6357 size_t number_of_new_blocks,
6358 size_t after) {
6359 DCHECK_LT(after, blocks->size());
6360 size_t old_size = blocks->size();
6361 size_t new_size = old_size + number_of_new_blocks;
6362 blocks->resize(new_size);
6363 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6364}
6365
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006366} // namespace art
6367
6368#endif // ART_COMPILER_OPTIMIZING_NODES_H_