blob: 711a6c1b2dc4aa66f19ebc9aa253ae91c0aaaa53 [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"
Vladimir Marko46817b82016-03-29 12:21:58 +010039#include "utils/intrusive_forward_list.h"
Vladimir Marko372f10e2016-05-17 16:30:10 +010040#include "utils/transform_array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041
42namespace art {
43
David Brazdil1abb4192015-02-17 18:33:36 +000044class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010046class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010048class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000050class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000053class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000054class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000056class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010057class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010058class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010059class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000061class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010062class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000064
Mathieu Chartier736b5602015-09-02 14:54:11 -070065namespace mirror {
66class DexCache;
67} // namespace mirror
68
Nicolas Geoffray818f2102014-02-18 16:43:35 +000069static const int kDefaultNumberOfBlocks = 8;
70static const int kDefaultNumberOfSuccessors = 2;
71static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010072static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010073static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000074static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075
Roland Levillain5b5b9312016-03-22 14:57:31 +000076// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
77static constexpr int32_t kMaxIntShiftDistance = 0x1f;
78// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
79static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000080
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010081static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070082static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010083
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010084static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
85
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060086static constexpr uint32_t kNoDexPc = -1;
87
Dave Allison20dfc792014-06-16 20:44:29 -070088enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070089 // All types.
90 kCondEQ, // ==
91 kCondNE, // !=
92 // Signed integers and floating-point numbers.
93 kCondLT, // <
94 kCondLE, // <=
95 kCondGT, // >
96 kCondGE, // >=
97 // Unsigned integers.
98 kCondB, // <
99 kCondBE, // <=
100 kCondA, // >
101 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700102};
103
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000104enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000105 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000106 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000107 kAnalysisFailThrowCatchLoop,
108 kAnalysisFailAmbiguousArrayOp,
109 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000110};
111
Vladimir Markof9f64412015-09-02 14:05:49 +0100112class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100113 public:
114 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
115
116 void AddInstruction(HInstruction* instruction);
117 void RemoveInstruction(HInstruction* instruction);
118
David Brazdilc3d743f2015-04-22 13:40:50 +0100119 // Insert `instruction` before/after an existing instruction `cursor`.
120 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
121 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
122
Roland Levillain6b469232014-09-25 10:10:38 +0100123 // Return true if this list contains `instruction`.
124 bool Contains(HInstruction* instruction) const;
125
Roland Levillainccc07a92014-09-16 14:48:16 +0100126 // Return true if `instruction1` is found before `instruction2` in
127 // this instruction list and false otherwise. Abort if none
128 // of these instructions is found.
129 bool FoundBefore(const HInstruction* instruction1,
130 const HInstruction* instruction2) const;
131
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000132 bool IsEmpty() const { return first_instruction_ == nullptr; }
133 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
134
135 // Update the block of all instructions to be `block`.
136 void SetBlockOfInstructions(HBasicBlock* block) const;
137
138 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000139 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000140 void Add(const HInstructionList& instruction_list);
141
David Brazdil2d7352b2015-04-20 14:52:42 +0100142 // Return the number of instructions in the list. This is an expensive operation.
143 size_t CountSize() const;
144
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145 private:
146 HInstruction* first_instruction_;
147 HInstruction* last_instruction_;
148
149 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000150 friend class HGraph;
151 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100152 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100153 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100154
155 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
156};
157
David Brazdil4833f5a2015-12-16 10:37:39 +0000158class ReferenceTypeInfo : ValueObject {
159 public:
160 typedef Handle<mirror::Class> TypeHandle;
161
Vladimir Markoa1de9182016-02-25 11:37:38 +0000162 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
163
164 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000165 return ReferenceTypeInfo(type_handle, is_exact);
166 }
167
168 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
169
Vladimir Markof39745e2016-01-26 12:16:55 +0000170 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000171 return handle.GetReference() != nullptr;
172 }
173
Vladimir Marko456307a2016-04-19 14:12:13 +0000174 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000175 return IsValidHandle(type_handle_);
176 }
177
178 bool IsExact() const { return is_exact_; }
179
180 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
181 DCHECK(IsValid());
182 return GetTypeHandle()->IsObjectClass();
183 }
184
185 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
186 DCHECK(IsValid());
187 return GetTypeHandle()->IsStringClass();
188 }
189
190 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
191 DCHECK(IsValid());
192 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
193 }
194
195 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
196 DCHECK(IsValid());
197 return GetTypeHandle()->IsInterface();
198 }
199
200 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
201 DCHECK(IsValid());
202 return GetTypeHandle()->IsArrayClass();
203 }
204
205 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
206 DCHECK(IsValid());
207 return GetTypeHandle()->IsPrimitiveArray();
208 }
209
210 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
211 DCHECK(IsValid());
212 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
213 }
214
215 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
216 DCHECK(IsValid());
217 if (!IsExact()) return false;
218 if (!IsArrayClass()) return false;
219 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
220 }
221
222 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
223 DCHECK(IsValid());
224 if (!IsExact()) return false;
225 if (!IsArrayClass()) return false;
226 if (!rti.IsArrayClass()) return false;
227 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
228 rti.GetTypeHandle()->GetComponentType());
229 }
230
231 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
232
233 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
234 DCHECK(IsValid());
235 DCHECK(rti.IsValid());
236 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
237 }
238
239 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
240 DCHECK(IsValid());
241 DCHECK(rti.IsValid());
242 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
243 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
244 }
245
246 // Returns true if the type information provide the same amount of details.
247 // Note that it does not mean that the instructions have the same actual type
248 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000249 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000250 if (!IsValid() && !rti.IsValid()) {
251 // Invalid types are equal.
252 return true;
253 }
254 if (!IsValid() || !rti.IsValid()) {
255 // One is valid, the other not.
256 return false;
257 }
258 return IsExact() == rti.IsExact()
259 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
260 }
261
262 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000263 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
264 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
265 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000266
267 // The class of the object.
268 TypeHandle type_handle_;
269 // Whether or not the type is exact or a superclass of the actual type.
270 // Whether or not we have any information about this type.
271 bool is_exact_;
272};
273
274std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
275
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000276// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100277class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100279 HGraph(ArenaAllocator* arena,
280 const DexFile& dex_file,
281 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100282 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700283 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100284 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100285 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000286 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100287 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000288 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100289 blocks_(arena->Adapter(kArenaAllocBlockList)),
290 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
291 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700292 entry_block_(nullptr),
293 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100294 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100295 number_of_vregs_(0),
296 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000297 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400298 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000299 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000300 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000301 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000302 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100303 dex_file_(dex_file),
304 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100305 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100306 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100307 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000309 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
311 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
312 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
313 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000314 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000315 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
316 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100317 blocks_.reserve(kDefaultNumberOfBlocks);
318 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000319
David Brazdilbadd8262016-02-02 16:28:56 +0000320 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
321 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
322
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000323 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100324 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
325
David Brazdil69ba7b72015-06-23 18:27:30 +0100326 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000327 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100328
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000329 HBasicBlock* GetEntryBlock() const { return entry_block_; }
330 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100331 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000332
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000333 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
334 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000335
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000336 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100337
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100338 void ComputeDominanceInformation();
339 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000340 void ClearLoopInformation();
341 void FindBackEdges(ArenaBitVector* visited);
342 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100343 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100344 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000345
David Brazdil4833f5a2015-12-16 10:37:39 +0000346 // Analyze all natural loops in this graph. Returns a code specifying that it
347 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000348 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000349 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100350
David Brazdilffee3d32015-07-06 11:48:53 +0100351 // Iterate over blocks to compute try block membership. Needs reverse post
352 // order and loop information.
353 void ComputeTryBlockInformation();
354
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000355 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000356 // Returns the instruction to replace the invoke expression or null if the
357 // invoke is for a void method. Note that the caller is responsible for replacing
358 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000359 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000360
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000361 // Update the loop and try membership of `block`, which was spawned from `reference`.
362 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
363 // should be the new back edge.
364 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
365 HBasicBlock* reference,
366 bool replace_if_back_edge);
367
Mingyao Yang3584bce2015-05-19 16:01:59 -0700368 // Need to add a couple of blocks to test if the loop body is entered and
369 // put deoptimization instructions, etc.
370 void TransformLoopHeaderForBCE(HBasicBlock* header);
371
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000372 // Removes `block` from the graph. Assumes `block` has been disconnected from
373 // other blocks and has no instructions or phis.
374 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000375
David Brazdilfc6a86a2015-06-26 10:33:45 +0000376 // Splits the edge between `block` and `successor` while preserving the
377 // indices in the predecessor/successor lists. If there are multiple edges
378 // between the blocks, the lowest indices are used.
379 // Returns the new block which is empty and has the same dex pc as `successor`.
380 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
381
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
383 void SimplifyLoop(HBasicBlock* header);
384
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000385 int32_t GetNextInstructionId() {
386 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000387 return current_instruction_id_++;
388 }
389
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000390 int32_t GetCurrentInstructionId() const {
391 return current_instruction_id_;
392 }
393
394 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000395 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396 current_instruction_id_ = id;
397 }
398
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100399 uint16_t GetMaximumNumberOfOutVRegs() const {
400 return maximum_number_of_out_vregs_;
401 }
402
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000403 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
404 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100405 }
406
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100407 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
408 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
409 }
410
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000411 void UpdateTemporariesVRegSlots(size_t slots) {
412 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 }
414
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000415 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100416 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000417 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100418 }
419
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100420 void SetNumberOfVRegs(uint16_t number_of_vregs) {
421 number_of_vregs_ = number_of_vregs;
422 }
423
424 uint16_t GetNumberOfVRegs() const {
425 return number_of_vregs_;
426 }
427
428 void SetNumberOfInVRegs(uint16_t value) {
429 number_of_in_vregs_ = value;
430 }
431
David Brazdildee58d62016-04-07 09:54:26 +0000432 uint16_t GetNumberOfInVRegs() const {
433 return number_of_in_vregs_;
434 }
435
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100436 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100437 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100438 return number_of_vregs_ - number_of_in_vregs_;
439 }
440
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100441 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100442 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100443 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100444
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100445 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100446 return linear_order_;
447 }
448
Mark Mendell1152c922015-04-24 17:06:35 -0400449 bool HasBoundsChecks() const {
450 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800451 }
452
Mark Mendell1152c922015-04-24 17:06:35 -0400453 void SetHasBoundsChecks(bool value) {
454 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800455 }
456
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100457 bool ShouldGenerateConstructorBarrier() const {
458 return should_generate_constructor_barrier_;
459 }
460
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000461 bool IsDebuggable() const { return debuggable_; }
462
David Brazdil8d5b8b22015-03-24 10:51:52 +0000463 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000464 // already, it is created and inserted into the graph. This method is only for
465 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600466 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000467
468 // TODO: This is problematic for the consistency of reference type propagation
469 // because it can be created anytime after the pass and thus it will be left
470 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600471 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000472
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600473 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
474 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000475 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600476 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
477 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000478 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600479 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
480 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000481 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600482 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
483 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000484 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000485
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100486 HCurrentMethod* GetCurrentMethod();
487
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100488 const DexFile& GetDexFile() const {
489 return dex_file_;
490 }
491
492 uint32_t GetMethodIdx() const {
493 return method_idx_;
494 }
495
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100496 InvokeType GetInvokeType() const {
497 return invoke_type_;
498 }
499
Mark Mendellc4701932015-04-10 13:18:51 -0400500 InstructionSet GetInstructionSet() const {
501 return instruction_set_;
502 }
503
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000504 bool IsCompilingOsr() const { return osr_; }
505
David Brazdil77a48ae2015-09-15 12:34:04 +0000506 bool HasTryCatch() const { return has_try_catch_; }
507 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100508
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000509 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
510 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
511
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100512 ArtMethod* GetArtMethod() const { return art_method_; }
513 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
514
Mark Mendellf6529172015-11-17 11:16:56 -0500515 // Returns an instruction with the opposite boolean value from 'cond'.
516 // The instruction has been inserted into the graph, either as a constant, or
517 // before cursor.
518 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
519
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000520 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
521
David Brazdil2d7352b2015-04-20 14:52:42 +0100522 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000523 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100524 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000525
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000526 template <class InstructionType, typename ValueType>
527 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600528 ArenaSafeMap<ValueType, InstructionType*>* cache,
529 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000530 // Try to find an existing constant of the given value.
531 InstructionType* constant = nullptr;
532 auto cached_constant = cache->find(value);
533 if (cached_constant != cache->end()) {
534 constant = cached_constant->second;
535 }
536
537 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100538 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000539 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600540 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000541 cache->Overwrite(value, constant);
542 InsertConstant(constant);
543 }
544 return constant;
545 }
546
David Brazdil8d5b8b22015-03-24 10:51:52 +0000547 void InsertConstant(HConstant* instruction);
548
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000549 // Cache a float constant into the graph. This method should only be
550 // called by the SsaBuilder when creating "equivalent" instructions.
551 void CacheFloatConstant(HFloatConstant* constant);
552
553 // See CacheFloatConstant comment.
554 void CacheDoubleConstant(HDoubleConstant* constant);
555
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000556 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557
558 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100559 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000560
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100561 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100562 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100564 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100565 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100566
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000567 HBasicBlock* entry_block_;
568 HBasicBlock* exit_block_;
569
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100570 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100571 uint16_t maximum_number_of_out_vregs_;
572
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100573 // The number of virtual registers in this method. Contains the parameters.
574 uint16_t number_of_vregs_;
575
576 // The number of virtual registers used by parameters of this method.
577 uint16_t number_of_in_vregs_;
578
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000579 // Number of vreg size slots that the temporaries use (used in baseline compiler).
580 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100581
Mark Mendell1152c922015-04-24 17:06:35 -0400582 // Has bounds checks. We can totally skip BCE if it's false.
583 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800584
David Brazdil77a48ae2015-09-15 12:34:04 +0000585 // Flag whether there are any try/catch blocks in the graph. We will skip
586 // try/catch-related passes if false.
587 bool has_try_catch_;
588
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000589 // Flag whether there are any irreducible loops in the graph.
590 bool has_irreducible_loops_;
591
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000592 // Indicates whether the graph should be compiled in a way that
593 // ensures full debuggability. If false, we can apply more
594 // aggressive optimizations that may limit the level of debugging.
595 const bool debuggable_;
596
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000597 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000598 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000599
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100600 // The dex file from which the method is from.
601 const DexFile& dex_file_;
602
603 // The method index in the dex file.
604 const uint32_t method_idx_;
605
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100606 // If inlined, this encodes how the callee is being invoked.
607 const InvokeType invoke_type_;
608
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100609 // Whether the graph has been transformed to SSA form. Only used
610 // in debug mode to ensure we are not using properties only valid
611 // for non-SSA form (like the number of temporaries).
612 bool in_ssa_form_;
613
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100614 const bool should_generate_constructor_barrier_;
615
Mathieu Chartiere401d142015-04-22 13:56:20 -0700616 const InstructionSet instruction_set_;
617
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000618 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000619 HNullConstant* cached_null_constant_;
620 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000622 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000623 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000624
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100625 HCurrentMethod* cached_current_method_;
626
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100627 // The ArtMethod this graph is for. Note that for AOT, it may be null,
628 // for example for methods whose declaring class could not be resolved
629 // (such as when the superclass could not be found).
630 ArtMethod* art_method_;
631
David Brazdil4833f5a2015-12-16 10:37:39 +0000632 // Keep the RTI of inexact Object to avoid having to pass stack handle
633 // collection pointer to passes which may create NullConstant.
634 ReferenceTypeInfo inexact_object_rti_;
635
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000636 // Whether we are compiling this graph for on stack replacement: this will
637 // make all loops seen as irreducible and emit special stack maps to mark
638 // compiled code entries which the interpreter can directly jump to.
639 const bool osr_;
640
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000641 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100642 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000643 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000644 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 DISALLOW_COPY_AND_ASSIGN(HGraph);
646};
647
Vladimir Markof9f64412015-09-02 14:05:49 +0100648class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000649 public:
650 HLoopInformation(HBasicBlock* header, HGraph* graph)
651 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100652 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000653 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100654 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100655 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100656 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000657 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100658 back_edges_.reserve(kDefaultNumberOfBackEdges);
659 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100660
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000661 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100662 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000663
664 void Dump(std::ostream& os);
665
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100666 HBasicBlock* GetHeader() const {
667 return header_;
668 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000669
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000670 void SetHeader(HBasicBlock* block) {
671 header_ = block;
672 }
673
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100674 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
675 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
676 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
677
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000678 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000680 }
681
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100682 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100683 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100684 }
685
David Brazdil46e2a392015-03-16 17:31:52 +0000686 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100687 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100688 }
689
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000690 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100691 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000692 }
693
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100695
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100696 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100698 }
699
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100700 // Returns the lifetime position of the back edge that has the
701 // greatest lifetime position.
702 size_t GetLifetimeEnd() const;
703
704 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100705 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100706 }
707
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000708 // Finds blocks that are part of this loop.
709 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100710
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100711 // Returns whether this loop information contains `block`.
712 // Note that this loop information *must* be populated before entering this function.
713 bool Contains(const HBasicBlock& block) const;
714
715 // Returns whether this loop information is an inner loop of `other`.
716 // Note that `other` *must* be populated before entering this function.
717 bool IsIn(const HLoopInformation& other) const;
718
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800719 // Returns true if instruction is not defined within this loop.
720 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700721
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100722 const ArenaBitVector& GetBlocks() const { return blocks_; }
723
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000724 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000725 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000726
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000727 void ClearAllBlocks() {
728 blocks_.ClearAllBits();
729 }
730
David Brazdil3f4a5222016-05-06 12:46:21 +0100731 bool HasBackEdgeNotDominatedByHeader() const;
732
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100733 bool IsPopulated() const {
734 return blocks_.GetHighestBitSet() != -1;
735 }
736
Anton Shaminf89381f2016-05-16 16:44:13 +0600737 bool DominatesAllBackEdges(HBasicBlock* block);
738
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000739 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100740 // Internal recursive implementation of `Populate`.
741 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100742 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100743
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000744 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100745 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000746 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100747 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100748 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100749 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000750
751 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
752};
753
David Brazdilec16f792015-08-19 15:04:01 +0100754// Stores try/catch information for basic blocks.
755// Note that HGraph is constructed so that catch blocks cannot simultaneously
756// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100757class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100758 public:
759 // Try block information constructor.
760 explicit TryCatchInformation(const HTryBoundary& try_entry)
761 : try_entry_(&try_entry),
762 catch_dex_file_(nullptr),
763 catch_type_index_(DexFile::kDexNoIndex16) {
764 DCHECK(try_entry_ != nullptr);
765 }
766
767 // Catch block information constructor.
768 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
769 : try_entry_(nullptr),
770 catch_dex_file_(&dex_file),
771 catch_type_index_(catch_type_index) {}
772
773 bool IsTryBlock() const { return try_entry_ != nullptr; }
774
775 const HTryBoundary& GetTryEntry() const {
776 DCHECK(IsTryBlock());
777 return *try_entry_;
778 }
779
780 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
781
782 bool IsCatchAllTypeIndex() const {
783 DCHECK(IsCatchBlock());
784 return catch_type_index_ == DexFile::kDexNoIndex16;
785 }
786
787 uint16_t GetCatchTypeIndex() const {
788 DCHECK(IsCatchBlock());
789 return catch_type_index_;
790 }
791
792 const DexFile& GetCatchDexFile() const {
793 DCHECK(IsCatchBlock());
794 return *catch_dex_file_;
795 }
796
797 private:
798 // One of possibly several TryBoundary instructions entering the block's try.
799 // Only set for try blocks.
800 const HTryBoundary* try_entry_;
801
802 // Exception type information. Only set for catch blocks.
803 const DexFile* catch_dex_file_;
804 const uint16_t catch_type_index_;
805};
806
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100807static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100808static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100809
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000810// A block in a method. Contains the list of instructions represented
811// as a double linked list. Each block knows its predecessors and
812// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100813
Vladimir Markof9f64412015-09-02 14:05:49 +0100814class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000815 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600816 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000818 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
819 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000820 loop_information_(nullptr),
821 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000822 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100823 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100824 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100825 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000826 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000827 try_catch_information_(nullptr) {
828 predecessors_.reserve(kDefaultNumberOfPredecessors);
829 successors_.reserve(kDefaultNumberOfSuccessors);
830 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
831 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000832
Vladimir Marko60584552015-09-03 13:35:12 +0000833 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100834 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000835 }
836
Vladimir Marko60584552015-09-03 13:35:12 +0000837 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100838 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000839 }
840
David Brazdild26a4112015-11-10 11:07:31 +0000841 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
842 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
843
Vladimir Marko60584552015-09-03 13:35:12 +0000844 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
845 return ContainsElement(successors_, block, start_from);
846 }
847
848 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100849 return dominated_blocks_;
850 }
851
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100852 bool IsEntryBlock() const {
853 return graph_->GetEntryBlock() == this;
854 }
855
856 bool IsExitBlock() const {
857 return graph_->GetExitBlock() == this;
858 }
859
David Brazdil46e2a392015-03-16 17:31:52 +0000860 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000861 bool IsSingleTryBoundary() const;
862
863 // Returns true if this block emits nothing but a jump.
864 bool IsSingleJump() const {
865 HLoopInformation* loop_info = GetLoopInformation();
866 return (IsSingleGoto() || IsSingleTryBoundary())
867 // Back edges generate a suspend check.
868 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
869 }
David Brazdil46e2a392015-03-16 17:31:52 +0000870
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871 void AddBackEdge(HBasicBlock* back_edge) {
872 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000873 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000874 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100875 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000876 loop_information_->AddBackEdge(back_edge);
877 }
878
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000879 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000880 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000881
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100882 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000883 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600884 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000885
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000886 HBasicBlock* GetDominator() const { return dominator_; }
887 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000888 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
889
890 void RemoveDominatedBlock(HBasicBlock* block) {
891 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100892 }
Vladimir Marko60584552015-09-03 13:35:12 +0000893
894 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
895 ReplaceElement(dominated_blocks_, existing, new_block);
896 }
897
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100898 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000899
900 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100901 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000902 }
903
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100904 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
905 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100906 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100907 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100908 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
909 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000911 HInstruction* GetFirstInstructionDisregardMoves() const;
912
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000913 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000914 successors_.push_back(block);
915 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916 }
917
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100918 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
919 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100920 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000921 new_block->predecessors_.push_back(this);
922 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000923 }
924
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000925 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
926 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000927 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000928 new_block->successors_.push_back(this);
929 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000930 }
931
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100932 // Insert `this` between `predecessor` and `successor. This method
933 // preserves the indicies, and will update the first edge found between
934 // `predecessor` and `successor`.
935 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
936 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100937 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000938 successor->predecessors_[predecessor_index] = this;
939 predecessor->successors_[successor_index] = this;
940 successors_.push_back(successor);
941 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100942 }
943
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100944 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000945 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100946 }
947
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000948 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000949 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000950 }
951
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100952 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000953 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100954 }
955
956 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000957 predecessors_.push_back(block);
958 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100959 }
960
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100961 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000962 DCHECK_EQ(predecessors_.size(), 2u);
963 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100964 }
965
David Brazdil769c9e52015-04-27 13:54:09 +0100966 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000967 DCHECK_EQ(successors_.size(), 2u);
968 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100969 }
970
David Brazdilfc6a86a2015-06-26 10:33:45 +0000971 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000972 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100973 }
974
David Brazdilfc6a86a2015-06-26 10:33:45 +0000975 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000976 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100977 }
978
David Brazdilfc6a86a2015-06-26 10:33:45 +0000979 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000980 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100981 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000982 }
983
984 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000985 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100986 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000987 }
988
989 // Returns whether the first occurrence of `predecessor` in the list of
990 // predecessors is at index `idx`.
991 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100992 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000993 return GetPredecessorIndexOf(predecessor) == idx;
994 }
995
David Brazdild7558da2015-09-22 13:04:14 +0100996 // Create a new block between this block and its predecessors. The new block
997 // is added to the graph, all predecessor edges are relinked to it and an edge
998 // is created to `this`. Returns the new empty block. Reverse post order or
999 // loop and try/catch information are not updated.
1000 HBasicBlock* CreateImmediateDominator();
1001
David Brazdilfc6a86a2015-06-26 10:33:45 +00001002 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001003 // created, latter block. Note that this method will add the block to the
1004 // graph, create a Goto at the end of the former block and will create an edge
1005 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001006 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001007 HBasicBlock* SplitBefore(HInstruction* cursor);
1008
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001009 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001010 // created block. Note that this method just updates raw block information,
1011 // like predecessors, successors, dominators, and instruction list. It does not
1012 // update the graph, reverse post order, loop information, nor make sure the
1013 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001014 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1015
1016 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1017 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001018
1019 // Merge `other` at the end of `this`. Successors and dominated blocks of
1020 // `other` are changed to be successors and dominated blocks of `this`. Note
1021 // that this method does not update the graph, reverse post order, loop
1022 // information, nor make sure the blocks are consistent (for example ending
1023 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001024 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025
1026 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1027 // of `this` are moved to `other`.
1028 // Note that this method does not update the graph, reverse post order, loop
1029 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001030 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001031 void ReplaceWith(HBasicBlock* other);
1032
David Brazdil2d7352b2015-04-20 14:52:42 +01001033 // Merge `other` at the end of `this`. This method updates loops, reverse post
1034 // order, links to predecessors, successors, dominators and deletes the block
1035 // from the graph. The two blocks must be successive, i.e. `this` the only
1036 // predecessor of `other` and vice versa.
1037 void MergeWith(HBasicBlock* other);
1038
1039 // Disconnects `this` from all its predecessors, successors and dominator,
1040 // removes it from all loops it is included in and eventually from the graph.
1041 // The block must not dominate any other block. Predecessors and successors
1042 // are safely updated.
1043 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001044
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001045 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001046 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001047 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001048 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001049 // Replace instruction `initial` with `replacement` within this block.
1050 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1051 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001052 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001053 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001054 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1055 // instruction list. With 'ensure_safety' set to true, it verifies that the
1056 // instruction is not in use and removes it from the use lists of its inputs.
1057 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1058 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001059 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001060
1061 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001062 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063 }
1064
Roland Levillain6b879dd2014-09-22 17:13:44 +01001065 bool IsLoopPreHeaderFirstPredecessor() const {
1066 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001067 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001068 }
1069
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001070 bool IsFirstPredecessorBackEdge() const {
1071 DCHECK(IsLoopHeader());
1072 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1073 }
1074
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075 HLoopInformation* GetLoopInformation() const {
1076 return loop_information_;
1077 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001078
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001079 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001080 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001081 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001082 void SetInLoop(HLoopInformation* info) {
1083 if (IsLoopHeader()) {
1084 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001085 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001086 loop_information_ = info;
1087 } else if (loop_information_->Contains(*info->GetHeader())) {
1088 // Block is currently part of an outer loop. Make it part of this inner loop.
1089 // Note that a non loop header having a loop information means this loop information
1090 // has already been populated
1091 loop_information_ = info;
1092 } else {
1093 // Block is part of an inner loop. Do not update the loop information.
1094 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1095 // at this point, because this method is being called while populating `info`.
1096 }
1097 }
1098
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001099 // Raw update of the loop information.
1100 void SetLoopInformation(HLoopInformation* info) {
1101 loop_information_ = info;
1102 }
1103
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001104 bool IsInLoop() const { return loop_information_ != nullptr; }
1105
David Brazdilec16f792015-08-19 15:04:01 +01001106 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1107
1108 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1109 try_catch_information_ = try_catch_information;
1110 }
1111
1112 bool IsTryBlock() const {
1113 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1114 }
1115
1116 bool IsCatchBlock() const {
1117 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1118 }
David Brazdilffee3d32015-07-06 11:48:53 +01001119
1120 // Returns the try entry that this block's successors should have. They will
1121 // be in the same try, unless the block ends in a try boundary. In that case,
1122 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001123 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001124
David Brazdild7558da2015-09-22 13:04:14 +01001125 bool HasThrowingInstructions() const;
1126
David Brazdila4b8c212015-05-07 09:59:30 +01001127 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001128 bool Dominates(HBasicBlock* block) const;
1129
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001130 size_t GetLifetimeStart() const { return lifetime_start_; }
1131 size_t GetLifetimeEnd() const { return lifetime_end_; }
1132
1133 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1134 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1135
David Brazdil8d5b8b22015-03-24 10:51:52 +00001136 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001137 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001138 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001139 bool HasSinglePhi() const;
1140
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001141 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001142 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001143 ArenaVector<HBasicBlock*> predecessors_;
1144 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001145 HInstructionList instructions_;
1146 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001147 HLoopInformation* loop_information_;
1148 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001149 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001150 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001151 // The dex program counter of the first instruction of this block.
1152 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001153 size_t lifetime_start_;
1154 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001155 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001156
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001157 friend class HGraph;
1158 friend class HInstruction;
1159
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001160 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1161};
1162
David Brazdilb2bd1c52015-03-25 11:17:37 +00001163// Iterates over the LoopInformation of all loops which contain 'block'
1164// from the innermost to the outermost.
1165class HLoopInformationOutwardIterator : public ValueObject {
1166 public:
1167 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1168 : current_(block.GetLoopInformation()) {}
1169
1170 bool Done() const { return current_ == nullptr; }
1171
1172 void Advance() {
1173 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001174 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001175 }
1176
1177 HLoopInformation* Current() const {
1178 DCHECK(!Done());
1179 return current_;
1180 }
1181
1182 private:
1183 HLoopInformation* current_;
1184
1185 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1186};
1187
Alexandre Ramesef20f712015-06-09 10:29:30 +01001188#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001189 M(Above, Condition) \
1190 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001191 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001192 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(ArrayGet, Instruction) \
1194 M(ArrayLength, Instruction) \
1195 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001196 M(Below, Condition) \
1197 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001198 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001200 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001201 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001202 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001203 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001204 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001205 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001206 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001207 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001208 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001209 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001211 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001212 M(Exit, Instruction) \
1213 M(FloatConstant, Constant) \
1214 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001215 M(GreaterThan, Condition) \
1216 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001217 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001218 M(InstanceFieldGet, Instruction) \
1219 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001220 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001222 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001223 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001224 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001226 M(LessThan, Condition) \
1227 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001228 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001229 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001230 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001231 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001232 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001233 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001234 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001235 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001236 M(Neg, UnaryOperation) \
1237 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001239 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001240 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001241 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001242 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001243 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001244 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001245 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001246 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001247 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001248 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 M(Return, Instruction) \
1250 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001251 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001252 M(Shl, BinaryOperation) \
1253 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001254 M(StaticFieldGet, Instruction) \
1255 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001256 M(UnresolvedInstanceFieldGet, Instruction) \
1257 M(UnresolvedInstanceFieldSet, Instruction) \
1258 M(UnresolvedStaticFieldGet, Instruction) \
1259 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001260 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001262 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001263 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001264 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001265 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001266 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001267 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001268
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001269/*
1270 * Instructions, shared across several (not all) architectures.
1271 */
1272#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1273#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1274#else
1275#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001276 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001277 M(MultiplyAccumulate, Instruction)
1278#endif
1279
Vladimir Markob4536b72015-11-24 13:45:23 +00001280#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001281#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001282#else
1283#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1284 M(ArmDexCacheArraysBase, Instruction)
1285#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001286
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001287#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001288#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001289#else
1290#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001291 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001292 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001293#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001294
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001295#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1296
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001297#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1298
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001299#ifndef ART_ENABLE_CODEGEN_x86
1300#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1301#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001302#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1303 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001304 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001305 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001306 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001307#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001308
1309#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1310
1311#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1312 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001313 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001314 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1315 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001317 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001318 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1319 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1320
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001321#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1322 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001323 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001324 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001325 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001326 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001327
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001328#define FOR_EACH_INSTRUCTION(M) \
1329 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1330 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1331
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001332#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001333FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1334#undef FORWARD_DECLARATION
1335
Vladimir Marko372f10e2016-05-17 16:30:10 +01001336#define DECLARE_INSTRUCTION(type) \
1337 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1338 const char* DebugName() const OVERRIDE { return #type; } \
1339 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1340 return other->Is##type(); \
1341 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001342 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001343
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001344#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1345 bool Is##type() const { return As##type() != nullptr; } \
1346 const H##type* As##type() const { return this; } \
1347 H##type* As##type() { return this; }
1348
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001349template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001350class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001351 public:
David Brazdiled596192015-01-23 10:39:45 +00001352 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001354 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001355
Vladimir Marko46817b82016-03-29 12:21:58 +01001356 // Hook for the IntrusiveForwardList<>.
1357 // TODO: Hide this better.
1358 IntrusiveForwardListHook hook;
1359
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001360 private:
David Brazdiled596192015-01-23 10:39:45 +00001361 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001362 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001363
1364 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001365 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001366
Vladimir Marko46817b82016-03-29 12:21:58 +01001367 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001368
1369 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1370};
1371
David Brazdiled596192015-01-23 10:39:45 +00001372template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001373using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001374
David Brazdil1abb4192015-02-17 18:33:36 +00001375// This class is used by HEnvironment and HInstruction classes to record the
1376// instructions they use and pointers to the corresponding HUseListNodes kept
1377// by the used instructions.
1378template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001379class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001380 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001381 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1382 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001383
Vladimir Marko46817b82016-03-29 12:21:58 +01001384 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1385 : HUserRecord(old_record.instruction_, before_use_node) {}
1386 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1387 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001388 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001389 }
1390
1391 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001392 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1393 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001394
1395 private:
1396 // Instruction used by the user.
1397 HInstruction* instruction_;
1398
Vladimir Marko46817b82016-03-29 12:21:58 +01001399 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1400 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001401};
1402
Aart Bik854a02b2015-07-14 16:07:00 -07001403/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001404 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001405 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001406 * For write/read dependences on fields/arrays, the dependence analysis uses
1407 * type disambiguation (e.g. a float field write cannot modify the value of an
1408 * integer field read) and the access type (e.g. a reference array write cannot
1409 * modify the value of a reference field read [although it may modify the
1410 * reference fetch prior to reading the field, which is represented by its own
1411 * write/read dependence]). The analysis makes conservative points-to
1412 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1413 * the same, and any reference read depends on any reference read without
1414 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001415 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001416 * The internal representation uses 38-bit and is described in the table below.
1417 * The first line indicates the side effect, and for field/array accesses the
1418 * second line indicates the type of the access (in the order of the
1419 * Primitive::Type enum).
1420 * The two numbered lines below indicate the bit position in the bitfield (read
1421 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001422 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001423 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1424 * +-------------+---------+---------+--------------+---------+---------+
1425 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1426 * | 3 |333333322|222222221| 1 |111111110|000000000|
1427 * | 7 |654321098|765432109| 8 |765432109|876543210|
1428 *
1429 * Note that, to ease the implementation, 'changes' bits are least significant
1430 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001431 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001432class SideEffects : public ValueObject {
1433 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001434 SideEffects() : flags_(0) {}
1435
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001436 static SideEffects None() {
1437 return SideEffects(0);
1438 }
1439
1440 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001441 return SideEffects(kAllChangeBits | kAllDependOnBits);
1442 }
1443
1444 static SideEffects AllChanges() {
1445 return SideEffects(kAllChangeBits);
1446 }
1447
1448 static SideEffects AllDependencies() {
1449 return SideEffects(kAllDependOnBits);
1450 }
1451
1452 static SideEffects AllExceptGCDependency() {
1453 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1454 }
1455
1456 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001457 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001458 }
1459
Aart Bik34c3ba92015-07-20 14:08:59 -07001460 static SideEffects AllWrites() {
1461 return SideEffects(kAllWrites);
1462 }
1463
1464 static SideEffects AllReads() {
1465 return SideEffects(kAllReads);
1466 }
1467
1468 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1469 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001470 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001471 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001472 }
1473
Aart Bik854a02b2015-07-14 16:07:00 -07001474 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001475 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001476 }
1477
Aart Bik34c3ba92015-07-20 14:08:59 -07001478 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1479 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001480 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001481 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001482 }
1483
1484 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001485 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001486 }
1487
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001488 static SideEffects CanTriggerGC() {
1489 return SideEffects(1ULL << kCanTriggerGCBit);
1490 }
1491
1492 static SideEffects DependsOnGC() {
1493 return SideEffects(1ULL << kDependsOnGCBit);
1494 }
1495
Aart Bik854a02b2015-07-14 16:07:00 -07001496 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001497 SideEffects Union(SideEffects other) const {
1498 return SideEffects(flags_ | other.flags_);
1499 }
1500
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001501 SideEffects Exclusion(SideEffects other) const {
1502 return SideEffects(flags_ & ~other.flags_);
1503 }
1504
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001505 void Add(SideEffects other) {
1506 flags_ |= other.flags_;
1507 }
1508
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001509 bool Includes(SideEffects other) const {
1510 return (other.flags_ & flags_) == other.flags_;
1511 }
1512
1513 bool HasSideEffects() const {
1514 return (flags_ & kAllChangeBits);
1515 }
1516
1517 bool HasDependencies() const {
1518 return (flags_ & kAllDependOnBits);
1519 }
1520
1521 // Returns true if there are no side effects or dependencies.
1522 bool DoesNothing() const {
1523 return flags_ == 0;
1524 }
1525
Aart Bik854a02b2015-07-14 16:07:00 -07001526 // Returns true if something is written.
1527 bool DoesAnyWrite() const {
1528 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001529 }
1530
Aart Bik854a02b2015-07-14 16:07:00 -07001531 // Returns true if something is read.
1532 bool DoesAnyRead() const {
1533 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001534 }
1535
Aart Bik854a02b2015-07-14 16:07:00 -07001536 // Returns true if potentially everything is written and read
1537 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001538 bool DoesAllReadWrite() const {
1539 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1540 }
1541
Aart Bik854a02b2015-07-14 16:07:00 -07001542 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001543 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001544 }
1545
Roland Levillain0d5a2812015-11-13 10:07:31 +00001546 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001547 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001548 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1549 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001550 }
1551
1552 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001554 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001555 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001556 for (int s = kLastBit; s >= 0; s--) {
1557 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1558 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1559 // This is a bit for the GC side effect.
1560 if (current_bit_is_set) {
1561 flags += "GC";
1562 }
Aart Bik854a02b2015-07-14 16:07:00 -07001563 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001564 } else {
1565 // This is a bit for the array/field analysis.
1566 // The underscore character stands for the 'can trigger GC' bit.
1567 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1568 if (current_bit_is_set) {
1569 flags += kDebug[s];
1570 }
1571 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1572 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1573 flags += "|";
1574 }
1575 }
Aart Bik854a02b2015-07-14 16:07:00 -07001576 }
1577 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001578 }
1579
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001580 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001581
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001582 private:
1583 static constexpr int kFieldArrayAnalysisBits = 9;
1584
1585 static constexpr int kFieldWriteOffset = 0;
1586 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1587 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1588 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1589
1590 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1591
1592 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1593 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1594 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1595 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1596
1597 static constexpr int kLastBit = kDependsOnGCBit;
1598 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1599
1600 // Aliases.
1601
1602 static_assert(kChangeBits == kDependOnBits,
1603 "the 'change' bits should match the 'depend on' bits.");
1604
1605 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1606 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1607 static constexpr uint64_t kAllWrites =
1608 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1609 static constexpr uint64_t kAllReads =
1610 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001611
Aart Bik854a02b2015-07-14 16:07:00 -07001612 // Translates type to bit flag.
1613 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1614 CHECK_NE(type, Primitive::kPrimVoid);
1615 const uint64_t one = 1;
1616 const int shift = type; // 0-based consecutive enum
1617 DCHECK_LE(kFieldWriteOffset, shift);
1618 DCHECK_LT(shift, kArrayWriteOffset);
1619 return one << (type + offset);
1620 }
1621
1622 // Private constructor on direct flags value.
1623 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1624
1625 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001626};
1627
David Brazdiled596192015-01-23 10:39:45 +00001628// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001629class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001630 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001631 HEnvironment(ArenaAllocator* arena,
1632 size_t number_of_vregs,
1633 const DexFile& dex_file,
1634 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001635 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001636 InvokeType invoke_type,
1637 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001638 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1639 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001640 parent_(nullptr),
1641 dex_file_(dex_file),
1642 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001643 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001644 invoke_type_(invoke_type),
1645 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001646 }
1647
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001648 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001649 : HEnvironment(arena,
1650 to_copy.Size(),
1651 to_copy.GetDexFile(),
1652 to_copy.GetMethodIdx(),
1653 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001654 to_copy.GetInvokeType(),
1655 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001656
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001657 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001658 if (parent_ != nullptr) {
1659 parent_->SetAndCopyParentChain(allocator, parent);
1660 } else {
1661 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1662 parent_->CopyFrom(parent);
1663 if (parent->GetParent() != nullptr) {
1664 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1665 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001666 }
David Brazdiled596192015-01-23 10:39:45 +00001667 }
1668
Vladimir Marko71bf8092015-09-15 15:33:14 +01001669 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001670 void CopyFrom(HEnvironment* environment);
1671
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001672 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1673 // input to the loop phi instead. This is for inserting instructions that
1674 // require an environment (like HDeoptimization) in the loop pre-header.
1675 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001676
1677 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001678 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001679 }
1680
1681 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001682 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001683 }
1684
David Brazdil1abb4192015-02-17 18:33:36 +00001685 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001686
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001687 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001688
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001689 HEnvironment* GetParent() const { return parent_; }
1690
1691 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001692 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001693 }
1694
1695 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001696 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001697 }
1698
1699 uint32_t GetDexPc() const {
1700 return dex_pc_;
1701 }
1702
1703 uint32_t GetMethodIdx() const {
1704 return method_idx_;
1705 }
1706
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001707 InvokeType GetInvokeType() const {
1708 return invoke_type_;
1709 }
1710
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001711 const DexFile& GetDexFile() const {
1712 return dex_file_;
1713 }
1714
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001715 HInstruction* GetHolder() const {
1716 return holder_;
1717 }
1718
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001719
1720 bool IsFromInlinedInvoke() const {
1721 return GetParent() != nullptr;
1722 }
1723
David Brazdiled596192015-01-23 10:39:45 +00001724 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001725 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1726 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001727 HEnvironment* parent_;
1728 const DexFile& dex_file_;
1729 const uint32_t method_idx_;
1730 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001731 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001732
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001733 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001734 HInstruction* const holder_;
1735
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001736 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001737
1738 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1739};
1740
Vladimir Markof9f64412015-09-02 14:05:49 +01001741class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001742 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001743 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001744 : previous_(nullptr),
1745 next_(nullptr),
1746 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001747 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001748 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001749 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001750 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001751 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001752 locations_(nullptr),
1753 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001754 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001755 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001756 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1757 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1758 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001759
Dave Allison20dfc792014-06-16 20:44:29 -07001760 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001761
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001762#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001763 enum InstructionKind {
1764 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1765 };
1766#undef DECLARE_KIND
1767
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001768 HInstruction* GetNext() const { return next_; }
1769 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001770
Calin Juravle77520bc2015-01-12 18:45:46 +00001771 HInstruction* GetNextDisregardingMoves() const;
1772 HInstruction* GetPreviousDisregardingMoves() const;
1773
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001774 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001775 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001776 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001777 bool IsInBlock() const { return block_ != nullptr; }
1778 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001779 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1780 bool IsIrreducibleLoopHeaderPhi() const {
1781 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1782 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001783
Vladimir Marko372f10e2016-05-17 16:30:10 +01001784 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1785
1786 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1787 // One virtual method is enough, just const_cast<> and then re-add the const.
1788 return ArrayRef<const HUserRecord<HInstruction*>>(
1789 const_cast<HInstruction*>(this)->GetInputRecords());
1790 }
1791
1792 auto GetInputs() {
1793 return MakeTransformArrayRef(
1794 GetInputRecords(),
1795 [](HUserRecord<HInstruction*>& record) -> HInstruction* {
1796 return record.GetInstruction();
1797 });
1798 }
1799
1800 auto GetInputs() const {
1801 return MakeTransformArrayRef(
1802 GetInputRecords(),
1803 [](const HUserRecord<HInstruction*>& record) -> const HInstruction* {
1804 return record.GetInstruction();
1805 });
1806 }
1807
1808 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001809 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810
Vladimir Marko372f10e2016-05-17 16:30:10 +01001811 void SetRawInputAt(size_t index, HInstruction* input) {
1812 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1813 }
1814
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001815 virtual void Accept(HGraphVisitor* visitor) = 0;
1816 virtual const char* DebugName() const = 0;
1817
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001818 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1819
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001820 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001821
1822 uint32_t GetDexPc() const { return dex_pc_; }
1823
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001824 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001825
Roland Levillaine161a2a2014-10-03 12:45:18 +01001826 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001827 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001828
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001829 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001830 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001831
Calin Juravle10e244f2015-01-26 18:54:32 +00001832 // Does not apply for all instructions, but having this at top level greatly
1833 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001834 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001835 virtual bool CanBeNull() const {
1836 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1837 return true;
1838 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001839
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001840 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001841 return false;
1842 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001843
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001844 virtual bool IsActualObject() const {
1845 return GetType() == Primitive::kPrimNot;
1846 }
1847
Calin Juravle2e768302015-07-28 14:41:11 +00001848 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001849
Calin Juravle61d544b2015-02-23 16:46:57 +00001850 ReferenceTypeInfo GetReferenceTypeInfo() const {
1851 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001852 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001853 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001854 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001856 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001857 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001858 // Note: fixup_end remains valid across push_front().
1859 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1860 HUseListNode<HInstruction*>* new_node =
1861 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1862 uses_.push_front(*new_node);
1863 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001864 }
1865
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001866 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001867 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001868 // Note: env_fixup_end remains valid across push_front().
1869 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1870 HUseListNode<HEnvironment*>* new_node =
1871 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1872 env_uses_.push_front(*new_node);
1873 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001874 }
1875
David Brazdil1abb4192015-02-17 18:33:36 +00001876 void RemoveAsUserOfInput(size_t input) {
1877 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001878 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1879 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1880 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001881 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001882
Vladimir Marko372f10e2016-05-17 16:30:10 +01001883 void RemoveAsUserOfAllInputs() {
1884 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1885 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1886 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1887 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1888 }
1889 }
1890
David Brazdil1abb4192015-02-17 18:33:36 +00001891 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1892 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001893
Vladimir Marko46817b82016-03-29 12:21:58 +01001894 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1895 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1896 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001897 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001898 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001899 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001900
Roland Levillain6c82d402014-10-13 16:10:27 +01001901 // Does this instruction strictly dominate `other_instruction`?
1902 // Returns false if this instruction and `other_instruction` are the same.
1903 // Aborts if this instruction and `other_instruction` are both phis.
1904 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001905
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001906 int GetId() const { return id_; }
1907 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001908
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001909 int GetSsaIndex() const { return ssa_index_; }
1910 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1911 bool HasSsaIndex() const { return ssa_index_ != -1; }
1912
1913 bool HasEnvironment() const { return environment_ != nullptr; }
1914 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001915 // Set the `environment_` field. Raw because this method does not
1916 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001917 void SetRawEnvironment(HEnvironment* environment) {
1918 DCHECK(environment_ == nullptr);
1919 DCHECK_EQ(environment->GetHolder(), this);
1920 environment_ = environment;
1921 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001922
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001923 void RemoveEnvironment();
1924
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001925 // Set the environment of this instruction, copying it from `environment`. While
1926 // copying, the uses lists are being updated.
1927 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001928 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001929 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001930 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001931 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001932 if (environment->GetParent() != nullptr) {
1933 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1934 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001935 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001936
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001937 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1938 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001939 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001940 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001941 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001942 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001943 if (environment->GetParent() != nullptr) {
1944 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1945 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001946 }
1947
Nicolas Geoffray39468442014-09-02 15:17:15 +01001948 // Returns the number of entries in the environment. Typically, that is the
1949 // number of dex registers in a method. It could be more in case of inlining.
1950 size_t EnvironmentSize() const;
1951
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001952 LocationSummary* GetLocations() const { return locations_; }
1953 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001954
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001955 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001956 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957
Alexandre Rames188d4312015-04-09 18:30:21 +01001958 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1959 // uses of this instruction by `other` are *not* updated.
1960 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1961 ReplaceWith(other);
1962 other->ReplaceInput(this, use_index);
1963 }
1964
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001965 // Move `this` instruction before `cursor`.
1966 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001967
Vladimir Markofb337ea2015-11-25 15:25:10 +00001968 // Move `this` before its first user and out of any loops. If there is no
1969 // out-of-loop user that dominates all other users, move the instruction
1970 // to the end of the out-of-loop common dominator of the user's blocks.
1971 //
1972 // This can be used only on non-throwing instructions with no side effects that
1973 // have at least one use but no environment uses.
1974 void MoveBeforeFirstUserAndOutOfLoops();
1975
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001976#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977 bool Is##type() const; \
1978 const H##type* As##type() const; \
1979 H##type* As##type();
1980
1981 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1982#undef INSTRUCTION_TYPE_CHECK
1983
1984#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001985 bool Is##type() const { return (As##type() != nullptr); } \
1986 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001987 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001989#undef INSTRUCTION_TYPE_CHECK
1990
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001991 // Returns whether the instruction can be moved within the graph.
1992 virtual bool CanBeMoved() const { return false; }
1993
1994 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001995 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001996 return false;
1997 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001998
1999 // Returns whether any data encoded in the two instructions is equal.
2000 // This method does not look at the inputs. Both instructions must be
2001 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002002 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002003 return false;
2004 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002005
2006 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002007 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002008 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002009 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002010
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2012 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2013 // the virtual function because the __attribute__((__pure__)) doesn't really
2014 // apply the strong requirement for virtual functions, preventing optimizations.
2015 InstructionKind GetKind() const PURE;
2016 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002017
2018 virtual size_t ComputeHashCode() const {
2019 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002020 for (const HInstruction* input : GetInputs()) {
2021 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002022 }
2023 return result;
2024 }
2025
2026 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002027 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002028 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002029
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002030 size_t GetLifetimePosition() const { return lifetime_position_; }
2031 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2032 LiveInterval* GetLiveInterval() const { return live_interval_; }
2033 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2034 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2035
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002036 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2037
2038 // Returns whether the code generation of the instruction will require to have access
2039 // to the current method. Such instructions are:
2040 // (1): Instructions that require an environment, as calling the runtime requires
2041 // to walk the stack and have the current method stored at a specific stack address.
2042 // (2): Object literals like classes and strings, that are loaded from the dex cache
2043 // fields of the current method.
2044 bool NeedsCurrentMethod() const {
2045 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2046 }
2047
Vladimir Markodc151b22015-10-15 18:02:30 +01002048 // Returns whether the code generation of the instruction will require to have access
2049 // to the dex cache of the current method's declaring class via the current method.
2050 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002051
Mark Mendellc4701932015-04-10 13:18:51 -04002052 // Does this instruction have any use in an environment before
2053 // control flow hits 'other'?
2054 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2055
2056 // Remove all references to environment uses of this instruction.
2057 // The caller must ensure that this is safe to do.
2058 void RemoveEnvironmentUsers();
2059
Vladimir Markoa1de9182016-02-25 11:37:38 +00002060 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2061 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002062
David Brazdil1abb4192015-02-17 18:33:36 +00002063 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002064 // If set, the machine code for this instruction is assumed to be generated by
2065 // its users. Used by liveness analysis to compute use positions accordingly.
2066 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2067 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2068 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2069 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2070
Vladimir Marko372f10e2016-05-17 16:30:10 +01002071 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2072 return GetInputRecords()[i];
2073 }
2074
2075 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2076 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2077 input_records[index] = input;
2078 }
David Brazdil1abb4192015-02-17 18:33:36 +00002079
Vladimir Markoa1de9182016-02-25 11:37:38 +00002080 uint32_t GetPackedFields() const {
2081 return packed_fields_;
2082 }
2083
2084 template <size_t flag>
2085 bool GetPackedFlag() const {
2086 return (packed_fields_ & (1u << flag)) != 0u;
2087 }
2088
2089 template <size_t flag>
2090 void SetPackedFlag(bool value = true) {
2091 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2092 }
2093
2094 template <typename BitFieldType>
2095 typename BitFieldType::value_type GetPackedField() const {
2096 return BitFieldType::Decode(packed_fields_);
2097 }
2098
2099 template <typename BitFieldType>
2100 void SetPackedField(typename BitFieldType::value_type value) {
2101 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2102 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2103 }
2104
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002105 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002106 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2107 auto before_use_node = uses_.before_begin();
2108 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2109 HInstruction* user = use_node->GetUser();
2110 size_t input_index = use_node->GetIndex();
2111 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2112 before_use_node = use_node;
2113 }
2114 }
2115
2116 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2117 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2118 if (next != uses_.end()) {
2119 HInstruction* next_user = next->GetUser();
2120 size_t next_index = next->GetIndex();
2121 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2122 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2123 }
2124 }
2125
2126 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2127 auto before_env_use_node = env_uses_.before_begin();
2128 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2129 HEnvironment* user = env_use_node->GetUser();
2130 size_t input_index = env_use_node->GetIndex();
2131 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2132 before_env_use_node = env_use_node;
2133 }
2134 }
2135
2136 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2137 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2138 if (next != env_uses_.end()) {
2139 HEnvironment* next_user = next->GetUser();
2140 size_t next_index = next->GetIndex();
2141 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2142 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2143 }
2144 }
David Brazdil1abb4192015-02-17 18:33:36 +00002145
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146 HInstruction* previous_;
2147 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002148 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002149 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002150
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002151 // An instruction gets an id when it is added to the graph.
2152 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002153 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002154 int id_;
2155
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002156 // When doing liveness analysis, instructions that have uses get an SSA index.
2157 int ssa_index_;
2158
Vladimir Markoa1de9182016-02-25 11:37:38 +00002159 // Packed fields.
2160 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002161
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002162 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002163 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002164
2165 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002166 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002167
Nicolas Geoffray39468442014-09-02 15:17:15 +01002168 // The environment associated with this instruction. Not null if the instruction
2169 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002170 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002171
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002172 // Set by the code generator.
2173 LocationSummary* locations_;
2174
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002175 // Set by the liveness analysis.
2176 LiveInterval* live_interval_;
2177
2178 // Set by the liveness analysis, this is the position in a linear
2179 // order of blocks where this instruction's live interval start.
2180 size_t lifetime_position_;
2181
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002182 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002183
Vladimir Markoa1de9182016-02-25 11:37:38 +00002184 // The reference handle part of the reference type info.
2185 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002186 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002187 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002188
David Brazdil1abb4192015-02-17 18:33:36 +00002189 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002190 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002191 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002192 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002194
2195 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2196};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002197std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002198
2199class HInstructionIterator : public ValueObject {
2200 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002201 explicit HInstructionIterator(const HInstructionList& instructions)
2202 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002203 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002204 }
2205
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002206 bool Done() const { return instruction_ == nullptr; }
2207 HInstruction* Current() const { return instruction_; }
2208 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002210 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002211 }
2212
2213 private:
2214 HInstruction* instruction_;
2215 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002216
2217 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002218};
2219
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002220class HBackwardInstructionIterator : public ValueObject {
2221 public:
2222 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2223 : instruction_(instructions.last_instruction_) {
2224 next_ = Done() ? nullptr : instruction_->GetPrevious();
2225 }
2226
2227 bool Done() const { return instruction_ == nullptr; }
2228 HInstruction* Current() const { return instruction_; }
2229 void Advance() {
2230 instruction_ = next_;
2231 next_ = Done() ? nullptr : instruction_->GetPrevious();
2232 }
2233
2234 private:
2235 HInstruction* instruction_;
2236 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002237
2238 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002239};
2240
Vladimir Markof9f64412015-09-02 14:05:49 +01002241template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242class HTemplateInstruction: public HInstruction {
2243 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002244 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002245 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002246 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002247
Vladimir Marko372f10e2016-05-17 16:30:10 +01002248 using HInstruction::GetInputRecords; // Keep the const version visible.
2249 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2250 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002251 }
2252
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002253 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002254 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002255
2256 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257};
2258
Vladimir Markof9f64412015-09-02 14:05:49 +01002259// HTemplateInstruction specialization for N=0.
2260template<>
2261class HTemplateInstruction<0>: public HInstruction {
2262 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002263 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002264 : HInstruction(side_effects, dex_pc) {}
2265
Vladimir Markof9f64412015-09-02 14:05:49 +01002266 virtual ~HTemplateInstruction() {}
2267
Vladimir Marko372f10e2016-05-17 16:30:10 +01002268 using HInstruction::GetInputRecords; // Keep the const version visible.
2269 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2270 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002271 }
2272
2273 private:
2274 friend class SsaBuilder;
2275};
2276
Dave Allison20dfc792014-06-16 20:44:29 -07002277template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002278class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002279 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002280 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002281 : HTemplateInstruction<N>(side_effects, dex_pc) {
2282 this->template SetPackedField<TypeField>(type);
2283 }
Dave Allison20dfc792014-06-16 20:44:29 -07002284 virtual ~HExpression() {}
2285
Vladimir Markoa1de9182016-02-25 11:37:38 +00002286 Primitive::Type GetType() const OVERRIDE {
2287 return TypeField::Decode(this->GetPackedFields());
2288 }
Dave Allison20dfc792014-06-16 20:44:29 -07002289
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002290 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002291 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2292 static constexpr size_t kFieldTypeSize =
2293 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2294 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2295 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2296 "Too many packed fields.");
2297 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
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.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002302class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303 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.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002317class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002318 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
Vladimir Markofcb503c2016-05-18 12:48:17 +01002332class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002333 public:
2334 HPhi(ArenaAllocator* arena,
2335 uint32_t reg_number,
2336 size_t number_of_inputs,
2337 Primitive::Type type,
2338 uint32_t dex_pc = kNoDexPc)
2339 : HInstruction(SideEffects::None(), dex_pc),
2340 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2341 reg_number_(reg_number) {
2342 SetPackedField<TypeField>(ToPhiType(type));
2343 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2344 // Phis are constructed live and marked dead if conflicting or unused.
2345 // Individual steps of SsaBuilder should assume that if a phi has been
2346 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2347 SetPackedFlag<kFlagIsLive>(true);
2348 SetPackedFlag<kFlagCanBeNull>(true);
2349 }
2350
2351 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2352 static Primitive::Type ToPhiType(Primitive::Type type) {
2353 return Primitive::PrimitiveKind(type);
2354 }
2355
2356 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2357
Vladimir Marko372f10e2016-05-17 16:30:10 +01002358 using HInstruction::GetInputRecords; // Keep the const version visible.
2359 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2360 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2361 }
David Brazdildee58d62016-04-07 09:54:26 +00002362
2363 void AddInput(HInstruction* input);
2364 void RemoveInputAt(size_t index);
2365
2366 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2367 void SetType(Primitive::Type new_type) {
2368 // Make sure that only valid type changes occur. The following are allowed:
2369 // (1) int -> float/ref (primitive type propagation),
2370 // (2) long -> double (primitive type propagation).
2371 DCHECK(GetType() == new_type ||
2372 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2373 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2374 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2375 SetPackedField<TypeField>(new_type);
2376 }
2377
2378 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2379 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2380
2381 uint32_t GetRegNumber() const { return reg_number_; }
2382
2383 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2384 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2385 bool IsDead() const { return !IsLive(); }
2386 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2387
2388 bool IsVRegEquivalentOf(HInstruction* other) const {
2389 return other != nullptr
2390 && other->IsPhi()
2391 && other->AsPhi()->GetBlock() == GetBlock()
2392 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2393 }
2394
2395 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2396 // An equivalent phi is a phi having the same dex register and type.
2397 // It assumes that phis with the same dex register are adjacent.
2398 HPhi* GetNextEquivalentPhiWithSameType() {
2399 HInstruction* next = GetNext();
2400 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2401 if (next->GetType() == GetType()) {
2402 return next->AsPhi();
2403 }
2404 next = next->GetNext();
2405 }
2406 return nullptr;
2407 }
2408
2409 DECLARE_INSTRUCTION(Phi);
2410
David Brazdildee58d62016-04-07 09:54:26 +00002411 private:
2412 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2413 static constexpr size_t kFieldTypeSize =
2414 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2415 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2416 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2417 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2418 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2419 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2420
Vladimir Marko372f10e2016-05-17 16:30:10 +01002421 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002422 const uint32_t reg_number_;
2423
2424 DISALLOW_COPY_AND_ASSIGN(HPhi);
2425};
2426
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002427// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002428// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002429// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002430class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002431 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002432 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002433
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002434 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002435
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002436 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002437
2438 private:
2439 DISALLOW_COPY_AND_ASSIGN(HExit);
2440};
2441
2442// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002443class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002444 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002445 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002446
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002447 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002448
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002449 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002450 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002451 }
2452
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002453 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002454
2455 private:
2456 DISALLOW_COPY_AND_ASSIGN(HGoto);
2457};
2458
Roland Levillain9867bc72015-08-05 10:21:34 +01002459class HConstant : public HExpression<0> {
2460 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002461 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2462 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002463
2464 bool CanBeMoved() const OVERRIDE { return true; }
2465
Roland Levillain1a653882016-03-18 18:05:57 +00002466 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002467 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002468 // Is this constant 0 in the arithmetic sense?
2469 virtual bool IsArithmeticZero() const { return false; }
2470 // Is this constant a 0-bit pattern?
2471 virtual bool IsZeroBitPattern() const { return false; }
2472 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002473 virtual bool IsOne() const { return false; }
2474
David Brazdil77a48ae2015-09-15 12:34:04 +00002475 virtual uint64_t GetValueAsUint64() const = 0;
2476
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002477 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002478
2479 private:
2480 DISALLOW_COPY_AND_ASSIGN(HConstant);
2481};
2482
Vladimir Markofcb503c2016-05-18 12:48:17 +01002483class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002484 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002485 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002486 return true;
2487 }
2488
David Brazdil77a48ae2015-09-15 12:34:04 +00002489 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2490
Roland Levillain9867bc72015-08-05 10:21:34 +01002491 size_t ComputeHashCode() const OVERRIDE { return 0; }
2492
Roland Levillain1a653882016-03-18 18:05:57 +00002493 // The null constant representation is a 0-bit pattern.
2494 virtual bool IsZeroBitPattern() const { return true; }
2495
Roland Levillain9867bc72015-08-05 10:21:34 +01002496 DECLARE_INSTRUCTION(NullConstant);
2497
2498 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002499 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002500
2501 friend class HGraph;
2502 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2503};
2504
2505// Constants of the type int. Those can be from Dex instructions, or
2506// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002507class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002508 public:
2509 int32_t GetValue() const { return value_; }
2510
David Brazdil9f389d42015-10-01 14:32:56 +01002511 uint64_t GetValueAsUint64() const OVERRIDE {
2512 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2513 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002514
Vladimir Marko372f10e2016-05-17 16:30:10 +01002515 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002516 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002517 return other->AsIntConstant()->value_ == value_;
2518 }
2519
2520 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2521
2522 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002523 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2524 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002525 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2526
Roland Levillain1a653882016-03-18 18:05:57 +00002527 // Integer constants are used to encode Boolean values as well,
2528 // where 1 means true and 0 means false.
2529 bool IsTrue() const { return GetValue() == 1; }
2530 bool IsFalse() const { return GetValue() == 0; }
2531
Roland Levillain9867bc72015-08-05 10:21:34 +01002532 DECLARE_INSTRUCTION(IntConstant);
2533
2534 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002535 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2536 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2537 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2538 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002539
2540 const int32_t value_;
2541
2542 friend class HGraph;
2543 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2544 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2545 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2546};
2547
Vladimir Markofcb503c2016-05-18 12:48:17 +01002548class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002549 public:
2550 int64_t GetValue() const { return value_; }
2551
David Brazdil77a48ae2015-09-15 12:34:04 +00002552 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2553
Vladimir Marko372f10e2016-05-17 16:30:10 +01002554 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002555 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002556 return other->AsLongConstant()->value_ == value_;
2557 }
2558
2559 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2560
2561 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002562 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2563 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002564 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2565
2566 DECLARE_INSTRUCTION(LongConstant);
2567
2568 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002569 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2570 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002571
2572 const int64_t value_;
2573
2574 friend class HGraph;
2575 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2576};
Dave Allison20dfc792014-06-16 20:44:29 -07002577
Vladimir Markofcb503c2016-05-18 12:48:17 +01002578class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002579 public:
2580 float GetValue() const { return value_; }
2581
2582 uint64_t GetValueAsUint64() const OVERRIDE {
2583 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2584 }
2585
Vladimir Marko372f10e2016-05-17 16:30:10 +01002586 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002587 DCHECK(other->IsFloatConstant()) << other->DebugName();
2588 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2589 }
2590
2591 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2592
2593 bool IsMinusOne() const OVERRIDE {
2594 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2595 }
Roland Levillain1a653882016-03-18 18:05:57 +00002596 bool IsArithmeticZero() const OVERRIDE {
2597 return std::fpclassify(value_) == FP_ZERO;
2598 }
2599 bool IsArithmeticPositiveZero() const {
2600 return IsArithmeticZero() && !std::signbit(value_);
2601 }
2602 bool IsArithmeticNegativeZero() const {
2603 return IsArithmeticZero() && std::signbit(value_);
2604 }
2605 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002606 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002607 }
2608 bool IsOne() const OVERRIDE {
2609 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2610 }
2611 bool IsNaN() const {
2612 return std::isnan(value_);
2613 }
2614
2615 DECLARE_INSTRUCTION(FloatConstant);
2616
2617 private:
2618 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2619 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2620 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2621 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2622
2623 const float value_;
2624
2625 // Only the SsaBuilder and HGraph can create floating-point constants.
2626 friend class SsaBuilder;
2627 friend class HGraph;
2628 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2629};
2630
Vladimir Markofcb503c2016-05-18 12:48:17 +01002631class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002632 public:
2633 double GetValue() const { return value_; }
2634
2635 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2636
Vladimir Marko372f10e2016-05-17 16:30:10 +01002637 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002638 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2639 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2640 }
2641
2642 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2643
2644 bool IsMinusOne() const OVERRIDE {
2645 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2646 }
Roland Levillain1a653882016-03-18 18:05:57 +00002647 bool IsArithmeticZero() const OVERRIDE {
2648 return std::fpclassify(value_) == FP_ZERO;
2649 }
2650 bool IsArithmeticPositiveZero() const {
2651 return IsArithmeticZero() && !std::signbit(value_);
2652 }
2653 bool IsArithmeticNegativeZero() const {
2654 return IsArithmeticZero() && std::signbit(value_);
2655 }
2656 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002657 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002658 }
2659 bool IsOne() const OVERRIDE {
2660 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2661 }
2662 bool IsNaN() const {
2663 return std::isnan(value_);
2664 }
2665
2666 DECLARE_INSTRUCTION(DoubleConstant);
2667
2668 private:
2669 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2670 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2671 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2672 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2673
2674 const double value_;
2675
2676 // Only the SsaBuilder and HGraph can create floating-point constants.
2677 friend class SsaBuilder;
2678 friend class HGraph;
2679 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2680};
2681
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002682// Conditional branch. A block ending with an HIf instruction must have
2683// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002684class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002685 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002686 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2687 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002688 SetRawInputAt(0, input);
2689 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002690
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002691 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002692
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002693 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002694 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002695 }
2696
2697 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002698 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002699 }
2700
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002701 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002702
2703 private:
2704 DISALLOW_COPY_AND_ASSIGN(HIf);
2705};
2706
David Brazdilfc6a86a2015-06-26 10:33:45 +00002707
2708// Abstract instruction which marks the beginning and/or end of a try block and
2709// links it to the respective exception handlers. Behaves the same as a Goto in
2710// non-exceptional control flow.
2711// Normal-flow successor is stored at index zero, exception handlers under
2712// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002713class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002714 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002715 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002716 kEntry,
2717 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002718 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002719 };
2720
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002722 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2723 SetPackedField<BoundaryKindField>(kind);
2724 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002725
2726 bool IsControlFlow() const OVERRIDE { return true; }
2727
2728 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002729 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002730
David Brazdild26a4112015-11-10 11:07:31 +00002731 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2732 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2733 }
2734
David Brazdilfc6a86a2015-06-26 10:33:45 +00002735 // Returns whether `handler` is among its exception handlers (non-zero index
2736 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002737 bool HasExceptionHandler(const HBasicBlock& handler) const {
2738 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002739 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002740 }
2741
2742 // If not present already, adds `handler` to its block's list of exception
2743 // handlers.
2744 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002745 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002746 GetBlock()->AddSuccessor(handler);
2747 }
2748 }
2749
Vladimir Markoa1de9182016-02-25 11:37:38 +00002750 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2751 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002752
David Brazdilffee3d32015-07-06 11:48:53 +01002753 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2754
David Brazdilfc6a86a2015-06-26 10:33:45 +00002755 DECLARE_INSTRUCTION(TryBoundary);
2756
2757 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002758 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2759 static constexpr size_t kFieldBoundaryKindSize =
2760 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2761 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2762 kFieldBoundaryKind + kFieldBoundaryKindSize;
2763 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2764 "Too many packed fields.");
2765 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002766
2767 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2768};
2769
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002770// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002771class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002772 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002773 // We set CanTriggerGC to prevent any intermediate address to be live
2774 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002775 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002776 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002777 SetRawInputAt(0, cond);
2778 }
2779
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002780 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002781 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002782 return true;
2783 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002784 bool NeedsEnvironment() const OVERRIDE { return true; }
2785 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002786
2787 DECLARE_INSTRUCTION(Deoptimize);
2788
2789 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002790 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2791};
2792
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002793// Represents the ArtMethod that was passed as a first argument to
2794// the method. It is used by instructions that depend on it, like
2795// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002796class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002797 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002798 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2799 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002800
2801 DECLARE_INSTRUCTION(CurrentMethod);
2802
2803 private:
2804 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2805};
2806
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002807// Fetches an ArtMethod from the virtual table or the interface method table
2808// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002809class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002810 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002811 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002812 kVTable,
2813 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002814 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002815 };
2816 HClassTableGet(HInstruction* cls,
2817 Primitive::Type type,
2818 TableKind kind,
2819 size_t index,
2820 uint32_t dex_pc)
2821 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002822 index_(index) {
2823 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002824 SetRawInputAt(0, cls);
2825 }
2826
2827 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002828 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002829 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002830 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002831 }
2832
Vladimir Markoa1de9182016-02-25 11:37:38 +00002833 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002834 size_t GetIndex() const { return index_; }
2835
2836 DECLARE_INSTRUCTION(ClassTableGet);
2837
2838 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002839 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2840 static constexpr size_t kFieldTableKindSize =
2841 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2842 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2843 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2844 "Too many packed fields.");
2845 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2846
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002847 // The index of the ArtMethod in the table.
2848 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002849
2850 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2851};
2852
Mark Mendellfe57faa2015-09-18 09:26:15 -04002853// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2854// have one successor for each entry in the switch table, and the final successor
2855// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002856class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002857 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002858 HPackedSwitch(int32_t start_value,
2859 uint32_t num_entries,
2860 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002861 uint32_t dex_pc = kNoDexPc)
2862 : HTemplateInstruction(SideEffects::None(), dex_pc),
2863 start_value_(start_value),
2864 num_entries_(num_entries) {
2865 SetRawInputAt(0, input);
2866 }
2867
2868 bool IsControlFlow() const OVERRIDE { return true; }
2869
2870 int32_t GetStartValue() const { return start_value_; }
2871
Vladimir Marko211c2112015-09-24 16:52:33 +01002872 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002873
2874 HBasicBlock* GetDefaultBlock() const {
2875 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002876 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002877 }
2878 DECLARE_INSTRUCTION(PackedSwitch);
2879
2880 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002881 const int32_t start_value_;
2882 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002883
2884 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2885};
2886
Roland Levillain88cb1752014-10-20 16:36:47 +01002887class HUnaryOperation : public HExpression<1> {
2888 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002889 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2890 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002891 SetRawInputAt(0, input);
2892 }
2893
2894 HInstruction* GetInput() const { return InputAt(0); }
2895 Primitive::Type GetResultType() const { return GetType(); }
2896
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002897 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002898 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002899 return true;
2900 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002901
Roland Levillain31dd3d62016-02-16 12:21:02 +00002902 // Try to statically evaluate `this` and return a HConstant
2903 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002904 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002905 HConstant* TryStaticEvaluation() const;
2906
2907 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002908 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2909 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002910 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2911 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002912
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002913 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002914
2915 private:
2916 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2917};
2918
Dave Allison20dfc792014-06-16 20:44:29 -07002919class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002920 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002921 HBinaryOperation(Primitive::Type result_type,
2922 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002923 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002924 SideEffects side_effects = SideEffects::None(),
2925 uint32_t dex_pc = kNoDexPc)
2926 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002927 SetRawInputAt(0, left);
2928 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002929 }
2930
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002931 HInstruction* GetLeft() const { return InputAt(0); }
2932 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002933 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002934
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002935 virtual bool IsCommutative() const { return false; }
2936
2937 // Put constant on the right.
2938 // Returns whether order is changed.
2939 bool OrderInputsWithConstantOnTheRight() {
2940 HInstruction* left = InputAt(0);
2941 HInstruction* right = InputAt(1);
2942 if (left->IsConstant() && !right->IsConstant()) {
2943 ReplaceInput(right, 0);
2944 ReplaceInput(left, 1);
2945 return true;
2946 }
2947 return false;
2948 }
2949
2950 // Order inputs by instruction id, but favor constant on the right side.
2951 // This helps GVN for commutative ops.
2952 void OrderInputs() {
2953 DCHECK(IsCommutative());
2954 HInstruction* left = InputAt(0);
2955 HInstruction* right = InputAt(1);
2956 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2957 return;
2958 }
2959 if (OrderInputsWithConstantOnTheRight()) {
2960 return;
2961 }
2962 // Order according to instruction id.
2963 if (left->GetId() > right->GetId()) {
2964 ReplaceInput(right, 0);
2965 ReplaceInput(left, 1);
2966 }
2967 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002968
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002969 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002970 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002971 return true;
2972 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002973
Roland Levillain31dd3d62016-02-16 12:21:02 +00002974 // Try to statically evaluate `this` and return a HConstant
2975 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002976 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002977 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002978
2979 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002980 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2981 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002982 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2983 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002984 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002985 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2986 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002987 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2988 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002989 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2990 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002991 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002992 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2993 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002994
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002995 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002996 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002997 HConstant* GetConstantRight() const;
2998
2999 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003000 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003001 HInstruction* GetLeastConstantLeft() const;
3002
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003003 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003004
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003005 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003006 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3007};
3008
Mark Mendellc4701932015-04-10 13:18:51 -04003009// The comparison bias applies for floating point operations and indicates how NaN
3010// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003011enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003012 kNoBias, // bias is not applicable (i.e. for long operation)
3013 kGtBias, // return 1 for NaN comparisons
3014 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003015 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003016};
3017
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3019
Dave Allison20dfc792014-06-16 20:44:29 -07003020class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003021 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003022 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003023 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3024 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3025 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003026
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003027 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003028 // `instruction`, and disregard moves in between.
3029 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003030
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003031 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003032
3033 virtual IfCondition GetCondition() const = 0;
3034
Mark Mendellc4701932015-04-10 13:18:51 -04003035 virtual IfCondition GetOppositeCondition() const = 0;
3036
Vladimir Markoa1de9182016-02-25 11:37:38 +00003037 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003038 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3039
Vladimir Markoa1de9182016-02-25 11:37:38 +00003040 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3041 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003042
Vladimir Marko372f10e2016-05-17 16:30:10 +01003043 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003044 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003045 }
3046
Roland Levillain4fa13f62015-07-06 18:11:54 +01003047 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003048 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003049 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003050 if (if_cond == kCondNE) {
3051 return true;
3052 } else if (if_cond == kCondEQ) {
3053 return false;
3054 }
3055 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003056 }
3057
3058 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003059 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003060 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003061 if (if_cond == kCondEQ) {
3062 return true;
3063 } else if (if_cond == kCondNE) {
3064 return false;
3065 }
3066 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003067 }
3068
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003070 // Needed if we merge a HCompare into a HCondition.
3071 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3072 static constexpr size_t kFieldComparisonBiasSize =
3073 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3074 static constexpr size_t kNumberOfConditionPackedBits =
3075 kFieldComparisonBias + kFieldComparisonBiasSize;
3076 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3077 using ComparisonBiasField =
3078 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3079
Roland Levillain31dd3d62016-02-16 12:21:02 +00003080 template <typename T>
3081 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3082
3083 template <typename T>
3084 int32_t CompareFP(T x, T y) const {
3085 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3086 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3087 // Handle the bias.
3088 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3089 }
3090
3091 // Return an integer constant containing the result of a condition evaluated at compile time.
3092 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3093 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3094 }
3095
Dave Allison20dfc792014-06-16 20:44:29 -07003096 private:
3097 DISALLOW_COPY_AND_ASSIGN(HCondition);
3098};
3099
3100// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003101class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003102 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003103 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3104 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003105
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003106 bool IsCommutative() const OVERRIDE { return true; }
3107
Vladimir Marko9e23df52015-11-10 17:14:35 +00003108 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3109 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003110 return MakeConstantCondition(true, GetDexPc());
3111 }
3112 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3113 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3114 }
3115 // In the following Evaluate methods, a HCompare instruction has
3116 // been merged into this HEqual instruction; evaluate it as
3117 // `Compare(x, y) == 0`.
3118 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3119 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3120 GetDexPc());
3121 }
3122 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3123 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3124 }
3125 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3126 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003127 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003128
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003129 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003130
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003131 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003132 return kCondEQ;
3133 }
3134
Mark Mendellc4701932015-04-10 13:18:51 -04003135 IfCondition GetOppositeCondition() const OVERRIDE {
3136 return kCondNE;
3137 }
3138
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003139 private:
Aart Bike9f37602015-10-09 11:15:55 -07003140 template <typename T> bool Compute(T x, T y) const { return x == y; }
3141
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003142 DISALLOW_COPY_AND_ASSIGN(HEqual);
3143};
3144
Vladimir Markofcb503c2016-05-18 12:48:17 +01003145class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003146 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003147 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3148 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003149
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003150 bool IsCommutative() const OVERRIDE { return true; }
3151
Vladimir Marko9e23df52015-11-10 17:14:35 +00003152 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3153 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003154 return MakeConstantCondition(false, GetDexPc());
3155 }
3156 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3157 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3158 }
3159 // In the following Evaluate methods, a HCompare instruction has
3160 // been merged into this HNotEqual instruction; evaluate it as
3161 // `Compare(x, y) != 0`.
3162 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3163 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3164 }
3165 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3166 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3167 }
3168 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3169 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003170 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003171
Dave Allison20dfc792014-06-16 20:44:29 -07003172 DECLARE_INSTRUCTION(NotEqual);
3173
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003174 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003175 return kCondNE;
3176 }
3177
Mark Mendellc4701932015-04-10 13:18:51 -04003178 IfCondition GetOppositeCondition() const OVERRIDE {
3179 return kCondEQ;
3180 }
3181
Dave Allison20dfc792014-06-16 20:44:29 -07003182 private:
Aart Bike9f37602015-10-09 11:15:55 -07003183 template <typename T> bool Compute(T x, T y) const { return x != y; }
3184
Dave Allison20dfc792014-06-16 20:44:29 -07003185 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3186};
3187
Vladimir Markofcb503c2016-05-18 12:48:17 +01003188class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003189 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003190 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3191 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003192
Roland Levillain9867bc72015-08-05 10:21:34 +01003193 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003194 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003195 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003196 // In the following Evaluate methods, a HCompare instruction has
3197 // been merged into this HLessThan instruction; evaluate it as
3198 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003199 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003200 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3201 }
3202 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3203 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3204 }
3205 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3206 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003207 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003208
Dave Allison20dfc792014-06-16 20:44:29 -07003209 DECLARE_INSTRUCTION(LessThan);
3210
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003211 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003212 return kCondLT;
3213 }
3214
Mark Mendellc4701932015-04-10 13:18:51 -04003215 IfCondition GetOppositeCondition() const OVERRIDE {
3216 return kCondGE;
3217 }
3218
Dave Allison20dfc792014-06-16 20:44:29 -07003219 private:
Aart Bike9f37602015-10-09 11:15:55 -07003220 template <typename T> bool Compute(T x, T y) const { return x < y; }
3221
Dave Allison20dfc792014-06-16 20:44:29 -07003222 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3223};
3224
Vladimir Markofcb503c2016-05-18 12:48:17 +01003225class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003226 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003227 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3228 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003229
Roland Levillain9867bc72015-08-05 10:21:34 +01003230 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003231 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003232 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003233 // In the following Evaluate methods, a HCompare instruction has
3234 // been merged into this HLessThanOrEqual instruction; evaluate it as
3235 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003236 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003237 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3238 }
3239 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3240 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3241 }
3242 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3243 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003244 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003245
Dave Allison20dfc792014-06-16 20:44:29 -07003246 DECLARE_INSTRUCTION(LessThanOrEqual);
3247
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003248 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003249 return kCondLE;
3250 }
3251
Mark Mendellc4701932015-04-10 13:18:51 -04003252 IfCondition GetOppositeCondition() const OVERRIDE {
3253 return kCondGT;
3254 }
3255
Dave Allison20dfc792014-06-16 20:44:29 -07003256 private:
Aart Bike9f37602015-10-09 11:15:55 -07003257 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3258
Dave Allison20dfc792014-06-16 20:44:29 -07003259 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3260};
3261
Vladimir Markofcb503c2016-05-18 12:48:17 +01003262class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003263 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003264 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3265 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003266
Roland Levillain9867bc72015-08-05 10:21:34 +01003267 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003268 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003269 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003270 // In the following Evaluate methods, a HCompare instruction has
3271 // been merged into this HGreaterThan instruction; evaluate it as
3272 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003273 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003274 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3275 }
3276 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3277 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3278 }
3279 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3280 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003281 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003282
Dave Allison20dfc792014-06-16 20:44:29 -07003283 DECLARE_INSTRUCTION(GreaterThan);
3284
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003285 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003286 return kCondGT;
3287 }
3288
Mark Mendellc4701932015-04-10 13:18:51 -04003289 IfCondition GetOppositeCondition() const OVERRIDE {
3290 return kCondLE;
3291 }
3292
Dave Allison20dfc792014-06-16 20:44:29 -07003293 private:
Aart Bike9f37602015-10-09 11:15:55 -07003294 template <typename T> bool Compute(T x, T y) const { return x > y; }
3295
Dave Allison20dfc792014-06-16 20:44:29 -07003296 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3297};
3298
Vladimir Markofcb503c2016-05-18 12:48:17 +01003299class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003300 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003301 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3302 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003303
Roland Levillain9867bc72015-08-05 10:21:34 +01003304 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003305 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003306 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003307 // In the following Evaluate methods, a HCompare instruction has
3308 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3309 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003310 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003311 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3312 }
3313 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3314 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3315 }
3316 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3317 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003318 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003319
Dave Allison20dfc792014-06-16 20:44:29 -07003320 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3321
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003322 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003323 return kCondGE;
3324 }
3325
Mark Mendellc4701932015-04-10 13:18:51 -04003326 IfCondition GetOppositeCondition() const OVERRIDE {
3327 return kCondLT;
3328 }
3329
Dave Allison20dfc792014-06-16 20:44:29 -07003330 private:
Aart Bike9f37602015-10-09 11:15:55 -07003331 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3332
Dave Allison20dfc792014-06-16 20:44:29 -07003333 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3334};
3335
Vladimir Markofcb503c2016-05-18 12:48:17 +01003336class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003337 public:
3338 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3339 : HCondition(first, second, dex_pc) {}
3340
3341 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003342 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003343 }
3344 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003345 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3346 }
3347 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3348 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3349 LOG(FATAL) << DebugName() << " is not defined for float values";
3350 UNREACHABLE();
3351 }
3352 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3353 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3354 LOG(FATAL) << DebugName() << " is not defined for double values";
3355 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003356 }
3357
3358 DECLARE_INSTRUCTION(Below);
3359
3360 IfCondition GetCondition() const OVERRIDE {
3361 return kCondB;
3362 }
3363
3364 IfCondition GetOppositeCondition() const OVERRIDE {
3365 return kCondAE;
3366 }
3367
3368 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003369 template <typename T> bool Compute(T x, T y) const {
3370 return MakeUnsigned(x) < MakeUnsigned(y);
3371 }
Aart Bike9f37602015-10-09 11:15:55 -07003372
3373 DISALLOW_COPY_AND_ASSIGN(HBelow);
3374};
3375
Vladimir Markofcb503c2016-05-18 12:48:17 +01003376class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003377 public:
3378 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3379 : HCondition(first, second, dex_pc) {}
3380
3381 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003382 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003383 }
3384 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003385 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3386 }
3387 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3388 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3389 LOG(FATAL) << DebugName() << " is not defined for float values";
3390 UNREACHABLE();
3391 }
3392 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3393 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3394 LOG(FATAL) << DebugName() << " is not defined for double values";
3395 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003396 }
3397
3398 DECLARE_INSTRUCTION(BelowOrEqual);
3399
3400 IfCondition GetCondition() const OVERRIDE {
3401 return kCondBE;
3402 }
3403
3404 IfCondition GetOppositeCondition() const OVERRIDE {
3405 return kCondA;
3406 }
3407
3408 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003409 template <typename T> bool Compute(T x, T y) const {
3410 return MakeUnsigned(x) <= MakeUnsigned(y);
3411 }
Aart Bike9f37602015-10-09 11:15:55 -07003412
3413 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3414};
3415
Vladimir Markofcb503c2016-05-18 12:48:17 +01003416class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003417 public:
3418 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3419 : HCondition(first, second, dex_pc) {}
3420
3421 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003422 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003423 }
3424 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003425 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3426 }
3427 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3428 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3429 LOG(FATAL) << DebugName() << " is not defined for float values";
3430 UNREACHABLE();
3431 }
3432 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3433 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3434 LOG(FATAL) << DebugName() << " is not defined for double values";
3435 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003436 }
3437
3438 DECLARE_INSTRUCTION(Above);
3439
3440 IfCondition GetCondition() const OVERRIDE {
3441 return kCondA;
3442 }
3443
3444 IfCondition GetOppositeCondition() const OVERRIDE {
3445 return kCondBE;
3446 }
3447
3448 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003449 template <typename T> bool Compute(T x, T y) const {
3450 return MakeUnsigned(x) > MakeUnsigned(y);
3451 }
Aart Bike9f37602015-10-09 11:15:55 -07003452
3453 DISALLOW_COPY_AND_ASSIGN(HAbove);
3454};
3455
Vladimir Markofcb503c2016-05-18 12:48:17 +01003456class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003457 public:
3458 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3459 : HCondition(first, second, dex_pc) {}
3460
3461 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003462 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003463 }
3464 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003465 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3466 }
3467 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3468 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3469 LOG(FATAL) << DebugName() << " is not defined for float values";
3470 UNREACHABLE();
3471 }
3472 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3473 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3474 LOG(FATAL) << DebugName() << " is not defined for double values";
3475 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003476 }
3477
3478 DECLARE_INSTRUCTION(AboveOrEqual);
3479
3480 IfCondition GetCondition() const OVERRIDE {
3481 return kCondAE;
3482 }
3483
3484 IfCondition GetOppositeCondition() const OVERRIDE {
3485 return kCondB;
3486 }
3487
3488 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003489 template <typename T> bool Compute(T x, T y) const {
3490 return MakeUnsigned(x) >= MakeUnsigned(y);
3491 }
Aart Bike9f37602015-10-09 11:15:55 -07003492
3493 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3494};
Dave Allison20dfc792014-06-16 20:44:29 -07003495
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003496// Instruction to check how two inputs compare to each other.
3497// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003498class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003499 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003500 // Note that `comparison_type` is the type of comparison performed
3501 // between the comparison's inputs, not the type of the instantiated
3502 // HCompare instruction (which is always Primitive::kPrimInt).
3503 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003504 HInstruction* first,
3505 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003506 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003507 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003508 : HBinaryOperation(Primitive::kPrimInt,
3509 first,
3510 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003511 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003512 dex_pc) {
3513 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003514 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3515 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003516 }
3517
Roland Levillain9867bc72015-08-05 10:21:34 +01003518 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003519 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3520
3521 template <typename T>
3522 int32_t ComputeFP(T x, T y) const {
3523 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3524 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3525 // Handle the bias.
3526 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3527 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003528
Roland Levillain9867bc72015-08-05 10:21:34 +01003529 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003530 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3531 // reach this code path when processing a freshly built HIR
3532 // graph. However HCompare integer instructions can be synthesized
3533 // by the instruction simplifier to implement IntegerCompare and
3534 // IntegerSignum intrinsics, so we have to handle this case.
3535 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003536 }
3537 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003538 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3539 }
3540 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3541 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3542 }
3543 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3544 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003545 }
3546
Vladimir Marko372f10e2016-05-17 16:30:10 +01003547 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003548 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003549 }
3550
Vladimir Markoa1de9182016-02-25 11:37:38 +00003551 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003552
Roland Levillain31dd3d62016-02-16 12:21:02 +00003553 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003554 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003555 bool IsGtBias() const {
3556 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003557 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003558 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003559
Roland Levillain1693a1f2016-03-15 14:57:31 +00003560 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3561 // Comparisons do not require a runtime call in any back end.
3562 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003563 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003564
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003565 DECLARE_INSTRUCTION(Compare);
3566
Roland Levillain31dd3d62016-02-16 12:21:02 +00003567 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003568 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3569 static constexpr size_t kFieldComparisonBiasSize =
3570 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3571 static constexpr size_t kNumberOfComparePackedBits =
3572 kFieldComparisonBias + kFieldComparisonBiasSize;
3573 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3574 using ComparisonBiasField =
3575 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3576
Roland Levillain31dd3d62016-02-16 12:21:02 +00003577 // Return an integer constant containing the result of a comparison evaluated at compile time.
3578 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3579 DCHECK(value == -1 || value == 0 || value == 1) << value;
3580 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3581 }
3582
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003583 private:
3584 DISALLOW_COPY_AND_ASSIGN(HCompare);
3585};
3586
Vladimir Markofcb503c2016-05-18 12:48:17 +01003587class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003588 public:
3589 HNewInstance(HInstruction* cls,
3590 HCurrentMethod* current_method,
3591 uint32_t dex_pc,
3592 uint16_t type_index,
3593 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003594 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003595 bool finalizable,
3596 QuickEntrypointEnum entrypoint)
3597 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3598 type_index_(type_index),
3599 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003600 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003601 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003602 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003603 SetRawInputAt(0, cls);
3604 SetRawInputAt(1, current_method);
3605 }
3606
3607 uint16_t GetTypeIndex() const { return type_index_; }
3608 const DexFile& GetDexFile() const { return dex_file_; }
3609
3610 // Calls runtime so needs an environment.
3611 bool NeedsEnvironment() const OVERRIDE { return true; }
3612
Mingyao Yang062157f2016-03-02 10:15:36 -08003613 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3614 bool CanThrow() const OVERRIDE { return true; }
3615
3616 // Needs to call into runtime to make sure it's instantiable/accessible.
3617 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003618
Vladimir Markoa1de9182016-02-25 11:37:38 +00003619 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003620
3621 bool CanBeNull() const OVERRIDE { return false; }
3622
3623 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3624
3625 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3626 entrypoint_ = entrypoint;
3627 }
3628
3629 bool IsStringAlloc() const;
3630
3631 DECLARE_INSTRUCTION(NewInstance);
3632
3633 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003634 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3635 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003636 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3637 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3638 "Too many packed fields.");
3639
David Brazdil6de19382016-01-08 17:37:10 +00003640 const uint16_t type_index_;
3641 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003642 QuickEntrypointEnum entrypoint_;
3643
3644 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3645};
3646
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003647enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003648#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3649 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003650#include "intrinsics_list.h"
3651 kNone,
3652 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3653#undef INTRINSICS_LIST
3654#undef OPTIMIZING_INTRINSICS
3655};
3656std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3657
Agi Csaki05f20562015-08-19 14:58:14 -07003658enum IntrinsicNeedsEnvironmentOrCache {
3659 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3660 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003661};
3662
Aart Bik5d75afe2015-12-14 11:57:01 -08003663enum IntrinsicSideEffects {
3664 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3665 kReadSideEffects, // Intrinsic may read heap memory.
3666 kWriteSideEffects, // Intrinsic may write heap memory.
3667 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3668};
3669
3670enum IntrinsicExceptions {
3671 kNoThrow, // Intrinsic does not throw any exceptions.
3672 kCanThrow // Intrinsic may throw exceptions.
3673};
3674
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003675class HInvoke : public HInstruction {
3676 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003677 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003678
Vladimir Marko372f10e2016-05-17 16:30:10 +01003679 using HInstruction::GetInputRecords; // Keep the const version visible.
3680 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3681 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3682 }
3683
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003684 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003685 SetRawInputAt(index, argument);
3686 }
3687
Roland Levillain3e3d7332015-04-28 11:00:54 +01003688 // Return the number of arguments. This number can be lower than
3689 // the number of inputs returned by InputCount(), as some invoke
3690 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3691 // inputs at the end of their list of inputs.
3692 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3693
Vladimir Markoa1de9182016-02-25 11:37:38 +00003694 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003695
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003696 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003697 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003698
Vladimir Markoa1de9182016-02-25 11:37:38 +00003699 InvokeType GetOriginalInvokeType() const {
3700 return GetPackedField<OriginalInvokeTypeField>();
3701 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003702
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003703 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003704 return intrinsic_;
3705 }
3706
Aart Bik5d75afe2015-12-14 11:57:01 -08003707 void SetIntrinsic(Intrinsics intrinsic,
3708 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3709 IntrinsicSideEffects side_effects,
3710 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003711
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003712 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003713 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003714 }
3715
Vladimir Markoa1de9182016-02-25 11:37:38 +00003716 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003717
3718 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3719
Vladimir Marko372f10e2016-05-17 16:30:10 +01003720 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003721 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3722 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003723
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003724 uint32_t* GetIntrinsicOptimizations() {
3725 return &intrinsic_optimizations_;
3726 }
3727
3728 const uint32_t* GetIntrinsicOptimizations() const {
3729 return &intrinsic_optimizations_;
3730 }
3731
3732 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3733
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003734 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003735
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003736 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003737 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3738 static constexpr size_t kFieldOriginalInvokeTypeSize =
3739 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3740 static constexpr size_t kFieldReturnType =
3741 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3742 static constexpr size_t kFieldReturnTypeSize =
3743 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3744 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3745 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3746 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3747 using OriginalInvokeTypeField =
3748 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3749 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3750
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003751 HInvoke(ArenaAllocator* arena,
3752 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003753 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003754 Primitive::Type return_type,
3755 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003756 uint32_t dex_method_index,
3757 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003758 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003759 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003760 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003761 inputs_(number_of_arguments + number_of_other_inputs,
3762 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003763 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003764 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003765 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003766 SetPackedField<ReturnTypeField>(return_type);
3767 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3768 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003769 }
3770
Vladimir Markoa1de9182016-02-25 11:37:38 +00003771 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003772
Roland Levillain3e3d7332015-04-28 11:00:54 +01003773 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003774 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003775 const uint32_t dex_method_index_;
3776 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003777
3778 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3779 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003780
3781 private:
3782 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3783};
3784
Vladimir Markofcb503c2016-05-18 12:48:17 +01003785class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003786 public:
3787 HInvokeUnresolved(ArenaAllocator* arena,
3788 uint32_t number_of_arguments,
3789 Primitive::Type return_type,
3790 uint32_t dex_pc,
3791 uint32_t dex_method_index,
3792 InvokeType invoke_type)
3793 : HInvoke(arena,
3794 number_of_arguments,
3795 0u /* number_of_other_inputs */,
3796 return_type,
3797 dex_pc,
3798 dex_method_index,
3799 invoke_type) {
3800 }
3801
3802 DECLARE_INSTRUCTION(InvokeUnresolved);
3803
3804 private:
3805 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3806};
3807
Vladimir Markofcb503c2016-05-18 12:48:17 +01003808class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003809 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003810 // Requirements of this method call regarding the class
3811 // initialization (clinit) check of its declaring class.
3812 enum class ClinitCheckRequirement {
3813 kNone, // Class already initialized.
3814 kExplicit, // Static call having explicit clinit check as last input.
3815 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003816 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003817 };
3818
Vladimir Marko58155012015-08-19 12:49:41 +00003819 // Determines how to load the target ArtMethod*.
3820 enum class MethodLoadKind {
3821 // Use a String init ArtMethod* loaded from Thread entrypoints.
3822 kStringInit,
3823
3824 // Use the method's own ArtMethod* loaded by the register allocator.
3825 kRecursive,
3826
3827 // Use ArtMethod* at a known address, embed the direct address in the code.
3828 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3829 kDirectAddress,
3830
3831 // Use ArtMethod* at an address that will be known at link time, embed the direct
3832 // address in the code. If the image is relocatable, emit .patch_oat entry.
3833 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3834 // the image relocatable or not.
3835 kDirectAddressWithFixup,
3836
Vladimir Markoa1de9182016-02-25 11:37:38 +00003837 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003838 // Used when we need to use the dex cache, for example for invoke-static that
3839 // may cause class initialization (the entry may point to a resolution method),
3840 // and we know that we can access the dex cache arrays using a PC-relative load.
3841 kDexCachePcRelative,
3842
3843 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3844 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3845 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3846 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3847 kDexCacheViaMethod,
3848 };
3849
3850 // Determines the location of the code pointer.
3851 enum class CodePtrLocation {
3852 // Recursive call, use local PC-relative call instruction.
3853 kCallSelf,
3854
3855 // Use PC-relative call instruction patched at link time.
3856 // Used for calls within an oat file, boot->boot or app->app.
3857 kCallPCRelative,
3858
3859 // Call to a known target address, embed the direct address in code.
3860 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3861 kCallDirect,
3862
3863 // Call to a target address that will be known at link time, embed the direct
3864 // address in code. If the image is relocatable, emit .patch_oat entry.
3865 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3866 // the image relocatable or not.
3867 kCallDirectWithFixup,
3868
3869 // Use code pointer from the ArtMethod*.
3870 // Used when we don't know the target code. This is also the last-resort-kind used when
3871 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3872 kCallArtMethod,
3873 };
3874
3875 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003876 MethodLoadKind method_load_kind;
3877 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003878 // The method load data holds
3879 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3880 // Note that there are multiple string init methods, each having its own offset.
3881 // - the method address for kDirectAddress
3882 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003883 uint64_t method_load_data;
3884 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003885 };
3886
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003887 HInvokeStaticOrDirect(ArenaAllocator* arena,
3888 uint32_t number_of_arguments,
3889 Primitive::Type return_type,
3890 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003891 uint32_t method_index,
3892 MethodReference target_method,
3893 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003894 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003895 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003896 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003897 : HInvoke(arena,
3898 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003899 // There is potentially one extra argument for the HCurrentMethod node, and
3900 // potentially one other if the clinit check is explicit, and potentially
3901 // one other if the method is a string factory.
3902 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003903 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003904 return_type,
3905 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003906 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003907 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003908 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003909 dispatch_info_(dispatch_info) {
3910 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3911 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3912 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003913
Vladimir Markodc151b22015-10-15 18:02:30 +01003914 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003915 bool had_current_method_input = HasCurrentMethodInput();
3916 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3917
3918 // Using the current method is the default and once we find a better
3919 // method load kind, we should not go back to using the current method.
3920 DCHECK(had_current_method_input || !needs_current_method_input);
3921
3922 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003923 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3924 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003925 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003926 dispatch_info_ = dispatch_info;
3927 }
3928
Vladimir Markoc53c0792015-11-19 15:48:33 +00003929 void AddSpecialInput(HInstruction* input) {
3930 // We allow only one special input.
3931 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3932 DCHECK(InputCount() == GetSpecialInputIndex() ||
3933 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3934 InsertInputAt(GetSpecialInputIndex(), input);
3935 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003936
Vladimir Marko372f10e2016-05-17 16:30:10 +01003937 using HInstruction::GetInputRecords; // Keep the const version visible.
3938 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3939 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3940 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3941 DCHECK(!input_records.empty());
3942 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3943 HInstruction* last_input = input_records.back().GetInstruction();
3944 // Note: `last_input` may be null during arguments setup.
3945 if (last_input != nullptr) {
3946 // `last_input` is the last input of a static invoke marked as having
3947 // an explicit clinit check. It must either be:
3948 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3949 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3950 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3951 }
3952 }
3953 return input_records;
3954 }
3955
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003956 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003957 // We access the method via the dex cache so we can't do an implicit null check.
3958 // TODO: for intrinsics we can generate implicit null checks.
3959 return false;
3960 }
3961
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003962 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003963 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003964 }
3965
Vladimir Markoc53c0792015-11-19 15:48:33 +00003966 // Get the index of the special input, if any.
3967 //
David Brazdil6de19382016-01-08 17:37:10 +00003968 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3969 // method pointer; otherwise there may be one platform-specific special input,
3970 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003971 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003972 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003973
Vladimir Markoa1de9182016-02-25 11:37:38 +00003974 InvokeType GetOptimizedInvokeType() const {
3975 return GetPackedField<OptimizedInvokeTypeField>();
3976 }
3977
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003978 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003979 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003980 }
3981
Vladimir Marko58155012015-08-19 12:49:41 +00003982 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3983 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3984 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003985 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003986 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003987 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003988 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003989 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3990 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003991 bool HasCurrentMethodInput() const {
3992 // This function can be called only after the invoke has been fully initialized by the builder.
3993 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003994 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003995 return true;
3996 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003997 DCHECK(InputCount() == GetSpecialInputIndex() ||
3998 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003999 return false;
4000 }
4001 }
Vladimir Marko58155012015-08-19 12:49:41 +00004002 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4003 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004004 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004005
4006 int32_t GetStringInitOffset() const {
4007 DCHECK(IsStringInit());
4008 return dispatch_info_.method_load_data;
4009 }
4010
4011 uint64_t GetMethodAddress() const {
4012 DCHECK(HasMethodAddress());
4013 return dispatch_info_.method_load_data;
4014 }
4015
4016 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004017 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004018 return dispatch_info_.method_load_data;
4019 }
4020
4021 uint64_t GetDirectCodePtr() const {
4022 DCHECK(HasDirectCodePtr());
4023 return dispatch_info_.direct_code_ptr;
4024 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004025
Vladimir Markoa1de9182016-02-25 11:37:38 +00004026 ClinitCheckRequirement GetClinitCheckRequirement() const {
4027 return GetPackedField<ClinitCheckRequirementField>();
4028 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004029
Roland Levillain4c0eb422015-04-24 16:43:49 +01004030 // Is this instruction a call to a static method?
4031 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004032 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004033 }
4034
Vladimir Markofbb184a2015-11-13 14:47:00 +00004035 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4036 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4037 // instruction; only relevant for static calls with explicit clinit check.
4038 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004039 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004040 size_t last_input_index = inputs_.size() - 1u;
4041 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004042 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004043 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004044 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004045 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004046 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004047 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004048 }
4049
4050 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004051 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004052 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004053 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004054 }
4055
4056 // Is this a call to a static method whose declaring class has an
4057 // implicit intialization check requirement?
4058 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004059 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004060 }
4061
Vladimir Markob554b5a2015-11-06 12:57:55 +00004062 // Does this method load kind need the current method as an input?
4063 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4064 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4065 }
4066
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004067 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004068
Roland Levillain4c0eb422015-04-24 16:43:49 +01004069 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004070 void InsertInputAt(size_t index, HInstruction* input);
4071 void RemoveInputAt(size_t index);
4072
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004073 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004074 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4075 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4076 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4077 static constexpr size_t kFieldClinitCheckRequirement =
4078 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4079 static constexpr size_t kFieldClinitCheckRequirementSize =
4080 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4081 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4082 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4083 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4084 "Too many packed fields.");
4085 using OptimizedInvokeTypeField =
4086 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4087 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4088 kFieldClinitCheckRequirement,
4089 kFieldClinitCheckRequirementSize>;
4090
Vladimir Marko58155012015-08-19 12:49:41 +00004091 // The target method may refer to different dex file or method index than the original
4092 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4093 // in derived class to increase visibility.
4094 MethodReference target_method_;
4095 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004096
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004097 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004098};
Vladimir Markof64242a2015-12-01 14:58:23 +00004099std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004100std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004101
Vladimir Markofcb503c2016-05-18 12:48:17 +01004102class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004103 public:
4104 HInvokeVirtual(ArenaAllocator* arena,
4105 uint32_t number_of_arguments,
4106 Primitive::Type return_type,
4107 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004108 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004109 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004110 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004111 vtable_index_(vtable_index) {}
4112
Calin Juravle641547a2015-04-21 22:08:51 +01004113 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004114 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004115 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004116 }
4117
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004118 uint32_t GetVTableIndex() const { return vtable_index_; }
4119
4120 DECLARE_INSTRUCTION(InvokeVirtual);
4121
4122 private:
4123 const uint32_t vtable_index_;
4124
4125 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4126};
4127
Vladimir Markofcb503c2016-05-18 12:48:17 +01004128class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004129 public:
4130 HInvokeInterface(ArenaAllocator* arena,
4131 uint32_t number_of_arguments,
4132 Primitive::Type return_type,
4133 uint32_t dex_pc,
4134 uint32_t dex_method_index,
4135 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004136 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004137 imt_index_(imt_index) {}
4138
Calin Juravle641547a2015-04-21 22:08:51 +01004139 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004140 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004141 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004142 }
4143
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004144 uint32_t GetImtIndex() const { return imt_index_; }
4145 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4146
4147 DECLARE_INSTRUCTION(InvokeInterface);
4148
4149 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004150 const uint32_t imt_index_;
4151
4152 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4153};
4154
Vladimir Markofcb503c2016-05-18 12:48:17 +01004155class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004156 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004157 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004158 : HUnaryOperation(result_type, input, dex_pc) {
4159 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4160 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004161
Roland Levillain9867bc72015-08-05 10:21:34 +01004162 template <typename T> T Compute(T x) const { return -x; }
4163
4164 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004165 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004166 }
4167 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004168 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004169 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004170 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4171 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4172 }
4173 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4174 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4175 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004176
Roland Levillain88cb1752014-10-20 16:36:47 +01004177 DECLARE_INSTRUCTION(Neg);
4178
4179 private:
4180 DISALLOW_COPY_AND_ASSIGN(HNeg);
4181};
4182
Vladimir Markofcb503c2016-05-18 12:48:17 +01004183class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004184 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004185 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004186 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004187 uint32_t dex_pc,
4188 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004189 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004190 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004191 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004192 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004193 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004194 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004195 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004196 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004197 }
4198
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004199 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004200 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004201
4202 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004203 bool NeedsEnvironment() const OVERRIDE { return true; }
4204
Mingyao Yang0c365e62015-03-31 15:09:29 -07004205 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4206 bool CanThrow() const OVERRIDE { return true; }
4207
Calin Juravle10e244f2015-01-26 18:54:32 +00004208 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004209
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004210 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4211
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004212 DECLARE_INSTRUCTION(NewArray);
4213
4214 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004215 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004216 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004217 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004218
4219 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4220};
4221
Vladimir Markofcb503c2016-05-18 12:48:17 +01004222class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004223 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004224 HAdd(Primitive::Type result_type,
4225 HInstruction* left,
4226 HInstruction* right,
4227 uint32_t dex_pc = kNoDexPc)
4228 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004229
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004230 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004231
Roland Levillain9867bc72015-08-05 10:21:34 +01004232 template <typename T> T Compute(T x, T y) const { return x + y; }
4233
4234 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004235 return GetBlock()->GetGraph()->GetIntConstant(
4236 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004237 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004238 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004239 return GetBlock()->GetGraph()->GetLongConstant(
4240 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004241 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004242 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4243 return GetBlock()->GetGraph()->GetFloatConstant(
4244 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4245 }
4246 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4247 return GetBlock()->GetGraph()->GetDoubleConstant(
4248 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4249 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004250
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004251 DECLARE_INSTRUCTION(Add);
4252
4253 private:
4254 DISALLOW_COPY_AND_ASSIGN(HAdd);
4255};
4256
Vladimir Markofcb503c2016-05-18 12:48:17 +01004257class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004258 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004259 HSub(Primitive::Type result_type,
4260 HInstruction* left,
4261 HInstruction* right,
4262 uint32_t dex_pc = kNoDexPc)
4263 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004264
Roland Levillain9867bc72015-08-05 10:21:34 +01004265 template <typename T> T Compute(T x, T y) const { return x - y; }
4266
4267 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004268 return GetBlock()->GetGraph()->GetIntConstant(
4269 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004270 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004271 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004272 return GetBlock()->GetGraph()->GetLongConstant(
4273 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004274 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004275 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4276 return GetBlock()->GetGraph()->GetFloatConstant(
4277 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4278 }
4279 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4280 return GetBlock()->GetGraph()->GetDoubleConstant(
4281 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4282 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004283
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004284 DECLARE_INSTRUCTION(Sub);
4285
4286 private:
4287 DISALLOW_COPY_AND_ASSIGN(HSub);
4288};
4289
Vladimir Markofcb503c2016-05-18 12:48:17 +01004290class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004291 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004292 HMul(Primitive::Type result_type,
4293 HInstruction* left,
4294 HInstruction* right,
4295 uint32_t dex_pc = kNoDexPc)
4296 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004297
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004298 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004299
Roland Levillain9867bc72015-08-05 10:21:34 +01004300 template <typename T> T Compute(T x, T y) const { return x * y; }
4301
4302 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004303 return GetBlock()->GetGraph()->GetIntConstant(
4304 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004305 }
4306 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004307 return GetBlock()->GetGraph()->GetLongConstant(
4308 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004309 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004310 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4311 return GetBlock()->GetGraph()->GetFloatConstant(
4312 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4313 }
4314 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4315 return GetBlock()->GetGraph()->GetDoubleConstant(
4316 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4317 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004318
4319 DECLARE_INSTRUCTION(Mul);
4320
4321 private:
4322 DISALLOW_COPY_AND_ASSIGN(HMul);
4323};
4324
Vladimir Markofcb503c2016-05-18 12:48:17 +01004325class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004326 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004327 HDiv(Primitive::Type result_type,
4328 HInstruction* left,
4329 HInstruction* right,
4330 uint32_t dex_pc)
4331 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004332
Roland Levillain9867bc72015-08-05 10:21:34 +01004333 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004334 T ComputeIntegral(T x, T y) const {
4335 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004336 // Our graph structure ensures we never have 0 for `y` during
4337 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004338 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004339 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004340 return (y == -1) ? -x : x / y;
4341 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004342
Roland Levillain31dd3d62016-02-16 12:21:02 +00004343 template <typename T>
4344 T ComputeFP(T x, T y) const {
4345 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4346 return x / y;
4347 }
4348
Roland Levillain9867bc72015-08-05 10:21:34 +01004349 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004350 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004351 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004352 }
4353 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004354 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004355 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4356 }
4357 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4358 return GetBlock()->GetGraph()->GetFloatConstant(
4359 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4360 }
4361 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4362 return GetBlock()->GetGraph()->GetDoubleConstant(
4363 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004364 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004365
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004366 static SideEffects SideEffectsForArchRuntimeCalls() {
4367 // The generated code can use a runtime call.
4368 return SideEffects::CanTriggerGC();
4369 }
4370
Calin Juravle7c4954d2014-10-28 16:57:40 +00004371 DECLARE_INSTRUCTION(Div);
4372
4373 private:
4374 DISALLOW_COPY_AND_ASSIGN(HDiv);
4375};
4376
Vladimir Markofcb503c2016-05-18 12:48:17 +01004377class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004378 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004379 HRem(Primitive::Type result_type,
4380 HInstruction* left,
4381 HInstruction* right,
4382 uint32_t dex_pc)
4383 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004384
Roland Levillain9867bc72015-08-05 10:21:34 +01004385 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004386 T ComputeIntegral(T x, T y) const {
4387 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004388 // Our graph structure ensures we never have 0 for `y` during
4389 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004390 DCHECK_NE(y, 0);
4391 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4392 return (y == -1) ? 0 : x % y;
4393 }
4394
Roland Levillain31dd3d62016-02-16 12:21:02 +00004395 template <typename T>
4396 T ComputeFP(T x, T y) const {
4397 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4398 return std::fmod(x, y);
4399 }
4400
Roland Levillain9867bc72015-08-05 10:21:34 +01004401 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004402 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004403 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004404 }
4405 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004406 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004407 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004408 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004409 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4410 return GetBlock()->GetGraph()->GetFloatConstant(
4411 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4412 }
4413 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4414 return GetBlock()->GetGraph()->GetDoubleConstant(
4415 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4416 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004417
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004418 static SideEffects SideEffectsForArchRuntimeCalls() {
4419 return SideEffects::CanTriggerGC();
4420 }
4421
Calin Juravlebacfec32014-11-14 15:54:36 +00004422 DECLARE_INSTRUCTION(Rem);
4423
4424 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004425 DISALLOW_COPY_AND_ASSIGN(HRem);
4426};
4427
Vladimir Markofcb503c2016-05-18 12:48:17 +01004428class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004429 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004430 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4431 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004432 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004433 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004434 SetRawInputAt(0, value);
4435 }
4436
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004437 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4438
Calin Juravled0d48522014-11-04 16:40:20 +00004439 bool CanBeMoved() const OVERRIDE { return true; }
4440
Vladimir Marko372f10e2016-05-17 16:30:10 +01004441 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004442 return true;
4443 }
4444
4445 bool NeedsEnvironment() const OVERRIDE { return true; }
4446 bool CanThrow() const OVERRIDE { return true; }
4447
Calin Juravled0d48522014-11-04 16:40:20 +00004448 DECLARE_INSTRUCTION(DivZeroCheck);
4449
4450 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004451 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4452};
4453
Vladimir Markofcb503c2016-05-18 12:48:17 +01004454class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004455 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004456 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004457 HInstruction* value,
4458 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004459 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004460 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4461 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4462 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004463 }
4464
Roland Levillain5b5b9312016-03-22 14:57:31 +00004465 template <typename T>
4466 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4467 return value << (distance & max_shift_distance);
4468 }
4469
4470 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004471 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004472 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004473 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004474 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004475 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004476 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004477 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004478 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4479 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4480 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4481 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004482 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004483 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4484 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004485 LOG(FATAL) << DebugName() << " is not defined for float values";
4486 UNREACHABLE();
4487 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004488 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4489 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004490 LOG(FATAL) << DebugName() << " is not defined for double values";
4491 UNREACHABLE();
4492 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004493
4494 DECLARE_INSTRUCTION(Shl);
4495
4496 private:
4497 DISALLOW_COPY_AND_ASSIGN(HShl);
4498};
4499
Vladimir Markofcb503c2016-05-18 12:48:17 +01004500class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004501 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004502 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004503 HInstruction* value,
4504 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004505 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004506 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4507 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4508 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004509 }
4510
Roland Levillain5b5b9312016-03-22 14:57:31 +00004511 template <typename T>
4512 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4513 return value >> (distance & max_shift_distance);
4514 }
4515
4516 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004517 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004518 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004519 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004520 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004521 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004522 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004523 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004524 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4525 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4526 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4527 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004528 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004529 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4530 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004531 LOG(FATAL) << DebugName() << " is not defined for float values";
4532 UNREACHABLE();
4533 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004534 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4535 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004536 LOG(FATAL) << DebugName() << " is not defined for double values";
4537 UNREACHABLE();
4538 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004539
4540 DECLARE_INSTRUCTION(Shr);
4541
4542 private:
4543 DISALLOW_COPY_AND_ASSIGN(HShr);
4544};
4545
Vladimir Markofcb503c2016-05-18 12:48:17 +01004546class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004547 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004548 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004549 HInstruction* value,
4550 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004551 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004552 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4553 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4554 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004555 }
4556
Roland Levillain5b5b9312016-03-22 14:57:31 +00004557 template <typename T>
4558 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4559 typedef typename std::make_unsigned<T>::type V;
4560 V ux = static_cast<V>(value);
4561 return static_cast<T>(ux >> (distance & max_shift_distance));
4562 }
4563
4564 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004565 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004566 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004567 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004568 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004569 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004570 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004571 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4573 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4574 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4575 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004576 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004577 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4578 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004579 LOG(FATAL) << DebugName() << " is not defined for float values";
4580 UNREACHABLE();
4581 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004582 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4583 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004584 LOG(FATAL) << DebugName() << " is not defined for double values";
4585 UNREACHABLE();
4586 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004587
4588 DECLARE_INSTRUCTION(UShr);
4589
4590 private:
4591 DISALLOW_COPY_AND_ASSIGN(HUShr);
4592};
4593
Vladimir Markofcb503c2016-05-18 12:48:17 +01004594class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004595 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004596 HAnd(Primitive::Type result_type,
4597 HInstruction* left,
4598 HInstruction* right,
4599 uint32_t dex_pc = kNoDexPc)
4600 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004601
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004602 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004603
Roland Levillaine53bd812016-02-24 14:54:18 +00004604 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004605
4606 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004607 return GetBlock()->GetGraph()->GetIntConstant(
4608 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004609 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004610 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 return GetBlock()->GetGraph()->GetLongConstant(
4612 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004613 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004614 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4615 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4616 LOG(FATAL) << DebugName() << " is not defined for float values";
4617 UNREACHABLE();
4618 }
4619 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4620 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4621 LOG(FATAL) << DebugName() << " is not defined for double values";
4622 UNREACHABLE();
4623 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004624
4625 DECLARE_INSTRUCTION(And);
4626
4627 private:
4628 DISALLOW_COPY_AND_ASSIGN(HAnd);
4629};
4630
Vladimir Markofcb503c2016-05-18 12:48:17 +01004631class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004633 HOr(Primitive::Type result_type,
4634 HInstruction* left,
4635 HInstruction* right,
4636 uint32_t dex_pc = kNoDexPc)
4637 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004638
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004639 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004640
Roland Levillaine53bd812016-02-24 14:54:18 +00004641 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004642
4643 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004644 return GetBlock()->GetGraph()->GetIntConstant(
4645 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004646 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004647 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004648 return GetBlock()->GetGraph()->GetLongConstant(
4649 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004651 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4652 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4653 LOG(FATAL) << DebugName() << " is not defined for float values";
4654 UNREACHABLE();
4655 }
4656 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4657 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4658 LOG(FATAL) << DebugName() << " is not defined for double values";
4659 UNREACHABLE();
4660 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004661
4662 DECLARE_INSTRUCTION(Or);
4663
4664 private:
4665 DISALLOW_COPY_AND_ASSIGN(HOr);
4666};
4667
Vladimir Markofcb503c2016-05-18 12:48:17 +01004668class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004669 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004670 HXor(Primitive::Type result_type,
4671 HInstruction* left,
4672 HInstruction* right,
4673 uint32_t dex_pc = kNoDexPc)
4674 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004675
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004676 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004677
Roland Levillaine53bd812016-02-24 14:54:18 +00004678 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004679
4680 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004681 return GetBlock()->GetGraph()->GetIntConstant(
4682 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004683 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004684 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004685 return GetBlock()->GetGraph()->GetLongConstant(
4686 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004687 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004688 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4689 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4690 LOG(FATAL) << DebugName() << " is not defined for float values";
4691 UNREACHABLE();
4692 }
4693 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4694 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4695 LOG(FATAL) << DebugName() << " is not defined for double values";
4696 UNREACHABLE();
4697 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004698
4699 DECLARE_INSTRUCTION(Xor);
4700
4701 private:
4702 DISALLOW_COPY_AND_ASSIGN(HXor);
4703};
4704
Vladimir Markofcb503c2016-05-18 12:48:17 +01004705class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004706 public:
4707 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004708 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004709 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4710 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004711 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004712
Roland Levillain5b5b9312016-03-22 14:57:31 +00004713 template <typename T>
4714 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4715 typedef typename std::make_unsigned<T>::type V;
4716 V ux = static_cast<V>(value);
4717 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004718 return static_cast<T>(ux);
4719 } else {
4720 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004721 return static_cast<T>(ux >> (distance & max_shift_value)) |
4722 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004723 }
4724 }
4725
Roland Levillain5b5b9312016-03-22 14:57:31 +00004726 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004727 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004728 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004729 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004730 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004731 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004732 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004733 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004734 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4735 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4736 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4737 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004738 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004739 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4740 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004741 LOG(FATAL) << DebugName() << " is not defined for float values";
4742 UNREACHABLE();
4743 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004744 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4745 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004746 LOG(FATAL) << DebugName() << " is not defined for double values";
4747 UNREACHABLE();
4748 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004749
4750 DECLARE_INSTRUCTION(Ror);
4751
4752 private:
4753 DISALLOW_COPY_AND_ASSIGN(HRor);
4754};
4755
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004756// The value of a parameter in this method. Its location depends on
4757// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004758class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004759 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004760 HParameterValue(const DexFile& dex_file,
4761 uint16_t type_index,
4762 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004763 Primitive::Type parameter_type,
4764 bool is_this = false)
4765 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004766 dex_file_(dex_file),
4767 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004768 index_(index) {
4769 SetPackedFlag<kFlagIsThis>(is_this);
4770 SetPackedFlag<kFlagCanBeNull>(!is_this);
4771 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004772
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004773 const DexFile& GetDexFile() const { return dex_file_; }
4774 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004775 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004776 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004777
Vladimir Markoa1de9182016-02-25 11:37:38 +00004778 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4779 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004780
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004781 DECLARE_INSTRUCTION(ParameterValue);
4782
4783 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004784 // Whether or not the parameter value corresponds to 'this' argument.
4785 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4786 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4787 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4788 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4789 "Too many packed fields.");
4790
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004791 const DexFile& dex_file_;
4792 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004793 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004794 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004795 const uint8_t index_;
4796
4797 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4798};
4799
Vladimir Markofcb503c2016-05-18 12:48:17 +01004800class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4803 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004804
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004805 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004806 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004807 return true;
4808 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004809
Roland Levillain9867bc72015-08-05 10:21:34 +01004810 template <typename T> T Compute(T x) const { return ~x; }
4811
4812 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004813 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 }
4815 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004816 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004817 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004818 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4819 LOG(FATAL) << DebugName() << " is not defined for float values";
4820 UNREACHABLE();
4821 }
4822 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4823 LOG(FATAL) << DebugName() << " is not defined for double values";
4824 UNREACHABLE();
4825 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004826
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004827 DECLARE_INSTRUCTION(Not);
4828
4829 private:
4830 DISALLOW_COPY_AND_ASSIGN(HNot);
4831};
4832
Vladimir Markofcb503c2016-05-18 12:48:17 +01004833class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004834 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4836 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004837
4838 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004839 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004840 return true;
4841 }
4842
Roland Levillain9867bc72015-08-05 10:21:34 +01004843 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004844 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004845 return !x;
4846 }
4847
Roland Levillain9867bc72015-08-05 10:21:34 +01004848 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004849 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004850 }
4851 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4852 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004853 UNREACHABLE();
4854 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004855 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4856 LOG(FATAL) << DebugName() << " is not defined for float values";
4857 UNREACHABLE();
4858 }
4859 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4860 LOG(FATAL) << DebugName() << " is not defined for double values";
4861 UNREACHABLE();
4862 }
David Brazdil66d126e2015-04-03 16:02:44 +01004863
4864 DECLARE_INSTRUCTION(BooleanNot);
4865
4866 private:
4867 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4868};
4869
Vladimir Markofcb503c2016-05-18 12:48:17 +01004870class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004871 public:
4872 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004873 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004874 : HExpression(result_type,
4875 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4876 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004877 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004878 // Invariant: We should never generate a conversion to a Boolean value.
4879 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004880 }
4881
4882 HInstruction* GetInput() const { return InputAt(0); }
4883 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4884 Primitive::Type GetResultType() const { return GetType(); }
4885
4886 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004887 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4888 return true;
4889 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004890
Mark Mendelle82549b2015-05-06 10:55:34 -04004891 // Try to statically evaluate the conversion and return a HConstant
4892 // containing the result. If the input cannot be converted, return nullptr.
4893 HConstant* TryStaticEvaluation() const;
4894
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004895 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4896 Primitive::Type result_type) {
4897 // Some architectures may not require the 'GC' side effects, but at this point
4898 // in the compilation process we do not know what architecture we will
4899 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004900 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4901 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004902 return SideEffects::CanTriggerGC();
4903 }
4904 return SideEffects::None();
4905 }
4906
Roland Levillaindff1f282014-11-05 14:15:05 +00004907 DECLARE_INSTRUCTION(TypeConversion);
4908
4909 private:
4910 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4911};
4912
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004913static constexpr uint32_t kNoRegNumber = -1;
4914
Vladimir Markofcb503c2016-05-18 12:48:17 +01004915class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004916 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004917 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4918 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004919 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004920 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004921 SetRawInputAt(0, value);
4922 }
4923
Calin Juravle10e244f2015-01-26 18:54:32 +00004924 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004925 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004926 return true;
4927 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004928
Calin Juravle10e244f2015-01-26 18:54:32 +00004929 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004930
Calin Juravle10e244f2015-01-26 18:54:32 +00004931 bool CanThrow() const OVERRIDE { return true; }
4932
4933 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004934
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004935
4936 DECLARE_INSTRUCTION(NullCheck);
4937
4938 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004939 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4940};
4941
4942class FieldInfo : public ValueObject {
4943 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004944 FieldInfo(MemberOffset field_offset,
4945 Primitive::Type field_type,
4946 bool is_volatile,
4947 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004948 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004949 const DexFile& dex_file,
4950 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004951 : field_offset_(field_offset),
4952 field_type_(field_type),
4953 is_volatile_(is_volatile),
4954 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004955 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004956 dex_file_(dex_file),
4957 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004958
4959 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004960 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004961 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004962 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004963 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004964 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004965 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004966
4967 private:
4968 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004969 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004970 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004971 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004972 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004973 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004974 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004975};
4976
Vladimir Markofcb503c2016-05-18 12:48:17 +01004977class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978 public:
4979 HInstanceFieldGet(HInstruction* value,
4980 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004981 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004982 bool is_volatile,
4983 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004984 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004985 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004986 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004987 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004988 : HExpression(field_type,
4989 SideEffects::FieldReadOfType(field_type, is_volatile),
4990 dex_pc),
4991 field_info_(field_offset,
4992 field_type,
4993 is_volatile,
4994 field_idx,
4995 declaring_class_def_index,
4996 dex_file,
4997 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004998 SetRawInputAt(0, value);
4999 }
5000
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005001 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005002
Vladimir Marko372f10e2016-05-17 16:30:10 +01005003 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5004 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005005 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005006 }
5007
Calin Juravle641547a2015-04-21 22:08:51 +01005008 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5009 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005010 }
5011
5012 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005013 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5014 }
5015
Calin Juravle52c48962014-12-16 17:02:57 +00005016 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005017 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005018 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005019 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005020
5021 DECLARE_INSTRUCTION(InstanceFieldGet);
5022
5023 private:
5024 const FieldInfo field_info_;
5025
5026 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5027};
5028
Vladimir Markofcb503c2016-05-18 12:48:17 +01005029class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005030 public:
5031 HInstanceFieldSet(HInstruction* object,
5032 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005033 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005034 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005035 bool is_volatile,
5036 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005037 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005038 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005039 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005040 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005041 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5042 dex_pc),
5043 field_info_(field_offset,
5044 field_type,
5045 is_volatile,
5046 field_idx,
5047 declaring_class_def_index,
5048 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005049 dex_cache) {
5050 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005051 SetRawInputAt(0, object);
5052 SetRawInputAt(1, value);
5053 }
5054
Calin Juravle641547a2015-04-21 22:08:51 +01005055 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5056 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005057 }
5058
Calin Juravle52c48962014-12-16 17:02:57 +00005059 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005060 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005061 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005062 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005063 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005064 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5065 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005066
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005067 DECLARE_INSTRUCTION(InstanceFieldSet);
5068
5069 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005070 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5071 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5072 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5073 "Too many packed fields.");
5074
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005075 const FieldInfo field_info_;
5076
5077 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5078};
5079
Vladimir Markofcb503c2016-05-18 12:48:17 +01005080class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 public:
Aart Bik18b36ab2016-04-13 16:41:35 -07005082 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type, uint32_t dex_pc)
5083 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005084 SetRawInputAt(0, array);
5085 SetRawInputAt(1, index);
5086 }
5087
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005088 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005089 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005090 return true;
5091 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005092 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005093 // TODO: We can be smarter here.
5094 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5095 // which generates the implicit null check. There are cases when these can be removed
5096 // to produce better code. If we ever add optimizations to do so we should allow an
5097 // implicit check here (as long as the address falls in the first page).
5098 return false;
5099 }
5100
David Brazdil4833f5a2015-12-16 10:37:39 +00005101 bool IsEquivalentOf(HArrayGet* other) const {
5102 bool result = (GetDexPc() == other->GetDexPc());
5103 if (kIsDebugBuild && result) {
5104 DCHECK_EQ(GetBlock(), other->GetBlock());
5105 DCHECK_EQ(GetArray(), other->GetArray());
5106 DCHECK_EQ(GetIndex(), other->GetIndex());
5107 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005108 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005109 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005110 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5111 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005112 }
5113 }
5114 return result;
5115 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005116
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005117 HInstruction* GetArray() const { return InputAt(0); }
5118 HInstruction* GetIndex() const { return InputAt(1); }
5119
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005120 DECLARE_INSTRUCTION(ArrayGet);
5121
5122 private:
5123 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5124};
5125
Vladimir Markofcb503c2016-05-18 12:48:17 +01005126class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127 public:
5128 HArraySet(HInstruction* array,
5129 HInstruction* index,
5130 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005131 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005132 uint32_t dex_pc)
5133 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005134 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5135 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5136 SetPackedFlag<kFlagValueCanBeNull>(true);
5137 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138 SetRawInputAt(0, array);
5139 SetRawInputAt(1, index);
5140 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005141 // Make a best guess now, may be refined during SSA building.
5142 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 }
5144
Calin Juravle77520bc2015-01-12 18:45:46 +00005145 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005146 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005147 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 }
5149
Mingyao Yang81014cb2015-06-02 03:16:27 -07005150 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005151 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005152
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005153 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005154 // TODO: Same as for ArrayGet.
5155 return false;
5156 }
5157
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005158 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005159 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005160 }
5161
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005162 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005163 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005164 }
5165
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005166 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005167 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005168 }
5169
Vladimir Markoa1de9182016-02-25 11:37:38 +00005170 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5171 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5172 bool StaticTypeOfArrayIsObjectArray() const {
5173 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5174 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005175
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005176 HInstruction* GetArray() const { return InputAt(0); }
5177 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005178 HInstruction* GetValue() const { return InputAt(2); }
5179
5180 Primitive::Type GetComponentType() const {
5181 // The Dex format does not type floating point index operations. Since the
5182 // `expected_component_type_` is set during building and can therefore not
5183 // be correct, we also check what is the value type. If it is a floating
5184 // point type, we must use that type.
5185 Primitive::Type value_type = GetValue()->GetType();
5186 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5187 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005188 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005189 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005190
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005191 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005192 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005193 }
5194
Aart Bik18b36ab2016-04-13 16:41:35 -07005195 void ComputeSideEffects() {
5196 Primitive::Type type = GetComponentType();
5197 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5198 SideEffectsForArchRuntimeCalls(type)));
5199 }
5200
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005201 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5202 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5203 }
5204
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205 DECLARE_INSTRUCTION(ArraySet);
5206
5207 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005208 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5209 static constexpr size_t kFieldExpectedComponentTypeSize =
5210 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5211 static constexpr size_t kFlagNeedsTypeCheck =
5212 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5213 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005214 // Cached information for the reference_type_info_ so that codegen
5215 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005216 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5217 static constexpr size_t kNumberOfArraySetPackedBits =
5218 kFlagStaticTypeOfArrayIsObjectArray + 1;
5219 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5220 using ExpectedComponentTypeField =
5221 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005222
5223 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5224};
5225
Vladimir Markofcb503c2016-05-18 12:48:17 +01005226class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005227 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005228 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005229 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005230 // Note that arrays do not change length, so the instruction does not
5231 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005232 SetRawInputAt(0, array);
5233 }
5234
Calin Juravle77520bc2015-01-12 18:45:46 +00005235 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005236 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005237 return true;
5238 }
Calin Juravle641547a2015-04-21 22:08:51 +01005239 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5240 return obj == InputAt(0);
5241 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005242
Vladimir Markodce016e2016-04-28 13:10:02 +01005243 void MarkAsStringLength() { SetPackedFlag<kFlagIsStringLength>(); }
5244 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5245
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 DECLARE_INSTRUCTION(ArrayLength);
5247
5248 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005249 // We treat a String as an array, creating the HArrayLength from String.length()
5250 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5251 // determine whether a particular HArrayLength is actually a String.length() by
5252 // looking at the type of the input but that requires holding the mutator lock, so
5253 // we prefer to use a flag, so that code generators don't need to do the locking.
5254 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5255 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5256 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5257 "Too many packed fields.");
5258
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5260};
5261
Vladimir Markofcb503c2016-05-18 12:48:17 +01005262class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005264 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5265 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005266 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005267 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005268 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 SetRawInputAt(0, index);
5270 SetRawInputAt(1, length);
5271 }
5272
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005273 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005274 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005275 return true;
5276 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005277
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005278 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005280 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005281
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005282 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283
5284 DECLARE_INSTRUCTION(BoundsCheck);
5285
5286 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5288};
5289
Vladimir Markofcb503c2016-05-18 12:48:17 +01005290class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005291 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005292 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005293 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005294
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005295 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005296 return true;
5297 }
5298
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005299 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5300 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005301
5302 DECLARE_INSTRUCTION(SuspendCheck);
5303
5304 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005305 // Only used for code generation, in order to share the same slow path between back edges
5306 // of a same loop.
5307 SlowPathCode* slow_path_;
5308
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005309 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5310};
5311
David Srbecky0cf44932015-12-09 14:09:59 +00005312// Pseudo-instruction which provides the native debugger with mapping information.
5313// It ensures that we can generate line number and local variables at this point.
5314class HNativeDebugInfo : public HTemplateInstruction<0> {
5315 public:
5316 explicit HNativeDebugInfo(uint32_t dex_pc)
5317 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5318
5319 bool NeedsEnvironment() const OVERRIDE {
5320 return true;
5321 }
5322
5323 DECLARE_INSTRUCTION(NativeDebugInfo);
5324
5325 private:
5326 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5327};
5328
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005329/**
5330 * Instruction to load a Class object.
5331 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005332class HLoadClass FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005333 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005334 HLoadClass(HCurrentMethod* current_method,
5335 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005336 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005337 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005338 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005339 bool needs_access_check,
5340 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005341 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005342 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005343 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005344 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005345 // Referrers class should not need access check. We never inline unverified
5346 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005347 DCHECK(!is_referrers_class || !needs_access_check);
5348
5349 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5350 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5351 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5352 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005353 SetRawInputAt(0, current_method);
5354 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005355
5356 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005357
Vladimir Marko372f10e2016-05-17 16:30:10 +01005358 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005359 // Note that we don't need to test for generate_clinit_check_.
5360 // Whether or not we need to generate the clinit check is processed in
5361 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005362 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005363 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005364 }
5365
5366 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5367
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005368 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005369 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005370
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005371 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005372 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005373 }
5374
Calin Juravle0ba218d2015-05-19 18:46:01 +01005375 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005376 // The entrypoint the code generator is going to call does not do
5377 // clinit of the class.
5378 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005379 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005380 }
5381
5382 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005383 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005384 (!IsReferrersClass() && !IsInDexCache()) ||
5385 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005386 }
5387
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005388
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005389 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005390 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005391 }
5392
Calin Juravleacf735c2015-02-12 15:25:22 +00005393 ReferenceTypeInfo GetLoadedClassRTI() {
5394 return loaded_class_rti_;
5395 }
5396
5397 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5398 // Make sure we only set exact types (the loaded class should never be merged).
5399 DCHECK(rti.IsExact());
5400 loaded_class_rti_ = rti;
5401 }
5402
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005403 const DexFile& GetDexFile() { return dex_file_; }
5404
Vladimir Markoa1de9182016-02-25 11:37:38 +00005405 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005406
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005407 static SideEffects SideEffectsForArchRuntimeCalls() {
5408 return SideEffects::CanTriggerGC();
5409 }
5410
Vladimir Markoa1de9182016-02-25 11:37:38 +00005411 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5412 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5413 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5414 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005416 DECLARE_INSTRUCTION(LoadClass);
5417
5418 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005419 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5420 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5421 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005422 // Whether this instruction must generate the initialization check.
5423 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005424 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5425 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5426 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5427
5428 const uint16_t type_index_;
5429 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005430
Calin Juravleacf735c2015-02-12 15:25:22 +00005431 ReferenceTypeInfo loaded_class_rti_;
5432
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005433 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5434};
5435
Vladimir Marko372f10e2016-05-17 16:30:10 +01005436class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005437 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005438 // Determines how to load the String.
5439 enum class LoadKind {
5440 // Use boot image String* address that will be known at link time.
5441 // Used for boot image strings referenced by boot image code in non-PIC mode.
5442 kBootImageLinkTimeAddress,
5443
5444 // Use PC-relative boot image String* address that will be known at link time.
5445 // Used for boot image strings referenced by boot image code in PIC mode.
5446 kBootImageLinkTimePcRelative,
5447
5448 // Use a known boot image String* address, embedded in the code by the codegen.
5449 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5450 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5451 // GetIncludePatchInformation().
5452 kBootImageAddress,
5453
5454 // Load from the resolved strings array at an absolute address.
5455 // Used for strings outside the boot image referenced by JIT-compiled code.
5456 kDexCacheAddress,
5457
5458 // Load from resolved strings array in the dex cache using a PC-relative load.
5459 // Used for strings outside boot image when we know that we can access
5460 // the dex cache arrays using a PC-relative load.
5461 kDexCachePcRelative,
5462
5463 // Load from resolved strings array accessed through the class loaded from
5464 // the compiled method's own ArtMethod*. This is the default access type when
5465 // all other types are unavailable.
5466 kDexCacheViaMethod,
5467
5468 kLast = kDexCacheViaMethod
5469 };
5470
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005471 HLoadString(HCurrentMethod* current_method,
5472 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005473 const DexFile& dex_file,
5474 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005475 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5476 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005477 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005478 SetPackedFlag<kFlagIsInDexCache>(false);
5479 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5480 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005481 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005482
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005483 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5484 DCHECK(HasAddress(load_kind));
5485 load_data_.address = address;
5486 SetLoadKindInternal(load_kind);
5487 }
5488
5489 void SetLoadKindWithStringReference(LoadKind load_kind,
5490 const DexFile& dex_file,
5491 uint32_t string_index) {
5492 DCHECK(HasStringReference(load_kind));
5493 load_data_.ref.dex_file = &dex_file;
5494 string_index_ = string_index;
5495 SetLoadKindInternal(load_kind);
5496 }
5497
5498 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5499 const DexFile& dex_file,
5500 uint32_t element_index) {
5501 DCHECK(HasDexCacheReference(load_kind));
5502 load_data_.ref.dex_file = &dex_file;
5503 load_data_.ref.dex_cache_element_index = element_index;
5504 SetLoadKindInternal(load_kind);
5505 }
5506
5507 LoadKind GetLoadKind() const {
5508 return GetPackedField<LoadKindField>();
5509 }
5510
5511 const DexFile& GetDexFile() const;
5512
5513 uint32_t GetStringIndex() const {
5514 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5515 return string_index_;
5516 }
5517
5518 uint32_t GetDexCacheElementOffset() const;
5519
5520 uint64_t GetAddress() const {
5521 DCHECK(HasAddress(GetLoadKind()));
5522 return load_data_.address;
5523 }
5524
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005525 bool CanBeMoved() const OVERRIDE { return true; }
5526
Vladimir Marko372f10e2016-05-17 16:30:10 +01005527 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005528
5529 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5530
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005531 // Will call the runtime if we need to load the string through
5532 // the dex cache and the string is not guaranteed to be there yet.
5533 bool NeedsEnvironment() const OVERRIDE {
5534 LoadKind load_kind = GetLoadKind();
5535 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5536 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5537 load_kind == LoadKind::kBootImageAddress) {
5538 return false;
5539 }
5540 return !IsInDexCache();
5541 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005542
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005543 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5544 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5545 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005546
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005547 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005548 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005549
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005550 static SideEffects SideEffectsForArchRuntimeCalls() {
5551 return SideEffects::CanTriggerGC();
5552 }
5553
Vladimir Markoa1de9182016-02-25 11:37:38 +00005554 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5555
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005556 void MarkInDexCache() {
5557 SetPackedFlag<kFlagIsInDexCache>(true);
5558 DCHECK(!NeedsEnvironment());
5559 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005560 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005561 }
5562
Vladimir Marko372f10e2016-05-17 16:30:10 +01005563 void AddSpecialInput(HInstruction* special_input);
5564
5565 using HInstruction::GetInputRecords; // Keep the const version visible.
5566 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5567 return ArrayRef<HUserRecord<HInstruction*>>(
5568 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005569 }
5570
Vladimir Marko372f10e2016-05-17 16:30:10 +01005571 Primitive::Type GetType() const OVERRIDE {
5572 return Primitive::kPrimNot;
5573 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005574
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005575 DECLARE_INSTRUCTION(LoadString);
5576
5577 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005578 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005579 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5580 static constexpr size_t kFieldLoadKindSize =
5581 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5582 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005583 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005584 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005585
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005586 static bool HasStringReference(LoadKind load_kind) {
5587 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5588 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5589 load_kind == LoadKind::kDexCacheViaMethod;
5590 }
5591
5592 static bool HasAddress(LoadKind load_kind) {
5593 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5594 }
5595
5596 static bool HasDexCacheReference(LoadKind load_kind) {
5597 return load_kind == LoadKind::kDexCachePcRelative;
5598 }
5599
5600 void SetLoadKindInternal(LoadKind load_kind);
5601
Vladimir Marko372f10e2016-05-17 16:30:10 +01005602 HUserRecord<HInstruction*> special_input_;
5603
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005604 // String index serves also as the hash code and it's also needed for slow-paths,
5605 // so it must not be overwritten with other load data.
5606 uint32_t string_index_;
5607
5608 union {
5609 struct {
5610 const DexFile* dex_file; // For string reference and dex cache reference.
5611 uint32_t dex_cache_element_index; // Only for dex cache reference.
5612 } ref;
5613 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5614 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005615
5616 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5617};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005618std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5619
5620// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5621inline const DexFile& HLoadString::GetDexFile() const {
5622 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5623 << GetLoadKind();
5624 return *load_data_.ref.dex_file;
5625}
5626
5627// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5628inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5629 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5630 return load_data_.ref.dex_cache_element_index;
5631}
5632
5633// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5634inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5635 // The special input is used for PC-relative loads on some architectures.
5636 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5637 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005638 // HLoadString::GetInputRecords() returns an empty array at this point,
5639 // so use the GetInputRecords() from the base class to set the input record.
5640 DCHECK(special_input_.GetInstruction() == nullptr);
5641 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005642 special_input->AddUseAt(this, 0);
5643}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005644
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005645/**
5646 * Performs an initialization check on its Class object input.
5647 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005648class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005649 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005650 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005651 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005652 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005653 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5654 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005655 SetRawInputAt(0, constant);
5656 }
5657
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005658 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005659 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005660 return true;
5661 }
5662
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005663 bool NeedsEnvironment() const OVERRIDE {
5664 // May call runtime to initialize the class.
5665 return true;
5666 }
5667
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005668 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005669
5670 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5671
5672 DECLARE_INSTRUCTION(ClinitCheck);
5673
5674 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005675 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5676};
5677
Vladimir Markofcb503c2016-05-18 12:48:17 +01005678class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005679 public:
5680 HStaticFieldGet(HInstruction* cls,
5681 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005682 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005683 bool is_volatile,
5684 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005685 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005686 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005687 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005688 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005689 : HExpression(field_type,
5690 SideEffects::FieldReadOfType(field_type, is_volatile),
5691 dex_pc),
5692 field_info_(field_offset,
5693 field_type,
5694 is_volatile,
5695 field_idx,
5696 declaring_class_def_index,
5697 dex_file,
5698 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005699 SetRawInputAt(0, cls);
5700 }
5701
Calin Juravle52c48962014-12-16 17:02:57 +00005702
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005703 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005704
Vladimir Marko372f10e2016-05-17 16:30:10 +01005705 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5706 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005707 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005708 }
5709
5710 size_t ComputeHashCode() const OVERRIDE {
5711 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5712 }
5713
Calin Juravle52c48962014-12-16 17:02:57 +00005714 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005715 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5716 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005717 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005718
5719 DECLARE_INSTRUCTION(StaticFieldGet);
5720
5721 private:
5722 const FieldInfo field_info_;
5723
5724 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5725};
5726
Vladimir Markofcb503c2016-05-18 12:48:17 +01005727class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005728 public:
5729 HStaticFieldSet(HInstruction* cls,
5730 HInstruction* value,
5731 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005732 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005733 bool is_volatile,
5734 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005735 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005736 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005737 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005738 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005739 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5740 dex_pc),
5741 field_info_(field_offset,
5742 field_type,
5743 is_volatile,
5744 field_idx,
5745 declaring_class_def_index,
5746 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005747 dex_cache) {
5748 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005749 SetRawInputAt(0, cls);
5750 SetRawInputAt(1, value);
5751 }
5752
Calin Juravle52c48962014-12-16 17:02:57 +00005753 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005754 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5755 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005756 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005757
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005758 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005759 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5760 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005761
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005762 DECLARE_INSTRUCTION(StaticFieldSet);
5763
5764 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005765 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5766 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5767 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5768 "Too many packed fields.");
5769
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005770 const FieldInfo field_info_;
5771
5772 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5773};
5774
Vladimir Markofcb503c2016-05-18 12:48:17 +01005775class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005776 public:
5777 HUnresolvedInstanceFieldGet(HInstruction* obj,
5778 Primitive::Type field_type,
5779 uint32_t field_index,
5780 uint32_t dex_pc)
5781 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5782 field_index_(field_index) {
5783 SetRawInputAt(0, obj);
5784 }
5785
5786 bool NeedsEnvironment() const OVERRIDE { return true; }
5787 bool CanThrow() const OVERRIDE { return true; }
5788
5789 Primitive::Type GetFieldType() const { return GetType(); }
5790 uint32_t GetFieldIndex() const { return field_index_; }
5791
5792 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5793
5794 private:
5795 const uint32_t field_index_;
5796
5797 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5798};
5799
Vladimir Markofcb503c2016-05-18 12:48:17 +01005800class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005801 public:
5802 HUnresolvedInstanceFieldSet(HInstruction* obj,
5803 HInstruction* value,
5804 Primitive::Type field_type,
5805 uint32_t field_index,
5806 uint32_t dex_pc)
5807 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005808 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005809 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005810 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005811 SetRawInputAt(0, obj);
5812 SetRawInputAt(1, value);
5813 }
5814
5815 bool NeedsEnvironment() const OVERRIDE { return true; }
5816 bool CanThrow() const OVERRIDE { return true; }
5817
Vladimir Markoa1de9182016-02-25 11:37:38 +00005818 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005819 uint32_t GetFieldIndex() const { return field_index_; }
5820
5821 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5822
5823 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005824 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5825 static constexpr size_t kFieldFieldTypeSize =
5826 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5827 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5828 kFieldFieldType + kFieldFieldTypeSize;
5829 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5830 "Too many packed fields.");
5831 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5832
Calin Juravlee460d1d2015-09-29 04:52:17 +01005833 const uint32_t field_index_;
5834
5835 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5836};
5837
Vladimir Markofcb503c2016-05-18 12:48:17 +01005838class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005839 public:
5840 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5841 uint32_t field_index,
5842 uint32_t dex_pc)
5843 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5844 field_index_(field_index) {
5845 }
5846
5847 bool NeedsEnvironment() const OVERRIDE { return true; }
5848 bool CanThrow() const OVERRIDE { return true; }
5849
5850 Primitive::Type GetFieldType() const { return GetType(); }
5851 uint32_t GetFieldIndex() const { return field_index_; }
5852
5853 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5854
5855 private:
5856 const uint32_t field_index_;
5857
5858 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5859};
5860
Vladimir Markofcb503c2016-05-18 12:48:17 +01005861class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005862 public:
5863 HUnresolvedStaticFieldSet(HInstruction* value,
5864 Primitive::Type field_type,
5865 uint32_t field_index,
5866 uint32_t dex_pc)
5867 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005868 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005869 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005870 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005871 SetRawInputAt(0, value);
5872 }
5873
5874 bool NeedsEnvironment() const OVERRIDE { return true; }
5875 bool CanThrow() const OVERRIDE { return true; }
5876
Vladimir Markoa1de9182016-02-25 11:37:38 +00005877 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005878 uint32_t GetFieldIndex() const { return field_index_; }
5879
5880 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5881
5882 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005883 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5884 static constexpr size_t kFieldFieldTypeSize =
5885 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5886 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5887 kFieldFieldType + kFieldFieldTypeSize;
5888 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5889 "Too many packed fields.");
5890 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5891
Calin Juravlee460d1d2015-09-29 04:52:17 +01005892 const uint32_t field_index_;
5893
5894 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5895};
5896
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005897// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005898class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005899 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005900 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5901 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005902
David Brazdilbbd733e2015-08-18 17:48:17 +01005903 bool CanBeNull() const OVERRIDE { return false; }
5904
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005905 DECLARE_INSTRUCTION(LoadException);
5906
5907 private:
5908 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5909};
5910
David Brazdilcb1c0552015-08-04 16:22:25 +01005911// Implicit part of move-exception which clears thread-local exception storage.
5912// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005913class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01005914 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005915 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5916 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005917
5918 DECLARE_INSTRUCTION(ClearException);
5919
5920 private:
5921 DISALLOW_COPY_AND_ASSIGN(HClearException);
5922};
5923
Vladimir Markofcb503c2016-05-18 12:48:17 +01005924class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005925 public:
5926 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005927 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005928 SetRawInputAt(0, exception);
5929 }
5930
5931 bool IsControlFlow() const OVERRIDE { return true; }
5932
5933 bool NeedsEnvironment() const OVERRIDE { return true; }
5934
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005935 bool CanThrow() const OVERRIDE { return true; }
5936
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005937
5938 DECLARE_INSTRUCTION(Throw);
5939
5940 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005941 DISALLOW_COPY_AND_ASSIGN(HThrow);
5942};
5943
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005944/**
5945 * Implementation strategies for the code generator of a HInstanceOf
5946 * or `HCheckCast`.
5947 */
5948enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005949 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005950 kExactCheck, // Can do a single class compare.
5951 kClassHierarchyCheck, // Can just walk the super class chain.
5952 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5953 kInterfaceCheck, // No optimization yet when checking against an interface.
5954 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005955 kArrayCheck, // No optimization yet when checking against a generic array.
5956 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957};
5958
Roland Levillain86503782016-02-11 19:07:30 +00005959std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5960
Vladimir Markofcb503c2016-05-18 12:48:17 +01005961class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005962 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005963 HInstanceOf(HInstruction* object,
5964 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005965 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005966 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005967 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969 dex_pc) {
5970 SetPackedField<TypeCheckKindField>(check_kind);
5971 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005972 SetRawInputAt(0, object);
5973 SetRawInputAt(1, constant);
5974 }
5975
5976 bool CanBeMoved() const OVERRIDE { return true; }
5977
Vladimir Marko372f10e2016-05-17 16:30:10 +01005978 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005979 return true;
5980 }
5981
5982 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005983 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005984 }
5985
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005986 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005987 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5988 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5989 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5990 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005991
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005992 static bool CanCallRuntime(TypeCheckKind check_kind) {
5993 // Mips currently does runtime calls for any other checks.
5994 return check_kind != TypeCheckKind::kExactCheck;
5995 }
5996
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005997 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005998 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005999 }
6000
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006001 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006002
6003 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6005 static constexpr size_t kFieldTypeCheckKindSize =
6006 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6007 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6008 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6009 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6010 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006011
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006012 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6013};
6014
Vladimir Markofcb503c2016-05-18 12:48:17 +01006015class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006016 public:
David Brazdilf5552582015-12-27 13:36:12 +00006017 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006018 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006019 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6020 SetPackedFlag<kFlagUpperCanBeNull>(true);
6021 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006022 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006023 SetRawInputAt(0, input);
6024 }
6025
David Brazdilf5552582015-12-27 13:36:12 +00006026 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006027 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006028 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006029 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006030
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006031 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006032 DCHECK(GetUpperCanBeNull() || !can_be_null);
6033 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006034 }
6035
Vladimir Markoa1de9182016-02-25 11:37:38 +00006036 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006037
Calin Juravleb1498f62015-02-16 13:13:29 +00006038 DECLARE_INSTRUCTION(BoundType);
6039
6040 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006041 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6042 // is false then CanBeNull() cannot be true).
6043 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6044 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6045 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6046 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6047
Calin Juravleb1498f62015-02-16 13:13:29 +00006048 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006049 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6050 // It is used to bound the type in cases like:
6051 // if (x instanceof ClassX) {
6052 // // uper_bound_ will be ClassX
6053 // }
David Brazdilf5552582015-12-27 13:36:12 +00006054 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006055
6056 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6057};
6058
Vladimir Markofcb503c2016-05-18 12:48:17 +01006059class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006060 public:
6061 HCheckCast(HInstruction* object,
6062 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006063 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006064 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006065 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6066 SetPackedField<TypeCheckKindField>(check_kind);
6067 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006068 SetRawInputAt(0, object);
6069 SetRawInputAt(1, constant);
6070 }
6071
6072 bool CanBeMoved() const OVERRIDE { return true; }
6073
Vladimir Marko372f10e2016-05-17 16:30:10 +01006074 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006075 return true;
6076 }
6077
6078 bool NeedsEnvironment() const OVERRIDE {
6079 // Instruction may throw a CheckCastError.
6080 return true;
6081 }
6082
6083 bool CanThrow() const OVERRIDE { return true; }
6084
Vladimir Markoa1de9182016-02-25 11:37:38 +00006085 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6086 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6087 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6088 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006089
6090 DECLARE_INSTRUCTION(CheckCast);
6091
6092 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006093 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6094 static constexpr size_t kFieldTypeCheckKindSize =
6095 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6096 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6097 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6098 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6099 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006100
6101 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006102};
6103
Vladimir Markofcb503c2016-05-18 12:48:17 +01006104class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006105 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006106 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006107 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006108 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6109 SetPackedField<BarrierKindField>(barrier_kind);
6110 }
Calin Juravle27df7582015-04-17 19:12:31 +01006111
Vladimir Markoa1de9182016-02-25 11:37:38 +00006112 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006113
6114 DECLARE_INSTRUCTION(MemoryBarrier);
6115
6116 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006117 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6118 static constexpr size_t kFieldBarrierKindSize =
6119 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6120 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6121 kFieldBarrierKind + kFieldBarrierKindSize;
6122 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6123 "Too many packed fields.");
6124 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006125
6126 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6127};
6128
Vladimir Markofcb503c2016-05-18 12:48:17 +01006129class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006130 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006131 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006132 kEnter,
6133 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006134 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006135 };
6136
6137 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006138 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006139 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6140 dex_pc) {
6141 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006142 SetRawInputAt(0, object);
6143 }
6144
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006145 // Instruction may go into runtime, so we need an environment.
6146 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006147
6148 bool CanThrow() const OVERRIDE {
6149 // Verifier guarantees that monitor-exit cannot throw.
6150 // This is important because it allows the HGraphBuilder to remove
6151 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6152 return IsEnter();
6153 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006154
Vladimir Markoa1de9182016-02-25 11:37:38 +00006155 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6156 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006157
6158 DECLARE_INSTRUCTION(MonitorOperation);
6159
Calin Juravle52c48962014-12-16 17:02:57 +00006160 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006161 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6162 static constexpr size_t kFieldOperationKindSize =
6163 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6164 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6165 kFieldOperationKind + kFieldOperationKindSize;
6166 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6167 "Too many packed fields.");
6168 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006169
6170 private:
6171 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6172};
6173
Vladimir Markofcb503c2016-05-18 12:48:17 +01006174class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006175 public:
6176 HSelect(HInstruction* condition,
6177 HInstruction* true_value,
6178 HInstruction* false_value,
6179 uint32_t dex_pc)
6180 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6181 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6182
6183 // First input must be `true_value` or `false_value` to allow codegens to
6184 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6185 // that architectures which implement HSelect as a conditional move also
6186 // will not need to invert the condition.
6187 SetRawInputAt(0, false_value);
6188 SetRawInputAt(1, true_value);
6189 SetRawInputAt(2, condition);
6190 }
6191
6192 HInstruction* GetFalseValue() const { return InputAt(0); }
6193 HInstruction* GetTrueValue() const { return InputAt(1); }
6194 HInstruction* GetCondition() const { return InputAt(2); }
6195
6196 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006197 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6198 return true;
6199 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006200
6201 bool CanBeNull() const OVERRIDE {
6202 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6203 }
6204
6205 DECLARE_INSTRUCTION(Select);
6206
6207 private:
6208 DISALLOW_COPY_AND_ASSIGN(HSelect);
6209};
6210
Vladimir Markof9f64412015-09-02 14:05:49 +01006211class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006212 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006213 MoveOperands(Location source,
6214 Location destination,
6215 Primitive::Type type,
6216 HInstruction* instruction)
6217 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006218
6219 Location GetSource() const { return source_; }
6220 Location GetDestination() const { return destination_; }
6221
6222 void SetSource(Location value) { source_ = value; }
6223 void SetDestination(Location value) { destination_ = value; }
6224
6225 // The parallel move resolver marks moves as "in-progress" by clearing the
6226 // destination (but not the source).
6227 Location MarkPending() {
6228 DCHECK(!IsPending());
6229 Location dest = destination_;
6230 destination_ = Location::NoLocation();
6231 return dest;
6232 }
6233
6234 void ClearPending(Location dest) {
6235 DCHECK(IsPending());
6236 destination_ = dest;
6237 }
6238
6239 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006240 DCHECK(source_.IsValid() || destination_.IsInvalid());
6241 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006242 }
6243
6244 // True if this blocks a move from the given location.
6245 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006246 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006247 }
6248
6249 // A move is redundant if it's been eliminated, if its source and
6250 // destination are the same, or if its destination is unneeded.
6251 bool IsRedundant() const {
6252 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6253 }
6254
6255 // We clear both operands to indicate move that's been eliminated.
6256 void Eliminate() {
6257 source_ = destination_ = Location::NoLocation();
6258 }
6259
6260 bool IsEliminated() const {
6261 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6262 return source_.IsInvalid();
6263 }
6264
Alexey Frunze4dda3372015-06-01 18:31:49 -07006265 Primitive::Type GetType() const { return type_; }
6266
Nicolas Geoffray90218252015-04-15 11:56:51 +01006267 bool Is64BitMove() const {
6268 return Primitive::Is64BitType(type_);
6269 }
6270
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006271 HInstruction* GetInstruction() const { return instruction_; }
6272
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006273 private:
6274 Location source_;
6275 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006276 // The type this move is for.
6277 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006278 // The instruction this move is assocatied with. Null when this move is
6279 // for moving an input in the expected locations of user (including a phi user).
6280 // This is only used in debug mode, to ensure we do not connect interval siblings
6281 // in the same parallel move.
6282 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006283};
6284
Roland Levillainc9285912015-12-18 10:38:42 +00006285std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6286
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006287static constexpr size_t kDefaultNumberOfMoves = 4;
6288
Vladimir Markofcb503c2016-05-18 12:48:17 +01006289class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006290 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006291 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006292 : HTemplateInstruction(SideEffects::None(), dex_pc),
6293 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6294 moves_.reserve(kDefaultNumberOfMoves);
6295 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006296
Nicolas Geoffray90218252015-04-15 11:56:51 +01006297 void AddMove(Location source,
6298 Location destination,
6299 Primitive::Type type,
6300 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006301 DCHECK(source.IsValid());
6302 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006303 if (kIsDebugBuild) {
6304 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006305 for (const MoveOperands& move : moves_) {
6306 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006307 // Special case the situation where the move is for the spill slot
6308 // of the instruction.
6309 if ((GetPrevious() == instruction)
6310 || ((GetPrevious() == nullptr)
6311 && instruction->IsPhi()
6312 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006313 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006314 << "Doing parallel moves for the same instruction.";
6315 } else {
6316 DCHECK(false) << "Doing parallel moves for the same instruction.";
6317 }
6318 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006319 }
6320 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006321 for (const MoveOperands& move : moves_) {
6322 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006323 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006324 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006325 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006326 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006327 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006328 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006329 }
6330
Vladimir Marko225b6462015-09-28 12:17:40 +01006331 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006332 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006333 }
6334
Vladimir Marko225b6462015-09-28 12:17:40 +01006335 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006336
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006337 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006338
6339 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006340 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006341
6342 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6343};
6344
Mark Mendell0616ae02015-04-17 12:49:27 -04006345} // namespace art
6346
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006347#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6348#include "nodes_shared.h"
6349#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006350#ifdef ART_ENABLE_CODEGEN_arm
6351#include "nodes_arm.h"
6352#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006353#ifdef ART_ENABLE_CODEGEN_arm64
6354#include "nodes_arm64.h"
6355#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006356#ifdef ART_ENABLE_CODEGEN_x86
6357#include "nodes_x86.h"
6358#endif
6359
6360namespace art {
6361
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006362class HGraphVisitor : public ValueObject {
6363 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006364 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6365 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006366
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006367 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006368 virtual void VisitBasicBlock(HBasicBlock* block);
6369
Roland Levillain633021e2014-10-01 14:12:25 +01006370 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006371 void VisitInsertionOrder();
6372
Roland Levillain633021e2014-10-01 14:12:25 +01006373 // Visit the graph following dominator tree reverse post-order.
6374 void VisitReversePostOrder();
6375
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006376 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006377
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006378 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006379#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006380 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6381
6382 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6383
6384#undef DECLARE_VISIT_INSTRUCTION
6385
6386 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006387 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006388
6389 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6390};
6391
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006392class HGraphDelegateVisitor : public HGraphVisitor {
6393 public:
6394 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6395 virtual ~HGraphDelegateVisitor() {}
6396
6397 // Visit functions that delegate to to super class.
6398#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006399 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006400
6401 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6402
6403#undef DECLARE_VISIT_INSTRUCTION
6404
6405 private:
6406 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6407};
6408
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006409class HInsertionOrderIterator : public ValueObject {
6410 public:
6411 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6412
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006413 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006414 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006415 void Advance() { ++index_; }
6416
6417 private:
6418 const HGraph& graph_;
6419 size_t index_;
6420
6421 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6422};
6423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006424class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006425 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006426 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6427 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006428 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006429 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006430
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006431 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6432 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006433 void Advance() { ++index_; }
6434
6435 private:
6436 const HGraph& graph_;
6437 size_t index_;
6438
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006439 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006440};
6441
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006442class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006443 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006444 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006445 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006446 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006447 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006448 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006449
6450 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006451 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006452 void Advance() { --index_; }
6453
6454 private:
6455 const HGraph& graph_;
6456 size_t index_;
6457
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006458 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006459};
6460
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006461class HLinearPostOrderIterator : public ValueObject {
6462 public:
6463 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006464 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006465
6466 bool Done() const { return index_ == 0; }
6467
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006468 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006469
6470 void Advance() {
6471 --index_;
6472 DCHECK_GE(index_, 0U);
6473 }
6474
6475 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006476 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006477 size_t index_;
6478
6479 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6480};
6481
6482class HLinearOrderIterator : public ValueObject {
6483 public:
6484 explicit HLinearOrderIterator(const HGraph& graph)
6485 : order_(graph.GetLinearOrder()), index_(0) {}
6486
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006487 bool Done() const { return index_ == order_.size(); }
6488 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006489 void Advance() { ++index_; }
6490
6491 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006492 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006493 size_t index_;
6494
6495 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6496};
6497
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006498// Iterator over the blocks that art part of the loop. Includes blocks part
6499// of an inner loop. The order in which the blocks are iterated is on their
6500// block id.
6501class HBlocksInLoopIterator : public ValueObject {
6502 public:
6503 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6504 : blocks_in_loop_(info.GetBlocks()),
6505 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6506 index_(0) {
6507 if (!blocks_in_loop_.IsBitSet(index_)) {
6508 Advance();
6509 }
6510 }
6511
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006512 bool Done() const { return index_ == blocks_.size(); }
6513 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006514 void Advance() {
6515 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006516 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006517 if (blocks_in_loop_.IsBitSet(index_)) {
6518 break;
6519 }
6520 }
6521 }
6522
6523 private:
6524 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006525 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006526 size_t index_;
6527
6528 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6529};
6530
Mingyao Yang3584bce2015-05-19 16:01:59 -07006531// Iterator over the blocks that art part of the loop. Includes blocks part
6532// of an inner loop. The order in which the blocks are iterated is reverse
6533// post order.
6534class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6535 public:
6536 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6537 : blocks_in_loop_(info.GetBlocks()),
6538 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6539 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006540 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006541 Advance();
6542 }
6543 }
6544
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006545 bool Done() const { return index_ == blocks_.size(); }
6546 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006547 void Advance() {
6548 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006549 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6550 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006551 break;
6552 }
6553 }
6554 }
6555
6556 private:
6557 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006558 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006559 size_t index_;
6560
6561 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6562};
6563
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006564inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006565 if (constant->IsIntConstant()) {
6566 return constant->AsIntConstant()->GetValue();
6567 } else if (constant->IsLongConstant()) {
6568 return constant->AsLongConstant()->GetValue();
6569 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006570 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006571 return 0;
6572 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006573}
6574
Vladimir Marko58155012015-08-19 12:49:41 +00006575inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6576 // For the purposes of the compiler, the dex files must actually be the same object
6577 // if we want to safely treat them as the same. This is especially important for JIT
6578 // as custom class loaders can open the same underlying file (or memory) multiple
6579 // times and provide different class resolution but no two class loaders should ever
6580 // use the same DexFile object - doing so is an unsupported hack that can lead to
6581 // all sorts of weird failures.
6582 return &lhs == &rhs;
6583}
6584
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006585#define INSTRUCTION_TYPE_CHECK(type, super) \
6586 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6587 inline const H##type* HInstruction::As##type() const { \
6588 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6589 } \
6590 inline H##type* HInstruction::As##type() { \
6591 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6592 }
6593
6594 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6595#undef INSTRUCTION_TYPE_CHECK
6596
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006597// Create space in `blocks` for adding `number_of_new_blocks` entries
6598// starting at location `at`. Blocks after `at` are moved accordingly.
6599inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6600 size_t number_of_new_blocks,
6601 size_t after) {
6602 DCHECK_LT(after, blocks->size());
6603 size_t old_size = blocks->size();
6604 size_t new_size = old_size + number_of_new_blocks;
6605 blocks->resize(new_size);
6606 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6607}
6608
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006609} // namespace art
6610
6611#endif // ART_COMPILER_OPTIMIZING_NODES_H_