blob: 74a4acae3fb4374524f7887dcff1e72937fb0a31 [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
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010088inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
89 // For the purposes of the compiler, the dex files must actually be the same object
90 // if we want to safely treat them as the same. This is especially important for JIT
91 // as custom class loaders can open the same underlying file (or memory) multiple
92 // times and provide different class resolution but no two class loaders should ever
93 // use the same DexFile object - doing so is an unsupported hack that can lead to
94 // all sorts of weird failures.
95 return &lhs == &rhs;
96}
97
Dave Allison20dfc792014-06-16 20:44:29 -070098enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070099 // All types.
100 kCondEQ, // ==
101 kCondNE, // !=
102 // Signed integers and floating-point numbers.
103 kCondLT, // <
104 kCondLE, // <=
105 kCondGT, // >
106 kCondGE, // >=
107 // Unsigned integers.
108 kCondB, // <
109 kCondBE, // <=
110 kCondA, // >
111 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700112};
113
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000114enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000115 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000116 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000117 kAnalysisFailThrowCatchLoop,
118 kAnalysisFailAmbiguousArrayOp,
119 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000120};
121
Vladimir Markof9f64412015-09-02 14:05:49 +0100122class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100123 public:
124 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
125
126 void AddInstruction(HInstruction* instruction);
127 void RemoveInstruction(HInstruction* instruction);
128
David Brazdilc3d743f2015-04-22 13:40:50 +0100129 // Insert `instruction` before/after an existing instruction `cursor`.
130 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
131 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
132
Roland Levillain6b469232014-09-25 10:10:38 +0100133 // Return true if this list contains `instruction`.
134 bool Contains(HInstruction* instruction) const;
135
Roland Levillainccc07a92014-09-16 14:48:16 +0100136 // Return true if `instruction1` is found before `instruction2` in
137 // this instruction list and false otherwise. Abort if none
138 // of these instructions is found.
139 bool FoundBefore(const HInstruction* instruction1,
140 const HInstruction* instruction2) const;
141
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000142 bool IsEmpty() const { return first_instruction_ == nullptr; }
143 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
144
145 // Update the block of all instructions to be `block`.
146 void SetBlockOfInstructions(HBasicBlock* block) const;
147
148 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000149 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000150 void Add(const HInstructionList& instruction_list);
151
David Brazdil2d7352b2015-04-20 14:52:42 +0100152 // Return the number of instructions in the list. This is an expensive operation.
153 size_t CountSize() const;
154
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100155 private:
156 HInstruction* first_instruction_;
157 HInstruction* last_instruction_;
158
159 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 friend class HGraph;
161 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100162 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100164
165 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
166};
167
David Brazdil4833f5a2015-12-16 10:37:39 +0000168class ReferenceTypeInfo : ValueObject {
169 public:
170 typedef Handle<mirror::Class> TypeHandle;
171
Vladimir Markoa1de9182016-02-25 11:37:38 +0000172 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
173
174 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000175 return ReferenceTypeInfo(type_handle, is_exact);
176 }
177
178 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
179
Vladimir Markof39745e2016-01-26 12:16:55 +0000180 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000181 return handle.GetReference() != nullptr;
182 }
183
Vladimir Marko456307a2016-04-19 14:12:13 +0000184 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000185 return IsValidHandle(type_handle_);
186 }
187
188 bool IsExact() const { return is_exact_; }
189
190 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
191 DCHECK(IsValid());
192 return GetTypeHandle()->IsObjectClass();
193 }
194
195 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
196 DCHECK(IsValid());
197 return GetTypeHandle()->IsStringClass();
198 }
199
200 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
201 DCHECK(IsValid());
202 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
203 }
204
205 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
206 DCHECK(IsValid());
207 return GetTypeHandle()->IsInterface();
208 }
209
210 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
211 DCHECK(IsValid());
212 return GetTypeHandle()->IsArrayClass();
213 }
214
215 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
216 DCHECK(IsValid());
217 return GetTypeHandle()->IsPrimitiveArray();
218 }
219
220 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
221 DCHECK(IsValid());
222 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
223 }
224
225 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
226 DCHECK(IsValid());
227 if (!IsExact()) return false;
228 if (!IsArrayClass()) return false;
229 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
230 }
231
232 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
233 DCHECK(IsValid());
234 if (!IsExact()) return false;
235 if (!IsArrayClass()) return false;
236 if (!rti.IsArrayClass()) return false;
237 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
238 rti.GetTypeHandle()->GetComponentType());
239 }
240
241 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
242
243 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
244 DCHECK(IsValid());
245 DCHECK(rti.IsValid());
246 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
247 }
248
249 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
250 DCHECK(IsValid());
251 DCHECK(rti.IsValid());
252 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
253 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
254 }
255
256 // Returns true if the type information provide the same amount of details.
257 // Note that it does not mean that the instructions have the same actual type
258 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000259 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000260 if (!IsValid() && !rti.IsValid()) {
261 // Invalid types are equal.
262 return true;
263 }
264 if (!IsValid() || !rti.IsValid()) {
265 // One is valid, the other not.
266 return false;
267 }
268 return IsExact() == rti.IsExact()
269 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
270 }
271
272 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000273 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
274 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
275 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000276
277 // The class of the object.
278 TypeHandle type_handle_;
279 // Whether or not the type is exact or a superclass of the actual type.
280 // Whether or not we have any information about this type.
281 bool is_exact_;
282};
283
284std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
285
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000286// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100287class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000288 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100289 HGraph(ArenaAllocator* arena,
290 const DexFile& dex_file,
291 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100292 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100294 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100295 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000296 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100297 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000298 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100299 blocks_(arena->Adapter(kArenaAllocBlockList)),
300 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
301 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700302 entry_block_(nullptr),
303 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100304 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100305 number_of_vregs_(0),
306 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000307 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400308 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000309 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000310 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000311 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000312 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 dex_file_(dex_file),
314 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100315 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100316 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100317 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000319 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100320 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
321 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
322 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
323 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000324 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000325 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
326 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100327 blocks_.reserve(kDefaultNumberOfBlocks);
328 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000329
David Brazdilbadd8262016-02-02 16:28:56 +0000330 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
331 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
332
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000333 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100334 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
335
David Brazdil69ba7b72015-06-23 18:27:30 +0100336 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000337 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100338
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000339 HBasicBlock* GetEntryBlock() const { return entry_block_; }
340 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100341 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000342
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000343 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
344 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000345
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100347
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100348 void ComputeDominanceInformation();
349 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000350 void ClearLoopInformation();
351 void FindBackEdges(ArenaBitVector* visited);
352 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100353 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100354 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355
David Brazdil4833f5a2015-12-16 10:37:39 +0000356 // Analyze all natural loops in this graph. Returns a code specifying that it
357 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000358 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000359 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100360
David Brazdilffee3d32015-07-06 11:48:53 +0100361 // Iterate over blocks to compute try block membership. Needs reverse post
362 // order and loop information.
363 void ComputeTryBlockInformation();
364
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000365 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000366 // Returns the instruction to replace the invoke expression or null if the
367 // invoke is for a void method. Note that the caller is responsible for replacing
368 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000369 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000370
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000371 // Update the loop and try membership of `block`, which was spawned from `reference`.
372 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
373 // should be the new back edge.
374 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
375 HBasicBlock* reference,
376 bool replace_if_back_edge);
377
Mingyao Yang3584bce2015-05-19 16:01:59 -0700378 // Need to add a couple of blocks to test if the loop body is entered and
379 // put deoptimization instructions, etc.
380 void TransformLoopHeaderForBCE(HBasicBlock* header);
381
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000382 // Removes `block` from the graph. Assumes `block` has been disconnected from
383 // other blocks and has no instructions or phis.
384 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000385
David Brazdilfc6a86a2015-06-26 10:33:45 +0000386 // Splits the edge between `block` and `successor` while preserving the
387 // indices in the predecessor/successor lists. If there are multiple edges
388 // between the blocks, the lowest indices are used.
389 // Returns the new block which is empty and has the same dex pc as `successor`.
390 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
391
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100392 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
393 void SimplifyLoop(HBasicBlock* header);
394
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000395 int32_t GetNextInstructionId() {
396 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000397 return current_instruction_id_++;
398 }
399
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000400 int32_t GetCurrentInstructionId() const {
401 return current_instruction_id_;
402 }
403
404 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000405 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000406 current_instruction_id_ = id;
407 }
408
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100409 uint16_t GetMaximumNumberOfOutVRegs() const {
410 return maximum_number_of_out_vregs_;
411 }
412
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000413 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
414 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100415 }
416
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100417 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
418 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
419 }
420
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000421 void UpdateTemporariesVRegSlots(size_t slots) {
422 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100423 }
424
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000425 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100426 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000427 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100428 }
429
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100430 void SetNumberOfVRegs(uint16_t number_of_vregs) {
431 number_of_vregs_ = number_of_vregs;
432 }
433
434 uint16_t GetNumberOfVRegs() const {
435 return number_of_vregs_;
436 }
437
438 void SetNumberOfInVRegs(uint16_t value) {
439 number_of_in_vregs_ = value;
440 }
441
David Brazdildee58d62016-04-07 09:54:26 +0000442 uint16_t GetNumberOfInVRegs() const {
443 return number_of_in_vregs_;
444 }
445
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100446 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100447 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100448 return number_of_vregs_ - number_of_in_vregs_;
449 }
450
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100451 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100452 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100453 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100454
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100455 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100456 return linear_order_;
457 }
458
Mark Mendell1152c922015-04-24 17:06:35 -0400459 bool HasBoundsChecks() const {
460 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800461 }
462
Mark Mendell1152c922015-04-24 17:06:35 -0400463 void SetHasBoundsChecks(bool value) {
464 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800465 }
466
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100467 bool ShouldGenerateConstructorBarrier() const {
468 return should_generate_constructor_barrier_;
469 }
470
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000471 bool IsDebuggable() const { return debuggable_; }
472
David Brazdil8d5b8b22015-03-24 10:51:52 +0000473 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000474 // already, it is created and inserted into the graph. This method is only for
475 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600476 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000477
478 // TODO: This is problematic for the consistency of reference type propagation
479 // because it can be created anytime after the pass and thus it will be left
480 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600481 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000482
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600483 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
484 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000485 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600486 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
487 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000488 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600489 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
490 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000491 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600492 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
493 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000494 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000495
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100496 HCurrentMethod* GetCurrentMethod();
497
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100498 const DexFile& GetDexFile() const {
499 return dex_file_;
500 }
501
502 uint32_t GetMethodIdx() const {
503 return method_idx_;
504 }
505
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100506 InvokeType GetInvokeType() const {
507 return invoke_type_;
508 }
509
Mark Mendellc4701932015-04-10 13:18:51 -0400510 InstructionSet GetInstructionSet() const {
511 return instruction_set_;
512 }
513
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000514 bool IsCompilingOsr() const { return osr_; }
515
David Brazdil77a48ae2015-09-15 12:34:04 +0000516 bool HasTryCatch() const { return has_try_catch_; }
517 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100518
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000519 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
520 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
521
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100522 ArtMethod* GetArtMethod() const { return art_method_; }
523 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
524
Mark Mendellf6529172015-11-17 11:16:56 -0500525 // Returns an instruction with the opposite boolean value from 'cond'.
526 // The instruction has been inserted into the graph, either as a constant, or
527 // before cursor.
528 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
529
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000530 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
531
David Brazdil2d7352b2015-04-20 14:52:42 +0100532 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000533 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100534 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000535
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000536 template <class InstructionType, typename ValueType>
537 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600538 ArenaSafeMap<ValueType, InstructionType*>* cache,
539 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000540 // Try to find an existing constant of the given value.
541 InstructionType* constant = nullptr;
542 auto cached_constant = cache->find(value);
543 if (cached_constant != cache->end()) {
544 constant = cached_constant->second;
545 }
546
547 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100548 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000549 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600550 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000551 cache->Overwrite(value, constant);
552 InsertConstant(constant);
553 }
554 return constant;
555 }
556
David Brazdil8d5b8b22015-03-24 10:51:52 +0000557 void InsertConstant(HConstant* instruction);
558
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000559 // Cache a float constant into the graph. This method should only be
560 // called by the SsaBuilder when creating "equivalent" instructions.
561 void CacheFloatConstant(HFloatConstant* constant);
562
563 // See CacheFloatConstant comment.
564 void CacheDoubleConstant(HDoubleConstant* constant);
565
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000566 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567
568 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100569 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000570
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100572 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000573
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100574 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100575 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100576
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000577 HBasicBlock* entry_block_;
578 HBasicBlock* exit_block_;
579
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100580 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100581 uint16_t maximum_number_of_out_vregs_;
582
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100583 // The number of virtual registers in this method. Contains the parameters.
584 uint16_t number_of_vregs_;
585
586 // The number of virtual registers used by parameters of this method.
587 uint16_t number_of_in_vregs_;
588
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000589 // Number of vreg size slots that the temporaries use (used in baseline compiler).
590 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100591
Mark Mendell1152c922015-04-24 17:06:35 -0400592 // Has bounds checks. We can totally skip BCE if it's false.
593 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800594
David Brazdil77a48ae2015-09-15 12:34:04 +0000595 // Flag whether there are any try/catch blocks in the graph. We will skip
596 // try/catch-related passes if false.
597 bool has_try_catch_;
598
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000599 // Flag whether there are any irreducible loops in the graph.
600 bool has_irreducible_loops_;
601
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000602 // Indicates whether the graph should be compiled in a way that
603 // ensures full debuggability. If false, we can apply more
604 // aggressive optimizations that may limit the level of debugging.
605 const bool debuggable_;
606
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000607 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000608 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000609
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100610 // The dex file from which the method is from.
611 const DexFile& dex_file_;
612
613 // The method index in the dex file.
614 const uint32_t method_idx_;
615
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100616 // If inlined, this encodes how the callee is being invoked.
617 const InvokeType invoke_type_;
618
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100619 // Whether the graph has been transformed to SSA form. Only used
620 // in debug mode to ensure we are not using properties only valid
621 // for non-SSA form (like the number of temporaries).
622 bool in_ssa_form_;
623
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100624 const bool should_generate_constructor_barrier_;
625
Mathieu Chartiere401d142015-04-22 13:56:20 -0700626 const InstructionSet instruction_set_;
627
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000628 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000629 HNullConstant* cached_null_constant_;
630 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000631 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000632 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000633 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000634
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100635 HCurrentMethod* cached_current_method_;
636
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100637 // The ArtMethod this graph is for. Note that for AOT, it may be null,
638 // for example for methods whose declaring class could not be resolved
639 // (such as when the superclass could not be found).
640 ArtMethod* art_method_;
641
David Brazdil4833f5a2015-12-16 10:37:39 +0000642 // Keep the RTI of inexact Object to avoid having to pass stack handle
643 // collection pointer to passes which may create NullConstant.
644 ReferenceTypeInfo inexact_object_rti_;
645
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000646 // Whether we are compiling this graph for on stack replacement: this will
647 // make all loops seen as irreducible and emit special stack maps to mark
648 // compiled code entries which the interpreter can directly jump to.
649 const bool osr_;
650
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000651 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100652 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000653 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000654 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000655 DISALLOW_COPY_AND_ASSIGN(HGraph);
656};
657
Vladimir Markof9f64412015-09-02 14:05:49 +0100658class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000659 public:
660 HLoopInformation(HBasicBlock* header, HGraph* graph)
661 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100662 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000663 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100664 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100665 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100666 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000667 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100668 back_edges_.reserve(kDefaultNumberOfBackEdges);
669 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000671 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100672 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000673
674 void Dump(std::ostream& os);
675
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 HBasicBlock* GetHeader() const {
677 return header_;
678 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000679
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000680 void SetHeader(HBasicBlock* block) {
681 header_ = block;
682 }
683
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100684 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
685 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
686 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
687
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000688 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100689 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000690 }
691
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100692 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100693 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 }
695
David Brazdil46e2a392015-03-16 17:31:52 +0000696 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100697 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100698 }
699
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000700 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100701 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702 }
703
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100704 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100705
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100706 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100707 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100708 }
709
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100710 // Returns the lifetime position of the back edge that has the
711 // greatest lifetime position.
712 size_t GetLifetimeEnd() const;
713
714 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100715 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100716 }
717
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000718 // Finds blocks that are part of this loop.
719 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100720
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100721 // Returns whether this loop information contains `block`.
722 // Note that this loop information *must* be populated before entering this function.
723 bool Contains(const HBasicBlock& block) const;
724
725 // Returns whether this loop information is an inner loop of `other`.
726 // Note that `other` *must* be populated before entering this function.
727 bool IsIn(const HLoopInformation& other) const;
728
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800729 // Returns true if instruction is not defined within this loop.
730 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700731
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100732 const ArenaBitVector& GetBlocks() const { return blocks_; }
733
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000734 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000735 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000736
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000737 void ClearAllBlocks() {
738 blocks_.ClearAllBits();
739 }
740
David Brazdil3f4a5222016-05-06 12:46:21 +0100741 bool HasBackEdgeNotDominatedByHeader() const;
742
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100743 bool IsPopulated() const {
744 return blocks_.GetHighestBitSet() != -1;
745 }
746
Anton Shaminf89381f2016-05-16 16:44:13 +0600747 bool DominatesAllBackEdges(HBasicBlock* block);
748
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000749 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100750 // Internal recursive implementation of `Populate`.
751 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100752 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100753
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000754 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100755 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000756 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100757 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100758 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000760
761 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
762};
763
David Brazdilec16f792015-08-19 15:04:01 +0100764// Stores try/catch information for basic blocks.
765// Note that HGraph is constructed so that catch blocks cannot simultaneously
766// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100767class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100768 public:
769 // Try block information constructor.
770 explicit TryCatchInformation(const HTryBoundary& try_entry)
771 : try_entry_(&try_entry),
772 catch_dex_file_(nullptr),
773 catch_type_index_(DexFile::kDexNoIndex16) {
774 DCHECK(try_entry_ != nullptr);
775 }
776
777 // Catch block information constructor.
778 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
779 : try_entry_(nullptr),
780 catch_dex_file_(&dex_file),
781 catch_type_index_(catch_type_index) {}
782
783 bool IsTryBlock() const { return try_entry_ != nullptr; }
784
785 const HTryBoundary& GetTryEntry() const {
786 DCHECK(IsTryBlock());
787 return *try_entry_;
788 }
789
790 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
791
792 bool IsCatchAllTypeIndex() const {
793 DCHECK(IsCatchBlock());
794 return catch_type_index_ == DexFile::kDexNoIndex16;
795 }
796
797 uint16_t GetCatchTypeIndex() const {
798 DCHECK(IsCatchBlock());
799 return catch_type_index_;
800 }
801
802 const DexFile& GetCatchDexFile() const {
803 DCHECK(IsCatchBlock());
804 return *catch_dex_file_;
805 }
806
807 private:
808 // One of possibly several TryBoundary instructions entering the block's try.
809 // Only set for try blocks.
810 const HTryBoundary* try_entry_;
811
812 // Exception type information. Only set for catch blocks.
813 const DexFile* catch_dex_file_;
814 const uint16_t catch_type_index_;
815};
816
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100817static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100818static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100819
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000820// A block in a method. Contains the list of instructions represented
821// as a double linked list. Each block knows its predecessors and
822// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100823
Vladimir Markof9f64412015-09-02 14:05:49 +0100824class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000825 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600826 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000827 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000828 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
829 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000830 loop_information_(nullptr),
831 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000832 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100833 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100834 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100835 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000836 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000837 try_catch_information_(nullptr) {
838 predecessors_.reserve(kDefaultNumberOfPredecessors);
839 successors_.reserve(kDefaultNumberOfSuccessors);
840 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
841 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000842
Vladimir Marko60584552015-09-03 13:35:12 +0000843 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100844 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000845 }
846
Vladimir Marko60584552015-09-03 13:35:12 +0000847 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100848 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000849 }
850
David Brazdild26a4112015-11-10 11:07:31 +0000851 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
852 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
853
Vladimir Marko60584552015-09-03 13:35:12 +0000854 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
855 return ContainsElement(successors_, block, start_from);
856 }
857
858 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100859 return dominated_blocks_;
860 }
861
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100862 bool IsEntryBlock() const {
863 return graph_->GetEntryBlock() == this;
864 }
865
866 bool IsExitBlock() const {
867 return graph_->GetExitBlock() == this;
868 }
869
David Brazdil46e2a392015-03-16 17:31:52 +0000870 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000871 bool IsSingleTryBoundary() const;
872
873 // Returns true if this block emits nothing but a jump.
874 bool IsSingleJump() const {
875 HLoopInformation* loop_info = GetLoopInformation();
876 return (IsSingleGoto() || IsSingleTryBoundary())
877 // Back edges generate a suspend check.
878 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
879 }
David Brazdil46e2a392015-03-16 17:31:52 +0000880
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000881 void AddBackEdge(HBasicBlock* back_edge) {
882 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000883 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000884 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100885 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000886 loop_information_->AddBackEdge(back_edge);
887 }
888
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000889 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000890 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000891
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100892 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000893 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600894 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000896 HBasicBlock* GetDominator() const { return dominator_; }
897 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000898 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
899
900 void RemoveDominatedBlock(HBasicBlock* block) {
901 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100902 }
Vladimir Marko60584552015-09-03 13:35:12 +0000903
904 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
905 ReplaceElement(dominated_blocks_, existing, new_block);
906 }
907
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100908 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000909
910 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100911 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000912 }
913
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100914 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
915 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100916 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100917 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100918 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
919 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000920
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000921 HInstruction* GetFirstInstructionDisregardMoves() const;
922
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000923 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000924 successors_.push_back(block);
925 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000926 }
927
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100928 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
929 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100930 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000931 new_block->predecessors_.push_back(this);
932 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000933 }
934
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000935 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
936 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000937 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000938 new_block->successors_.push_back(this);
939 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000940 }
941
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100942 // Insert `this` between `predecessor` and `successor. This method
943 // preserves the indicies, and will update the first edge found between
944 // `predecessor` and `successor`.
945 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
946 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100947 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000948 successor->predecessors_[predecessor_index] = this;
949 predecessor->successors_[successor_index] = this;
950 successors_.push_back(successor);
951 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100952 }
953
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100954 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000955 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100956 }
957
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000958 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000960 }
961
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100962 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000963 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100964 }
965
966 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000967 predecessors_.push_back(block);
968 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100969 }
970
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100971 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000972 DCHECK_EQ(predecessors_.size(), 2u);
973 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100974 }
975
David Brazdil769c9e52015-04-27 13:54:09 +0100976 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000977 DCHECK_EQ(successors_.size(), 2u);
978 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100979 }
980
David Brazdilfc6a86a2015-06-26 10:33:45 +0000981 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000982 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100983 }
984
David Brazdilfc6a86a2015-06-26 10:33:45 +0000985 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000986 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100987 }
988
David Brazdilfc6a86a2015-06-26 10:33:45 +0000989 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000990 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100991 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000992 }
993
994 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000995 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100996 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000997 }
998
999 // Returns whether the first occurrence of `predecessor` in the list of
1000 // predecessors is at index `idx`.
1001 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001002 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001003 return GetPredecessorIndexOf(predecessor) == idx;
1004 }
1005
David Brazdild7558da2015-09-22 13:04:14 +01001006 // Create a new block between this block and its predecessors. The new block
1007 // is added to the graph, all predecessor edges are relinked to it and an edge
1008 // is created to `this`. Returns the new empty block. Reverse post order or
1009 // loop and try/catch information are not updated.
1010 HBasicBlock* CreateImmediateDominator();
1011
David Brazdilfc6a86a2015-06-26 10:33:45 +00001012 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001013 // created, latter block. Note that this method will add the block to the
1014 // graph, create a Goto at the end of the former block and will create an edge
1015 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001016 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001017 HBasicBlock* SplitBefore(HInstruction* cursor);
1018
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001019 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001020 // created block. Note that this method just updates raw block information,
1021 // like predecessors, successors, dominators, and instruction list. It does not
1022 // update the graph, reverse post order, loop information, nor make sure the
1023 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001024 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1025
1026 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1027 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001028
1029 // Merge `other` at the end of `this`. Successors and dominated blocks of
1030 // `other` are changed to be successors and dominated blocks of `this`. Note
1031 // that this method does not update the graph, reverse post order, loop
1032 // information, nor make sure the blocks are consistent (for example ending
1033 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001034 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001035
1036 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1037 // of `this` are moved to `other`.
1038 // Note that this method does not update the graph, reverse post order, loop
1039 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001040 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001041 void ReplaceWith(HBasicBlock* other);
1042
David Brazdil2d7352b2015-04-20 14:52:42 +01001043 // Merge `other` at the end of `this`. This method updates loops, reverse post
1044 // order, links to predecessors, successors, dominators and deletes the block
1045 // from the graph. The two blocks must be successive, i.e. `this` the only
1046 // predecessor of `other` and vice versa.
1047 void MergeWith(HBasicBlock* other);
1048
1049 // Disconnects `this` from all its predecessors, successors and dominator,
1050 // removes it from all loops it is included in and eventually from the graph.
1051 // The block must not dominate any other block. Predecessors and successors
1052 // are safely updated.
1053 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001054
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001056 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001057 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001058 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001059 // Replace instruction `initial` with `replacement` within this block.
1060 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1061 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001062 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001063 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001064 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1065 // instruction list. With 'ensure_safety' set to true, it verifies that the
1066 // instruction is not in use and removes it from the use lists of its inputs.
1067 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1068 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001069 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001070
1071 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001072 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001073 }
1074
Roland Levillain6b879dd2014-09-22 17:13:44 +01001075 bool IsLoopPreHeaderFirstPredecessor() const {
1076 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001077 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001078 }
1079
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001080 bool IsFirstPredecessorBackEdge() const {
1081 DCHECK(IsLoopHeader());
1082 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1083 }
1084
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001085 HLoopInformation* GetLoopInformation() const {
1086 return loop_information_;
1087 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001088
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001089 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001090 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001091 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001092 void SetInLoop(HLoopInformation* info) {
1093 if (IsLoopHeader()) {
1094 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001095 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001096 loop_information_ = info;
1097 } else if (loop_information_->Contains(*info->GetHeader())) {
1098 // Block is currently part of an outer loop. Make it part of this inner loop.
1099 // Note that a non loop header having a loop information means this loop information
1100 // has already been populated
1101 loop_information_ = info;
1102 } else {
1103 // Block is part of an inner loop. Do not update the loop information.
1104 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1105 // at this point, because this method is being called while populating `info`.
1106 }
1107 }
1108
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001109 // Raw update of the loop information.
1110 void SetLoopInformation(HLoopInformation* info) {
1111 loop_information_ = info;
1112 }
1113
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001114 bool IsInLoop() const { return loop_information_ != nullptr; }
1115
David Brazdilec16f792015-08-19 15:04:01 +01001116 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1117
1118 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1119 try_catch_information_ = try_catch_information;
1120 }
1121
1122 bool IsTryBlock() const {
1123 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1124 }
1125
1126 bool IsCatchBlock() const {
1127 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1128 }
David Brazdilffee3d32015-07-06 11:48:53 +01001129
1130 // Returns the try entry that this block's successors should have. They will
1131 // be in the same try, unless the block ends in a try boundary. In that case,
1132 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001133 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001134
David Brazdild7558da2015-09-22 13:04:14 +01001135 bool HasThrowingInstructions() const;
1136
David Brazdila4b8c212015-05-07 09:59:30 +01001137 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001138 bool Dominates(HBasicBlock* block) const;
1139
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001140 size_t GetLifetimeStart() const { return lifetime_start_; }
1141 size_t GetLifetimeEnd() const { return lifetime_end_; }
1142
1143 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1144 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1145
David Brazdil8d5b8b22015-03-24 10:51:52 +00001146 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001147 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001148 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001149 bool HasSinglePhi() const;
1150
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001151 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001152 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001153 ArenaVector<HBasicBlock*> predecessors_;
1154 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155 HInstructionList instructions_;
1156 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001157 HLoopInformation* loop_information_;
1158 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001159 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001160 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001161 // The dex program counter of the first instruction of this block.
1162 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001163 size_t lifetime_start_;
1164 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001165 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001166
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001167 friend class HGraph;
1168 friend class HInstruction;
1169
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001170 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1171};
1172
David Brazdilb2bd1c52015-03-25 11:17:37 +00001173// Iterates over the LoopInformation of all loops which contain 'block'
1174// from the innermost to the outermost.
1175class HLoopInformationOutwardIterator : public ValueObject {
1176 public:
1177 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1178 : current_(block.GetLoopInformation()) {}
1179
1180 bool Done() const { return current_ == nullptr; }
1181
1182 void Advance() {
1183 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001184 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001185 }
1186
1187 HLoopInformation* Current() const {
1188 DCHECK(!Done());
1189 return current_;
1190 }
1191
1192 private:
1193 HLoopInformation* current_;
1194
1195 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1196};
1197
Alexandre Ramesef20f712015-06-09 10:29:30 +01001198#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001199 M(Above, Condition) \
1200 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001201 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001202 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001203 M(ArrayGet, Instruction) \
1204 M(ArrayLength, Instruction) \
1205 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001206 M(Below, Condition) \
1207 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001208 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001209 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001210 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001211 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001212 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001213 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001214 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001215 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001216 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001217 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001218 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001219 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001220 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Exit, Instruction) \
1223 M(FloatConstant, Constant) \
1224 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 M(GreaterThan, Condition) \
1226 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001227 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001228 M(InstanceFieldGet, Instruction) \
1229 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001230 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001231 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001232 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001233 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001234 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001235 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001236 M(LessThan, Condition) \
1237 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001238 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001239 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001240 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001241 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001242 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001243 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001244 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001245 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001246 M(Neg, UnaryOperation) \
1247 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001248 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001249 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001250 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001251 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001252 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001253 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001254 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001255 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001256 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001257 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001258 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001259 M(Return, Instruction) \
1260 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001261 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001262 M(Shl, BinaryOperation) \
1263 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001264 M(StaticFieldGet, Instruction) \
1265 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 M(UnresolvedInstanceFieldGet, Instruction) \
1267 M(UnresolvedInstanceFieldSet, Instruction) \
1268 M(UnresolvedStaticFieldGet, Instruction) \
1269 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001270 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001271 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001272 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001273 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001274 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001275 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001276 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001277 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001278
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001279/*
1280 * Instructions, shared across several (not all) architectures.
1281 */
1282#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1283#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1284#else
1285#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001286 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001287 M(MultiplyAccumulate, Instruction)
1288#endif
1289
Vladimir Markob4536b72015-11-24 13:45:23 +00001290#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001291#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001292#else
1293#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1294 M(ArmDexCacheArraysBase, Instruction)
1295#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001296
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001297#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001298#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001299#else
1300#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001301 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001302 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001303#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001304
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001305#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1306
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001307#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1308
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001309#ifndef ART_ENABLE_CODEGEN_x86
1310#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1311#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001312#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1313 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001314 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001315 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001316 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001317#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001318
1319#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1320
1321#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1322 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001323 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001324 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1325 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001326 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001327 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001328 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1329 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1330
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001331#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1332 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001333 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001334 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001335 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001336 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001337
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001338#define FOR_EACH_INSTRUCTION(M) \
1339 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1340 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1341
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001342#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001343FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1344#undef FORWARD_DECLARATION
1345
Vladimir Marko372f10e2016-05-17 16:30:10 +01001346#define DECLARE_INSTRUCTION(type) \
1347 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1348 const char* DebugName() const OVERRIDE { return #type; } \
1349 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1350 return other->Is##type(); \
1351 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001352 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001353
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001354#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1355 bool Is##type() const { return As##type() != nullptr; } \
1356 const H##type* As##type() const { return this; } \
1357 H##type* As##type() { return this; }
1358
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001359template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001360class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001361 public:
David Brazdiled596192015-01-23 10:39:45 +00001362 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001363 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001364 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001365
Vladimir Marko46817b82016-03-29 12:21:58 +01001366 // Hook for the IntrusiveForwardList<>.
1367 // TODO: Hide this better.
1368 IntrusiveForwardListHook hook;
1369
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001370 private:
David Brazdiled596192015-01-23 10:39:45 +00001371 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001372 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001373
1374 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001375 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001376
Vladimir Marko46817b82016-03-29 12:21:58 +01001377 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001378
1379 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1380};
1381
David Brazdiled596192015-01-23 10:39:45 +00001382template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001383using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001384
David Brazdil1abb4192015-02-17 18:33:36 +00001385// This class is used by HEnvironment and HInstruction classes to record the
1386// instructions they use and pointers to the corresponding HUseListNodes kept
1387// by the used instructions.
1388template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001389class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001390 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001391 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1392 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001393
Vladimir Marko46817b82016-03-29 12:21:58 +01001394 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1395 : HUserRecord(old_record.instruction_, before_use_node) {}
1396 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1397 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001398 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001399 }
1400
1401 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001402 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1403 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001404
1405 private:
1406 // Instruction used by the user.
1407 HInstruction* instruction_;
1408
Vladimir Marko46817b82016-03-29 12:21:58 +01001409 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1410 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001411};
1412
Aart Bik854a02b2015-07-14 16:07:00 -07001413/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001415 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001416 * For write/read dependences on fields/arrays, the dependence analysis uses
1417 * type disambiguation (e.g. a float field write cannot modify the value of an
1418 * integer field read) and the access type (e.g. a reference array write cannot
1419 * modify the value of a reference field read [although it may modify the
1420 * reference fetch prior to reading the field, which is represented by its own
1421 * write/read dependence]). The analysis makes conservative points-to
1422 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1423 * the same, and any reference read depends on any reference read without
1424 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001425 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001426 * The internal representation uses 38-bit and is described in the table below.
1427 * The first line indicates the side effect, and for field/array accesses the
1428 * second line indicates the type of the access (in the order of the
1429 * Primitive::Type enum).
1430 * The two numbered lines below indicate the bit position in the bitfield (read
1431 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001432 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001433 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1434 * +-------------+---------+---------+--------------+---------+---------+
1435 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1436 * | 3 |333333322|222222221| 1 |111111110|000000000|
1437 * | 7 |654321098|765432109| 8 |765432109|876543210|
1438 *
1439 * Note that, to ease the implementation, 'changes' bits are least significant
1440 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001441 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001442class SideEffects : public ValueObject {
1443 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001444 SideEffects() : flags_(0) {}
1445
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001446 static SideEffects None() {
1447 return SideEffects(0);
1448 }
1449
1450 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 return SideEffects(kAllChangeBits | kAllDependOnBits);
1452 }
1453
1454 static SideEffects AllChanges() {
1455 return SideEffects(kAllChangeBits);
1456 }
1457
1458 static SideEffects AllDependencies() {
1459 return SideEffects(kAllDependOnBits);
1460 }
1461
1462 static SideEffects AllExceptGCDependency() {
1463 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1464 }
1465
1466 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001467 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001468 }
1469
Aart Bik34c3ba92015-07-20 14:08:59 -07001470 static SideEffects AllWrites() {
1471 return SideEffects(kAllWrites);
1472 }
1473
1474 static SideEffects AllReads() {
1475 return SideEffects(kAllReads);
1476 }
1477
1478 static SideEffects FieldWriteOfType(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, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001482 }
1483
Aart Bik854a02b2015-07-14 16:07:00 -07001484 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001485 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001486 }
1487
Aart Bik34c3ba92015-07-20 14:08:59 -07001488 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1489 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001490 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001491 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001492 }
1493
1494 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001495 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001496 }
1497
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001498 static SideEffects CanTriggerGC() {
1499 return SideEffects(1ULL << kCanTriggerGCBit);
1500 }
1501
1502 static SideEffects DependsOnGC() {
1503 return SideEffects(1ULL << kDependsOnGCBit);
1504 }
1505
Aart Bik854a02b2015-07-14 16:07:00 -07001506 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001507 SideEffects Union(SideEffects other) const {
1508 return SideEffects(flags_ | other.flags_);
1509 }
1510
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001511 SideEffects Exclusion(SideEffects other) const {
1512 return SideEffects(flags_ & ~other.flags_);
1513 }
1514
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001515 void Add(SideEffects other) {
1516 flags_ |= other.flags_;
1517 }
1518
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001519 bool Includes(SideEffects other) const {
1520 return (other.flags_ & flags_) == other.flags_;
1521 }
1522
1523 bool HasSideEffects() const {
1524 return (flags_ & kAllChangeBits);
1525 }
1526
1527 bool HasDependencies() const {
1528 return (flags_ & kAllDependOnBits);
1529 }
1530
1531 // Returns true if there are no side effects or dependencies.
1532 bool DoesNothing() const {
1533 return flags_ == 0;
1534 }
1535
Aart Bik854a02b2015-07-14 16:07:00 -07001536 // Returns true if something is written.
1537 bool DoesAnyWrite() const {
1538 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001539 }
1540
Aart Bik854a02b2015-07-14 16:07:00 -07001541 // Returns true if something is read.
1542 bool DoesAnyRead() const {
1543 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001544 }
1545
Aart Bik854a02b2015-07-14 16:07:00 -07001546 // Returns true if potentially everything is written and read
1547 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001548 bool DoesAllReadWrite() const {
1549 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1550 }
1551
Aart Bik854a02b2015-07-14 16:07:00 -07001552 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001554 }
1555
Roland Levillain0d5a2812015-11-13 10:07:31 +00001556 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001557 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001558 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1559 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001560 }
1561
1562 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001563 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001564 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001565 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001566 for (int s = kLastBit; s >= 0; s--) {
1567 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1568 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1569 // This is a bit for the GC side effect.
1570 if (current_bit_is_set) {
1571 flags += "GC";
1572 }
Aart Bik854a02b2015-07-14 16:07:00 -07001573 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001574 } else {
1575 // This is a bit for the array/field analysis.
1576 // The underscore character stands for the 'can trigger GC' bit.
1577 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1578 if (current_bit_is_set) {
1579 flags += kDebug[s];
1580 }
1581 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1582 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1583 flags += "|";
1584 }
1585 }
Aart Bik854a02b2015-07-14 16:07:00 -07001586 }
1587 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001588 }
1589
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001590 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001591
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001592 private:
1593 static constexpr int kFieldArrayAnalysisBits = 9;
1594
1595 static constexpr int kFieldWriteOffset = 0;
1596 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1597 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1598 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1599
1600 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1601
1602 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1603 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1604 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1605 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1606
1607 static constexpr int kLastBit = kDependsOnGCBit;
1608 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1609
1610 // Aliases.
1611
1612 static_assert(kChangeBits == kDependOnBits,
1613 "the 'change' bits should match the 'depend on' bits.");
1614
1615 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1616 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1617 static constexpr uint64_t kAllWrites =
1618 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1619 static constexpr uint64_t kAllReads =
1620 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001621
Aart Bik854a02b2015-07-14 16:07:00 -07001622 // Translates type to bit flag.
1623 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1624 CHECK_NE(type, Primitive::kPrimVoid);
1625 const uint64_t one = 1;
1626 const int shift = type; // 0-based consecutive enum
1627 DCHECK_LE(kFieldWriteOffset, shift);
1628 DCHECK_LT(shift, kArrayWriteOffset);
1629 return one << (type + offset);
1630 }
1631
1632 // Private constructor on direct flags value.
1633 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1634
1635 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001636};
1637
David Brazdiled596192015-01-23 10:39:45 +00001638// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001639class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001640 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001641 HEnvironment(ArenaAllocator* arena,
1642 size_t number_of_vregs,
1643 const DexFile& dex_file,
1644 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001645 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001646 InvokeType invoke_type,
1647 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001648 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1649 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001650 parent_(nullptr),
1651 dex_file_(dex_file),
1652 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001653 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001654 invoke_type_(invoke_type),
1655 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001656 }
1657
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001658 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001659 : HEnvironment(arena,
1660 to_copy.Size(),
1661 to_copy.GetDexFile(),
1662 to_copy.GetMethodIdx(),
1663 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001664 to_copy.GetInvokeType(),
1665 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001666
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001667 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001668 if (parent_ != nullptr) {
1669 parent_->SetAndCopyParentChain(allocator, parent);
1670 } else {
1671 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1672 parent_->CopyFrom(parent);
1673 if (parent->GetParent() != nullptr) {
1674 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1675 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001676 }
David Brazdiled596192015-01-23 10:39:45 +00001677 }
1678
Vladimir Marko71bf8092015-09-15 15:33:14 +01001679 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001680 void CopyFrom(HEnvironment* environment);
1681
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001682 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1683 // input to the loop phi instead. This is for inserting instructions that
1684 // require an environment (like HDeoptimization) in the loop pre-header.
1685 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001686
1687 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001688 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001689 }
1690
1691 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001692 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001693 }
1694
David Brazdil1abb4192015-02-17 18:33:36 +00001695 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001696
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001697 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001698
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001699 HEnvironment* GetParent() const { return parent_; }
1700
1701 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001702 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001703 }
1704
1705 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001706 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001707 }
1708
1709 uint32_t GetDexPc() const {
1710 return dex_pc_;
1711 }
1712
1713 uint32_t GetMethodIdx() const {
1714 return method_idx_;
1715 }
1716
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001717 InvokeType GetInvokeType() const {
1718 return invoke_type_;
1719 }
1720
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001721 const DexFile& GetDexFile() const {
1722 return dex_file_;
1723 }
1724
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001725 HInstruction* GetHolder() const {
1726 return holder_;
1727 }
1728
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001729
1730 bool IsFromInlinedInvoke() const {
1731 return GetParent() != nullptr;
1732 }
1733
David Brazdiled596192015-01-23 10:39:45 +00001734 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001735 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1736 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001737 HEnvironment* parent_;
1738 const DexFile& dex_file_;
1739 const uint32_t method_idx_;
1740 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001741 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001742
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001743 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001744 HInstruction* const holder_;
1745
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001746 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001747
1748 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1749};
1750
Vladimir Markof9f64412015-09-02 14:05:49 +01001751class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001752 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001753 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001754 : previous_(nullptr),
1755 next_(nullptr),
1756 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001757 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001758 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001759 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001760 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001761 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001762 locations_(nullptr),
1763 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001764 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001765 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001766 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1767 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1768 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001769
Dave Allison20dfc792014-06-16 20:44:29 -07001770 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001771
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001772#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001773 enum InstructionKind {
1774 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1775 };
1776#undef DECLARE_KIND
1777
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001778 HInstruction* GetNext() const { return next_; }
1779 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001780
Calin Juravle77520bc2015-01-12 18:45:46 +00001781 HInstruction* GetNextDisregardingMoves() const;
1782 HInstruction* GetPreviousDisregardingMoves() const;
1783
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001784 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001785 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001786 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001787 bool IsInBlock() const { return block_ != nullptr; }
1788 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001789 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1790 bool IsIrreducibleLoopHeaderPhi() const {
1791 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1792 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001793
Vladimir Marko372f10e2016-05-17 16:30:10 +01001794 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1795
1796 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1797 // One virtual method is enough, just const_cast<> and then re-add the const.
1798 return ArrayRef<const HUserRecord<HInstruction*>>(
1799 const_cast<HInstruction*>(this)->GetInputRecords());
1800 }
1801
1802 auto GetInputs() {
1803 return MakeTransformArrayRef(
1804 GetInputRecords(),
1805 [](HUserRecord<HInstruction*>& record) -> HInstruction* {
1806 return record.GetInstruction();
1807 });
1808 }
1809
1810 auto GetInputs() const {
1811 return MakeTransformArrayRef(
1812 GetInputRecords(),
1813 [](const HUserRecord<HInstruction*>& record) -> const HInstruction* {
1814 return record.GetInstruction();
1815 });
1816 }
1817
1818 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001819 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001820
Vladimir Marko372f10e2016-05-17 16:30:10 +01001821 void SetRawInputAt(size_t index, HInstruction* input) {
1822 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1823 }
1824
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001825 virtual void Accept(HGraphVisitor* visitor) = 0;
1826 virtual const char* DebugName() const = 0;
1827
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001828 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1829
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001830 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001831
1832 uint32_t GetDexPc() const { return dex_pc_; }
1833
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001834 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001835
Roland Levillaine161a2a2014-10-03 12:45:18 +01001836 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001837 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001838
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001839 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001840 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001841
Calin Juravle10e244f2015-01-26 18:54:32 +00001842 // Does not apply for all instructions, but having this at top level greatly
1843 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001844 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001845 virtual bool CanBeNull() const {
1846 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1847 return true;
1848 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001849
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001850 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001851 return false;
1852 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001853
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001854 virtual bool IsActualObject() const {
1855 return GetType() == Primitive::kPrimNot;
1856 }
1857
Calin Juravle2e768302015-07-28 14:41:11 +00001858 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001859
Calin Juravle61d544b2015-02-23 16:46:57 +00001860 ReferenceTypeInfo GetReferenceTypeInfo() const {
1861 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001862 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001863 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001864 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001865
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001866 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001867 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001868 // Note: fixup_end remains valid across push_front().
1869 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1870 HUseListNode<HInstruction*>* new_node =
1871 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1872 uses_.push_front(*new_node);
1873 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001874 }
1875
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001876 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001877 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001878 // Note: env_fixup_end remains valid across push_front().
1879 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1880 HUseListNode<HEnvironment*>* new_node =
1881 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1882 env_uses_.push_front(*new_node);
1883 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001884 }
1885
David Brazdil1abb4192015-02-17 18:33:36 +00001886 void RemoveAsUserOfInput(size_t input) {
1887 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001888 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1889 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1890 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001891 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001892
Vladimir Marko372f10e2016-05-17 16:30:10 +01001893 void RemoveAsUserOfAllInputs() {
1894 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1895 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1896 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1897 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1898 }
1899 }
1900
David Brazdil1abb4192015-02-17 18:33:36 +00001901 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1902 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001903
Vladimir Marko46817b82016-03-29 12:21:58 +01001904 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1905 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1906 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001907 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001908 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001909 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001910
Roland Levillain6c82d402014-10-13 16:10:27 +01001911 // Does this instruction strictly dominate `other_instruction`?
1912 // Returns false if this instruction and `other_instruction` are the same.
1913 // Aborts if this instruction and `other_instruction` are both phis.
1914 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001915
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001916 int GetId() const { return id_; }
1917 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001918
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001919 int GetSsaIndex() const { return ssa_index_; }
1920 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1921 bool HasSsaIndex() const { return ssa_index_ != -1; }
1922
1923 bool HasEnvironment() const { return environment_ != nullptr; }
1924 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001925 // Set the `environment_` field. Raw because this method does not
1926 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001927 void SetRawEnvironment(HEnvironment* environment) {
1928 DCHECK(environment_ == nullptr);
1929 DCHECK_EQ(environment->GetHolder(), this);
1930 environment_ = environment;
1931 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001932
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001933 void RemoveEnvironment();
1934
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001935 // Set the environment of this instruction, copying it from `environment`. While
1936 // copying, the uses lists are being updated.
1937 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001938 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001939 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001940 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001941 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001942 if (environment->GetParent() != nullptr) {
1943 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1944 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001945 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001946
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001947 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1948 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001949 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001950 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001951 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001952 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001953 if (environment->GetParent() != nullptr) {
1954 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1955 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001956 }
1957
Nicolas Geoffray39468442014-09-02 15:17:15 +01001958 // Returns the number of entries in the environment. Typically, that is the
1959 // number of dex registers in a method. It could be more in case of inlining.
1960 size_t EnvironmentSize() const;
1961
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001962 LocationSummary* GetLocations() const { return locations_; }
1963 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001964
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001966 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001967
Alexandre Rames188d4312015-04-09 18:30:21 +01001968 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1969 // uses of this instruction by `other` are *not* updated.
1970 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1971 ReplaceWith(other);
1972 other->ReplaceInput(this, use_index);
1973 }
1974
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001975 // Move `this` instruction before `cursor`.
1976 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001977
Vladimir Markofb337ea2015-11-25 15:25:10 +00001978 // Move `this` before its first user and out of any loops. If there is no
1979 // out-of-loop user that dominates all other users, move the instruction
1980 // to the end of the out-of-loop common dominator of the user's blocks.
1981 //
1982 // This can be used only on non-throwing instructions with no side effects that
1983 // have at least one use but no environment uses.
1984 void MoveBeforeFirstUserAndOutOfLoops();
1985
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001986#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 bool Is##type() const; \
1988 const H##type* As##type() const; \
1989 H##type* As##type();
1990
1991 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1992#undef INSTRUCTION_TYPE_CHECK
1993
1994#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001995 bool Is##type() const { return (As##type() != nullptr); } \
1996 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001997 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001999#undef INSTRUCTION_TYPE_CHECK
2000
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002001 // Returns whether the instruction can be moved within the graph.
2002 virtual bool CanBeMoved() const { return false; }
2003
2004 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002005 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002006 return false;
2007 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002008
2009 // Returns whether any data encoded in the two instructions is equal.
2010 // This method does not look at the inputs. Both instructions must be
2011 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002012 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002013 return false;
2014 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002015
2016 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002017 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002018 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002019 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002020
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002021 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2022 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2023 // the virtual function because the __attribute__((__pure__)) doesn't really
2024 // apply the strong requirement for virtual functions, preventing optimizations.
2025 InstructionKind GetKind() const PURE;
2026 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002027
2028 virtual size_t ComputeHashCode() const {
2029 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002030 for (const HInstruction* input : GetInputs()) {
2031 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002032 }
2033 return result;
2034 }
2035
2036 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002037 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002038 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002039
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002040 size_t GetLifetimePosition() const { return lifetime_position_; }
2041 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2042 LiveInterval* GetLiveInterval() const { return live_interval_; }
2043 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2044 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2045
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002046 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2047
2048 // Returns whether the code generation of the instruction will require to have access
2049 // to the current method. Such instructions are:
2050 // (1): Instructions that require an environment, as calling the runtime requires
2051 // to walk the stack and have the current method stored at a specific stack address.
2052 // (2): Object literals like classes and strings, that are loaded from the dex cache
2053 // fields of the current method.
2054 bool NeedsCurrentMethod() const {
2055 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2056 }
2057
Vladimir Markodc151b22015-10-15 18:02:30 +01002058 // Returns whether the code generation of the instruction will require to have access
2059 // to the dex cache of the current method's declaring class via the current method.
2060 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002061
Mark Mendellc4701932015-04-10 13:18:51 -04002062 // Does this instruction have any use in an environment before
2063 // control flow hits 'other'?
2064 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2065
2066 // Remove all references to environment uses of this instruction.
2067 // The caller must ensure that this is safe to do.
2068 void RemoveEnvironmentUsers();
2069
Vladimir Markoa1de9182016-02-25 11:37:38 +00002070 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2071 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002072
David Brazdil1abb4192015-02-17 18:33:36 +00002073 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002074 // If set, the machine code for this instruction is assumed to be generated by
2075 // its users. Used by liveness analysis to compute use positions accordingly.
2076 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2077 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2078 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2079 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2080
Vladimir Marko372f10e2016-05-17 16:30:10 +01002081 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2082 return GetInputRecords()[i];
2083 }
2084
2085 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2086 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2087 input_records[index] = input;
2088 }
David Brazdil1abb4192015-02-17 18:33:36 +00002089
Vladimir Markoa1de9182016-02-25 11:37:38 +00002090 uint32_t GetPackedFields() const {
2091 return packed_fields_;
2092 }
2093
2094 template <size_t flag>
2095 bool GetPackedFlag() const {
2096 return (packed_fields_ & (1u << flag)) != 0u;
2097 }
2098
2099 template <size_t flag>
2100 void SetPackedFlag(bool value = true) {
2101 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2102 }
2103
2104 template <typename BitFieldType>
2105 typename BitFieldType::value_type GetPackedField() const {
2106 return BitFieldType::Decode(packed_fields_);
2107 }
2108
2109 template <typename BitFieldType>
2110 void SetPackedField(typename BitFieldType::value_type value) {
2111 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2112 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2113 }
2114
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002116 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2117 auto before_use_node = uses_.before_begin();
2118 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2119 HInstruction* user = use_node->GetUser();
2120 size_t input_index = use_node->GetIndex();
2121 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2122 before_use_node = use_node;
2123 }
2124 }
2125
2126 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2127 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2128 if (next != uses_.end()) {
2129 HInstruction* next_user = next->GetUser();
2130 size_t next_index = next->GetIndex();
2131 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2132 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2133 }
2134 }
2135
2136 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2137 auto before_env_use_node = env_uses_.before_begin();
2138 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2139 HEnvironment* user = env_use_node->GetUser();
2140 size_t input_index = env_use_node->GetIndex();
2141 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2142 before_env_use_node = env_use_node;
2143 }
2144 }
2145
2146 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2147 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2148 if (next != env_uses_.end()) {
2149 HEnvironment* next_user = next->GetUser();
2150 size_t next_index = next->GetIndex();
2151 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2152 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2153 }
2154 }
David Brazdil1abb4192015-02-17 18:33:36 +00002155
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156 HInstruction* previous_;
2157 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002158 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002159 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002160
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002161 // An instruction gets an id when it is added to the graph.
2162 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002163 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002164 int id_;
2165
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002166 // When doing liveness analysis, instructions that have uses get an SSA index.
2167 int ssa_index_;
2168
Vladimir Markoa1de9182016-02-25 11:37:38 +00002169 // Packed fields.
2170 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002171
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002172 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002173 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002174
2175 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002176 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002177
Nicolas Geoffray39468442014-09-02 15:17:15 +01002178 // The environment associated with this instruction. Not null if the instruction
2179 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002180 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002181
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002182 // Set by the code generator.
2183 LocationSummary* locations_;
2184
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002185 // Set by the liveness analysis.
2186 LiveInterval* live_interval_;
2187
2188 // Set by the liveness analysis, this is the position in a linear
2189 // order of blocks where this instruction's live interval start.
2190 size_t lifetime_position_;
2191
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002192 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002193
Vladimir Markoa1de9182016-02-25 11:37:38 +00002194 // The reference handle part of the reference type info.
2195 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002196 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002197 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002198
David Brazdil1abb4192015-02-17 18:33:36 +00002199 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002200 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002201 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002202 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002203 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002204
2205 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2206};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002207std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002208
2209class HInstructionIterator : public ValueObject {
2210 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002211 explicit HInstructionIterator(const HInstructionList& instructions)
2212 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002213 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002214 }
2215
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002216 bool Done() const { return instruction_ == nullptr; }
2217 HInstruction* Current() const { return instruction_; }
2218 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002220 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221 }
2222
2223 private:
2224 HInstruction* instruction_;
2225 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002226
2227 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002228};
2229
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002230class HBackwardInstructionIterator : public ValueObject {
2231 public:
2232 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2233 : instruction_(instructions.last_instruction_) {
2234 next_ = Done() ? nullptr : instruction_->GetPrevious();
2235 }
2236
2237 bool Done() const { return instruction_ == nullptr; }
2238 HInstruction* Current() const { return instruction_; }
2239 void Advance() {
2240 instruction_ = next_;
2241 next_ = Done() ? nullptr : instruction_->GetPrevious();
2242 }
2243
2244 private:
2245 HInstruction* instruction_;
2246 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002247
2248 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002249};
2250
Vladimir Markof9f64412015-09-02 14:05:49 +01002251template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002252class HTemplateInstruction: public HInstruction {
2253 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002254 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002255 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002256 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257
Vladimir Marko372f10e2016-05-17 16:30:10 +01002258 using HInstruction::GetInputRecords; // Keep the const version visible.
2259 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2260 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002261 }
2262
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002263 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002264 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002265
2266 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002267};
2268
Vladimir Markof9f64412015-09-02 14:05:49 +01002269// HTemplateInstruction specialization for N=0.
2270template<>
2271class HTemplateInstruction<0>: public HInstruction {
2272 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002273 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002274 : HInstruction(side_effects, dex_pc) {}
2275
Vladimir Markof9f64412015-09-02 14:05:49 +01002276 virtual ~HTemplateInstruction() {}
2277
Vladimir Marko372f10e2016-05-17 16:30:10 +01002278 using HInstruction::GetInputRecords; // Keep the const version visible.
2279 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2280 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002281 }
2282
2283 private:
2284 friend class SsaBuilder;
2285};
2286
Dave Allison20dfc792014-06-16 20:44:29 -07002287template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002288class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002289 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002290 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002291 : HTemplateInstruction<N>(side_effects, dex_pc) {
2292 this->template SetPackedField<TypeField>(type);
2293 }
Dave Allison20dfc792014-06-16 20:44:29 -07002294 virtual ~HExpression() {}
2295
Vladimir Markoa1de9182016-02-25 11:37:38 +00002296 Primitive::Type GetType() const OVERRIDE {
2297 return TypeField::Decode(this->GetPackedFields());
2298 }
Dave Allison20dfc792014-06-16 20:44:29 -07002299
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002300 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002301 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2302 static constexpr size_t kFieldTypeSize =
2303 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2304 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2305 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2306 "Too many packed fields.");
2307 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002308};
2309
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002310// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2311// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002312class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002313 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002314 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2315 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002316
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002317 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002319 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002320
2321 private:
2322 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2323};
2324
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002325// Represents dex's RETURN opcodes. A HReturn is a control flow
2326// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002327class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002328 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002329 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2330 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002331 SetRawInputAt(0, value);
2332 }
2333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002334 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002335
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002336 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002337
2338 private:
2339 DISALLOW_COPY_AND_ASSIGN(HReturn);
2340};
2341
Vladimir Markofcb503c2016-05-18 12:48:17 +01002342class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002343 public:
2344 HPhi(ArenaAllocator* arena,
2345 uint32_t reg_number,
2346 size_t number_of_inputs,
2347 Primitive::Type type,
2348 uint32_t dex_pc = kNoDexPc)
2349 : HInstruction(SideEffects::None(), dex_pc),
2350 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2351 reg_number_(reg_number) {
2352 SetPackedField<TypeField>(ToPhiType(type));
2353 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2354 // Phis are constructed live and marked dead if conflicting or unused.
2355 // Individual steps of SsaBuilder should assume that if a phi has been
2356 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2357 SetPackedFlag<kFlagIsLive>(true);
2358 SetPackedFlag<kFlagCanBeNull>(true);
2359 }
2360
2361 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2362 static Primitive::Type ToPhiType(Primitive::Type type) {
2363 return Primitive::PrimitiveKind(type);
2364 }
2365
2366 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2367
Vladimir Marko372f10e2016-05-17 16:30:10 +01002368 using HInstruction::GetInputRecords; // Keep the const version visible.
2369 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2370 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2371 }
David Brazdildee58d62016-04-07 09:54:26 +00002372
2373 void AddInput(HInstruction* input);
2374 void RemoveInputAt(size_t index);
2375
2376 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2377 void SetType(Primitive::Type new_type) {
2378 // Make sure that only valid type changes occur. The following are allowed:
2379 // (1) int -> float/ref (primitive type propagation),
2380 // (2) long -> double (primitive type propagation).
2381 DCHECK(GetType() == new_type ||
2382 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2383 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2384 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2385 SetPackedField<TypeField>(new_type);
2386 }
2387
2388 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2389 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2390
2391 uint32_t GetRegNumber() const { return reg_number_; }
2392
2393 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2394 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2395 bool IsDead() const { return !IsLive(); }
2396 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2397
2398 bool IsVRegEquivalentOf(HInstruction* other) const {
2399 return other != nullptr
2400 && other->IsPhi()
2401 && other->AsPhi()->GetBlock() == GetBlock()
2402 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2403 }
2404
2405 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2406 // An equivalent phi is a phi having the same dex register and type.
2407 // It assumes that phis with the same dex register are adjacent.
2408 HPhi* GetNextEquivalentPhiWithSameType() {
2409 HInstruction* next = GetNext();
2410 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2411 if (next->GetType() == GetType()) {
2412 return next->AsPhi();
2413 }
2414 next = next->GetNext();
2415 }
2416 return nullptr;
2417 }
2418
2419 DECLARE_INSTRUCTION(Phi);
2420
David Brazdildee58d62016-04-07 09:54:26 +00002421 private:
2422 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2423 static constexpr size_t kFieldTypeSize =
2424 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2425 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2426 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2427 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2428 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2429 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2430
Vladimir Marko372f10e2016-05-17 16:30:10 +01002431 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002432 const uint32_t reg_number_;
2433
2434 DISALLOW_COPY_AND_ASSIGN(HPhi);
2435};
2436
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002437// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002438// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002439// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002440class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002441 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002442 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002443
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002444 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002445
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002446 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002447
2448 private:
2449 DISALLOW_COPY_AND_ASSIGN(HExit);
2450};
2451
2452// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002453class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002455 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002456
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002457 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002458
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002459 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002460 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002461 }
2462
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002463 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002464
2465 private:
2466 DISALLOW_COPY_AND_ASSIGN(HGoto);
2467};
2468
Roland Levillain9867bc72015-08-05 10:21:34 +01002469class HConstant : public HExpression<0> {
2470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002471 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2472 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002473
2474 bool CanBeMoved() const OVERRIDE { return true; }
2475
Roland Levillain1a653882016-03-18 18:05:57 +00002476 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002477 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002478 // Is this constant 0 in the arithmetic sense?
2479 virtual bool IsArithmeticZero() const { return false; }
2480 // Is this constant a 0-bit pattern?
2481 virtual bool IsZeroBitPattern() const { return false; }
2482 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002483 virtual bool IsOne() const { return false; }
2484
David Brazdil77a48ae2015-09-15 12:34:04 +00002485 virtual uint64_t GetValueAsUint64() const = 0;
2486
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002487 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002488
2489 private:
2490 DISALLOW_COPY_AND_ASSIGN(HConstant);
2491};
2492
Vladimir Markofcb503c2016-05-18 12:48:17 +01002493class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002494 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002495 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002496 return true;
2497 }
2498
David Brazdil77a48ae2015-09-15 12:34:04 +00002499 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2500
Roland Levillain9867bc72015-08-05 10:21:34 +01002501 size_t ComputeHashCode() const OVERRIDE { return 0; }
2502
Roland Levillain1a653882016-03-18 18:05:57 +00002503 // The null constant representation is a 0-bit pattern.
2504 virtual bool IsZeroBitPattern() const { return true; }
2505
Roland Levillain9867bc72015-08-05 10:21:34 +01002506 DECLARE_INSTRUCTION(NullConstant);
2507
2508 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002509 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002510
2511 friend class HGraph;
2512 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2513};
2514
2515// Constants of the type int. Those can be from Dex instructions, or
2516// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002517class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002518 public:
2519 int32_t GetValue() const { return value_; }
2520
David Brazdil9f389d42015-10-01 14:32:56 +01002521 uint64_t GetValueAsUint64() const OVERRIDE {
2522 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2523 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002524
Vladimir Marko372f10e2016-05-17 16:30:10 +01002525 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002526 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002527 return other->AsIntConstant()->value_ == value_;
2528 }
2529
2530 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2531
2532 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002533 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2534 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002535 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2536
Roland Levillain1a653882016-03-18 18:05:57 +00002537 // Integer constants are used to encode Boolean values as well,
2538 // where 1 means true and 0 means false.
2539 bool IsTrue() const { return GetValue() == 1; }
2540 bool IsFalse() const { return GetValue() == 0; }
2541
Roland Levillain9867bc72015-08-05 10:21:34 +01002542 DECLARE_INSTRUCTION(IntConstant);
2543
2544 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002545 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2546 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2547 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2548 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002549
2550 const int32_t value_;
2551
2552 friend class HGraph;
2553 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2554 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2555 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2556};
2557
Vladimir Markofcb503c2016-05-18 12:48:17 +01002558class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002559 public:
2560 int64_t GetValue() const { return value_; }
2561
David Brazdil77a48ae2015-09-15 12:34:04 +00002562 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2563
Vladimir Marko372f10e2016-05-17 16:30:10 +01002564 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002565 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002566 return other->AsLongConstant()->value_ == value_;
2567 }
2568
2569 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2570
2571 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002572 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2573 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002574 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2575
2576 DECLARE_INSTRUCTION(LongConstant);
2577
2578 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002579 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2580 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002581
2582 const int64_t value_;
2583
2584 friend class HGraph;
2585 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2586};
Dave Allison20dfc792014-06-16 20:44:29 -07002587
Vladimir Markofcb503c2016-05-18 12:48:17 +01002588class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002589 public:
2590 float GetValue() const { return value_; }
2591
2592 uint64_t GetValueAsUint64() const OVERRIDE {
2593 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2594 }
2595
Vladimir Marko372f10e2016-05-17 16:30:10 +01002596 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002597 DCHECK(other->IsFloatConstant()) << other->DebugName();
2598 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2599 }
2600
2601 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2602
2603 bool IsMinusOne() const OVERRIDE {
2604 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2605 }
Roland Levillain1a653882016-03-18 18:05:57 +00002606 bool IsArithmeticZero() const OVERRIDE {
2607 return std::fpclassify(value_) == FP_ZERO;
2608 }
2609 bool IsArithmeticPositiveZero() const {
2610 return IsArithmeticZero() && !std::signbit(value_);
2611 }
2612 bool IsArithmeticNegativeZero() const {
2613 return IsArithmeticZero() && std::signbit(value_);
2614 }
2615 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002616 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002617 }
2618 bool IsOne() const OVERRIDE {
2619 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2620 }
2621 bool IsNaN() const {
2622 return std::isnan(value_);
2623 }
2624
2625 DECLARE_INSTRUCTION(FloatConstant);
2626
2627 private:
2628 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2629 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2630 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2631 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2632
2633 const float value_;
2634
2635 // Only the SsaBuilder and HGraph can create floating-point constants.
2636 friend class SsaBuilder;
2637 friend class HGraph;
2638 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2639};
2640
Vladimir Markofcb503c2016-05-18 12:48:17 +01002641class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002642 public:
2643 double GetValue() const { return value_; }
2644
2645 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2646
Vladimir Marko372f10e2016-05-17 16:30:10 +01002647 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002648 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2649 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2650 }
2651
2652 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2653
2654 bool IsMinusOne() const OVERRIDE {
2655 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2656 }
Roland Levillain1a653882016-03-18 18:05:57 +00002657 bool IsArithmeticZero() const OVERRIDE {
2658 return std::fpclassify(value_) == FP_ZERO;
2659 }
2660 bool IsArithmeticPositiveZero() const {
2661 return IsArithmeticZero() && !std::signbit(value_);
2662 }
2663 bool IsArithmeticNegativeZero() const {
2664 return IsArithmeticZero() && std::signbit(value_);
2665 }
2666 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002667 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002668 }
2669 bool IsOne() const OVERRIDE {
2670 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2671 }
2672 bool IsNaN() const {
2673 return std::isnan(value_);
2674 }
2675
2676 DECLARE_INSTRUCTION(DoubleConstant);
2677
2678 private:
2679 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2680 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2681 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2682 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2683
2684 const double value_;
2685
2686 // Only the SsaBuilder and HGraph can create floating-point constants.
2687 friend class SsaBuilder;
2688 friend class HGraph;
2689 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2690};
2691
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002692// Conditional branch. A block ending with an HIf instruction must have
2693// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002694class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002695 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002696 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2697 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002698 SetRawInputAt(0, input);
2699 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002700
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002701 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002702
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002703 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002704 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002705 }
2706
2707 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002708 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002709 }
2710
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002711 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002712
2713 private:
2714 DISALLOW_COPY_AND_ASSIGN(HIf);
2715};
2716
David Brazdilfc6a86a2015-06-26 10:33:45 +00002717
2718// Abstract instruction which marks the beginning and/or end of a try block and
2719// links it to the respective exception handlers. Behaves the same as a Goto in
2720// non-exceptional control flow.
2721// Normal-flow successor is stored at index zero, exception handlers under
2722// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002723class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002724 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002725 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002726 kEntry,
2727 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002728 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002729 };
2730
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002731 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002732 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2733 SetPackedField<BoundaryKindField>(kind);
2734 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002735
2736 bool IsControlFlow() const OVERRIDE { return true; }
2737
2738 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002739 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002740
David Brazdild26a4112015-11-10 11:07:31 +00002741 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2742 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2743 }
2744
David Brazdilfc6a86a2015-06-26 10:33:45 +00002745 // Returns whether `handler` is among its exception handlers (non-zero index
2746 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002747 bool HasExceptionHandler(const HBasicBlock& handler) const {
2748 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002749 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002750 }
2751
2752 // If not present already, adds `handler` to its block's list of exception
2753 // handlers.
2754 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002755 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002756 GetBlock()->AddSuccessor(handler);
2757 }
2758 }
2759
Vladimir Markoa1de9182016-02-25 11:37:38 +00002760 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2761 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002762
David Brazdilffee3d32015-07-06 11:48:53 +01002763 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2764
David Brazdilfc6a86a2015-06-26 10:33:45 +00002765 DECLARE_INSTRUCTION(TryBoundary);
2766
2767 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002768 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2769 static constexpr size_t kFieldBoundaryKindSize =
2770 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2771 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2772 kFieldBoundaryKind + kFieldBoundaryKindSize;
2773 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2774 "Too many packed fields.");
2775 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002776
2777 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2778};
2779
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002780// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002781class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002782 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002783 // We set CanTriggerGC to prevent any intermediate address to be live
2784 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002785 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002786 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002787 SetRawInputAt(0, cond);
2788 }
2789
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002790 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002791 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002792 return true;
2793 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002794 bool NeedsEnvironment() const OVERRIDE { return true; }
2795 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002796
2797 DECLARE_INSTRUCTION(Deoptimize);
2798
2799 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002800 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2801};
2802
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002803// Represents the ArtMethod that was passed as a first argument to
2804// the method. It is used by instructions that depend on it, like
2805// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002806class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002807 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002808 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2809 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002810
2811 DECLARE_INSTRUCTION(CurrentMethod);
2812
2813 private:
2814 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2815};
2816
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002817// Fetches an ArtMethod from the virtual table or the interface method table
2818// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002819class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002820 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002821 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002822 kVTable,
2823 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002824 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002825 };
2826 HClassTableGet(HInstruction* cls,
2827 Primitive::Type type,
2828 TableKind kind,
2829 size_t index,
2830 uint32_t dex_pc)
2831 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002832 index_(index) {
2833 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002834 SetRawInputAt(0, cls);
2835 }
2836
2837 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002838 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002839 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002840 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002841 }
2842
Vladimir Markoa1de9182016-02-25 11:37:38 +00002843 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002844 size_t GetIndex() const { return index_; }
2845
2846 DECLARE_INSTRUCTION(ClassTableGet);
2847
2848 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002849 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2850 static constexpr size_t kFieldTableKindSize =
2851 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2852 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2853 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2854 "Too many packed fields.");
2855 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2856
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002857 // The index of the ArtMethod in the table.
2858 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002859
2860 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2861};
2862
Mark Mendellfe57faa2015-09-18 09:26:15 -04002863// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2864// have one successor for each entry in the switch table, and the final successor
2865// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002866class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002867 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002868 HPackedSwitch(int32_t start_value,
2869 uint32_t num_entries,
2870 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002871 uint32_t dex_pc = kNoDexPc)
2872 : HTemplateInstruction(SideEffects::None(), dex_pc),
2873 start_value_(start_value),
2874 num_entries_(num_entries) {
2875 SetRawInputAt(0, input);
2876 }
2877
2878 bool IsControlFlow() const OVERRIDE { return true; }
2879
2880 int32_t GetStartValue() const { return start_value_; }
2881
Vladimir Marko211c2112015-09-24 16:52:33 +01002882 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002883
2884 HBasicBlock* GetDefaultBlock() const {
2885 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002886 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002887 }
2888 DECLARE_INSTRUCTION(PackedSwitch);
2889
2890 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002891 const int32_t start_value_;
2892 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002893
2894 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2895};
2896
Roland Levillain88cb1752014-10-20 16:36:47 +01002897class HUnaryOperation : public HExpression<1> {
2898 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002899 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2900 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002901 SetRawInputAt(0, input);
2902 }
2903
2904 HInstruction* GetInput() const { return InputAt(0); }
2905 Primitive::Type GetResultType() const { return GetType(); }
2906
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002907 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002908 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002909 return true;
2910 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002911
Roland Levillain31dd3d62016-02-16 12:21:02 +00002912 // Try to statically evaluate `this` and return a HConstant
2913 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002914 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002915 HConstant* TryStaticEvaluation() const;
2916
2917 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002918 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2919 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002920 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2921 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002922
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002923 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002924
2925 private:
2926 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2927};
2928
Dave Allison20dfc792014-06-16 20:44:29 -07002929class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002930 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002931 HBinaryOperation(Primitive::Type result_type,
2932 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002933 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002934 SideEffects side_effects = SideEffects::None(),
2935 uint32_t dex_pc = kNoDexPc)
2936 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002937 SetRawInputAt(0, left);
2938 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002939 }
2940
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002941 HInstruction* GetLeft() const { return InputAt(0); }
2942 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002943 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002944
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002945 virtual bool IsCommutative() const { return false; }
2946
2947 // Put constant on the right.
2948 // Returns whether order is changed.
2949 bool OrderInputsWithConstantOnTheRight() {
2950 HInstruction* left = InputAt(0);
2951 HInstruction* right = InputAt(1);
2952 if (left->IsConstant() && !right->IsConstant()) {
2953 ReplaceInput(right, 0);
2954 ReplaceInput(left, 1);
2955 return true;
2956 }
2957 return false;
2958 }
2959
2960 // Order inputs by instruction id, but favor constant on the right side.
2961 // This helps GVN for commutative ops.
2962 void OrderInputs() {
2963 DCHECK(IsCommutative());
2964 HInstruction* left = InputAt(0);
2965 HInstruction* right = InputAt(1);
2966 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2967 return;
2968 }
2969 if (OrderInputsWithConstantOnTheRight()) {
2970 return;
2971 }
2972 // Order according to instruction id.
2973 if (left->GetId() > right->GetId()) {
2974 ReplaceInput(right, 0);
2975 ReplaceInput(left, 1);
2976 }
2977 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002978
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002979 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002980 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002981 return true;
2982 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002983
Roland Levillain31dd3d62016-02-16 12:21:02 +00002984 // Try to statically evaluate `this` and return a HConstant
2985 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002986 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002987 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002988
2989 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002990 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2991 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002992 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2993 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002994 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002995 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2996 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002997 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2998 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002999 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3000 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003001 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003002 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3003 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003004
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003005 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003006 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003007 HConstant* GetConstantRight() const;
3008
3009 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003010 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003011 HInstruction* GetLeastConstantLeft() const;
3012
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003013 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003014
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003015 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003016 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3017};
3018
Mark Mendellc4701932015-04-10 13:18:51 -04003019// The comparison bias applies for floating point operations and indicates how NaN
3020// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003021enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003022 kNoBias, // bias is not applicable (i.e. for long operation)
3023 kGtBias, // return 1 for NaN comparisons
3024 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003025 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003026};
3027
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3029
Dave Allison20dfc792014-06-16 20:44:29 -07003030class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003031 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003032 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003033 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3034 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3035 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003036
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003037 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003038 // `instruction`, and disregard moves in between.
3039 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003040
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003041 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003042
3043 virtual IfCondition GetCondition() const = 0;
3044
Mark Mendellc4701932015-04-10 13:18:51 -04003045 virtual IfCondition GetOppositeCondition() const = 0;
3046
Vladimir Markoa1de9182016-02-25 11:37:38 +00003047 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003048 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3049
Vladimir Markoa1de9182016-02-25 11:37:38 +00003050 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3051 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003052
Vladimir Marko372f10e2016-05-17 16:30:10 +01003053 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003054 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003055 }
3056
Roland Levillain4fa13f62015-07-06 18:11:54 +01003057 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003058 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003059 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003060 if (if_cond == kCondNE) {
3061 return true;
3062 } else if (if_cond == kCondEQ) {
3063 return false;
3064 }
3065 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003066 }
3067
3068 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003070 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003071 if (if_cond == kCondEQ) {
3072 return true;
3073 } else if (if_cond == kCondNE) {
3074 return false;
3075 }
3076 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003077 }
3078
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003080 // Needed if we merge a HCompare into a HCondition.
3081 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3082 static constexpr size_t kFieldComparisonBiasSize =
3083 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3084 static constexpr size_t kNumberOfConditionPackedBits =
3085 kFieldComparisonBias + kFieldComparisonBiasSize;
3086 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3087 using ComparisonBiasField =
3088 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3089
Roland Levillain31dd3d62016-02-16 12:21:02 +00003090 template <typename T>
3091 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3092
3093 template <typename T>
3094 int32_t CompareFP(T x, T y) const {
3095 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3096 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3097 // Handle the bias.
3098 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3099 }
3100
3101 // Return an integer constant containing the result of a condition evaluated at compile time.
3102 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3103 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3104 }
3105
Dave Allison20dfc792014-06-16 20:44:29 -07003106 private:
3107 DISALLOW_COPY_AND_ASSIGN(HCondition);
3108};
3109
3110// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003111class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003112 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003113 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3114 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003115
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003116 bool IsCommutative() const OVERRIDE { return true; }
3117
Vladimir Marko9e23df52015-11-10 17:14:35 +00003118 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3119 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120 return MakeConstantCondition(true, GetDexPc());
3121 }
3122 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3123 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3124 }
3125 // In the following Evaluate methods, a HCompare instruction has
3126 // been merged into this HEqual instruction; evaluate it as
3127 // `Compare(x, y) == 0`.
3128 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3129 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3130 GetDexPc());
3131 }
3132 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3133 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3134 }
3135 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3136 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003137 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003138
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003139 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003140
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003141 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003142 return kCondEQ;
3143 }
3144
Mark Mendellc4701932015-04-10 13:18:51 -04003145 IfCondition GetOppositeCondition() const OVERRIDE {
3146 return kCondNE;
3147 }
3148
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003149 private:
Aart Bike9f37602015-10-09 11:15:55 -07003150 template <typename T> bool Compute(T x, T y) const { return x == y; }
3151
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003152 DISALLOW_COPY_AND_ASSIGN(HEqual);
3153};
3154
Vladimir Markofcb503c2016-05-18 12:48:17 +01003155class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003156 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003157 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3158 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003159
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003160 bool IsCommutative() const OVERRIDE { return true; }
3161
Vladimir Marko9e23df52015-11-10 17:14:35 +00003162 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3163 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003164 return MakeConstantCondition(false, GetDexPc());
3165 }
3166 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3167 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3168 }
3169 // In the following Evaluate methods, a HCompare instruction has
3170 // been merged into this HNotEqual instruction; evaluate it as
3171 // `Compare(x, y) != 0`.
3172 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3173 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3174 }
3175 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3176 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3177 }
3178 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3179 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003180 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003181
Dave Allison20dfc792014-06-16 20:44:29 -07003182 DECLARE_INSTRUCTION(NotEqual);
3183
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003184 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003185 return kCondNE;
3186 }
3187
Mark Mendellc4701932015-04-10 13:18:51 -04003188 IfCondition GetOppositeCondition() const OVERRIDE {
3189 return kCondEQ;
3190 }
3191
Dave Allison20dfc792014-06-16 20:44:29 -07003192 private:
Aart Bike9f37602015-10-09 11:15:55 -07003193 template <typename T> bool Compute(T x, T y) const { return x != y; }
3194
Dave Allison20dfc792014-06-16 20:44:29 -07003195 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3196};
3197
Vladimir Markofcb503c2016-05-18 12:48:17 +01003198class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003199 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003200 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3201 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003202
Roland Levillain9867bc72015-08-05 10:21:34 +01003203 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003204 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003205 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003206 // In the following Evaluate methods, a HCompare instruction has
3207 // been merged into this HLessThan instruction; evaluate it as
3208 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003209 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003210 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3211 }
3212 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3213 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3214 }
3215 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3216 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003217 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003218
Dave Allison20dfc792014-06-16 20:44:29 -07003219 DECLARE_INSTRUCTION(LessThan);
3220
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003221 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003222 return kCondLT;
3223 }
3224
Mark Mendellc4701932015-04-10 13:18:51 -04003225 IfCondition GetOppositeCondition() const OVERRIDE {
3226 return kCondGE;
3227 }
3228
Dave Allison20dfc792014-06-16 20:44:29 -07003229 private:
Aart Bike9f37602015-10-09 11:15:55 -07003230 template <typename T> bool Compute(T x, T y) const { return x < y; }
3231
Dave Allison20dfc792014-06-16 20:44:29 -07003232 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3233};
3234
Vladimir Markofcb503c2016-05-18 12:48:17 +01003235class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003236 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003237 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3238 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003239
Roland Levillain9867bc72015-08-05 10:21:34 +01003240 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003241 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003242 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003243 // In the following Evaluate methods, a HCompare instruction has
3244 // been merged into this HLessThanOrEqual instruction; evaluate it as
3245 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003246 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003247 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3248 }
3249 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3250 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3251 }
3252 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3253 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003254 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003255
Dave Allison20dfc792014-06-16 20:44:29 -07003256 DECLARE_INSTRUCTION(LessThanOrEqual);
3257
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003258 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003259 return kCondLE;
3260 }
3261
Mark Mendellc4701932015-04-10 13:18:51 -04003262 IfCondition GetOppositeCondition() const OVERRIDE {
3263 return kCondGT;
3264 }
3265
Dave Allison20dfc792014-06-16 20:44:29 -07003266 private:
Aart Bike9f37602015-10-09 11:15:55 -07003267 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3268
Dave Allison20dfc792014-06-16 20:44:29 -07003269 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3270};
3271
Vladimir Markofcb503c2016-05-18 12:48:17 +01003272class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003273 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003274 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3275 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003276
Roland Levillain9867bc72015-08-05 10:21:34 +01003277 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003278 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003279 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003280 // In the following Evaluate methods, a HCompare instruction has
3281 // been merged into this HGreaterThan instruction; evaluate it as
3282 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003283 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003284 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3285 }
3286 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3287 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3288 }
3289 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3290 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003291 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003292
Dave Allison20dfc792014-06-16 20:44:29 -07003293 DECLARE_INSTRUCTION(GreaterThan);
3294
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003295 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003296 return kCondGT;
3297 }
3298
Mark Mendellc4701932015-04-10 13:18:51 -04003299 IfCondition GetOppositeCondition() const OVERRIDE {
3300 return kCondLE;
3301 }
3302
Dave Allison20dfc792014-06-16 20:44:29 -07003303 private:
Aart Bike9f37602015-10-09 11:15:55 -07003304 template <typename T> bool Compute(T x, T y) const { return x > y; }
3305
Dave Allison20dfc792014-06-16 20:44:29 -07003306 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3307};
3308
Vladimir Markofcb503c2016-05-18 12:48:17 +01003309class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003310 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003311 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3312 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003313
Roland Levillain9867bc72015-08-05 10:21:34 +01003314 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003315 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003316 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003317 // In the following Evaluate methods, a HCompare instruction has
3318 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3319 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003320 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003321 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3322 }
3323 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3324 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3325 }
3326 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3327 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003328 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003329
Dave Allison20dfc792014-06-16 20:44:29 -07003330 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3331
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003332 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003333 return kCondGE;
3334 }
3335
Mark Mendellc4701932015-04-10 13:18:51 -04003336 IfCondition GetOppositeCondition() const OVERRIDE {
3337 return kCondLT;
3338 }
3339
Dave Allison20dfc792014-06-16 20:44:29 -07003340 private:
Aart Bike9f37602015-10-09 11:15:55 -07003341 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3342
Dave Allison20dfc792014-06-16 20:44:29 -07003343 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3344};
3345
Vladimir Markofcb503c2016-05-18 12:48:17 +01003346class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003347 public:
3348 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3349 : HCondition(first, second, dex_pc) {}
3350
3351 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003352 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003353 }
3354 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003355 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3356 }
3357 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3358 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3359 LOG(FATAL) << DebugName() << " is not defined for float values";
3360 UNREACHABLE();
3361 }
3362 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3363 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3364 LOG(FATAL) << DebugName() << " is not defined for double values";
3365 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003366 }
3367
3368 DECLARE_INSTRUCTION(Below);
3369
3370 IfCondition GetCondition() const OVERRIDE {
3371 return kCondB;
3372 }
3373
3374 IfCondition GetOppositeCondition() const OVERRIDE {
3375 return kCondAE;
3376 }
3377
3378 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003379 template <typename T> bool Compute(T x, T y) const {
3380 return MakeUnsigned(x) < MakeUnsigned(y);
3381 }
Aart Bike9f37602015-10-09 11:15:55 -07003382
3383 DISALLOW_COPY_AND_ASSIGN(HBelow);
3384};
3385
Vladimir Markofcb503c2016-05-18 12:48:17 +01003386class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003387 public:
3388 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3389 : HCondition(first, second, dex_pc) {}
3390
3391 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003392 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003393 }
3394 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003395 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3396 }
3397 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3398 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3399 LOG(FATAL) << DebugName() << " is not defined for float values";
3400 UNREACHABLE();
3401 }
3402 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3403 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3404 LOG(FATAL) << DebugName() << " is not defined for double values";
3405 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003406 }
3407
3408 DECLARE_INSTRUCTION(BelowOrEqual);
3409
3410 IfCondition GetCondition() const OVERRIDE {
3411 return kCondBE;
3412 }
3413
3414 IfCondition GetOppositeCondition() const OVERRIDE {
3415 return kCondA;
3416 }
3417
3418 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003419 template <typename T> bool Compute(T x, T y) const {
3420 return MakeUnsigned(x) <= MakeUnsigned(y);
3421 }
Aart Bike9f37602015-10-09 11:15:55 -07003422
3423 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3424};
3425
Vladimir Markofcb503c2016-05-18 12:48:17 +01003426class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003427 public:
3428 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3429 : HCondition(first, second, dex_pc) {}
3430
3431 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003432 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003433 }
3434 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003435 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3436 }
3437 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3438 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3439 LOG(FATAL) << DebugName() << " is not defined for float values";
3440 UNREACHABLE();
3441 }
3442 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3443 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3444 LOG(FATAL) << DebugName() << " is not defined for double values";
3445 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003446 }
3447
3448 DECLARE_INSTRUCTION(Above);
3449
3450 IfCondition GetCondition() const OVERRIDE {
3451 return kCondA;
3452 }
3453
3454 IfCondition GetOppositeCondition() const OVERRIDE {
3455 return kCondBE;
3456 }
3457
3458 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003459 template <typename T> bool Compute(T x, T y) const {
3460 return MakeUnsigned(x) > MakeUnsigned(y);
3461 }
Aart Bike9f37602015-10-09 11:15:55 -07003462
3463 DISALLOW_COPY_AND_ASSIGN(HAbove);
3464};
3465
Vladimir Markofcb503c2016-05-18 12:48:17 +01003466class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003467 public:
3468 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3469 : HCondition(first, second, dex_pc) {}
3470
3471 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003472 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003473 }
3474 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003475 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3476 }
3477 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3478 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3479 LOG(FATAL) << DebugName() << " is not defined for float values";
3480 UNREACHABLE();
3481 }
3482 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3483 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3484 LOG(FATAL) << DebugName() << " is not defined for double values";
3485 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003486 }
3487
3488 DECLARE_INSTRUCTION(AboveOrEqual);
3489
3490 IfCondition GetCondition() const OVERRIDE {
3491 return kCondAE;
3492 }
3493
3494 IfCondition GetOppositeCondition() const OVERRIDE {
3495 return kCondB;
3496 }
3497
3498 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003499 template <typename T> bool Compute(T x, T y) const {
3500 return MakeUnsigned(x) >= MakeUnsigned(y);
3501 }
Aart Bike9f37602015-10-09 11:15:55 -07003502
3503 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3504};
Dave Allison20dfc792014-06-16 20:44:29 -07003505
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003506// Instruction to check how two inputs compare to each other.
3507// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003508class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003509 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003510 // Note that `comparison_type` is the type of comparison performed
3511 // between the comparison's inputs, not the type of the instantiated
3512 // HCompare instruction (which is always Primitive::kPrimInt).
3513 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003514 HInstruction* first,
3515 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003516 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003517 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003518 : HBinaryOperation(Primitive::kPrimInt,
3519 first,
3520 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003521 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003522 dex_pc) {
3523 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003524 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3525 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003526 }
3527
Roland Levillain9867bc72015-08-05 10:21:34 +01003528 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003529 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3530
3531 template <typename T>
3532 int32_t ComputeFP(T x, T y) const {
3533 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3534 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3535 // Handle the bias.
3536 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3537 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003538
Roland Levillain9867bc72015-08-05 10:21:34 +01003539 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003540 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3541 // reach this code path when processing a freshly built HIR
3542 // graph. However HCompare integer instructions can be synthesized
3543 // by the instruction simplifier to implement IntegerCompare and
3544 // IntegerSignum intrinsics, so we have to handle this case.
3545 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003546 }
3547 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3549 }
3550 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3551 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3552 }
3553 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3554 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003555 }
3556
Vladimir Marko372f10e2016-05-17 16:30:10 +01003557 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003558 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003559 }
3560
Vladimir Markoa1de9182016-02-25 11:37:38 +00003561 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003562
Roland Levillain31dd3d62016-02-16 12:21:02 +00003563 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003564 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003565 bool IsGtBias() const {
3566 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003567 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003568 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003569
Roland Levillain1693a1f2016-03-15 14:57:31 +00003570 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3571 // Comparisons do not require a runtime call in any back end.
3572 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003573 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003574
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003575 DECLARE_INSTRUCTION(Compare);
3576
Roland Levillain31dd3d62016-02-16 12:21:02 +00003577 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003578 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3579 static constexpr size_t kFieldComparisonBiasSize =
3580 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3581 static constexpr size_t kNumberOfComparePackedBits =
3582 kFieldComparisonBias + kFieldComparisonBiasSize;
3583 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3584 using ComparisonBiasField =
3585 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3586
Roland Levillain31dd3d62016-02-16 12:21:02 +00003587 // Return an integer constant containing the result of a comparison evaluated at compile time.
3588 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3589 DCHECK(value == -1 || value == 0 || value == 1) << value;
3590 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3591 }
3592
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003593 private:
3594 DISALLOW_COPY_AND_ASSIGN(HCompare);
3595};
3596
Vladimir Markofcb503c2016-05-18 12:48:17 +01003597class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003598 public:
3599 HNewInstance(HInstruction* cls,
3600 HCurrentMethod* current_method,
3601 uint32_t dex_pc,
3602 uint16_t type_index,
3603 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003604 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003605 bool finalizable,
3606 QuickEntrypointEnum entrypoint)
3607 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3608 type_index_(type_index),
3609 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003610 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003611 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003612 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003613 SetRawInputAt(0, cls);
3614 SetRawInputAt(1, current_method);
3615 }
3616
3617 uint16_t GetTypeIndex() const { return type_index_; }
3618 const DexFile& GetDexFile() const { return dex_file_; }
3619
3620 // Calls runtime so needs an environment.
3621 bool NeedsEnvironment() const OVERRIDE { return true; }
3622
Mingyao Yang062157f2016-03-02 10:15:36 -08003623 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3624 bool CanThrow() const OVERRIDE { return true; }
3625
3626 // Needs to call into runtime to make sure it's instantiable/accessible.
3627 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003628
Vladimir Markoa1de9182016-02-25 11:37:38 +00003629 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003630
3631 bool CanBeNull() const OVERRIDE { return false; }
3632
3633 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3634
3635 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3636 entrypoint_ = entrypoint;
3637 }
3638
3639 bool IsStringAlloc() const;
3640
3641 DECLARE_INSTRUCTION(NewInstance);
3642
3643 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003644 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3645 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003646 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3647 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3648 "Too many packed fields.");
3649
David Brazdil6de19382016-01-08 17:37:10 +00003650 const uint16_t type_index_;
3651 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003652 QuickEntrypointEnum entrypoint_;
3653
3654 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3655};
3656
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003657enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003658#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3659 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003660#include "intrinsics_list.h"
3661 kNone,
3662 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3663#undef INTRINSICS_LIST
3664#undef OPTIMIZING_INTRINSICS
3665};
3666std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3667
Agi Csaki05f20562015-08-19 14:58:14 -07003668enum IntrinsicNeedsEnvironmentOrCache {
3669 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3670 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003671};
3672
Aart Bik5d75afe2015-12-14 11:57:01 -08003673enum IntrinsicSideEffects {
3674 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3675 kReadSideEffects, // Intrinsic may read heap memory.
3676 kWriteSideEffects, // Intrinsic may write heap memory.
3677 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3678};
3679
3680enum IntrinsicExceptions {
3681 kNoThrow, // Intrinsic does not throw any exceptions.
3682 kCanThrow // Intrinsic may throw exceptions.
3683};
3684
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003685class HInvoke : public HInstruction {
3686 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003687 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003688
Vladimir Marko372f10e2016-05-17 16:30:10 +01003689 using HInstruction::GetInputRecords; // Keep the const version visible.
3690 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3691 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3692 }
3693
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003694 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003695 SetRawInputAt(index, argument);
3696 }
3697
Roland Levillain3e3d7332015-04-28 11:00:54 +01003698 // Return the number of arguments. This number can be lower than
3699 // the number of inputs returned by InputCount(), as some invoke
3700 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3701 // inputs at the end of their list of inputs.
3702 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3703
Vladimir Markoa1de9182016-02-25 11:37:38 +00003704 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003705
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003706 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003707 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003708
Vladimir Markoa1de9182016-02-25 11:37:38 +00003709 InvokeType GetOriginalInvokeType() const {
3710 return GetPackedField<OriginalInvokeTypeField>();
3711 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003712
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003713 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003714 return intrinsic_;
3715 }
3716
Aart Bik5d75afe2015-12-14 11:57:01 -08003717 void SetIntrinsic(Intrinsics intrinsic,
3718 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3719 IntrinsicSideEffects side_effects,
3720 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003721
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003722 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003723 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003724 }
3725
Vladimir Markoa1de9182016-02-25 11:37:38 +00003726 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003727
3728 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3729
Vladimir Marko372f10e2016-05-17 16:30:10 +01003730 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003731 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3732 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003733
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003734 uint32_t* GetIntrinsicOptimizations() {
3735 return &intrinsic_optimizations_;
3736 }
3737
3738 const uint32_t* GetIntrinsicOptimizations() const {
3739 return &intrinsic_optimizations_;
3740 }
3741
3742 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3743
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003744 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003745
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003746 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003747 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3748 static constexpr size_t kFieldOriginalInvokeTypeSize =
3749 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3750 static constexpr size_t kFieldReturnType =
3751 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3752 static constexpr size_t kFieldReturnTypeSize =
3753 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3754 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3755 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3756 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3757 using OriginalInvokeTypeField =
3758 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3759 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3760
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003761 HInvoke(ArenaAllocator* arena,
3762 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003763 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003764 Primitive::Type return_type,
3765 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003766 uint32_t dex_method_index,
3767 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003768 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003769 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003770 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003771 inputs_(number_of_arguments + number_of_other_inputs,
3772 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003773 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003774 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003775 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003776 SetPackedField<ReturnTypeField>(return_type);
3777 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3778 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003779 }
3780
Vladimir Markoa1de9182016-02-25 11:37:38 +00003781 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003782
Roland Levillain3e3d7332015-04-28 11:00:54 +01003783 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003784 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003785 const uint32_t dex_method_index_;
3786 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003787
3788 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3789 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003790
3791 private:
3792 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3793};
3794
Vladimir Markofcb503c2016-05-18 12:48:17 +01003795class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003796 public:
3797 HInvokeUnresolved(ArenaAllocator* arena,
3798 uint32_t number_of_arguments,
3799 Primitive::Type return_type,
3800 uint32_t dex_pc,
3801 uint32_t dex_method_index,
3802 InvokeType invoke_type)
3803 : HInvoke(arena,
3804 number_of_arguments,
3805 0u /* number_of_other_inputs */,
3806 return_type,
3807 dex_pc,
3808 dex_method_index,
3809 invoke_type) {
3810 }
3811
3812 DECLARE_INSTRUCTION(InvokeUnresolved);
3813
3814 private:
3815 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3816};
3817
Vladimir Markofcb503c2016-05-18 12:48:17 +01003818class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003819 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003820 // Requirements of this method call regarding the class
3821 // initialization (clinit) check of its declaring class.
3822 enum class ClinitCheckRequirement {
3823 kNone, // Class already initialized.
3824 kExplicit, // Static call having explicit clinit check as last input.
3825 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003826 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003827 };
3828
Vladimir Marko58155012015-08-19 12:49:41 +00003829 // Determines how to load the target ArtMethod*.
3830 enum class MethodLoadKind {
3831 // Use a String init ArtMethod* loaded from Thread entrypoints.
3832 kStringInit,
3833
3834 // Use the method's own ArtMethod* loaded by the register allocator.
3835 kRecursive,
3836
3837 // Use ArtMethod* at a known address, embed the direct address in the code.
3838 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3839 kDirectAddress,
3840
3841 // Use ArtMethod* at an address that will be known at link time, embed the direct
3842 // address in the code. If the image is relocatable, emit .patch_oat entry.
3843 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3844 // the image relocatable or not.
3845 kDirectAddressWithFixup,
3846
Vladimir Markoa1de9182016-02-25 11:37:38 +00003847 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003848 // Used when we need to use the dex cache, for example for invoke-static that
3849 // may cause class initialization (the entry may point to a resolution method),
3850 // and we know that we can access the dex cache arrays using a PC-relative load.
3851 kDexCachePcRelative,
3852
3853 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3854 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3855 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3856 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3857 kDexCacheViaMethod,
3858 };
3859
3860 // Determines the location of the code pointer.
3861 enum class CodePtrLocation {
3862 // Recursive call, use local PC-relative call instruction.
3863 kCallSelf,
3864
3865 // Use PC-relative call instruction patched at link time.
3866 // Used for calls within an oat file, boot->boot or app->app.
3867 kCallPCRelative,
3868
3869 // Call to a known target address, embed the direct address in code.
3870 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3871 kCallDirect,
3872
3873 // Call to a target address that will be known at link time, embed the direct
3874 // address in code. If the image is relocatable, emit .patch_oat entry.
3875 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3876 // the image relocatable or not.
3877 kCallDirectWithFixup,
3878
3879 // Use code pointer from the ArtMethod*.
3880 // Used when we don't know the target code. This is also the last-resort-kind used when
3881 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3882 kCallArtMethod,
3883 };
3884
3885 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003886 MethodLoadKind method_load_kind;
3887 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003888 // The method load data holds
3889 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3890 // Note that there are multiple string init methods, each having its own offset.
3891 // - the method address for kDirectAddress
3892 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003893 uint64_t method_load_data;
3894 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003895 };
3896
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003897 HInvokeStaticOrDirect(ArenaAllocator* arena,
3898 uint32_t number_of_arguments,
3899 Primitive::Type return_type,
3900 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003901 uint32_t method_index,
3902 MethodReference target_method,
3903 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003904 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003905 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003906 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003907 : HInvoke(arena,
3908 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003909 // There is potentially one extra argument for the HCurrentMethod node, and
3910 // potentially one other if the clinit check is explicit, and potentially
3911 // one other if the method is a string factory.
3912 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003913 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003914 return_type,
3915 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003916 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003917 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003918 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003919 dispatch_info_(dispatch_info) {
3920 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3921 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3922 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003923
Vladimir Markodc151b22015-10-15 18:02:30 +01003924 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003925 bool had_current_method_input = HasCurrentMethodInput();
3926 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3927
3928 // Using the current method is the default and once we find a better
3929 // method load kind, we should not go back to using the current method.
3930 DCHECK(had_current_method_input || !needs_current_method_input);
3931
3932 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003933 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3934 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003935 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003936 dispatch_info_ = dispatch_info;
3937 }
3938
Vladimir Markoc53c0792015-11-19 15:48:33 +00003939 void AddSpecialInput(HInstruction* input) {
3940 // We allow only one special input.
3941 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3942 DCHECK(InputCount() == GetSpecialInputIndex() ||
3943 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3944 InsertInputAt(GetSpecialInputIndex(), input);
3945 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003946
Vladimir Marko372f10e2016-05-17 16:30:10 +01003947 using HInstruction::GetInputRecords; // Keep the const version visible.
3948 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3949 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3950 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3951 DCHECK(!input_records.empty());
3952 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3953 HInstruction* last_input = input_records.back().GetInstruction();
3954 // Note: `last_input` may be null during arguments setup.
3955 if (last_input != nullptr) {
3956 // `last_input` is the last input of a static invoke marked as having
3957 // an explicit clinit check. It must either be:
3958 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3959 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3960 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3961 }
3962 }
3963 return input_records;
3964 }
3965
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003966 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003967 // We access the method via the dex cache so we can't do an implicit null check.
3968 // TODO: for intrinsics we can generate implicit null checks.
3969 return false;
3970 }
3971
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003972 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003973 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003974 }
3975
Vladimir Markoc53c0792015-11-19 15:48:33 +00003976 // Get the index of the special input, if any.
3977 //
David Brazdil6de19382016-01-08 17:37:10 +00003978 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3979 // method pointer; otherwise there may be one platform-specific special input,
3980 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003981 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003982 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003983
Vladimir Markoa1de9182016-02-25 11:37:38 +00003984 InvokeType GetOptimizedInvokeType() const {
3985 return GetPackedField<OptimizedInvokeTypeField>();
3986 }
3987
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003988 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003989 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003990 }
3991
Vladimir Marko58155012015-08-19 12:49:41 +00003992 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3993 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3994 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003995 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003996 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003997 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003998 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003999 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4000 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004001 bool HasCurrentMethodInput() const {
4002 // This function can be called only after the invoke has been fully initialized by the builder.
4003 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004004 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004005 return true;
4006 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004007 DCHECK(InputCount() == GetSpecialInputIndex() ||
4008 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004009 return false;
4010 }
4011 }
Vladimir Marko58155012015-08-19 12:49:41 +00004012 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4013 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004014 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004015
4016 int32_t GetStringInitOffset() const {
4017 DCHECK(IsStringInit());
4018 return dispatch_info_.method_load_data;
4019 }
4020
4021 uint64_t GetMethodAddress() const {
4022 DCHECK(HasMethodAddress());
4023 return dispatch_info_.method_load_data;
4024 }
4025
4026 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004027 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004028 return dispatch_info_.method_load_data;
4029 }
4030
4031 uint64_t GetDirectCodePtr() const {
4032 DCHECK(HasDirectCodePtr());
4033 return dispatch_info_.direct_code_ptr;
4034 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004035
Vladimir Markoa1de9182016-02-25 11:37:38 +00004036 ClinitCheckRequirement GetClinitCheckRequirement() const {
4037 return GetPackedField<ClinitCheckRequirementField>();
4038 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004039
Roland Levillain4c0eb422015-04-24 16:43:49 +01004040 // Is this instruction a call to a static method?
4041 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004042 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004043 }
4044
Vladimir Markofbb184a2015-11-13 14:47:00 +00004045 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4046 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4047 // instruction; only relevant for static calls with explicit clinit check.
4048 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004049 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004050 size_t last_input_index = inputs_.size() - 1u;
4051 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004052 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004053 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004054 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004055 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004056 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004057 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004058 }
4059
4060 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004061 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004062 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004063 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004064 }
4065
4066 // Is this a call to a static method whose declaring class has an
4067 // implicit intialization check requirement?
4068 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004069 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004070 }
4071
Vladimir Markob554b5a2015-11-06 12:57:55 +00004072 // Does this method load kind need the current method as an input?
4073 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4074 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4075 }
4076
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004077 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004078
Roland Levillain4c0eb422015-04-24 16:43:49 +01004079 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004080 void InsertInputAt(size_t index, HInstruction* input);
4081 void RemoveInputAt(size_t index);
4082
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004083 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004084 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4085 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4086 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4087 static constexpr size_t kFieldClinitCheckRequirement =
4088 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4089 static constexpr size_t kFieldClinitCheckRequirementSize =
4090 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4091 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4092 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4093 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4094 "Too many packed fields.");
4095 using OptimizedInvokeTypeField =
4096 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4097 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4098 kFieldClinitCheckRequirement,
4099 kFieldClinitCheckRequirementSize>;
4100
Vladimir Marko58155012015-08-19 12:49:41 +00004101 // The target method may refer to different dex file or method index than the original
4102 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4103 // in derived class to increase visibility.
4104 MethodReference target_method_;
4105 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004106
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004107 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004108};
Vladimir Markof64242a2015-12-01 14:58:23 +00004109std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004110std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004111
Vladimir Markofcb503c2016-05-18 12:48:17 +01004112class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004113 public:
4114 HInvokeVirtual(ArenaAllocator* arena,
4115 uint32_t number_of_arguments,
4116 Primitive::Type return_type,
4117 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004118 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004119 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004120 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004121 vtable_index_(vtable_index) {}
4122
Calin Juravle641547a2015-04-21 22:08:51 +01004123 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004124 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004125 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004126 }
4127
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004128 uint32_t GetVTableIndex() const { return vtable_index_; }
4129
4130 DECLARE_INSTRUCTION(InvokeVirtual);
4131
4132 private:
4133 const uint32_t vtable_index_;
4134
4135 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4136};
4137
Vladimir Markofcb503c2016-05-18 12:48:17 +01004138class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004139 public:
4140 HInvokeInterface(ArenaAllocator* arena,
4141 uint32_t number_of_arguments,
4142 Primitive::Type return_type,
4143 uint32_t dex_pc,
4144 uint32_t dex_method_index,
4145 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004146 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004147 imt_index_(imt_index) {}
4148
Calin Juravle641547a2015-04-21 22:08:51 +01004149 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004150 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004151 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004152 }
4153
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004154 uint32_t GetImtIndex() const { return imt_index_; }
4155 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4156
4157 DECLARE_INSTRUCTION(InvokeInterface);
4158
4159 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004160 const uint32_t imt_index_;
4161
4162 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4163};
4164
Vladimir Markofcb503c2016-05-18 12:48:17 +01004165class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004166 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004167 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004168 : HUnaryOperation(result_type, input, dex_pc) {
4169 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4170 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004171
Roland Levillain9867bc72015-08-05 10:21:34 +01004172 template <typename T> T Compute(T x) const { return -x; }
4173
4174 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004175 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004176 }
4177 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004180 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4181 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4182 }
4183 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4184 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4185 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004186
Roland Levillain88cb1752014-10-20 16:36:47 +01004187 DECLARE_INSTRUCTION(Neg);
4188
4189 private:
4190 DISALLOW_COPY_AND_ASSIGN(HNeg);
4191};
4192
Vladimir Markofcb503c2016-05-18 12:48:17 +01004193class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004194 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004195 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004196 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004197 uint32_t dex_pc,
4198 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004199 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004200 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004201 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004202 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004203 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004204 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004205 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004206 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004207 }
4208
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004209 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004210 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004211
4212 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004213 bool NeedsEnvironment() const OVERRIDE { return true; }
4214
Mingyao Yang0c365e62015-03-31 15:09:29 -07004215 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4216 bool CanThrow() const OVERRIDE { return true; }
4217
Calin Juravle10e244f2015-01-26 18:54:32 +00004218 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004219
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004220 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4221
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004222 DECLARE_INSTRUCTION(NewArray);
4223
4224 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004225 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004226 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004227 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004228
4229 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4230};
4231
Vladimir Markofcb503c2016-05-18 12:48:17 +01004232class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004233 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004234 HAdd(Primitive::Type result_type,
4235 HInstruction* left,
4236 HInstruction* right,
4237 uint32_t dex_pc = kNoDexPc)
4238 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004239
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004240 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004241
Roland Levillain9867bc72015-08-05 10:21:34 +01004242 template <typename T> T Compute(T x, T y) const { return x + y; }
4243
4244 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004245 return GetBlock()->GetGraph()->GetIntConstant(
4246 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004247 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004248 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004249 return GetBlock()->GetGraph()->GetLongConstant(
4250 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004251 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004252 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4253 return GetBlock()->GetGraph()->GetFloatConstant(
4254 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4255 }
4256 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4257 return GetBlock()->GetGraph()->GetDoubleConstant(
4258 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4259 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004260
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004261 DECLARE_INSTRUCTION(Add);
4262
4263 private:
4264 DISALLOW_COPY_AND_ASSIGN(HAdd);
4265};
4266
Vladimir Markofcb503c2016-05-18 12:48:17 +01004267class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004268 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004269 HSub(Primitive::Type result_type,
4270 HInstruction* left,
4271 HInstruction* right,
4272 uint32_t dex_pc = kNoDexPc)
4273 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004274
Roland Levillain9867bc72015-08-05 10:21:34 +01004275 template <typename T> T Compute(T x, T y) const { return x - y; }
4276
4277 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004278 return GetBlock()->GetGraph()->GetIntConstant(
4279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004280 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004281 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004282 return GetBlock()->GetGraph()->GetLongConstant(
4283 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004284 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004285 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4286 return GetBlock()->GetGraph()->GetFloatConstant(
4287 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4288 }
4289 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4290 return GetBlock()->GetGraph()->GetDoubleConstant(
4291 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4292 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004293
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004294 DECLARE_INSTRUCTION(Sub);
4295
4296 private:
4297 DISALLOW_COPY_AND_ASSIGN(HSub);
4298};
4299
Vladimir Markofcb503c2016-05-18 12:48:17 +01004300class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004301 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004302 HMul(Primitive::Type result_type,
4303 HInstruction* left,
4304 HInstruction* right,
4305 uint32_t dex_pc = kNoDexPc)
4306 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004308 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004309
Roland Levillain9867bc72015-08-05 10:21:34 +01004310 template <typename T> T Compute(T x, T y) const { return x * y; }
4311
4312 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004313 return GetBlock()->GetGraph()->GetIntConstant(
4314 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004315 }
4316 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004317 return GetBlock()->GetGraph()->GetLongConstant(
4318 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004319 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004320 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4321 return GetBlock()->GetGraph()->GetFloatConstant(
4322 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4323 }
4324 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4325 return GetBlock()->GetGraph()->GetDoubleConstant(
4326 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4327 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004328
4329 DECLARE_INSTRUCTION(Mul);
4330
4331 private:
4332 DISALLOW_COPY_AND_ASSIGN(HMul);
4333};
4334
Vladimir Markofcb503c2016-05-18 12:48:17 +01004335class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004336 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004337 HDiv(Primitive::Type result_type,
4338 HInstruction* left,
4339 HInstruction* right,
4340 uint32_t dex_pc)
4341 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004342
Roland Levillain9867bc72015-08-05 10:21:34 +01004343 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004344 T ComputeIntegral(T x, T y) const {
4345 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004346 // Our graph structure ensures we never have 0 for `y` during
4347 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004348 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004349 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004350 return (y == -1) ? -x : x / y;
4351 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004352
Roland Levillain31dd3d62016-02-16 12:21:02 +00004353 template <typename T>
4354 T ComputeFP(T x, T y) const {
4355 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4356 return x / y;
4357 }
4358
Roland Levillain9867bc72015-08-05 10:21:34 +01004359 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004360 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004361 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004362 }
4363 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004364 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004365 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4366 }
4367 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4368 return GetBlock()->GetGraph()->GetFloatConstant(
4369 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4370 }
4371 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4372 return GetBlock()->GetGraph()->GetDoubleConstant(
4373 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004374 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004375
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004376 static SideEffects SideEffectsForArchRuntimeCalls() {
4377 // The generated code can use a runtime call.
4378 return SideEffects::CanTriggerGC();
4379 }
4380
Calin Juravle7c4954d2014-10-28 16:57:40 +00004381 DECLARE_INSTRUCTION(Div);
4382
4383 private:
4384 DISALLOW_COPY_AND_ASSIGN(HDiv);
4385};
4386
Vladimir Markofcb503c2016-05-18 12:48:17 +01004387class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004388 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004389 HRem(Primitive::Type result_type,
4390 HInstruction* left,
4391 HInstruction* right,
4392 uint32_t dex_pc)
4393 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004394
Roland Levillain9867bc72015-08-05 10:21:34 +01004395 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004396 T ComputeIntegral(T x, T y) const {
4397 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004398 // Our graph structure ensures we never have 0 for `y` during
4399 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004400 DCHECK_NE(y, 0);
4401 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4402 return (y == -1) ? 0 : x % y;
4403 }
4404
Roland Levillain31dd3d62016-02-16 12:21:02 +00004405 template <typename T>
4406 T ComputeFP(T x, T y) const {
4407 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4408 return std::fmod(x, y);
4409 }
4410
Roland Levillain9867bc72015-08-05 10:21:34 +01004411 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004412 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004413 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004414 }
4415 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004416 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004417 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004418 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004419 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4420 return GetBlock()->GetGraph()->GetFloatConstant(
4421 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4422 }
4423 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4424 return GetBlock()->GetGraph()->GetDoubleConstant(
4425 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4426 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004427
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004428 static SideEffects SideEffectsForArchRuntimeCalls() {
4429 return SideEffects::CanTriggerGC();
4430 }
4431
Calin Juravlebacfec32014-11-14 15:54:36 +00004432 DECLARE_INSTRUCTION(Rem);
4433
4434 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004435 DISALLOW_COPY_AND_ASSIGN(HRem);
4436};
4437
Vladimir Markofcb503c2016-05-18 12:48:17 +01004438class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004439 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004440 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4441 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004442 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004443 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004444 SetRawInputAt(0, value);
4445 }
4446
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004447 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4448
Calin Juravled0d48522014-11-04 16:40:20 +00004449 bool CanBeMoved() const OVERRIDE { return true; }
4450
Vladimir Marko372f10e2016-05-17 16:30:10 +01004451 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004452 return true;
4453 }
4454
4455 bool NeedsEnvironment() const OVERRIDE { return true; }
4456 bool CanThrow() const OVERRIDE { return true; }
4457
Calin Juravled0d48522014-11-04 16:40:20 +00004458 DECLARE_INSTRUCTION(DivZeroCheck);
4459
4460 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004461 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4462};
4463
Vladimir Markofcb503c2016-05-18 12:48:17 +01004464class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004465 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004466 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004467 HInstruction* value,
4468 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004469 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004470 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4471 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4472 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004473 }
4474
Roland Levillain5b5b9312016-03-22 14:57:31 +00004475 template <typename T>
4476 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4477 return value << (distance & max_shift_distance);
4478 }
4479
4480 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004481 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004482 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004483 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004484 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004485 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004486 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004487 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004488 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4489 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4490 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4491 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004492 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004493 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4494 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004495 LOG(FATAL) << DebugName() << " is not defined for float values";
4496 UNREACHABLE();
4497 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004498 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4499 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004500 LOG(FATAL) << DebugName() << " is not defined for double values";
4501 UNREACHABLE();
4502 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004503
4504 DECLARE_INSTRUCTION(Shl);
4505
4506 private:
4507 DISALLOW_COPY_AND_ASSIGN(HShl);
4508};
4509
Vladimir Markofcb503c2016-05-18 12:48:17 +01004510class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004511 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004512 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004513 HInstruction* value,
4514 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004515 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004516 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4517 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4518 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004519 }
4520
Roland Levillain5b5b9312016-03-22 14:57:31 +00004521 template <typename T>
4522 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4523 return value >> (distance & max_shift_distance);
4524 }
4525
4526 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004527 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004528 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004529 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004530 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004531 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004532 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004533 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004534 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4535 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4536 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4537 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004538 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004539 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4540 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004541 LOG(FATAL) << DebugName() << " is not defined for float values";
4542 UNREACHABLE();
4543 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004544 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4545 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004546 LOG(FATAL) << DebugName() << " is not defined for double values";
4547 UNREACHABLE();
4548 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004549
4550 DECLARE_INSTRUCTION(Shr);
4551
4552 private:
4553 DISALLOW_COPY_AND_ASSIGN(HShr);
4554};
4555
Vladimir Markofcb503c2016-05-18 12:48:17 +01004556class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004557 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004558 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004559 HInstruction* value,
4560 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004561 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004562 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4563 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4564 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004565 }
4566
Roland Levillain5b5b9312016-03-22 14:57:31 +00004567 template <typename T>
4568 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4569 typedef typename std::make_unsigned<T>::type V;
4570 V ux = static_cast<V>(value);
4571 return static_cast<T>(ux >> (distance & max_shift_distance));
4572 }
4573
4574 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004575 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004576 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004577 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004578 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004579 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004580 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004581 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004582 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4583 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4584 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4585 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004586 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4588 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004589 LOG(FATAL) << DebugName() << " is not defined for float values";
4590 UNREACHABLE();
4591 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004592 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4593 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004594 LOG(FATAL) << DebugName() << " is not defined for double values";
4595 UNREACHABLE();
4596 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004597
4598 DECLARE_INSTRUCTION(UShr);
4599
4600 private:
4601 DISALLOW_COPY_AND_ASSIGN(HUShr);
4602};
4603
Vladimir Markofcb503c2016-05-18 12:48:17 +01004604class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004605 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004606 HAnd(Primitive::Type result_type,
4607 HInstruction* left,
4608 HInstruction* right,
4609 uint32_t dex_pc = kNoDexPc)
4610 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004611
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004612 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004613
Roland Levillaine53bd812016-02-24 14:54:18 +00004614 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004615
4616 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004617 return GetBlock()->GetGraph()->GetIntConstant(
4618 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004619 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004620 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004621 return GetBlock()->GetGraph()->GetLongConstant(
4622 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004623 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004624 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4625 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4626 LOG(FATAL) << DebugName() << " is not defined for float values";
4627 UNREACHABLE();
4628 }
4629 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4630 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4631 LOG(FATAL) << DebugName() << " is not defined for double values";
4632 UNREACHABLE();
4633 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004634
4635 DECLARE_INSTRUCTION(And);
4636
4637 private:
4638 DISALLOW_COPY_AND_ASSIGN(HAnd);
4639};
4640
Vladimir Markofcb503c2016-05-18 12:48:17 +01004641class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004642 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004643 HOr(Primitive::Type result_type,
4644 HInstruction* left,
4645 HInstruction* right,
4646 uint32_t dex_pc = kNoDexPc)
4647 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004648
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004649 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004650
Roland Levillaine53bd812016-02-24 14:54:18 +00004651 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004652
4653 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 return GetBlock()->GetGraph()->GetIntConstant(
4655 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004657 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004658 return GetBlock()->GetGraph()->GetLongConstant(
4659 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004660 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004661 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4662 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4663 LOG(FATAL) << DebugName() << " is not defined for float values";
4664 UNREACHABLE();
4665 }
4666 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4667 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4668 LOG(FATAL) << DebugName() << " is not defined for double values";
4669 UNREACHABLE();
4670 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004671
4672 DECLARE_INSTRUCTION(Or);
4673
4674 private:
4675 DISALLOW_COPY_AND_ASSIGN(HOr);
4676};
4677
Vladimir Markofcb503c2016-05-18 12:48:17 +01004678class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004679 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 HXor(Primitive::Type result_type,
4681 HInstruction* left,
4682 HInstruction* right,
4683 uint32_t dex_pc = kNoDexPc)
4684 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004685
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004686 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004687
Roland Levillaine53bd812016-02-24 14:54:18 +00004688 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004689
4690 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004691 return GetBlock()->GetGraph()->GetIntConstant(
4692 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004693 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004694 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004695 return GetBlock()->GetGraph()->GetLongConstant(
4696 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004697 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004698 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4699 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4700 LOG(FATAL) << DebugName() << " is not defined for float values";
4701 UNREACHABLE();
4702 }
4703 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4704 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4705 LOG(FATAL) << DebugName() << " is not defined for double values";
4706 UNREACHABLE();
4707 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004708
4709 DECLARE_INSTRUCTION(Xor);
4710
4711 private:
4712 DISALLOW_COPY_AND_ASSIGN(HXor);
4713};
4714
Vladimir Markofcb503c2016-05-18 12:48:17 +01004715class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004716 public:
4717 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004718 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004719 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4720 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004721 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004722
Roland Levillain5b5b9312016-03-22 14:57:31 +00004723 template <typename T>
4724 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4725 typedef typename std::make_unsigned<T>::type V;
4726 V ux = static_cast<V>(value);
4727 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004728 return static_cast<T>(ux);
4729 } else {
4730 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004731 return static_cast<T>(ux >> (distance & max_shift_value)) |
4732 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004733 }
4734 }
4735
Roland Levillain5b5b9312016-03-22 14:57:31 +00004736 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004738 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004739 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004740 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004741 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004742 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004743 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004744 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4745 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4746 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4747 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004748 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004749 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4750 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004751 LOG(FATAL) << DebugName() << " is not defined for float values";
4752 UNREACHABLE();
4753 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004754 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4755 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004756 LOG(FATAL) << DebugName() << " is not defined for double values";
4757 UNREACHABLE();
4758 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004759
4760 DECLARE_INSTRUCTION(Ror);
4761
4762 private:
4763 DISALLOW_COPY_AND_ASSIGN(HRor);
4764};
4765
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004766// The value of a parameter in this method. Its location depends on
4767// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004768class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004769 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004770 HParameterValue(const DexFile& dex_file,
4771 uint16_t type_index,
4772 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004773 Primitive::Type parameter_type,
4774 bool is_this = false)
4775 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004776 dex_file_(dex_file),
4777 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004778 index_(index) {
4779 SetPackedFlag<kFlagIsThis>(is_this);
4780 SetPackedFlag<kFlagCanBeNull>(!is_this);
4781 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004782
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004783 const DexFile& GetDexFile() const { return dex_file_; }
4784 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004785 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004786 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004787
Vladimir Markoa1de9182016-02-25 11:37:38 +00004788 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4789 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004790
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004791 DECLARE_INSTRUCTION(ParameterValue);
4792
4793 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004794 // Whether or not the parameter value corresponds to 'this' argument.
4795 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4796 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4797 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4798 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4799 "Too many packed fields.");
4800
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004801 const DexFile& dex_file_;
4802 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004803 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004804 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004805 const uint8_t index_;
4806
4807 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4808};
4809
Vladimir Markofcb503c2016-05-18 12:48:17 +01004810class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004811 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004812 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4813 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004815 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004816 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004817 return true;
4818 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004819
Roland Levillain9867bc72015-08-05 10:21:34 +01004820 template <typename T> T Compute(T x) const { return ~x; }
4821
4822 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004823 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004824 }
4825 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004826 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004827 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004828 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4829 LOG(FATAL) << DebugName() << " is not defined for float values";
4830 UNREACHABLE();
4831 }
4832 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4833 LOG(FATAL) << DebugName() << " is not defined for double values";
4834 UNREACHABLE();
4835 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004836
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004837 DECLARE_INSTRUCTION(Not);
4838
4839 private:
4840 DISALLOW_COPY_AND_ASSIGN(HNot);
4841};
4842
Vladimir Markofcb503c2016-05-18 12:48:17 +01004843class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004844 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004845 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4846 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004847
4848 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004849 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004850 return true;
4851 }
4852
Roland Levillain9867bc72015-08-05 10:21:34 +01004853 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004854 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004855 return !x;
4856 }
4857
Roland Levillain9867bc72015-08-05 10:21:34 +01004858 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004859 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004860 }
4861 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4862 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004863 UNREACHABLE();
4864 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004865 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4866 LOG(FATAL) << DebugName() << " is not defined for float values";
4867 UNREACHABLE();
4868 }
4869 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4870 LOG(FATAL) << DebugName() << " is not defined for double values";
4871 UNREACHABLE();
4872 }
David Brazdil66d126e2015-04-03 16:02:44 +01004873
4874 DECLARE_INSTRUCTION(BooleanNot);
4875
4876 private:
4877 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4878};
4879
Vladimir Markofcb503c2016-05-18 12:48:17 +01004880class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004881 public:
4882 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004883 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004884 : HExpression(result_type,
4885 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4886 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004887 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004888 // Invariant: We should never generate a conversion to a Boolean value.
4889 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004890 }
4891
4892 HInstruction* GetInput() const { return InputAt(0); }
4893 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4894 Primitive::Type GetResultType() const { return GetType(); }
4895
4896 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004897 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4898 return true;
4899 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004900
Mark Mendelle82549b2015-05-06 10:55:34 -04004901 // Try to statically evaluate the conversion and return a HConstant
4902 // containing the result. If the input cannot be converted, return nullptr.
4903 HConstant* TryStaticEvaluation() const;
4904
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004905 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4906 Primitive::Type result_type) {
4907 // Some architectures may not require the 'GC' side effects, but at this point
4908 // in the compilation process we do not know what architecture we will
4909 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004910 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4911 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004912 return SideEffects::CanTriggerGC();
4913 }
4914 return SideEffects::None();
4915 }
4916
Roland Levillaindff1f282014-11-05 14:15:05 +00004917 DECLARE_INSTRUCTION(TypeConversion);
4918
4919 private:
4920 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4921};
4922
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004923static constexpr uint32_t kNoRegNumber = -1;
4924
Vladimir Markofcb503c2016-05-18 12:48:17 +01004925class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004926 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004927 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4928 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004929 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004930 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004931 SetRawInputAt(0, value);
4932 }
4933
Calin Juravle10e244f2015-01-26 18:54:32 +00004934 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004935 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004936 return true;
4937 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004938
Calin Juravle10e244f2015-01-26 18:54:32 +00004939 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004940
Calin Juravle10e244f2015-01-26 18:54:32 +00004941 bool CanThrow() const OVERRIDE { return true; }
4942
4943 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004944
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004945
4946 DECLARE_INSTRUCTION(NullCheck);
4947
4948 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004949 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4950};
4951
4952class FieldInfo : public ValueObject {
4953 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004954 FieldInfo(MemberOffset field_offset,
4955 Primitive::Type field_type,
4956 bool is_volatile,
4957 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004958 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004959 const DexFile& dex_file,
4960 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004961 : field_offset_(field_offset),
4962 field_type_(field_type),
4963 is_volatile_(is_volatile),
4964 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004965 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004966 dex_file_(dex_file),
4967 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004968
4969 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004970 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004971 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004972 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004973 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004974 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004975 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004976
4977 private:
4978 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004979 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004980 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004981 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004982 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004983 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004984 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985};
4986
Vladimir Markofcb503c2016-05-18 12:48:17 +01004987class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988 public:
4989 HInstanceFieldGet(HInstruction* value,
4990 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004991 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004992 bool is_volatile,
4993 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004994 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004995 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004996 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004997 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004998 : HExpression(field_type,
4999 SideEffects::FieldReadOfType(field_type, is_volatile),
5000 dex_pc),
5001 field_info_(field_offset,
5002 field_type,
5003 is_volatile,
5004 field_idx,
5005 declaring_class_def_index,
5006 dex_file,
5007 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005008 SetRawInputAt(0, value);
5009 }
5010
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005011 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005012
Vladimir Marko372f10e2016-05-17 16:30:10 +01005013 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5014 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005015 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005016 }
5017
Calin Juravle641547a2015-04-21 22:08:51 +01005018 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5019 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005020 }
5021
5022 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005023 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5024 }
5025
Calin Juravle52c48962014-12-16 17:02:57 +00005026 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005027 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005028 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005029 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005030
5031 DECLARE_INSTRUCTION(InstanceFieldGet);
5032
5033 private:
5034 const FieldInfo field_info_;
5035
5036 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5037};
5038
Vladimir Markofcb503c2016-05-18 12:48:17 +01005039class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005040 public:
5041 HInstanceFieldSet(HInstruction* object,
5042 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005043 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005044 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005045 bool is_volatile,
5046 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005047 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005048 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005049 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005050 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005051 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5052 dex_pc),
5053 field_info_(field_offset,
5054 field_type,
5055 is_volatile,
5056 field_idx,
5057 declaring_class_def_index,
5058 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005059 dex_cache) {
5060 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005061 SetRawInputAt(0, object);
5062 SetRawInputAt(1, value);
5063 }
5064
Calin Juravle641547a2015-04-21 22:08:51 +01005065 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5066 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005067 }
5068
Calin Juravle52c48962014-12-16 17:02:57 +00005069 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005070 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005071 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005072 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005073 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005074 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5075 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005076
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005077 DECLARE_INSTRUCTION(InstanceFieldSet);
5078
5079 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005080 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5081 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5082 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5083 "Too many packed fields.");
5084
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005085 const FieldInfo field_info_;
5086
5087 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5088};
5089
Vladimir Markofcb503c2016-05-18 12:48:17 +01005090class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005091 public:
Aart Bik18b36ab2016-04-13 16:41:35 -07005092 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type, uint32_t dex_pc)
5093 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005094 SetRawInputAt(0, array);
5095 SetRawInputAt(1, index);
5096 }
5097
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005098 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005099 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005100 return true;
5101 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005102 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005103 // TODO: We can be smarter here.
5104 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5105 // which generates the implicit null check. There are cases when these can be removed
5106 // to produce better code. If we ever add optimizations to do so we should allow an
5107 // implicit check here (as long as the address falls in the first page).
5108 return false;
5109 }
5110
David Brazdil4833f5a2015-12-16 10:37:39 +00005111 bool IsEquivalentOf(HArrayGet* other) const {
5112 bool result = (GetDexPc() == other->GetDexPc());
5113 if (kIsDebugBuild && result) {
5114 DCHECK_EQ(GetBlock(), other->GetBlock());
5115 DCHECK_EQ(GetArray(), other->GetArray());
5116 DCHECK_EQ(GetIndex(), other->GetIndex());
5117 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005118 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005119 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005120 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5121 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005122 }
5123 }
5124 return result;
5125 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005126
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005127 HInstruction* GetArray() const { return InputAt(0); }
5128 HInstruction* GetIndex() const { return InputAt(1); }
5129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005130 DECLARE_INSTRUCTION(ArrayGet);
5131
5132 private:
5133 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5134};
5135
Vladimir Markofcb503c2016-05-18 12:48:17 +01005136class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 public:
5138 HArraySet(HInstruction* array,
5139 HInstruction* index,
5140 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005141 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005142 uint32_t dex_pc)
5143 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005144 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5145 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5146 SetPackedFlag<kFlagValueCanBeNull>(true);
5147 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 SetRawInputAt(0, array);
5149 SetRawInputAt(1, index);
5150 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005151 // Make a best guess now, may be refined during SSA building.
5152 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 }
5154
Calin Juravle77520bc2015-01-12 18:45:46 +00005155 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005156 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005157 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005158 }
5159
Mingyao Yang81014cb2015-06-02 03:16:27 -07005160 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005161 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005162
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005163 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005164 // TODO: Same as for ArrayGet.
5165 return false;
5166 }
5167
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005168 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005169 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005170 }
5171
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005172 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005173 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005174 }
5175
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005176 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005177 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005178 }
5179
Vladimir Markoa1de9182016-02-25 11:37:38 +00005180 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5181 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5182 bool StaticTypeOfArrayIsObjectArray() const {
5183 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5184 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005185
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005186 HInstruction* GetArray() const { return InputAt(0); }
5187 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005188 HInstruction* GetValue() const { return InputAt(2); }
5189
5190 Primitive::Type GetComponentType() const {
5191 // The Dex format does not type floating point index operations. Since the
5192 // `expected_component_type_` is set during building and can therefore not
5193 // be correct, we also check what is the value type. If it is a floating
5194 // point type, we must use that type.
5195 Primitive::Type value_type = GetValue()->GetType();
5196 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5197 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005198 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005199 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005200
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005201 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005202 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005203 }
5204
Aart Bik18b36ab2016-04-13 16:41:35 -07005205 void ComputeSideEffects() {
5206 Primitive::Type type = GetComponentType();
5207 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5208 SideEffectsForArchRuntimeCalls(type)));
5209 }
5210
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005211 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5212 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5213 }
5214
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 DECLARE_INSTRUCTION(ArraySet);
5216
5217 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005218 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5219 static constexpr size_t kFieldExpectedComponentTypeSize =
5220 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5221 static constexpr size_t kFlagNeedsTypeCheck =
5222 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5223 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005224 // Cached information for the reference_type_info_ so that codegen
5225 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005226 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5227 static constexpr size_t kNumberOfArraySetPackedBits =
5228 kFlagStaticTypeOfArrayIsObjectArray + 1;
5229 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5230 using ExpectedComponentTypeField =
5231 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005232
5233 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5234};
5235
Vladimir Markofcb503c2016-05-18 12:48:17 +01005236class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005238 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005239 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005240 // Note that arrays do not change length, so the instruction does not
5241 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005242 SetRawInputAt(0, array);
5243 }
5244
Calin Juravle77520bc2015-01-12 18:45:46 +00005245 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005246 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005247 return true;
5248 }
Calin Juravle641547a2015-04-21 22:08:51 +01005249 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5250 return obj == InputAt(0);
5251 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005252
Vladimir Markodce016e2016-04-28 13:10:02 +01005253 void MarkAsStringLength() { SetPackedFlag<kFlagIsStringLength>(); }
5254 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5255
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256 DECLARE_INSTRUCTION(ArrayLength);
5257
5258 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005259 // We treat a String as an array, creating the HArrayLength from String.length()
5260 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5261 // determine whether a particular HArrayLength is actually a String.length() by
5262 // looking at the type of the input but that requires holding the mutator lock, so
5263 // we prefer to use a flag, so that code generators don't need to do the locking.
5264 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5265 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5266 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5267 "Too many packed fields.");
5268
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5270};
5271
Vladimir Markofcb503c2016-05-18 12:48:17 +01005272class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005274 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5275 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005277 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005278 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 SetRawInputAt(0, index);
5280 SetRawInputAt(1, length);
5281 }
5282
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005283 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005284 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005285 return true;
5286 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005287
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005288 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005289
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005290 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005291
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005292 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293
5294 DECLARE_INSTRUCTION(BoundsCheck);
5295
5296 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005297 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5298};
5299
Vladimir Markofcb503c2016-05-18 12:48:17 +01005300class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005301 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005302 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005303 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005304
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005305 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005306 return true;
5307 }
5308
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005309 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5310 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005311
5312 DECLARE_INSTRUCTION(SuspendCheck);
5313
5314 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005315 // Only used for code generation, in order to share the same slow path between back edges
5316 // of a same loop.
5317 SlowPathCode* slow_path_;
5318
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005319 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5320};
5321
David Srbecky0cf44932015-12-09 14:09:59 +00005322// Pseudo-instruction which provides the native debugger with mapping information.
5323// It ensures that we can generate line number and local variables at this point.
5324class HNativeDebugInfo : public HTemplateInstruction<0> {
5325 public:
5326 explicit HNativeDebugInfo(uint32_t dex_pc)
5327 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5328
5329 bool NeedsEnvironment() const OVERRIDE {
5330 return true;
5331 }
5332
5333 DECLARE_INSTRUCTION(NativeDebugInfo);
5334
5335 private:
5336 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5337};
5338
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005339/**
5340 * Instruction to load a Class object.
5341 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005342class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005343 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005344 // Determines how to load the Class.
5345 enum class LoadKind {
5346 // Use the Class* from the method's own ArtMethod*.
5347 kReferrersClass,
5348
5349 // Use boot image Class* address that will be known at link time.
5350 // Used for boot image classes referenced by boot image code in non-PIC mode.
5351 kBootImageLinkTimeAddress,
5352
5353 // Use PC-relative boot image Class* address that will be known at link time.
5354 // Used for boot image classes referenced by boot image code in PIC mode.
5355 kBootImageLinkTimePcRelative,
5356
5357 // Use a known boot image Class* address, embedded in the code by the codegen.
5358 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5359 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5360 // GetIncludePatchInformation().
5361 kBootImageAddress,
5362
5363 // Load from the resolved types array at an absolute address.
5364 // Used for classes outside the boot image referenced by JIT-compiled code.
5365 kDexCacheAddress,
5366
5367 // Load from resolved types array in the dex cache using a PC-relative load.
5368 // Used for classes outside boot image when we know that we can access
5369 // the dex cache arrays using a PC-relative load.
5370 kDexCachePcRelative,
5371
5372 // Load from resolved types array accessed through the class loaded from
5373 // the compiled method's own ArtMethod*. This is the default access type when
5374 // all other types are unavailable.
5375 kDexCacheViaMethod,
5376
5377 kLast = kDexCacheViaMethod
5378 };
5379
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005380 HLoadClass(HCurrentMethod* current_method,
5381 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005382 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005383 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005384 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005385 bool needs_access_check,
5386 bool is_in_dex_cache)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005387 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5388 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005389 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005390 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005391 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005392 // Referrers class should not need access check. We never inline unverified
5393 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005394 DCHECK(!is_referrers_class || !needs_access_check);
5395
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005396 SetPackedField<LoadKindField>(
5397 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005398 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5399 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5400 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005401 }
5402
5403 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5404 DCHECK(HasAddress(load_kind));
5405 load_data_.address = address;
5406 SetLoadKindInternal(load_kind);
5407 }
5408
5409 void SetLoadKindWithTypeReference(LoadKind load_kind,
5410 const DexFile& dex_file,
5411 uint32_t type_index) {
5412 DCHECK(HasTypeReference(load_kind));
5413 DCHECK(IsSameDexFile(dex_file_, dex_file));
5414 DCHECK_EQ(type_index_, type_index);
5415 SetLoadKindInternal(load_kind);
5416 }
5417
5418 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5419 const DexFile& dex_file,
5420 uint32_t element_index) {
5421 DCHECK(HasDexCacheReference(load_kind));
5422 DCHECK(IsSameDexFile(dex_file_, dex_file));
5423 load_data_.dex_cache_element_index = element_index;
5424 SetLoadKindInternal(load_kind);
5425 }
5426
5427 LoadKind GetLoadKind() const {
5428 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005429 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005430
5431 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005432
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005433 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005434
5435 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5436
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005437 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005438
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005439 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005440 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005441 }
5442
Calin Juravle0ba218d2015-05-19 18:46:01 +01005443 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005444 // The entrypoint the code generator is going to call does not do
5445 // clinit of the class.
5446 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005447 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005448 }
5449
5450 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005451 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005452 (!IsReferrersClass() && !IsInDexCache()) ||
5453 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005454 }
5455
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005456
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005457 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005458 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005459 }
5460
Calin Juravleacf735c2015-02-12 15:25:22 +00005461 ReferenceTypeInfo GetLoadedClassRTI() {
5462 return loaded_class_rti_;
5463 }
5464
5465 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5466 // Make sure we only set exact types (the loaded class should never be merged).
5467 DCHECK(rti.IsExact());
5468 loaded_class_rti_ = rti;
5469 }
5470
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005471 uint32_t GetTypeIndex() const { return type_index_; }
5472 const DexFile& GetDexFile() const { return dex_file_; }
5473
5474 uint32_t GetDexCacheElementOffset() const;
5475
5476 uint64_t GetAddress() const {
5477 DCHECK(HasAddress(GetLoadKind()));
5478 return load_data_.address;
5479 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005480
Vladimir Markoa1de9182016-02-25 11:37:38 +00005481 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005482
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005483 static SideEffects SideEffectsForArchRuntimeCalls() {
5484 return SideEffects::CanTriggerGC();
5485 }
5486
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005487 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005488 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5489 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5490 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005491
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005492 void MarkInDexCache() {
5493 SetPackedFlag<kFlagIsInDexCache>(true);
5494 DCHECK(!NeedsEnvironment());
5495 RemoveEnvironment();
5496 SetSideEffects(SideEffects::None());
5497 }
5498
5499 void AddSpecialInput(HInstruction* special_input);
5500
5501 using HInstruction::GetInputRecords; // Keep the const version visible.
5502 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5503 return ArrayRef<HUserRecord<HInstruction*>>(
5504 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5505 }
5506
5507 Primitive::Type GetType() const OVERRIDE {
5508 return Primitive::kPrimNot;
5509 }
5510
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005511 DECLARE_INSTRUCTION(LoadClass);
5512
5513 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005514 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005515 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005516 // Whether this instruction must generate the initialization check.
5517 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005518 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005519 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5520 static constexpr size_t kFieldLoadKindSize =
5521 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5522 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005523 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005524 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5525
5526 static bool HasTypeReference(LoadKind load_kind) {
5527 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5528 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5529 load_kind == LoadKind::kDexCacheViaMethod ||
5530 load_kind == LoadKind::kReferrersClass;
5531 }
5532
5533 static bool HasAddress(LoadKind load_kind) {
5534 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5535 }
5536
5537 static bool HasDexCacheReference(LoadKind load_kind) {
5538 return load_kind == LoadKind::kDexCachePcRelative;
5539 }
5540
5541 void SetLoadKindInternal(LoadKind load_kind);
5542
5543 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5544 // For other load kinds it's empty or possibly some architecture-specific instruction
5545 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5546 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005547
5548 const uint16_t type_index_;
5549 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005550
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005551 union {
5552 uint32_t dex_cache_element_index; // Only for dex cache reference.
5553 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5554 } load_data_;
5555
Calin Juravleacf735c2015-02-12 15:25:22 +00005556 ReferenceTypeInfo loaded_class_rti_;
5557
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005558 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5559};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005560std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5561
5562// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5563inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5564 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5565 return load_data_.dex_cache_element_index;
5566}
5567
5568// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5569inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
5570 // The special input is used for PC-relative loads on some architectures.
5571 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5572 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5573 DCHECK(special_input_.GetInstruction() == nullptr);
5574 special_input_ = HUserRecord<HInstruction*>(special_input);
5575 special_input->AddUseAt(this, 0);
5576}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005577
Vladimir Marko372f10e2016-05-17 16:30:10 +01005578class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005579 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005580 // Determines how to load the String.
5581 enum class LoadKind {
5582 // Use boot image String* address that will be known at link time.
5583 // Used for boot image strings referenced by boot image code in non-PIC mode.
5584 kBootImageLinkTimeAddress,
5585
5586 // Use PC-relative boot image String* address that will be known at link time.
5587 // Used for boot image strings referenced by boot image code in PIC mode.
5588 kBootImageLinkTimePcRelative,
5589
5590 // Use a known boot image String* address, embedded in the code by the codegen.
5591 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5592 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5593 // GetIncludePatchInformation().
5594 kBootImageAddress,
5595
5596 // Load from the resolved strings array at an absolute address.
5597 // Used for strings outside the boot image referenced by JIT-compiled code.
5598 kDexCacheAddress,
5599
5600 // Load from resolved strings array in the dex cache using a PC-relative load.
5601 // Used for strings outside boot image when we know that we can access
5602 // the dex cache arrays using a PC-relative load.
5603 kDexCachePcRelative,
5604
5605 // Load from resolved strings array accessed through the class loaded from
5606 // the compiled method's own ArtMethod*. This is the default access type when
5607 // all other types are unavailable.
5608 kDexCacheViaMethod,
5609
5610 kLast = kDexCacheViaMethod
5611 };
5612
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005613 HLoadString(HCurrentMethod* current_method,
5614 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005615 const DexFile& dex_file,
5616 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005617 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5618 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005619 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005620 SetPackedFlag<kFlagIsInDexCache>(false);
5621 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5622 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005623 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005624
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005625 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5626 DCHECK(HasAddress(load_kind));
5627 load_data_.address = address;
5628 SetLoadKindInternal(load_kind);
5629 }
5630
5631 void SetLoadKindWithStringReference(LoadKind load_kind,
5632 const DexFile& dex_file,
5633 uint32_t string_index) {
5634 DCHECK(HasStringReference(load_kind));
5635 load_data_.ref.dex_file = &dex_file;
5636 string_index_ = string_index;
5637 SetLoadKindInternal(load_kind);
5638 }
5639
5640 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5641 const DexFile& dex_file,
5642 uint32_t element_index) {
5643 DCHECK(HasDexCacheReference(load_kind));
5644 load_data_.ref.dex_file = &dex_file;
5645 load_data_.ref.dex_cache_element_index = element_index;
5646 SetLoadKindInternal(load_kind);
5647 }
5648
5649 LoadKind GetLoadKind() const {
5650 return GetPackedField<LoadKindField>();
5651 }
5652
5653 const DexFile& GetDexFile() const;
5654
5655 uint32_t GetStringIndex() const {
5656 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5657 return string_index_;
5658 }
5659
5660 uint32_t GetDexCacheElementOffset() const;
5661
5662 uint64_t GetAddress() const {
5663 DCHECK(HasAddress(GetLoadKind()));
5664 return load_data_.address;
5665 }
5666
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005667 bool CanBeMoved() const OVERRIDE { return true; }
5668
Vladimir Marko372f10e2016-05-17 16:30:10 +01005669 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005670
5671 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5672
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005673 // Will call the runtime if we need to load the string through
5674 // the dex cache and the string is not guaranteed to be there yet.
5675 bool NeedsEnvironment() const OVERRIDE {
5676 LoadKind load_kind = GetLoadKind();
5677 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5678 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5679 load_kind == LoadKind::kBootImageAddress) {
5680 return false;
5681 }
5682 return !IsInDexCache();
5683 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005684
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005685 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5686 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5687 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005688
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005689 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005690 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005691
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005692 static SideEffects SideEffectsForArchRuntimeCalls() {
5693 return SideEffects::CanTriggerGC();
5694 }
5695
Vladimir Markoa1de9182016-02-25 11:37:38 +00005696 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5697
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005698 void MarkInDexCache() {
5699 SetPackedFlag<kFlagIsInDexCache>(true);
5700 DCHECK(!NeedsEnvironment());
5701 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005702 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005703 }
5704
Vladimir Marko372f10e2016-05-17 16:30:10 +01005705 void AddSpecialInput(HInstruction* special_input);
5706
5707 using HInstruction::GetInputRecords; // Keep the const version visible.
5708 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5709 return ArrayRef<HUserRecord<HInstruction*>>(
5710 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005711 }
5712
Vladimir Marko372f10e2016-05-17 16:30:10 +01005713 Primitive::Type GetType() const OVERRIDE {
5714 return Primitive::kPrimNot;
5715 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005716
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005717 DECLARE_INSTRUCTION(LoadString);
5718
5719 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005720 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005721 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5722 static constexpr size_t kFieldLoadKindSize =
5723 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5724 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005725 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005726 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005727
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005728 static bool HasStringReference(LoadKind load_kind) {
5729 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5730 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5731 load_kind == LoadKind::kDexCacheViaMethod;
5732 }
5733
5734 static bool HasAddress(LoadKind load_kind) {
5735 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5736 }
5737
5738 static bool HasDexCacheReference(LoadKind load_kind) {
5739 return load_kind == LoadKind::kDexCachePcRelative;
5740 }
5741
5742 void SetLoadKindInternal(LoadKind load_kind);
5743
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005744 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5745 // For other load kinds it's empty or possibly some architecture-specific instruction
5746 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005747 HUserRecord<HInstruction*> special_input_;
5748
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005749 // String index serves also as the hash code and it's also needed for slow-paths,
5750 // so it must not be overwritten with other load data.
5751 uint32_t string_index_;
5752
5753 union {
5754 struct {
5755 const DexFile* dex_file; // For string reference and dex cache reference.
5756 uint32_t dex_cache_element_index; // Only for dex cache reference.
5757 } ref;
5758 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5759 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005760
5761 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5762};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005763std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5764
5765// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5766inline const DexFile& HLoadString::GetDexFile() const {
5767 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5768 << GetLoadKind();
5769 return *load_data_.ref.dex_file;
5770}
5771
5772// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5773inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5774 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5775 return load_data_.ref.dex_cache_element_index;
5776}
5777
5778// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5779inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5780 // The special input is used for PC-relative loads on some architectures.
5781 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5782 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005783 // HLoadString::GetInputRecords() returns an empty array at this point,
5784 // so use the GetInputRecords() from the base class to set the input record.
5785 DCHECK(special_input_.GetInstruction() == nullptr);
5786 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005787 special_input->AddUseAt(this, 0);
5788}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005789
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005790/**
5791 * Performs an initialization check on its Class object input.
5792 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005793class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005794 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005795 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005796 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005797 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005798 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5799 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005800 SetRawInputAt(0, constant);
5801 }
5802
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005803 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005804 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005805 return true;
5806 }
5807
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005808 bool NeedsEnvironment() const OVERRIDE {
5809 // May call runtime to initialize the class.
5810 return true;
5811 }
5812
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005813 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005814
5815 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5816
5817 DECLARE_INSTRUCTION(ClinitCheck);
5818
5819 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005820 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5821};
5822
Vladimir Markofcb503c2016-05-18 12:48:17 +01005823class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005824 public:
5825 HStaticFieldGet(HInstruction* cls,
5826 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005827 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005828 bool is_volatile,
5829 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005830 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005831 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005832 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005833 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005834 : HExpression(field_type,
5835 SideEffects::FieldReadOfType(field_type, is_volatile),
5836 dex_pc),
5837 field_info_(field_offset,
5838 field_type,
5839 is_volatile,
5840 field_idx,
5841 declaring_class_def_index,
5842 dex_file,
5843 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005844 SetRawInputAt(0, cls);
5845 }
5846
Calin Juravle52c48962014-12-16 17:02:57 +00005847
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005848 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005849
Vladimir Marko372f10e2016-05-17 16:30:10 +01005850 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5851 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005852 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005853 }
5854
5855 size_t ComputeHashCode() const OVERRIDE {
5856 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5857 }
5858
Calin Juravle52c48962014-12-16 17:02:57 +00005859 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005860 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5861 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005862 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005863
5864 DECLARE_INSTRUCTION(StaticFieldGet);
5865
5866 private:
5867 const FieldInfo field_info_;
5868
5869 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5870};
5871
Vladimir Markofcb503c2016-05-18 12:48:17 +01005872class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005873 public:
5874 HStaticFieldSet(HInstruction* cls,
5875 HInstruction* value,
5876 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005877 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005878 bool is_volatile,
5879 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005880 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005881 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005882 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005883 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005884 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5885 dex_pc),
5886 field_info_(field_offset,
5887 field_type,
5888 is_volatile,
5889 field_idx,
5890 declaring_class_def_index,
5891 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005892 dex_cache) {
5893 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005894 SetRawInputAt(0, cls);
5895 SetRawInputAt(1, value);
5896 }
5897
Calin Juravle52c48962014-12-16 17:02:57 +00005898 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005899 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5900 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005901 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005902
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005903 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005904 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5905 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005906
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005907 DECLARE_INSTRUCTION(StaticFieldSet);
5908
5909 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005910 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5911 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5912 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5913 "Too many packed fields.");
5914
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005915 const FieldInfo field_info_;
5916
5917 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5918};
5919
Vladimir Markofcb503c2016-05-18 12:48:17 +01005920class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005921 public:
5922 HUnresolvedInstanceFieldGet(HInstruction* obj,
5923 Primitive::Type field_type,
5924 uint32_t field_index,
5925 uint32_t dex_pc)
5926 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5927 field_index_(field_index) {
5928 SetRawInputAt(0, obj);
5929 }
5930
5931 bool NeedsEnvironment() const OVERRIDE { return true; }
5932 bool CanThrow() const OVERRIDE { return true; }
5933
5934 Primitive::Type GetFieldType() const { return GetType(); }
5935 uint32_t GetFieldIndex() const { return field_index_; }
5936
5937 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5938
5939 private:
5940 const uint32_t field_index_;
5941
5942 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5943};
5944
Vladimir Markofcb503c2016-05-18 12:48:17 +01005945class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005946 public:
5947 HUnresolvedInstanceFieldSet(HInstruction* obj,
5948 HInstruction* value,
5949 Primitive::Type field_type,
5950 uint32_t field_index,
5951 uint32_t dex_pc)
5952 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005953 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005954 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005955 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005956 SetRawInputAt(0, obj);
5957 SetRawInputAt(1, value);
5958 }
5959
5960 bool NeedsEnvironment() const OVERRIDE { return true; }
5961 bool CanThrow() const OVERRIDE { return true; }
5962
Vladimir Markoa1de9182016-02-25 11:37:38 +00005963 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005964 uint32_t GetFieldIndex() const { return field_index_; }
5965
5966 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5967
5968 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5970 static constexpr size_t kFieldFieldTypeSize =
5971 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5972 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5973 kFieldFieldType + kFieldFieldTypeSize;
5974 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5975 "Too many packed fields.");
5976 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5977
Calin Juravlee460d1d2015-09-29 04:52:17 +01005978 const uint32_t field_index_;
5979
5980 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5981};
5982
Vladimir Markofcb503c2016-05-18 12:48:17 +01005983class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005984 public:
5985 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5986 uint32_t field_index,
5987 uint32_t dex_pc)
5988 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5989 field_index_(field_index) {
5990 }
5991
5992 bool NeedsEnvironment() const OVERRIDE { return true; }
5993 bool CanThrow() const OVERRIDE { return true; }
5994
5995 Primitive::Type GetFieldType() const { return GetType(); }
5996 uint32_t GetFieldIndex() const { return field_index_; }
5997
5998 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5999
6000 private:
6001 const uint32_t field_index_;
6002
6003 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6004};
6005
Vladimir Markofcb503c2016-05-18 12:48:17 +01006006class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006007 public:
6008 HUnresolvedStaticFieldSet(HInstruction* value,
6009 Primitive::Type field_type,
6010 uint32_t field_index,
6011 uint32_t dex_pc)
6012 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006013 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006014 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006015 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006016 SetRawInputAt(0, value);
6017 }
6018
6019 bool NeedsEnvironment() const OVERRIDE { return true; }
6020 bool CanThrow() const OVERRIDE { return true; }
6021
Vladimir Markoa1de9182016-02-25 11:37:38 +00006022 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006023 uint32_t GetFieldIndex() const { return field_index_; }
6024
6025 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6026
6027 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006028 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6029 static constexpr size_t kFieldFieldTypeSize =
6030 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6031 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6032 kFieldFieldType + kFieldFieldTypeSize;
6033 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6034 "Too many packed fields.");
6035 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6036
Calin Juravlee460d1d2015-09-29 04:52:17 +01006037 const uint32_t field_index_;
6038
6039 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6040};
6041
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006042// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006043class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006044 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006045 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6046 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006047
David Brazdilbbd733e2015-08-18 17:48:17 +01006048 bool CanBeNull() const OVERRIDE { return false; }
6049
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006050 DECLARE_INSTRUCTION(LoadException);
6051
6052 private:
6053 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6054};
6055
David Brazdilcb1c0552015-08-04 16:22:25 +01006056// Implicit part of move-exception which clears thread-local exception storage.
6057// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006058class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006059 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006060 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6061 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006062
6063 DECLARE_INSTRUCTION(ClearException);
6064
6065 private:
6066 DISALLOW_COPY_AND_ASSIGN(HClearException);
6067};
6068
Vladimir Markofcb503c2016-05-18 12:48:17 +01006069class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006070 public:
6071 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006072 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006073 SetRawInputAt(0, exception);
6074 }
6075
6076 bool IsControlFlow() const OVERRIDE { return true; }
6077
6078 bool NeedsEnvironment() const OVERRIDE { return true; }
6079
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006080 bool CanThrow() const OVERRIDE { return true; }
6081
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006082
6083 DECLARE_INSTRUCTION(Throw);
6084
6085 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006086 DISALLOW_COPY_AND_ASSIGN(HThrow);
6087};
6088
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006089/**
6090 * Implementation strategies for the code generator of a HInstanceOf
6091 * or `HCheckCast`.
6092 */
6093enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006094 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006095 kExactCheck, // Can do a single class compare.
6096 kClassHierarchyCheck, // Can just walk the super class chain.
6097 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6098 kInterfaceCheck, // No optimization yet when checking against an interface.
6099 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006100 kArrayCheck, // No optimization yet when checking against a generic array.
6101 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006102};
6103
Roland Levillain86503782016-02-11 19:07:30 +00006104std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6105
Vladimir Markofcb503c2016-05-18 12:48:17 +01006106class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006107 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006108 HInstanceOf(HInstruction* object,
6109 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006110 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006111 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006112 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006113 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006114 dex_pc) {
6115 SetPackedField<TypeCheckKindField>(check_kind);
6116 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006117 SetRawInputAt(0, object);
6118 SetRawInputAt(1, constant);
6119 }
6120
6121 bool CanBeMoved() const OVERRIDE { return true; }
6122
Vladimir Marko372f10e2016-05-17 16:30:10 +01006123 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006124 return true;
6125 }
6126
6127 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006128 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006129 }
6130
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006131 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006132 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6133 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6134 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6135 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006136
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006137 static bool CanCallRuntime(TypeCheckKind check_kind) {
6138 // Mips currently does runtime calls for any other checks.
6139 return check_kind != TypeCheckKind::kExactCheck;
6140 }
6141
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006143 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006144 }
6145
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006146 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006147
6148 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006149 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6150 static constexpr size_t kFieldTypeCheckKindSize =
6151 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6152 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6153 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6154 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6155 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006156
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006157 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6158};
6159
Vladimir Markofcb503c2016-05-18 12:48:17 +01006160class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006161 public:
David Brazdilf5552582015-12-27 13:36:12 +00006162 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006163 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006164 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6165 SetPackedFlag<kFlagUpperCanBeNull>(true);
6166 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006167 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006168 SetRawInputAt(0, input);
6169 }
6170
David Brazdilf5552582015-12-27 13:36:12 +00006171 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006172 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006173 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006174 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006175
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006176 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006177 DCHECK(GetUpperCanBeNull() || !can_be_null);
6178 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006179 }
6180
Vladimir Markoa1de9182016-02-25 11:37:38 +00006181 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006182
Calin Juravleb1498f62015-02-16 13:13:29 +00006183 DECLARE_INSTRUCTION(BoundType);
6184
6185 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006186 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6187 // is false then CanBeNull() cannot be true).
6188 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6189 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6190 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6191 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6192
Calin Juravleb1498f62015-02-16 13:13:29 +00006193 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006194 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6195 // It is used to bound the type in cases like:
6196 // if (x instanceof ClassX) {
6197 // // uper_bound_ will be ClassX
6198 // }
David Brazdilf5552582015-12-27 13:36:12 +00006199 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006200
6201 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6202};
6203
Vladimir Markofcb503c2016-05-18 12:48:17 +01006204class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006205 public:
6206 HCheckCast(HInstruction* object,
6207 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006208 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006209 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006210 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6211 SetPackedField<TypeCheckKindField>(check_kind);
6212 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006213 SetRawInputAt(0, object);
6214 SetRawInputAt(1, constant);
6215 }
6216
6217 bool CanBeMoved() const OVERRIDE { return true; }
6218
Vladimir Marko372f10e2016-05-17 16:30:10 +01006219 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006220 return true;
6221 }
6222
6223 bool NeedsEnvironment() const OVERRIDE {
6224 // Instruction may throw a CheckCastError.
6225 return true;
6226 }
6227
6228 bool CanThrow() const OVERRIDE { return true; }
6229
Vladimir Markoa1de9182016-02-25 11:37:38 +00006230 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6231 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6232 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6233 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006234
6235 DECLARE_INSTRUCTION(CheckCast);
6236
6237 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006238 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6239 static constexpr size_t kFieldTypeCheckKindSize =
6240 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6241 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6242 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6243 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6244 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006245
6246 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006247};
6248
Vladimir Markofcb503c2016-05-18 12:48:17 +01006249class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006250 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006251 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006252 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006253 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6254 SetPackedField<BarrierKindField>(barrier_kind);
6255 }
Calin Juravle27df7582015-04-17 19:12:31 +01006256
Vladimir Markoa1de9182016-02-25 11:37:38 +00006257 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006258
6259 DECLARE_INSTRUCTION(MemoryBarrier);
6260
6261 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006262 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6263 static constexpr size_t kFieldBarrierKindSize =
6264 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6265 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6266 kFieldBarrierKind + kFieldBarrierKindSize;
6267 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6268 "Too many packed fields.");
6269 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006270
6271 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6272};
6273
Vladimir Markofcb503c2016-05-18 12:48:17 +01006274class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006275 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006276 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006277 kEnter,
6278 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006279 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006280 };
6281
6282 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006283 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006284 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6285 dex_pc) {
6286 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006287 SetRawInputAt(0, object);
6288 }
6289
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006290 // Instruction may go into runtime, so we need an environment.
6291 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006292
6293 bool CanThrow() const OVERRIDE {
6294 // Verifier guarantees that monitor-exit cannot throw.
6295 // This is important because it allows the HGraphBuilder to remove
6296 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6297 return IsEnter();
6298 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006299
Vladimir Markoa1de9182016-02-25 11:37:38 +00006300 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6301 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006302
6303 DECLARE_INSTRUCTION(MonitorOperation);
6304
Calin Juravle52c48962014-12-16 17:02:57 +00006305 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006306 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6307 static constexpr size_t kFieldOperationKindSize =
6308 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6309 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6310 kFieldOperationKind + kFieldOperationKindSize;
6311 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6312 "Too many packed fields.");
6313 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006314
6315 private:
6316 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6317};
6318
Vladimir Markofcb503c2016-05-18 12:48:17 +01006319class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006320 public:
6321 HSelect(HInstruction* condition,
6322 HInstruction* true_value,
6323 HInstruction* false_value,
6324 uint32_t dex_pc)
6325 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6326 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6327
6328 // First input must be `true_value` or `false_value` to allow codegens to
6329 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6330 // that architectures which implement HSelect as a conditional move also
6331 // will not need to invert the condition.
6332 SetRawInputAt(0, false_value);
6333 SetRawInputAt(1, true_value);
6334 SetRawInputAt(2, condition);
6335 }
6336
6337 HInstruction* GetFalseValue() const { return InputAt(0); }
6338 HInstruction* GetTrueValue() const { return InputAt(1); }
6339 HInstruction* GetCondition() const { return InputAt(2); }
6340
6341 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006342 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6343 return true;
6344 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006345
6346 bool CanBeNull() const OVERRIDE {
6347 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6348 }
6349
6350 DECLARE_INSTRUCTION(Select);
6351
6352 private:
6353 DISALLOW_COPY_AND_ASSIGN(HSelect);
6354};
6355
Vladimir Markof9f64412015-09-02 14:05:49 +01006356class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006357 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006358 MoveOperands(Location source,
6359 Location destination,
6360 Primitive::Type type,
6361 HInstruction* instruction)
6362 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006363
6364 Location GetSource() const { return source_; }
6365 Location GetDestination() const { return destination_; }
6366
6367 void SetSource(Location value) { source_ = value; }
6368 void SetDestination(Location value) { destination_ = value; }
6369
6370 // The parallel move resolver marks moves as "in-progress" by clearing the
6371 // destination (but not the source).
6372 Location MarkPending() {
6373 DCHECK(!IsPending());
6374 Location dest = destination_;
6375 destination_ = Location::NoLocation();
6376 return dest;
6377 }
6378
6379 void ClearPending(Location dest) {
6380 DCHECK(IsPending());
6381 destination_ = dest;
6382 }
6383
6384 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006385 DCHECK(source_.IsValid() || destination_.IsInvalid());
6386 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006387 }
6388
6389 // True if this blocks a move from the given location.
6390 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006391 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006392 }
6393
6394 // A move is redundant if it's been eliminated, if its source and
6395 // destination are the same, or if its destination is unneeded.
6396 bool IsRedundant() const {
6397 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6398 }
6399
6400 // We clear both operands to indicate move that's been eliminated.
6401 void Eliminate() {
6402 source_ = destination_ = Location::NoLocation();
6403 }
6404
6405 bool IsEliminated() const {
6406 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6407 return source_.IsInvalid();
6408 }
6409
Alexey Frunze4dda3372015-06-01 18:31:49 -07006410 Primitive::Type GetType() const { return type_; }
6411
Nicolas Geoffray90218252015-04-15 11:56:51 +01006412 bool Is64BitMove() const {
6413 return Primitive::Is64BitType(type_);
6414 }
6415
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006416 HInstruction* GetInstruction() const { return instruction_; }
6417
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006418 private:
6419 Location source_;
6420 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006421 // The type this move is for.
6422 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006423 // The instruction this move is assocatied with. Null when this move is
6424 // for moving an input in the expected locations of user (including a phi user).
6425 // This is only used in debug mode, to ensure we do not connect interval siblings
6426 // in the same parallel move.
6427 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006428};
6429
Roland Levillainc9285912015-12-18 10:38:42 +00006430std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6431
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006432static constexpr size_t kDefaultNumberOfMoves = 4;
6433
Vladimir Markofcb503c2016-05-18 12:48:17 +01006434class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006435 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006436 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006437 : HTemplateInstruction(SideEffects::None(), dex_pc),
6438 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6439 moves_.reserve(kDefaultNumberOfMoves);
6440 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006441
Nicolas Geoffray90218252015-04-15 11:56:51 +01006442 void AddMove(Location source,
6443 Location destination,
6444 Primitive::Type type,
6445 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006446 DCHECK(source.IsValid());
6447 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006448 if (kIsDebugBuild) {
6449 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006450 for (const MoveOperands& move : moves_) {
6451 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006452 // Special case the situation where the move is for the spill slot
6453 // of the instruction.
6454 if ((GetPrevious() == instruction)
6455 || ((GetPrevious() == nullptr)
6456 && instruction->IsPhi()
6457 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006458 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006459 << "Doing parallel moves for the same instruction.";
6460 } else {
6461 DCHECK(false) << "Doing parallel moves for the same instruction.";
6462 }
6463 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006464 }
6465 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006466 for (const MoveOperands& move : moves_) {
6467 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006468 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006469 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006470 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006471 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006472 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006473 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006474 }
6475
Vladimir Marko225b6462015-09-28 12:17:40 +01006476 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006477 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006478 }
6479
Vladimir Marko225b6462015-09-28 12:17:40 +01006480 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006481
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006482 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006483
6484 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006485 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006486
6487 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6488};
6489
Mark Mendell0616ae02015-04-17 12:49:27 -04006490} // namespace art
6491
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006492#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6493#include "nodes_shared.h"
6494#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006495#ifdef ART_ENABLE_CODEGEN_arm
6496#include "nodes_arm.h"
6497#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006498#ifdef ART_ENABLE_CODEGEN_arm64
6499#include "nodes_arm64.h"
6500#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006501#ifdef ART_ENABLE_CODEGEN_x86
6502#include "nodes_x86.h"
6503#endif
6504
6505namespace art {
6506
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006507class HGraphVisitor : public ValueObject {
6508 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006509 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6510 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006511
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006512 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006513 virtual void VisitBasicBlock(HBasicBlock* block);
6514
Roland Levillain633021e2014-10-01 14:12:25 +01006515 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006516 void VisitInsertionOrder();
6517
Roland Levillain633021e2014-10-01 14:12:25 +01006518 // Visit the graph following dominator tree reverse post-order.
6519 void VisitReversePostOrder();
6520
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006521 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006522
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006523 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006524#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006525 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6526
6527 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6528
6529#undef DECLARE_VISIT_INSTRUCTION
6530
6531 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006532 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006533
6534 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6535};
6536
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006537class HGraphDelegateVisitor : public HGraphVisitor {
6538 public:
6539 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6540 virtual ~HGraphDelegateVisitor() {}
6541
6542 // Visit functions that delegate to to super class.
6543#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006544 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006545
6546 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6547
6548#undef DECLARE_VISIT_INSTRUCTION
6549
6550 private:
6551 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6552};
6553
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006554class HInsertionOrderIterator : public ValueObject {
6555 public:
6556 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6557
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006558 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006559 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006560 void Advance() { ++index_; }
6561
6562 private:
6563 const HGraph& graph_;
6564 size_t index_;
6565
6566 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6567};
6568
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006569class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006570 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006571 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6572 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006573 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006574 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006575
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006576 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6577 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006578 void Advance() { ++index_; }
6579
6580 private:
6581 const HGraph& graph_;
6582 size_t index_;
6583
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006584 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006585};
6586
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006587class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006588 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006589 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006590 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006591 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006592 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006593 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006594
6595 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006596 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006597 void Advance() { --index_; }
6598
6599 private:
6600 const HGraph& graph_;
6601 size_t index_;
6602
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006603 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006604};
6605
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006606class HLinearPostOrderIterator : public ValueObject {
6607 public:
6608 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006609 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006610
6611 bool Done() const { return index_ == 0; }
6612
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006613 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006614
6615 void Advance() {
6616 --index_;
6617 DCHECK_GE(index_, 0U);
6618 }
6619
6620 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006621 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006622 size_t index_;
6623
6624 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6625};
6626
6627class HLinearOrderIterator : public ValueObject {
6628 public:
6629 explicit HLinearOrderIterator(const HGraph& graph)
6630 : order_(graph.GetLinearOrder()), index_(0) {}
6631
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006632 bool Done() const { return index_ == order_.size(); }
6633 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006634 void Advance() { ++index_; }
6635
6636 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006637 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006638 size_t index_;
6639
6640 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6641};
6642
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006643// Iterator over the blocks that art part of the loop. Includes blocks part
6644// of an inner loop. The order in which the blocks are iterated is on their
6645// block id.
6646class HBlocksInLoopIterator : public ValueObject {
6647 public:
6648 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6649 : blocks_in_loop_(info.GetBlocks()),
6650 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6651 index_(0) {
6652 if (!blocks_in_loop_.IsBitSet(index_)) {
6653 Advance();
6654 }
6655 }
6656
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006657 bool Done() const { return index_ == blocks_.size(); }
6658 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006659 void Advance() {
6660 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006661 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006662 if (blocks_in_loop_.IsBitSet(index_)) {
6663 break;
6664 }
6665 }
6666 }
6667
6668 private:
6669 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006670 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006671 size_t index_;
6672
6673 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6674};
6675
Mingyao Yang3584bce2015-05-19 16:01:59 -07006676// Iterator over the blocks that art part of the loop. Includes blocks part
6677// of an inner loop. The order in which the blocks are iterated is reverse
6678// post order.
6679class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6680 public:
6681 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6682 : blocks_in_loop_(info.GetBlocks()),
6683 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6684 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006685 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006686 Advance();
6687 }
6688 }
6689
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006690 bool Done() const { return index_ == blocks_.size(); }
6691 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006692 void Advance() {
6693 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006694 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6695 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006696 break;
6697 }
6698 }
6699 }
6700
6701 private:
6702 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006703 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006704 size_t index_;
6705
6706 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6707};
6708
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006709inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006710 if (constant->IsIntConstant()) {
6711 return constant->AsIntConstant()->GetValue();
6712 } else if (constant->IsLongConstant()) {
6713 return constant->AsLongConstant()->GetValue();
6714 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006715 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006716 return 0;
6717 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006718}
6719
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006720#define INSTRUCTION_TYPE_CHECK(type, super) \
6721 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6722 inline const H##type* HInstruction::As##type() const { \
6723 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6724 } \
6725 inline H##type* HInstruction::As##type() { \
6726 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6727 }
6728
6729 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6730#undef INSTRUCTION_TYPE_CHECK
6731
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006732// Create space in `blocks` for adding `number_of_new_blocks` entries
6733// starting at location `at`. Blocks after `at` are moved accordingly.
6734inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6735 size_t number_of_new_blocks,
6736 size_t after) {
6737 DCHECK_LT(after, blocks->size());
6738 size_t old_size = blocks->size();
6739 size_t new_size = old_size + number_of_new_blocks;
6740 blocks->resize(new_size);
6741 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6742}
6743
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006744} // namespace art
6745
6746#endif // ART_COMPILER_OPTIMIZING_NODES_H_