blob: d98dd0608b19650322aa9f159b2c7bc45081e477 [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"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010029#include "dex_file.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000030#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000031#include "handle.h"
32#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000033#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010034#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000035#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000036#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010037#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070038#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000039#include "utils/array_ref.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010040#include "utils/intrusive_forward_list.h"
Vladimir Marko372f10e2016-05-17 16:30:10 +010041#include "utils/transform_array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
43namespace art {
44
David Brazdil1abb4192015-02-17 18:33:36 +000045class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000051class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000054class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000055class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000057class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010058class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010059class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010060class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000062class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010063class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000064class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000065
Mathieu Chartier736b5602015-09-02 14:54:11 -070066namespace mirror {
67class DexCache;
68} // namespace mirror
69
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070static const int kDefaultNumberOfBlocks = 8;
71static const int kDefaultNumberOfSuccessors = 2;
72static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010073static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010074static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076
Roland Levillain5b5b9312016-03-22 14:57:31 +000077// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
78static constexpr int32_t kMaxIntShiftDistance = 0x1f;
79// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
80static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000081
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010082static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070083static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010085static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
86
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060087static constexpr uint32_t kNoDexPc = -1;
88
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010089inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
90 // For the purposes of the compiler, the dex files must actually be the same object
91 // if we want to safely treat them as the same. This is especially important for JIT
92 // as custom class loaders can open the same underlying file (or memory) multiple
93 // times and provide different class resolution but no two class loaders should ever
94 // use the same DexFile object - doing so is an unsupported hack that can lead to
95 // all sorts of weird failures.
96 return &lhs == &rhs;
97}
98
Dave Allison20dfc792014-06-16 20:44:29 -070099enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700100 // All types.
101 kCondEQ, // ==
102 kCondNE, // !=
103 // Signed integers and floating-point numbers.
104 kCondLT, // <
105 kCondLE, // <=
106 kCondGT, // >
107 kCondGE, // >=
108 // Unsigned integers.
109 kCondB, // <
110 kCondBE, // <=
111 kCondA, // >
112 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700113};
114
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000115enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000116 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000117 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000118 kAnalysisFailThrowCatchLoop,
119 kAnalysisFailAmbiguousArrayOp,
120 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000121};
122
Vladimir Markof9f64412015-09-02 14:05:49 +0100123class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100124 public:
125 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
126
127 void AddInstruction(HInstruction* instruction);
128 void RemoveInstruction(HInstruction* instruction);
129
David Brazdilc3d743f2015-04-22 13:40:50 +0100130 // Insert `instruction` before/after an existing instruction `cursor`.
131 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
132 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
133
Roland Levillain6b469232014-09-25 10:10:38 +0100134 // Return true if this list contains `instruction`.
135 bool Contains(HInstruction* instruction) const;
136
Roland Levillainccc07a92014-09-16 14:48:16 +0100137 // Return true if `instruction1` is found before `instruction2` in
138 // this instruction list and false otherwise. Abort if none
139 // of these instructions is found.
140 bool FoundBefore(const HInstruction* instruction1,
141 const HInstruction* instruction2) const;
142
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000143 bool IsEmpty() const { return first_instruction_ == nullptr; }
144 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
145
146 // Update the block of all instructions to be `block`.
147 void SetBlockOfInstructions(HBasicBlock* block) const;
148
149 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000150 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000151 void Add(const HInstructionList& instruction_list);
152
David Brazdil2d7352b2015-04-20 14:52:42 +0100153 // Return the number of instructions in the list. This is an expensive operation.
154 size_t CountSize() const;
155
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100156 private:
157 HInstruction* first_instruction_;
158 HInstruction* last_instruction_;
159
160 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000161 friend class HGraph;
162 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100163 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100164 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100165
166 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
167};
168
David Brazdil4833f5a2015-12-16 10:37:39 +0000169class ReferenceTypeInfo : ValueObject {
170 public:
171 typedef Handle<mirror::Class> TypeHandle;
172
Vladimir Markoa1de9182016-02-25 11:37:38 +0000173 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
174
David Brazdil94ab38f2016-06-21 17:48:19 +0100175 static ReferenceTypeInfo Create(TypeHandle type_handle) SHARED_REQUIRES(Locks::mutator_lock_) {
176 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
177 }
178
Vladimir Markoa1de9182016-02-25 11:37:38 +0000179 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000180 return ReferenceTypeInfo(type_handle, is_exact);
181 }
182
183 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
184
Vladimir Markof39745e2016-01-26 12:16:55 +0000185 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000186 return handle.GetReference() != nullptr;
187 }
188
Vladimir Marko456307a2016-04-19 14:12:13 +0000189 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000190 return IsValidHandle(type_handle_);
191 }
192
193 bool IsExact() const { return is_exact_; }
194
195 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
196 DCHECK(IsValid());
197 return GetTypeHandle()->IsObjectClass();
198 }
199
200 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
201 DCHECK(IsValid());
202 return GetTypeHandle()->IsStringClass();
203 }
204
205 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
206 DCHECK(IsValid());
207 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
208 }
209
210 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
211 DCHECK(IsValid());
212 return GetTypeHandle()->IsInterface();
213 }
214
215 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
216 DCHECK(IsValid());
217 return GetTypeHandle()->IsArrayClass();
218 }
219
220 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
221 DCHECK(IsValid());
222 return GetTypeHandle()->IsPrimitiveArray();
223 }
224
225 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
226 DCHECK(IsValid());
227 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
228 }
229
230 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
231 DCHECK(IsValid());
232 if (!IsExact()) return false;
233 if (!IsArrayClass()) return false;
234 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
235 }
236
237 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
238 DCHECK(IsValid());
239 if (!IsExact()) return false;
240 if (!IsArrayClass()) return false;
241 if (!rti.IsArrayClass()) return false;
242 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
243 rti.GetTypeHandle()->GetComponentType());
244 }
245
246 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
247
248 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
249 DCHECK(IsValid());
250 DCHECK(rti.IsValid());
251 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
252 }
253
254 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
255 DCHECK(IsValid());
256 DCHECK(rti.IsValid());
257 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
258 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
259 }
260
261 // Returns true if the type information provide the same amount of details.
262 // Note that it does not mean that the instructions have the same actual type
263 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000264 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000265 if (!IsValid() && !rti.IsValid()) {
266 // Invalid types are equal.
267 return true;
268 }
269 if (!IsValid() || !rti.IsValid()) {
270 // One is valid, the other not.
271 return false;
272 }
273 return IsExact() == rti.IsExact()
274 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
275 }
276
277 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000278 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
279 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
280 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000281
282 // The class of the object.
283 TypeHandle type_handle_;
284 // Whether or not the type is exact or a superclass of the actual type.
285 // Whether or not we have any information about this type.
286 bool is_exact_;
287};
288
289std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
290
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000291// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100292class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000293 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100294 HGraph(ArenaAllocator* arena,
295 const DexFile& dex_file,
296 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100297 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100299 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100300 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000301 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100302 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000303 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100304 blocks_(arena->Adapter(kArenaAllocBlockList)),
305 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
306 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700307 entry_block_(nullptr),
308 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100309 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100310 number_of_vregs_(0),
311 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000312 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400313 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000314 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000315 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000316 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000317 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100318 dex_file_(dex_file),
319 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100320 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100321 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100322 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000324 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100325 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
326 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
327 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
328 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000329 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000330 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
331 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100332 blocks_.reserve(kDefaultNumberOfBlocks);
333 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334
David Brazdilbadd8262016-02-02 16:28:56 +0000335 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
336 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
337
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000338 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100339 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
340
David Brazdil69ba7b72015-06-23 18:27:30 +0100341 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000342 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100343
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000344 HBasicBlock* GetEntryBlock() const { return entry_block_; }
345 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100346 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000347
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000348 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
349 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000350
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100352
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100353 void ComputeDominanceInformation();
354 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000355 void ClearLoopInformation();
356 void FindBackEdges(ArenaBitVector* visited);
357 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100358 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100359 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000360
David Brazdil4833f5a2015-12-16 10:37:39 +0000361 // Analyze all natural loops in this graph. Returns a code specifying that it
362 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000363 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000364 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365
David Brazdilffee3d32015-07-06 11:48:53 +0100366 // Iterate over blocks to compute try block membership. Needs reverse post
367 // order and loop information.
368 void ComputeTryBlockInformation();
369
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000370 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000371 // Returns the instruction to replace the invoke expression or null if the
372 // invoke is for a void method. Note that the caller is responsible for replacing
373 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000374 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000375
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000376 // Update the loop and try membership of `block`, which was spawned from `reference`.
377 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
378 // should be the new back edge.
379 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
380 HBasicBlock* reference,
381 bool replace_if_back_edge);
382
Mingyao Yang3584bce2015-05-19 16:01:59 -0700383 // Need to add a couple of blocks to test if the loop body is entered and
384 // put deoptimization instructions, etc.
385 void TransformLoopHeaderForBCE(HBasicBlock* header);
386
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000387 // Removes `block` from the graph. Assumes `block` has been disconnected from
388 // other blocks and has no instructions or phis.
389 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000390
David Brazdilfc6a86a2015-06-26 10:33:45 +0000391 // Splits the edge between `block` and `successor` while preserving the
392 // indices in the predecessor/successor lists. If there are multiple edges
393 // between the blocks, the lowest indices are used.
394 // Returns the new block which is empty and has the same dex pc as `successor`.
395 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
396
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100397 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
398 void SimplifyLoop(HBasicBlock* header);
399
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000400 int32_t GetNextInstructionId() {
401 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000402 return current_instruction_id_++;
403 }
404
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000405 int32_t GetCurrentInstructionId() const {
406 return current_instruction_id_;
407 }
408
409 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000410 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000411 current_instruction_id_ = id;
412 }
413
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100414 uint16_t GetMaximumNumberOfOutVRegs() const {
415 return maximum_number_of_out_vregs_;
416 }
417
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
419 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100420 }
421
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100422 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
423 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
424 }
425
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000426 void UpdateTemporariesVRegSlots(size_t slots) {
427 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100428 }
429
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000430 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100431 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000432 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100433 }
434
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100435 void SetNumberOfVRegs(uint16_t number_of_vregs) {
436 number_of_vregs_ = number_of_vregs;
437 }
438
439 uint16_t GetNumberOfVRegs() const {
440 return number_of_vregs_;
441 }
442
443 void SetNumberOfInVRegs(uint16_t value) {
444 number_of_in_vregs_ = value;
445 }
446
David Brazdildee58d62016-04-07 09:54:26 +0000447 uint16_t GetNumberOfInVRegs() const {
448 return number_of_in_vregs_;
449 }
450
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100451 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100452 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100453 return number_of_vregs_ - number_of_in_vregs_;
454 }
455
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100456 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100457 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100458 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100459
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100460 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100461 return linear_order_;
462 }
463
Mark Mendell1152c922015-04-24 17:06:35 -0400464 bool HasBoundsChecks() const {
465 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800466 }
467
Mark Mendell1152c922015-04-24 17:06:35 -0400468 void SetHasBoundsChecks(bool value) {
469 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800470 }
471
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100472 bool ShouldGenerateConstructorBarrier() const {
473 return should_generate_constructor_barrier_;
474 }
475
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000476 bool IsDebuggable() const { return debuggable_; }
477
David Brazdil8d5b8b22015-03-24 10:51:52 +0000478 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000479 // already, it is created and inserted into the graph. This method is only for
480 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600481 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000482
483 // TODO: This is problematic for the consistency of reference type propagation
484 // because it can be created anytime after the pass and thus it will be left
485 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600486 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000487
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600488 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
489 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000490 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600491 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
492 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000493 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600494 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
495 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000496 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600497 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
498 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000499 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000500
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100501 HCurrentMethod* GetCurrentMethod();
502
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100503 const DexFile& GetDexFile() const {
504 return dex_file_;
505 }
506
507 uint32_t GetMethodIdx() const {
508 return method_idx_;
509 }
510
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100511 InvokeType GetInvokeType() const {
512 return invoke_type_;
513 }
514
Mark Mendellc4701932015-04-10 13:18:51 -0400515 InstructionSet GetInstructionSet() const {
516 return instruction_set_;
517 }
518
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000519 bool IsCompilingOsr() const { return osr_; }
520
David Brazdil77a48ae2015-09-15 12:34:04 +0000521 bool HasTryCatch() const { return has_try_catch_; }
522 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100523
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000524 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
525 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
526
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100527 ArtMethod* GetArtMethod() const { return art_method_; }
528 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
529
Mark Mendellf6529172015-11-17 11:16:56 -0500530 // Returns an instruction with the opposite boolean value from 'cond'.
531 // The instruction has been inserted into the graph, either as a constant, or
532 // before cursor.
533 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
534
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000535 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
536
David Brazdil2d7352b2015-04-20 14:52:42 +0100537 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000538 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100539 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000540
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000541 template <class InstructionType, typename ValueType>
542 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600543 ArenaSafeMap<ValueType, InstructionType*>* cache,
544 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000545 // Try to find an existing constant of the given value.
546 InstructionType* constant = nullptr;
547 auto cached_constant = cache->find(value);
548 if (cached_constant != cache->end()) {
549 constant = cached_constant->second;
550 }
551
552 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100553 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000554 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600555 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000556 cache->Overwrite(value, constant);
557 InsertConstant(constant);
558 }
559 return constant;
560 }
561
David Brazdil8d5b8b22015-03-24 10:51:52 +0000562 void InsertConstant(HConstant* instruction);
563
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000564 // Cache a float constant into the graph. This method should only be
565 // called by the SsaBuilder when creating "equivalent" instructions.
566 void CacheFloatConstant(HFloatConstant* constant);
567
568 // See CacheFloatConstant comment.
569 void CacheDoubleConstant(HDoubleConstant* constant);
570
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000571 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000572
573 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100574 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000575
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100576 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100577 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000578
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100579 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100580 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100581
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000582 HBasicBlock* entry_block_;
583 HBasicBlock* exit_block_;
584
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100585 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100586 uint16_t maximum_number_of_out_vregs_;
587
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100588 // The number of virtual registers in this method. Contains the parameters.
589 uint16_t number_of_vregs_;
590
591 // The number of virtual registers used by parameters of this method.
592 uint16_t number_of_in_vregs_;
593
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000594 // Number of vreg size slots that the temporaries use (used in baseline compiler).
595 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100596
Mark Mendell1152c922015-04-24 17:06:35 -0400597 // Has bounds checks. We can totally skip BCE if it's false.
598 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800599
David Brazdil77a48ae2015-09-15 12:34:04 +0000600 // Flag whether there are any try/catch blocks in the graph. We will skip
601 // try/catch-related passes if false.
602 bool has_try_catch_;
603
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000604 // Flag whether there are any irreducible loops in the graph.
605 bool has_irreducible_loops_;
606
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000607 // Indicates whether the graph should be compiled in a way that
608 // ensures full debuggability. If false, we can apply more
609 // aggressive optimizations that may limit the level of debugging.
610 const bool debuggable_;
611
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000612 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000613 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000614
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100615 // The dex file from which the method is from.
616 const DexFile& dex_file_;
617
618 // The method index in the dex file.
619 const uint32_t method_idx_;
620
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100621 // If inlined, this encodes how the callee is being invoked.
622 const InvokeType invoke_type_;
623
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100624 // Whether the graph has been transformed to SSA form. Only used
625 // in debug mode to ensure we are not using properties only valid
626 // for non-SSA form (like the number of temporaries).
627 bool in_ssa_form_;
628
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100629 const bool should_generate_constructor_barrier_;
630
Mathieu Chartiere401d142015-04-22 13:56:20 -0700631 const InstructionSet instruction_set_;
632
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000633 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000634 HNullConstant* cached_null_constant_;
635 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000636 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000637 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000638 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000639
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100640 HCurrentMethod* cached_current_method_;
641
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100642 // The ArtMethod this graph is for. Note that for AOT, it may be null,
643 // for example for methods whose declaring class could not be resolved
644 // (such as when the superclass could not be found).
645 ArtMethod* art_method_;
646
David Brazdil4833f5a2015-12-16 10:37:39 +0000647 // Keep the RTI of inexact Object to avoid having to pass stack handle
648 // collection pointer to passes which may create NullConstant.
649 ReferenceTypeInfo inexact_object_rti_;
650
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000651 // Whether we are compiling this graph for on stack replacement: this will
652 // make all loops seen as irreducible and emit special stack maps to mark
653 // compiled code entries which the interpreter can directly jump to.
654 const bool osr_;
655
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000656 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100657 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000658 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000659 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660 DISALLOW_COPY_AND_ASSIGN(HGraph);
661};
662
Vladimir Markof9f64412015-09-02 14:05:49 +0100663class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000664 public:
665 HLoopInformation(HBasicBlock* header, HGraph* graph)
666 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100667 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000668 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100669 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100670 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100671 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000672 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100673 back_edges_.reserve(kDefaultNumberOfBackEdges);
674 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100675
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000676 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100677 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000678
679 void Dump(std::ostream& os);
680
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 HBasicBlock* GetHeader() const {
682 return header_;
683 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000684
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000685 void SetHeader(HBasicBlock* block) {
686 header_ = block;
687 }
688
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100689 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
690 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
691 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
692
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100694 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000695 }
696
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100698 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100699 }
700
David Brazdil46e2a392015-03-16 17:31:52 +0000701 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 }
704
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000705 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100706 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000707 }
708
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100709 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100710
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100711 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100712 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100713 }
714
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100715 // Returns the lifetime position of the back edge that has the
716 // greatest lifetime position.
717 size_t GetLifetimeEnd() const;
718
719 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100720 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100721 }
722
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000723 // Finds blocks that are part of this loop.
724 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100725
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100726 // Returns whether this loop information contains `block`.
727 // Note that this loop information *must* be populated before entering this function.
728 bool Contains(const HBasicBlock& block) const;
729
730 // Returns whether this loop information is an inner loop of `other`.
731 // Note that `other` *must* be populated before entering this function.
732 bool IsIn(const HLoopInformation& other) const;
733
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800734 // Returns true if instruction is not defined within this loop.
735 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700736
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100737 const ArenaBitVector& GetBlocks() const { return blocks_; }
738
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000739 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000740 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000741
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000742 void ClearAllBlocks() {
743 blocks_.ClearAllBits();
744 }
745
David Brazdil3f4a5222016-05-06 12:46:21 +0100746 bool HasBackEdgeNotDominatedByHeader() const;
747
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100748 bool IsPopulated() const {
749 return blocks_.GetHighestBitSet() != -1;
750 }
751
Anton Shaminf89381f2016-05-16 16:44:13 +0600752 bool DominatesAllBackEdges(HBasicBlock* block);
753
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000754 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100755 // Internal recursive implementation of `Populate`.
756 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100757 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000759 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100760 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000761 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100762 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100763 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000765
766 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
767};
768
David Brazdilec16f792015-08-19 15:04:01 +0100769// Stores try/catch information for basic blocks.
770// Note that HGraph is constructed so that catch blocks cannot simultaneously
771// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100772class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100773 public:
774 // Try block information constructor.
775 explicit TryCatchInformation(const HTryBoundary& try_entry)
776 : try_entry_(&try_entry),
777 catch_dex_file_(nullptr),
778 catch_type_index_(DexFile::kDexNoIndex16) {
779 DCHECK(try_entry_ != nullptr);
780 }
781
782 // Catch block information constructor.
783 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
784 : try_entry_(nullptr),
785 catch_dex_file_(&dex_file),
786 catch_type_index_(catch_type_index) {}
787
788 bool IsTryBlock() const { return try_entry_ != nullptr; }
789
790 const HTryBoundary& GetTryEntry() const {
791 DCHECK(IsTryBlock());
792 return *try_entry_;
793 }
794
795 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
796
797 bool IsCatchAllTypeIndex() const {
798 DCHECK(IsCatchBlock());
799 return catch_type_index_ == DexFile::kDexNoIndex16;
800 }
801
802 uint16_t GetCatchTypeIndex() const {
803 DCHECK(IsCatchBlock());
804 return catch_type_index_;
805 }
806
807 const DexFile& GetCatchDexFile() const {
808 DCHECK(IsCatchBlock());
809 return *catch_dex_file_;
810 }
811
812 private:
813 // One of possibly several TryBoundary instructions entering the block's try.
814 // Only set for try blocks.
815 const HTryBoundary* try_entry_;
816
817 // Exception type information. Only set for catch blocks.
818 const DexFile* catch_dex_file_;
819 const uint16_t catch_type_index_;
820};
821
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100822static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100823static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100824
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000825// A block in a method. Contains the list of instructions represented
826// as a double linked list. Each block knows its predecessors and
827// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100828
Vladimir Markof9f64412015-09-02 14:05:49 +0100829class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600831 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000832 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000833 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
834 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000835 loop_information_(nullptr),
836 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000837 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100838 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100839 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100840 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000841 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000842 try_catch_information_(nullptr) {
843 predecessors_.reserve(kDefaultNumberOfPredecessors);
844 successors_.reserve(kDefaultNumberOfSuccessors);
845 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
846 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000847
Vladimir Marko60584552015-09-03 13:35:12 +0000848 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000850 }
851
Vladimir Marko60584552015-09-03 13:35:12 +0000852 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100853 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000854 }
855
David Brazdild26a4112015-11-10 11:07:31 +0000856 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
857 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
858
Vladimir Marko60584552015-09-03 13:35:12 +0000859 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
860 return ContainsElement(successors_, block, start_from);
861 }
862
863 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100864 return dominated_blocks_;
865 }
866
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100867 bool IsEntryBlock() const {
868 return graph_->GetEntryBlock() == this;
869 }
870
871 bool IsExitBlock() const {
872 return graph_->GetExitBlock() == this;
873 }
874
David Brazdil46e2a392015-03-16 17:31:52 +0000875 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000876 bool IsSingleTryBoundary() const;
877
878 // Returns true if this block emits nothing but a jump.
879 bool IsSingleJump() const {
880 HLoopInformation* loop_info = GetLoopInformation();
881 return (IsSingleGoto() || IsSingleTryBoundary())
882 // Back edges generate a suspend check.
883 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
884 }
David Brazdil46e2a392015-03-16 17:31:52 +0000885
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000886 void AddBackEdge(HBasicBlock* back_edge) {
887 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000888 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000889 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100890 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000891 loop_information_->AddBackEdge(back_edge);
892 }
893
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000894 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000895 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100897 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000898 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600899 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000901 HBasicBlock* GetDominator() const { return dominator_; }
902 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000903 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
904
905 void RemoveDominatedBlock(HBasicBlock* block) {
906 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100907 }
Vladimir Marko60584552015-09-03 13:35:12 +0000908
909 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
910 ReplaceElement(dominated_blocks_, existing, new_block);
911 }
912
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100913 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000914
915 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100916 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000917 }
918
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100919 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
920 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100921 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100922 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100923 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
924 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000925
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000926 HInstruction* GetFirstInstructionDisregardMoves() const;
927
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000928 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000929 successors_.push_back(block);
930 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000931 }
932
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100933 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
934 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100935 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000936 new_block->predecessors_.push_back(this);
937 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000938 }
939
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000940 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
941 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000942 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000943 new_block->successors_.push_back(this);
944 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000945 }
946
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100947 // Insert `this` between `predecessor` and `successor. This method
948 // preserves the indicies, and will update the first edge found between
949 // `predecessor` and `successor`.
950 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
951 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100952 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000953 successor->predecessors_[predecessor_index] = this;
954 predecessor->successors_[successor_index] = this;
955 successors_.push_back(successor);
956 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100957 }
958
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100959 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000960 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100961 }
962
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000963 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000965 }
966
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100967 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100969 }
970
971 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000972 predecessors_.push_back(block);
973 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100974 }
975
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100976 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000977 DCHECK_EQ(predecessors_.size(), 2u);
978 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100979 }
980
David Brazdil769c9e52015-04-27 13:54:09 +0100981 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000982 DCHECK_EQ(successors_.size(), 2u);
983 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100984 }
985
David Brazdilfc6a86a2015-06-26 10:33:45 +0000986 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000987 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100988 }
989
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000991 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100992 }
993
David Brazdilfc6a86a2015-06-26 10:33:45 +0000994 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000995 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100996 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000997 }
998
999 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001000 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001001 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001002 }
1003
1004 // Returns whether the first occurrence of `predecessor` in the list of
1005 // predecessors is at index `idx`.
1006 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001007 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001008 return GetPredecessorIndexOf(predecessor) == idx;
1009 }
1010
David Brazdild7558da2015-09-22 13:04:14 +01001011 // Create a new block between this block and its predecessors. The new block
1012 // is added to the graph, all predecessor edges are relinked to it and an edge
1013 // is created to `this`. Returns the new empty block. Reverse post order or
1014 // loop and try/catch information are not updated.
1015 HBasicBlock* CreateImmediateDominator();
1016
David Brazdilfc6a86a2015-06-26 10:33:45 +00001017 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001018 // created, latter block. Note that this method will add the block to the
1019 // graph, create a Goto at the end of the former block and will create an edge
1020 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001021 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001022 HBasicBlock* SplitBefore(HInstruction* cursor);
1023
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001024 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025 // created block. Note that this method just updates raw block information,
1026 // like predecessors, successors, dominators, and instruction list. It does not
1027 // update the graph, reverse post order, loop information, nor make sure the
1028 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001029 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1030
1031 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1032 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001033
1034 // Merge `other` at the end of `this`. Successors and dominated blocks of
1035 // `other` are changed to be successors and dominated blocks of `this`. Note
1036 // that this method does not update the graph, reverse post order, loop
1037 // information, nor make sure the blocks are consistent (for example ending
1038 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001039 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001040
1041 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1042 // of `this` are moved to `other`.
1043 // Note that this method does not update the graph, reverse post order, loop
1044 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001045 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001046 void ReplaceWith(HBasicBlock* other);
1047
David Brazdil2d7352b2015-04-20 14:52:42 +01001048 // Merge `other` at the end of `this`. This method updates loops, reverse post
1049 // order, links to predecessors, successors, dominators and deletes the block
1050 // from the graph. The two blocks must be successive, i.e. `this` the only
1051 // predecessor of `other` and vice versa.
1052 void MergeWith(HBasicBlock* other);
1053
1054 // Disconnects `this` from all its predecessors, successors and dominator,
1055 // removes it from all loops it is included in and eventually from the graph.
1056 // The block must not dominate any other block. Predecessors and successors
1057 // are safely updated.
1058 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001059
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001061 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001062 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001063 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001064 // Replace instruction `initial` with `replacement` within this block.
1065 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1066 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001067 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001068 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001069 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1070 // instruction list. With 'ensure_safety' set to true, it verifies that the
1071 // instruction is not in use and removes it from the use lists of its inputs.
1072 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1073 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001074 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075
1076 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001077 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001078 }
1079
Roland Levillain6b879dd2014-09-22 17:13:44 +01001080 bool IsLoopPreHeaderFirstPredecessor() const {
1081 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001082 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001083 }
1084
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001085 bool IsFirstPredecessorBackEdge() const {
1086 DCHECK(IsLoopHeader());
1087 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1088 }
1089
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001090 HLoopInformation* GetLoopInformation() const {
1091 return loop_information_;
1092 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001093
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001095 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001097 void SetInLoop(HLoopInformation* info) {
1098 if (IsLoopHeader()) {
1099 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001100 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001101 loop_information_ = info;
1102 } else if (loop_information_->Contains(*info->GetHeader())) {
1103 // Block is currently part of an outer loop. Make it part of this inner loop.
1104 // Note that a non loop header having a loop information means this loop information
1105 // has already been populated
1106 loop_information_ = info;
1107 } else {
1108 // Block is part of an inner loop. Do not update the loop information.
1109 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1110 // at this point, because this method is being called while populating `info`.
1111 }
1112 }
1113
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001114 // Raw update of the loop information.
1115 void SetLoopInformation(HLoopInformation* info) {
1116 loop_information_ = info;
1117 }
1118
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001119 bool IsInLoop() const { return loop_information_ != nullptr; }
1120
David Brazdilec16f792015-08-19 15:04:01 +01001121 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1122
1123 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1124 try_catch_information_ = try_catch_information;
1125 }
1126
1127 bool IsTryBlock() const {
1128 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1129 }
1130
1131 bool IsCatchBlock() const {
1132 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1133 }
David Brazdilffee3d32015-07-06 11:48:53 +01001134
1135 // Returns the try entry that this block's successors should have. They will
1136 // be in the same try, unless the block ends in a try boundary. In that case,
1137 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001138 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001139
David Brazdild7558da2015-09-22 13:04:14 +01001140 bool HasThrowingInstructions() const;
1141
David Brazdila4b8c212015-05-07 09:59:30 +01001142 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001143 bool Dominates(HBasicBlock* block) const;
1144
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001145 size_t GetLifetimeStart() const { return lifetime_start_; }
1146 size_t GetLifetimeEnd() const { return lifetime_end_; }
1147
1148 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1149 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1150
David Brazdil8d5b8b22015-03-24 10:51:52 +00001151 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001152 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001153 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001154 bool HasSinglePhi() const;
1155
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001156 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001157 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001158 ArenaVector<HBasicBlock*> predecessors_;
1159 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001160 HInstructionList instructions_;
1161 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001162 HLoopInformation* loop_information_;
1163 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001164 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001165 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001166 // The dex program counter of the first instruction of this block.
1167 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001168 size_t lifetime_start_;
1169 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001170 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001171
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001172 friend class HGraph;
1173 friend class HInstruction;
1174
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001175 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1176};
1177
David Brazdilb2bd1c52015-03-25 11:17:37 +00001178// Iterates over the LoopInformation of all loops which contain 'block'
1179// from the innermost to the outermost.
1180class HLoopInformationOutwardIterator : public ValueObject {
1181 public:
1182 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1183 : current_(block.GetLoopInformation()) {}
1184
1185 bool Done() const { return current_ == nullptr; }
1186
1187 void Advance() {
1188 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001189 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001190 }
1191
1192 HLoopInformation* Current() const {
1193 DCHECK(!Done());
1194 return current_;
1195 }
1196
1197 private:
1198 HLoopInformation* current_;
1199
1200 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1201};
1202
Alexandre Ramesef20f712015-06-09 10:29:30 +01001203#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001204 M(Above, Condition) \
1205 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001207 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001208 M(ArrayGet, Instruction) \
1209 M(ArrayLength, Instruction) \
1210 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001211 M(Below, Condition) \
1212 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001213 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001214 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001215 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001216 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001217 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001218 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001219 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001220 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001221 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001222 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001223 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001224 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001226 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(Exit, Instruction) \
1228 M(FloatConstant, Constant) \
1229 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001230 M(GreaterThan, Condition) \
1231 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001232 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001233 M(InstanceFieldGet, Instruction) \
1234 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001235 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001237 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001238 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001239 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001241 M(LessThan, Condition) \
1242 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001243 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001244 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001245 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001246 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001247 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001248 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001249 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001250 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001251 M(Neg, UnaryOperation) \
1252 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001253 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001254 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001255 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001256 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001257 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001258 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001259 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001260 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001261 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001262 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001263 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001264 M(Return, Instruction) \
1265 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001266 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001267 M(Shl, BinaryOperation) \
1268 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001269 M(StaticFieldGet, Instruction) \
1270 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 M(UnresolvedInstanceFieldGet, Instruction) \
1272 M(UnresolvedInstanceFieldSet, Instruction) \
1273 M(UnresolvedStaticFieldGet, Instruction) \
1274 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001275 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001276 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001277 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001278 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001279 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001280 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001281 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001282 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001283
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001284/*
1285 * Instructions, shared across several (not all) architectures.
1286 */
1287#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1288#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1289#else
1290#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001291 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001292 M(MultiplyAccumulate, Instruction)
1293#endif
1294
Vladimir Markob4536b72015-11-24 13:45:23 +00001295#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001296#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001297#else
1298#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1299 M(ArmDexCacheArraysBase, Instruction)
1300#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001301
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001302#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001303#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001304#else
1305#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001306 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001307 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001308#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001309
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1311
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001312#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1313
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001314#ifndef ART_ENABLE_CODEGEN_x86
1315#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1316#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001317#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1318 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001319 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001320 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001321 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001322#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001323
1324#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1325
1326#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1327 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001328 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001329 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1330 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001331 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001332 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001333 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1334 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1335
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001336#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1337 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001338 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001339 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001340 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001341 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001342
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001343#define FOR_EACH_INSTRUCTION(M) \
1344 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1345 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1346
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001347#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001348FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1349#undef FORWARD_DECLARATION
1350
Vladimir Marko372f10e2016-05-17 16:30:10 +01001351#define DECLARE_INSTRUCTION(type) \
1352 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1353 const char* DebugName() const OVERRIDE { return #type; } \
1354 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1355 return other->Is##type(); \
1356 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001357 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001358
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001359#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1360 bool Is##type() const { return As##type() != nullptr; } \
1361 const H##type* As##type() const { return this; } \
1362 H##type* As##type() { return this; }
1363
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001364template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001365class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001366 public:
David Brazdiled596192015-01-23 10:39:45 +00001367 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001368 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001369 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001370
Vladimir Marko46817b82016-03-29 12:21:58 +01001371 // Hook for the IntrusiveForwardList<>.
1372 // TODO: Hide this better.
1373 IntrusiveForwardListHook hook;
1374
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375 private:
David Brazdiled596192015-01-23 10:39:45 +00001376 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001377 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001378
1379 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001380 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001381
Vladimir Marko46817b82016-03-29 12:21:58 +01001382 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001383
1384 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1385};
1386
David Brazdiled596192015-01-23 10:39:45 +00001387template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001388using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001389
David Brazdil1abb4192015-02-17 18:33:36 +00001390// This class is used by HEnvironment and HInstruction classes to record the
1391// instructions they use and pointers to the corresponding HUseListNodes kept
1392// by the used instructions.
1393template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001394class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001395 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001396 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1397 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001398
Vladimir Marko46817b82016-03-29 12:21:58 +01001399 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1400 : HUserRecord(old_record.instruction_, before_use_node) {}
1401 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1402 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001403 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001404 }
1405
1406 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001407 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1408 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001409
1410 private:
1411 // Instruction used by the user.
1412 HInstruction* instruction_;
1413
Vladimir Marko46817b82016-03-29 12:21:58 +01001414 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1415 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001416};
1417
Vladimir Markoe9004912016-06-16 16:50:52 +01001418// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1419// This is used for HInstruction::GetInputs() to return a container wrapper providing
1420// HInstruction* values even though the underlying container has HUserRecord<>s.
1421struct HInputExtractor {
1422 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1423 return record.GetInstruction();
1424 }
1425 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1426 return record.GetInstruction();
1427 }
1428};
1429
1430using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1431using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1432
Aart Bik854a02b2015-07-14 16:07:00 -07001433/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001434 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001435 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001436 * For write/read dependences on fields/arrays, the dependence analysis uses
1437 * type disambiguation (e.g. a float field write cannot modify the value of an
1438 * integer field read) and the access type (e.g. a reference array write cannot
1439 * modify the value of a reference field read [although it may modify the
1440 * reference fetch prior to reading the field, which is represented by its own
1441 * write/read dependence]). The analysis makes conservative points-to
1442 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1443 * the same, and any reference read depends on any reference read without
1444 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001445 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001446 * The internal representation uses 38-bit and is described in the table below.
1447 * The first line indicates the side effect, and for field/array accesses the
1448 * second line indicates the type of the access (in the order of the
1449 * Primitive::Type enum).
1450 * The two numbered lines below indicate the bit position in the bitfield (read
1451 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001452 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001453 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1454 * +-------------+---------+---------+--------------+---------+---------+
1455 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1456 * | 3 |333333322|222222221| 1 |111111110|000000000|
1457 * | 7 |654321098|765432109| 8 |765432109|876543210|
1458 *
1459 * Note that, to ease the implementation, 'changes' bits are least significant
1460 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001461 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001462class SideEffects : public ValueObject {
1463 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001464 SideEffects() : flags_(0) {}
1465
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001466 static SideEffects None() {
1467 return SideEffects(0);
1468 }
1469
1470 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001471 return SideEffects(kAllChangeBits | kAllDependOnBits);
1472 }
1473
1474 static SideEffects AllChanges() {
1475 return SideEffects(kAllChangeBits);
1476 }
1477
1478 static SideEffects AllDependencies() {
1479 return SideEffects(kAllDependOnBits);
1480 }
1481
1482 static SideEffects AllExceptGCDependency() {
1483 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1484 }
1485
1486 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001487 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001488 }
1489
Aart Bik34c3ba92015-07-20 14:08:59 -07001490 static SideEffects AllWrites() {
1491 return SideEffects(kAllWrites);
1492 }
1493
1494 static SideEffects AllReads() {
1495 return SideEffects(kAllReads);
1496 }
1497
1498 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1499 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001500 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001501 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001502 }
1503
Aart Bik854a02b2015-07-14 16:07:00 -07001504 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001505 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001506 }
1507
Aart Bik34c3ba92015-07-20 14:08:59 -07001508 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1509 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001510 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001511 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001512 }
1513
1514 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001515 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001516 }
1517
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001518 static SideEffects CanTriggerGC() {
1519 return SideEffects(1ULL << kCanTriggerGCBit);
1520 }
1521
1522 static SideEffects DependsOnGC() {
1523 return SideEffects(1ULL << kDependsOnGCBit);
1524 }
1525
Aart Bik854a02b2015-07-14 16:07:00 -07001526 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001527 SideEffects Union(SideEffects other) const {
1528 return SideEffects(flags_ | other.flags_);
1529 }
1530
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001531 SideEffects Exclusion(SideEffects other) const {
1532 return SideEffects(flags_ & ~other.flags_);
1533 }
1534
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001535 void Add(SideEffects other) {
1536 flags_ |= other.flags_;
1537 }
1538
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001539 bool Includes(SideEffects other) const {
1540 return (other.flags_ & flags_) == other.flags_;
1541 }
1542
1543 bool HasSideEffects() const {
1544 return (flags_ & kAllChangeBits);
1545 }
1546
1547 bool HasDependencies() const {
1548 return (flags_ & kAllDependOnBits);
1549 }
1550
1551 // Returns true if there are no side effects or dependencies.
1552 bool DoesNothing() const {
1553 return flags_ == 0;
1554 }
1555
Aart Bik854a02b2015-07-14 16:07:00 -07001556 // Returns true if something is written.
1557 bool DoesAnyWrite() const {
1558 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001559 }
1560
Aart Bik854a02b2015-07-14 16:07:00 -07001561 // Returns true if something is read.
1562 bool DoesAnyRead() const {
1563 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001564 }
1565
Aart Bik854a02b2015-07-14 16:07:00 -07001566 // Returns true if potentially everything is written and read
1567 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001568 bool DoesAllReadWrite() const {
1569 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1570 }
1571
Aart Bik854a02b2015-07-14 16:07:00 -07001572 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001573 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001574 }
1575
Roland Levillain0d5a2812015-11-13 10:07:31 +00001576 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001577 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001578 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1579 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001580 }
1581
1582 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001583 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001584 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001585 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 for (int s = kLastBit; s >= 0; s--) {
1587 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1588 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1589 // This is a bit for the GC side effect.
1590 if (current_bit_is_set) {
1591 flags += "GC";
1592 }
Aart Bik854a02b2015-07-14 16:07:00 -07001593 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001594 } else {
1595 // This is a bit for the array/field analysis.
1596 // The underscore character stands for the 'can trigger GC' bit.
1597 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1598 if (current_bit_is_set) {
1599 flags += kDebug[s];
1600 }
1601 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1602 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1603 flags += "|";
1604 }
1605 }
Aart Bik854a02b2015-07-14 16:07:00 -07001606 }
1607 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001608 }
1609
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001610 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001611
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001612 private:
1613 static constexpr int kFieldArrayAnalysisBits = 9;
1614
1615 static constexpr int kFieldWriteOffset = 0;
1616 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1617 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1618 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1619
1620 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1621
1622 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1623 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1624 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1625 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1626
1627 static constexpr int kLastBit = kDependsOnGCBit;
1628 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1629
1630 // Aliases.
1631
1632 static_assert(kChangeBits == kDependOnBits,
1633 "the 'change' bits should match the 'depend on' bits.");
1634
1635 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1636 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1637 static constexpr uint64_t kAllWrites =
1638 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1639 static constexpr uint64_t kAllReads =
1640 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641
Aart Bik854a02b2015-07-14 16:07:00 -07001642 // Translates type to bit flag.
1643 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1644 CHECK_NE(type, Primitive::kPrimVoid);
1645 const uint64_t one = 1;
1646 const int shift = type; // 0-based consecutive enum
1647 DCHECK_LE(kFieldWriteOffset, shift);
1648 DCHECK_LT(shift, kArrayWriteOffset);
1649 return one << (type + offset);
1650 }
1651
1652 // Private constructor on direct flags value.
1653 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1654
1655 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001656};
1657
David Brazdiled596192015-01-23 10:39:45 +00001658// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001659class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001660 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001661 HEnvironment(ArenaAllocator* arena,
1662 size_t number_of_vregs,
1663 const DexFile& dex_file,
1664 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001665 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001666 InvokeType invoke_type,
1667 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001668 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1669 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001670 parent_(nullptr),
1671 dex_file_(dex_file),
1672 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001673 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001674 invoke_type_(invoke_type),
1675 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001676 }
1677
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001678 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001679 : HEnvironment(arena,
1680 to_copy.Size(),
1681 to_copy.GetDexFile(),
1682 to_copy.GetMethodIdx(),
1683 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001684 to_copy.GetInvokeType(),
1685 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001686
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001687 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001688 if (parent_ != nullptr) {
1689 parent_->SetAndCopyParentChain(allocator, parent);
1690 } else {
1691 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1692 parent_->CopyFrom(parent);
1693 if (parent->GetParent() != nullptr) {
1694 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1695 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001696 }
David Brazdiled596192015-01-23 10:39:45 +00001697 }
1698
Vladimir Marko71bf8092015-09-15 15:33:14 +01001699 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001700 void CopyFrom(HEnvironment* environment);
1701
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001702 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1703 // input to the loop phi instead. This is for inserting instructions that
1704 // require an environment (like HDeoptimization) in the loop pre-header.
1705 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001706
1707 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001708 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001709 }
1710
1711 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001712 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001713 }
1714
David Brazdil1abb4192015-02-17 18:33:36 +00001715 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001716
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001717 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001718
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001719 HEnvironment* GetParent() const { return parent_; }
1720
1721 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001722 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001723 }
1724
1725 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001726 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001727 }
1728
1729 uint32_t GetDexPc() const {
1730 return dex_pc_;
1731 }
1732
1733 uint32_t GetMethodIdx() const {
1734 return method_idx_;
1735 }
1736
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001737 InvokeType GetInvokeType() const {
1738 return invoke_type_;
1739 }
1740
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001741 const DexFile& GetDexFile() const {
1742 return dex_file_;
1743 }
1744
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001745 HInstruction* GetHolder() const {
1746 return holder_;
1747 }
1748
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001749
1750 bool IsFromInlinedInvoke() const {
1751 return GetParent() != nullptr;
1752 }
1753
David Brazdiled596192015-01-23 10:39:45 +00001754 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001755 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1756 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001757 HEnvironment* parent_;
1758 const DexFile& dex_file_;
1759 const uint32_t method_idx_;
1760 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001761 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001762
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001763 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001764 HInstruction* const holder_;
1765
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001766 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001767
1768 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1769};
1770
Vladimir Markof9f64412015-09-02 14:05:49 +01001771class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001772 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001773 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001774 : previous_(nullptr),
1775 next_(nullptr),
1776 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001777 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001778 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001779 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001780 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001781 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001782 locations_(nullptr),
1783 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001784 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001785 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001786 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1787 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1788 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001789
Dave Allison20dfc792014-06-16 20:44:29 -07001790 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001791
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001792#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001793 enum InstructionKind {
1794 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1795 };
1796#undef DECLARE_KIND
1797
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001798 HInstruction* GetNext() const { return next_; }
1799 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001800
Calin Juravle77520bc2015-01-12 18:45:46 +00001801 HInstruction* GetNextDisregardingMoves() const;
1802 HInstruction* GetPreviousDisregardingMoves() const;
1803
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001804 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001805 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001806 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001807 bool IsInBlock() const { return block_ != nullptr; }
1808 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001809 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1810 bool IsIrreducibleLoopHeaderPhi() const {
1811 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1812 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001813
Vladimir Marko372f10e2016-05-17 16:30:10 +01001814 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1815
1816 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1817 // One virtual method is enough, just const_cast<> and then re-add the const.
1818 return ArrayRef<const HUserRecord<HInstruction*>>(
1819 const_cast<HInstruction*>(this)->GetInputRecords());
1820 }
1821
Vladimir Markoe9004912016-06-16 16:50:52 +01001822 HInputsRef GetInputs() {
1823 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001824 }
1825
Vladimir Markoe9004912016-06-16 16:50:52 +01001826 HConstInputsRef GetInputs() const {
1827 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001828 }
1829
1830 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001831 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001832
Vladimir Marko372f10e2016-05-17 16:30:10 +01001833 void SetRawInputAt(size_t index, HInstruction* input) {
1834 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1835 }
1836
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001837 virtual void Accept(HGraphVisitor* visitor) = 0;
1838 virtual const char* DebugName() const = 0;
1839
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001840 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1841
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001843
1844 uint32_t GetDexPc() const { return dex_pc_; }
1845
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001846 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001847
Roland Levillaine161a2a2014-10-03 12:45:18 +01001848 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001849 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001850
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001851 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001852 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001853
Calin Juravle10e244f2015-01-26 18:54:32 +00001854 // Does not apply for all instructions, but having this at top level greatly
1855 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001856 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001857 virtual bool CanBeNull() const {
1858 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1859 return true;
1860 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001861
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001862 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001863 return false;
1864 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001865
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001866 virtual bool IsActualObject() const {
1867 return GetType() == Primitive::kPrimNot;
1868 }
1869
Calin Juravle2e768302015-07-28 14:41:11 +00001870 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001871
Calin Juravle61d544b2015-02-23 16:46:57 +00001872 ReferenceTypeInfo GetReferenceTypeInfo() const {
1873 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001874 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001875 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001876 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001879 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001880 // Note: fixup_end remains valid across push_front().
1881 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1882 HUseListNode<HInstruction*>* new_node =
1883 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1884 uses_.push_front(*new_node);
1885 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001886 }
1887
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001888 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001889 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001890 // Note: env_fixup_end remains valid across push_front().
1891 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1892 HUseListNode<HEnvironment*>* new_node =
1893 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1894 env_uses_.push_front(*new_node);
1895 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001896 }
1897
David Brazdil1abb4192015-02-17 18:33:36 +00001898 void RemoveAsUserOfInput(size_t input) {
1899 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001900 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1901 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1902 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001903 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001904
Vladimir Marko372f10e2016-05-17 16:30:10 +01001905 void RemoveAsUserOfAllInputs() {
1906 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1907 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1908 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1909 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1910 }
1911 }
1912
David Brazdil1abb4192015-02-17 18:33:36 +00001913 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1914 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001915
Vladimir Marko46817b82016-03-29 12:21:58 +01001916 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1917 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1918 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001919 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001920 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001921 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001922
Roland Levillain6c82d402014-10-13 16:10:27 +01001923 // Does this instruction strictly dominate `other_instruction`?
1924 // Returns false if this instruction and `other_instruction` are the same.
1925 // Aborts if this instruction and `other_instruction` are both phis.
1926 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001927
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001928 int GetId() const { return id_; }
1929 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001930
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001931 int GetSsaIndex() const { return ssa_index_; }
1932 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1933 bool HasSsaIndex() const { return ssa_index_ != -1; }
1934
1935 bool HasEnvironment() const { return environment_ != nullptr; }
1936 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001937 // Set the `environment_` field. Raw because this method does not
1938 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001939 void SetRawEnvironment(HEnvironment* environment) {
1940 DCHECK(environment_ == nullptr);
1941 DCHECK_EQ(environment->GetHolder(), this);
1942 environment_ = environment;
1943 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001944
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001945 void InsertRawEnvironment(HEnvironment* environment) {
1946 DCHECK(environment_ != nullptr);
1947 DCHECK_EQ(environment->GetHolder(), this);
1948 DCHECK(environment->GetParent() == nullptr);
1949 environment->parent_ = environment_;
1950 environment_ = environment;
1951 }
1952
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001953 void RemoveEnvironment();
1954
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001955 // Set the environment of this instruction, copying it from `environment`. While
1956 // copying, the uses lists are being updated.
1957 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001958 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001959 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001960 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001961 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001962 if (environment->GetParent() != nullptr) {
1963 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1964 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001965 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001966
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001967 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1968 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001969 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001970 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001971 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001972 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001973 if (environment->GetParent() != nullptr) {
1974 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1975 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001976 }
1977
Nicolas Geoffray39468442014-09-02 15:17:15 +01001978 // Returns the number of entries in the environment. Typically, that is the
1979 // number of dex registers in a method. It could be more in case of inlining.
1980 size_t EnvironmentSize() const;
1981
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001982 LocationSummary* GetLocations() const { return locations_; }
1983 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001984
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001985 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001986 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001987
Alexandre Rames188d4312015-04-09 18:30:21 +01001988 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1989 // uses of this instruction by `other` are *not* updated.
1990 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1991 ReplaceWith(other);
1992 other->ReplaceInput(this, use_index);
1993 }
1994
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001995 // Move `this` instruction before `cursor`.
1996 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001997
Vladimir Markofb337ea2015-11-25 15:25:10 +00001998 // Move `this` before its first user and out of any loops. If there is no
1999 // out-of-loop user that dominates all other users, move the instruction
2000 // to the end of the out-of-loop common dominator of the user's blocks.
2001 //
2002 // This can be used only on non-throwing instructions with no side effects that
2003 // have at least one use but no environment uses.
2004 void MoveBeforeFirstUserAndOutOfLoops();
2005
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002006#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 bool Is##type() const; \
2008 const H##type* As##type() const; \
2009 H##type* As##type();
2010
2011 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2012#undef INSTRUCTION_TYPE_CHECK
2013
2014#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002015 bool Is##type() const { return (As##type() != nullptr); } \
2016 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002017 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002018 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002019#undef INSTRUCTION_TYPE_CHECK
2020
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002021 // Returns whether the instruction can be moved within the graph.
2022 virtual bool CanBeMoved() const { return false; }
2023
2024 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002025 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002026 return false;
2027 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002028
2029 // Returns whether any data encoded in the two instructions is equal.
2030 // This method does not look at the inputs. Both instructions must be
2031 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002032 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002033 return false;
2034 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002035
2036 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002037 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002038 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002039 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002040
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002041 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2042 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2043 // the virtual function because the __attribute__((__pure__)) doesn't really
2044 // apply the strong requirement for virtual functions, preventing optimizations.
2045 InstructionKind GetKind() const PURE;
2046 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002047
2048 virtual size_t ComputeHashCode() const {
2049 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002050 for (const HInstruction* input : GetInputs()) {
2051 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002052 }
2053 return result;
2054 }
2055
2056 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002057 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002058 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002059
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002060 size_t GetLifetimePosition() const { return lifetime_position_; }
2061 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2062 LiveInterval* GetLiveInterval() const { return live_interval_; }
2063 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2064 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2065
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002066 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2067
2068 // Returns whether the code generation of the instruction will require to have access
2069 // to the current method. Such instructions are:
2070 // (1): Instructions that require an environment, as calling the runtime requires
2071 // to walk the stack and have the current method stored at a specific stack address.
2072 // (2): Object literals like classes and strings, that are loaded from the dex cache
2073 // fields of the current method.
2074 bool NeedsCurrentMethod() const {
2075 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2076 }
2077
Vladimir Markodc151b22015-10-15 18:02:30 +01002078 // Returns whether the code generation of the instruction will require to have access
2079 // to the dex cache of the current method's declaring class via the current method.
2080 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002081
Mark Mendellc4701932015-04-10 13:18:51 -04002082 // Does this instruction have any use in an environment before
2083 // control flow hits 'other'?
2084 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2085
2086 // Remove all references to environment uses of this instruction.
2087 // The caller must ensure that this is safe to do.
2088 void RemoveEnvironmentUsers();
2089
Vladimir Markoa1de9182016-02-25 11:37:38 +00002090 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2091 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002092
David Brazdil1abb4192015-02-17 18:33:36 +00002093 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002094 // If set, the machine code for this instruction is assumed to be generated by
2095 // its users. Used by liveness analysis to compute use positions accordingly.
2096 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2097 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2098 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2099 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2100
Vladimir Marko372f10e2016-05-17 16:30:10 +01002101 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2102 return GetInputRecords()[i];
2103 }
2104
2105 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2106 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2107 input_records[index] = input;
2108 }
David Brazdil1abb4192015-02-17 18:33:36 +00002109
Vladimir Markoa1de9182016-02-25 11:37:38 +00002110 uint32_t GetPackedFields() const {
2111 return packed_fields_;
2112 }
2113
2114 template <size_t flag>
2115 bool GetPackedFlag() const {
2116 return (packed_fields_ & (1u << flag)) != 0u;
2117 }
2118
2119 template <size_t flag>
2120 void SetPackedFlag(bool value = true) {
2121 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2122 }
2123
2124 template <typename BitFieldType>
2125 typename BitFieldType::value_type GetPackedField() const {
2126 return BitFieldType::Decode(packed_fields_);
2127 }
2128
2129 template <typename BitFieldType>
2130 void SetPackedField(typename BitFieldType::value_type value) {
2131 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2132 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2133 }
2134
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002135 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002136 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2137 auto before_use_node = uses_.before_begin();
2138 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2139 HInstruction* user = use_node->GetUser();
2140 size_t input_index = use_node->GetIndex();
2141 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2142 before_use_node = use_node;
2143 }
2144 }
2145
2146 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2147 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2148 if (next != uses_.end()) {
2149 HInstruction* next_user = next->GetUser();
2150 size_t next_index = next->GetIndex();
2151 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2152 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2153 }
2154 }
2155
2156 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2157 auto before_env_use_node = env_uses_.before_begin();
2158 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2159 HEnvironment* user = env_use_node->GetUser();
2160 size_t input_index = env_use_node->GetIndex();
2161 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2162 before_env_use_node = env_use_node;
2163 }
2164 }
2165
2166 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2167 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2168 if (next != env_uses_.end()) {
2169 HEnvironment* next_user = next->GetUser();
2170 size_t next_index = next->GetIndex();
2171 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2172 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2173 }
2174 }
David Brazdil1abb4192015-02-17 18:33:36 +00002175
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176 HInstruction* previous_;
2177 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002178 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002179 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002180
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002181 // An instruction gets an id when it is added to the graph.
2182 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002183 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002184 int id_;
2185
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002186 // When doing liveness analysis, instructions that have uses get an SSA index.
2187 int ssa_index_;
2188
Vladimir Markoa1de9182016-02-25 11:37:38 +00002189 // Packed fields.
2190 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002191
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002193 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002194
2195 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002196 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002197
Nicolas Geoffray39468442014-09-02 15:17:15 +01002198 // The environment associated with this instruction. Not null if the instruction
2199 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002200 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002201
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002202 // Set by the code generator.
2203 LocationSummary* locations_;
2204
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002205 // Set by the liveness analysis.
2206 LiveInterval* live_interval_;
2207
2208 // Set by the liveness analysis, this is the position in a linear
2209 // order of blocks where this instruction's live interval start.
2210 size_t lifetime_position_;
2211
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002212 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002213
Vladimir Markoa1de9182016-02-25 11:37:38 +00002214 // The reference handle part of the reference type info.
2215 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002216 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002217 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002218
David Brazdil1abb4192015-02-17 18:33:36 +00002219 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002220 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002221 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002222 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002223 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002224
2225 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2226};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002227std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002228
2229class HInstructionIterator : public ValueObject {
2230 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002231 explicit HInstructionIterator(const HInstructionList& instructions)
2232 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002233 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002234 }
2235
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002236 bool Done() const { return instruction_ == nullptr; }
2237 HInstruction* Current() const { return instruction_; }
2238 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002239 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002240 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241 }
2242
2243 private:
2244 HInstruction* instruction_;
2245 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002246
2247 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002248};
2249
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002250class HBackwardInstructionIterator : public ValueObject {
2251 public:
2252 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2253 : instruction_(instructions.last_instruction_) {
2254 next_ = Done() ? nullptr : instruction_->GetPrevious();
2255 }
2256
2257 bool Done() const { return instruction_ == nullptr; }
2258 HInstruction* Current() const { return instruction_; }
2259 void Advance() {
2260 instruction_ = next_;
2261 next_ = Done() ? nullptr : instruction_->GetPrevious();
2262 }
2263
2264 private:
2265 HInstruction* instruction_;
2266 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002267
2268 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002269};
2270
Vladimir Markof9f64412015-09-02 14:05:49 +01002271template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002272class HTemplateInstruction: public HInstruction {
2273 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002274 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002275 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002276 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002277
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*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002281 }
2282
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002283 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002284 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002285
2286 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002287};
2288
Vladimir Markof9f64412015-09-02 14:05:49 +01002289// HTemplateInstruction specialization for N=0.
2290template<>
2291class HTemplateInstruction<0>: public HInstruction {
2292 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002293 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002294 : HInstruction(side_effects, dex_pc) {}
2295
Vladimir Markof9f64412015-09-02 14:05:49 +01002296 virtual ~HTemplateInstruction() {}
2297
Vladimir Marko372f10e2016-05-17 16:30:10 +01002298 using HInstruction::GetInputRecords; // Keep the const version visible.
2299 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2300 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002301 }
2302
2303 private:
2304 friend class SsaBuilder;
2305};
2306
Dave Allison20dfc792014-06-16 20:44:29 -07002307template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002308class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002309 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002310 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002311 : HTemplateInstruction<N>(side_effects, dex_pc) {
2312 this->template SetPackedField<TypeField>(type);
2313 }
Dave Allison20dfc792014-06-16 20:44:29 -07002314 virtual ~HExpression() {}
2315
Vladimir Markoa1de9182016-02-25 11:37:38 +00002316 Primitive::Type GetType() const OVERRIDE {
2317 return TypeField::Decode(this->GetPackedFields());
2318 }
Dave Allison20dfc792014-06-16 20:44:29 -07002319
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002320 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002321 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2322 static constexpr size_t kFieldTypeSize =
2323 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2324 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2325 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2326 "Too many packed fields.");
2327 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002328};
2329
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002330// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2331// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002332class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002333 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002334 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2335 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002336
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002337 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002338
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002339 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002340
2341 private:
2342 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2343};
2344
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002345// Represents dex's RETURN opcodes. A HReturn is a control flow
2346// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002347class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002348 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002349 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2350 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002351 SetRawInputAt(0, value);
2352 }
2353
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002354 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002355
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002356 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002357
2358 private:
2359 DISALLOW_COPY_AND_ASSIGN(HReturn);
2360};
2361
Vladimir Markofcb503c2016-05-18 12:48:17 +01002362class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002363 public:
2364 HPhi(ArenaAllocator* arena,
2365 uint32_t reg_number,
2366 size_t number_of_inputs,
2367 Primitive::Type type,
2368 uint32_t dex_pc = kNoDexPc)
2369 : HInstruction(SideEffects::None(), dex_pc),
2370 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2371 reg_number_(reg_number) {
2372 SetPackedField<TypeField>(ToPhiType(type));
2373 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2374 // Phis are constructed live and marked dead if conflicting or unused.
2375 // Individual steps of SsaBuilder should assume that if a phi has been
2376 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2377 SetPackedFlag<kFlagIsLive>(true);
2378 SetPackedFlag<kFlagCanBeNull>(true);
2379 }
2380
2381 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2382 static Primitive::Type ToPhiType(Primitive::Type type) {
2383 return Primitive::PrimitiveKind(type);
2384 }
2385
2386 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2387
Vladimir Marko372f10e2016-05-17 16:30:10 +01002388 using HInstruction::GetInputRecords; // Keep the const version visible.
2389 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2390 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2391 }
David Brazdildee58d62016-04-07 09:54:26 +00002392
2393 void AddInput(HInstruction* input);
2394 void RemoveInputAt(size_t index);
2395
2396 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2397 void SetType(Primitive::Type new_type) {
2398 // Make sure that only valid type changes occur. The following are allowed:
2399 // (1) int -> float/ref (primitive type propagation),
2400 // (2) long -> double (primitive type propagation).
2401 DCHECK(GetType() == new_type ||
2402 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2403 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2404 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2405 SetPackedField<TypeField>(new_type);
2406 }
2407
2408 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2409 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2410
2411 uint32_t GetRegNumber() const { return reg_number_; }
2412
2413 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2414 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2415 bool IsDead() const { return !IsLive(); }
2416 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2417
Vladimir Markoe9004912016-06-16 16:50:52 +01002418 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002419 return other != nullptr
2420 && other->IsPhi()
2421 && other->AsPhi()->GetBlock() == GetBlock()
2422 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2423 }
2424
2425 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2426 // An equivalent phi is a phi having the same dex register and type.
2427 // It assumes that phis with the same dex register are adjacent.
2428 HPhi* GetNextEquivalentPhiWithSameType() {
2429 HInstruction* next = GetNext();
2430 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2431 if (next->GetType() == GetType()) {
2432 return next->AsPhi();
2433 }
2434 next = next->GetNext();
2435 }
2436 return nullptr;
2437 }
2438
2439 DECLARE_INSTRUCTION(Phi);
2440
David Brazdildee58d62016-04-07 09:54:26 +00002441 private:
2442 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2443 static constexpr size_t kFieldTypeSize =
2444 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2445 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2446 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2447 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2448 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2449 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2450
Vladimir Marko372f10e2016-05-17 16:30:10 +01002451 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002452 const uint32_t reg_number_;
2453
2454 DISALLOW_COPY_AND_ASSIGN(HPhi);
2455};
2456
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002457// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002458// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002459// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002460class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002461 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002462 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002463
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002464 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002465
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002466 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002467
2468 private:
2469 DISALLOW_COPY_AND_ASSIGN(HExit);
2470};
2471
2472// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002473class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002474 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002475 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002476
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002477 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002478
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002479 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002480 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002481 }
2482
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002483 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002484
2485 private:
2486 DISALLOW_COPY_AND_ASSIGN(HGoto);
2487};
2488
Roland Levillain9867bc72015-08-05 10:21:34 +01002489class HConstant : public HExpression<0> {
2490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002491 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2492 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002493
2494 bool CanBeMoved() const OVERRIDE { return true; }
2495
Roland Levillain1a653882016-03-18 18:05:57 +00002496 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002497 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002498 // Is this constant 0 in the arithmetic sense?
2499 virtual bool IsArithmeticZero() const { return false; }
2500 // Is this constant a 0-bit pattern?
2501 virtual bool IsZeroBitPattern() const { return false; }
2502 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002503 virtual bool IsOne() const { return false; }
2504
David Brazdil77a48ae2015-09-15 12:34:04 +00002505 virtual uint64_t GetValueAsUint64() const = 0;
2506
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002507 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002508
2509 private:
2510 DISALLOW_COPY_AND_ASSIGN(HConstant);
2511};
2512
Vladimir Markofcb503c2016-05-18 12:48:17 +01002513class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002514 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002515 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002516 return true;
2517 }
2518
David Brazdil77a48ae2015-09-15 12:34:04 +00002519 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2520
Roland Levillain9867bc72015-08-05 10:21:34 +01002521 size_t ComputeHashCode() const OVERRIDE { return 0; }
2522
Roland Levillain1a653882016-03-18 18:05:57 +00002523 // The null constant representation is a 0-bit pattern.
2524 virtual bool IsZeroBitPattern() const { return true; }
2525
Roland Levillain9867bc72015-08-05 10:21:34 +01002526 DECLARE_INSTRUCTION(NullConstant);
2527
2528 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002529 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002530
2531 friend class HGraph;
2532 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2533};
2534
2535// Constants of the type int. Those can be from Dex instructions, or
2536// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002537class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002538 public:
2539 int32_t GetValue() const { return value_; }
2540
David Brazdil9f389d42015-10-01 14:32:56 +01002541 uint64_t GetValueAsUint64() const OVERRIDE {
2542 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2543 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002544
Vladimir Marko372f10e2016-05-17 16:30:10 +01002545 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002546 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002547 return other->AsIntConstant()->value_ == value_;
2548 }
2549
2550 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2551
2552 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002553 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2554 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002555 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2556
Roland Levillain1a653882016-03-18 18:05:57 +00002557 // Integer constants are used to encode Boolean values as well,
2558 // where 1 means true and 0 means false.
2559 bool IsTrue() const { return GetValue() == 1; }
2560 bool IsFalse() const { return GetValue() == 0; }
2561
Roland Levillain9867bc72015-08-05 10:21:34 +01002562 DECLARE_INSTRUCTION(IntConstant);
2563
2564 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002565 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2566 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2567 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2568 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002569
2570 const int32_t value_;
2571
2572 friend class HGraph;
2573 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2574 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2575 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2576};
2577
Vladimir Markofcb503c2016-05-18 12:48:17 +01002578class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002579 public:
2580 int64_t GetValue() const { return value_; }
2581
David Brazdil77a48ae2015-09-15 12:34:04 +00002582 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2583
Vladimir Marko372f10e2016-05-17 16:30:10 +01002584 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002585 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002586 return other->AsLongConstant()->value_ == value_;
2587 }
2588
2589 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2590
2591 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002592 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2593 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002594 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2595
2596 DECLARE_INSTRUCTION(LongConstant);
2597
2598 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002599 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2600 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002601
2602 const int64_t value_;
2603
2604 friend class HGraph;
2605 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2606};
Dave Allison20dfc792014-06-16 20:44:29 -07002607
Vladimir Markofcb503c2016-05-18 12:48:17 +01002608class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002609 public:
2610 float GetValue() const { return value_; }
2611
2612 uint64_t GetValueAsUint64() const OVERRIDE {
2613 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2614 }
2615
Vladimir Marko372f10e2016-05-17 16:30:10 +01002616 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002617 DCHECK(other->IsFloatConstant()) << other->DebugName();
2618 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2619 }
2620
2621 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2622
2623 bool IsMinusOne() const OVERRIDE {
2624 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2625 }
Roland Levillain1a653882016-03-18 18:05:57 +00002626 bool IsArithmeticZero() const OVERRIDE {
2627 return std::fpclassify(value_) == FP_ZERO;
2628 }
2629 bool IsArithmeticPositiveZero() const {
2630 return IsArithmeticZero() && !std::signbit(value_);
2631 }
2632 bool IsArithmeticNegativeZero() const {
2633 return IsArithmeticZero() && std::signbit(value_);
2634 }
2635 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002636 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002637 }
2638 bool IsOne() const OVERRIDE {
2639 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2640 }
2641 bool IsNaN() const {
2642 return std::isnan(value_);
2643 }
2644
2645 DECLARE_INSTRUCTION(FloatConstant);
2646
2647 private:
2648 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2649 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2650 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2651 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2652
2653 const float value_;
2654
2655 // Only the SsaBuilder and HGraph can create floating-point constants.
2656 friend class SsaBuilder;
2657 friend class HGraph;
2658 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2659};
2660
Vladimir Markofcb503c2016-05-18 12:48:17 +01002661class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002662 public:
2663 double GetValue() const { return value_; }
2664
2665 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2666
Vladimir Marko372f10e2016-05-17 16:30:10 +01002667 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002668 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2669 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2670 }
2671
2672 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2673
2674 bool IsMinusOne() const OVERRIDE {
2675 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2676 }
Roland Levillain1a653882016-03-18 18:05:57 +00002677 bool IsArithmeticZero() const OVERRIDE {
2678 return std::fpclassify(value_) == FP_ZERO;
2679 }
2680 bool IsArithmeticPositiveZero() const {
2681 return IsArithmeticZero() && !std::signbit(value_);
2682 }
2683 bool IsArithmeticNegativeZero() const {
2684 return IsArithmeticZero() && std::signbit(value_);
2685 }
2686 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002687 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002688 }
2689 bool IsOne() const OVERRIDE {
2690 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2691 }
2692 bool IsNaN() const {
2693 return std::isnan(value_);
2694 }
2695
2696 DECLARE_INSTRUCTION(DoubleConstant);
2697
2698 private:
2699 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2700 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2701 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2702 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2703
2704 const double value_;
2705
2706 // Only the SsaBuilder and HGraph can create floating-point constants.
2707 friend class SsaBuilder;
2708 friend class HGraph;
2709 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2710};
2711
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002712// Conditional branch. A block ending with an HIf instruction must have
2713// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002714class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002715 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002716 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2717 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002718 SetRawInputAt(0, input);
2719 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002720
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002721 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002722
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002723 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002724 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002725 }
2726
2727 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002728 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002729 }
2730
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002731 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002732
2733 private:
2734 DISALLOW_COPY_AND_ASSIGN(HIf);
2735};
2736
David Brazdilfc6a86a2015-06-26 10:33:45 +00002737
2738// Abstract instruction which marks the beginning and/or end of a try block and
2739// links it to the respective exception handlers. Behaves the same as a Goto in
2740// non-exceptional control flow.
2741// Normal-flow successor is stored at index zero, exception handlers under
2742// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002743class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002744 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002745 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002746 kEntry,
2747 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002748 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002749 };
2750
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002751 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002752 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2753 SetPackedField<BoundaryKindField>(kind);
2754 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002755
2756 bool IsControlFlow() const OVERRIDE { return true; }
2757
2758 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002759 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002760
David Brazdild26a4112015-11-10 11:07:31 +00002761 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2762 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2763 }
2764
David Brazdilfc6a86a2015-06-26 10:33:45 +00002765 // Returns whether `handler` is among its exception handlers (non-zero index
2766 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002767 bool HasExceptionHandler(const HBasicBlock& handler) const {
2768 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002769 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002770 }
2771
2772 // If not present already, adds `handler` to its block's list of exception
2773 // handlers.
2774 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002775 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002776 GetBlock()->AddSuccessor(handler);
2777 }
2778 }
2779
Vladimir Markoa1de9182016-02-25 11:37:38 +00002780 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2781 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002782
David Brazdilffee3d32015-07-06 11:48:53 +01002783 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2784
David Brazdilfc6a86a2015-06-26 10:33:45 +00002785 DECLARE_INSTRUCTION(TryBoundary);
2786
2787 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002788 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2789 static constexpr size_t kFieldBoundaryKindSize =
2790 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2791 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2792 kFieldBoundaryKind + kFieldBoundaryKindSize;
2793 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2794 "Too many packed fields.");
2795 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002796
2797 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2798};
2799
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002800// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002801class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002802 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002803 // We set CanTriggerGC to prevent any intermediate address to be live
2804 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002805 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002806 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002807 SetRawInputAt(0, cond);
2808 }
2809
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002810 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002811 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002812 return true;
2813 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002814 bool NeedsEnvironment() const OVERRIDE { return true; }
2815 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002816
2817 DECLARE_INSTRUCTION(Deoptimize);
2818
2819 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002820 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2821};
2822
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002823// Represents the ArtMethod that was passed as a first argument to
2824// the method. It is used by instructions that depend on it, like
2825// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002826class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002827 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002828 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2829 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002830
2831 DECLARE_INSTRUCTION(CurrentMethod);
2832
2833 private:
2834 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2835};
2836
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002837// Fetches an ArtMethod from the virtual table or the interface method table
2838// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002839class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002840 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002841 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002842 kVTable,
2843 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002844 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002845 };
2846 HClassTableGet(HInstruction* cls,
2847 Primitive::Type type,
2848 TableKind kind,
2849 size_t index,
2850 uint32_t dex_pc)
2851 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002852 index_(index) {
2853 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002854 SetRawInputAt(0, cls);
2855 }
2856
2857 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002858 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002859 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002860 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002861 }
2862
Vladimir Markoa1de9182016-02-25 11:37:38 +00002863 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002864 size_t GetIndex() const { return index_; }
2865
2866 DECLARE_INSTRUCTION(ClassTableGet);
2867
2868 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002869 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2870 static constexpr size_t kFieldTableKindSize =
2871 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2872 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2873 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2874 "Too many packed fields.");
2875 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2876
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002877 // The index of the ArtMethod in the table.
2878 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002879
2880 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2881};
2882
Mark Mendellfe57faa2015-09-18 09:26:15 -04002883// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2884// have one successor for each entry in the switch table, and the final successor
2885// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002886class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002887 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002888 HPackedSwitch(int32_t start_value,
2889 uint32_t num_entries,
2890 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002891 uint32_t dex_pc = kNoDexPc)
2892 : HTemplateInstruction(SideEffects::None(), dex_pc),
2893 start_value_(start_value),
2894 num_entries_(num_entries) {
2895 SetRawInputAt(0, input);
2896 }
2897
2898 bool IsControlFlow() const OVERRIDE { return true; }
2899
2900 int32_t GetStartValue() const { return start_value_; }
2901
Vladimir Marko211c2112015-09-24 16:52:33 +01002902 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002903
2904 HBasicBlock* GetDefaultBlock() const {
2905 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002906 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002907 }
2908 DECLARE_INSTRUCTION(PackedSwitch);
2909
2910 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002911 const int32_t start_value_;
2912 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002913
2914 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2915};
2916
Roland Levillain88cb1752014-10-20 16:36:47 +01002917class HUnaryOperation : public HExpression<1> {
2918 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002919 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2920 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002921 SetRawInputAt(0, input);
2922 }
2923
2924 HInstruction* GetInput() const { return InputAt(0); }
2925 Primitive::Type GetResultType() const { return GetType(); }
2926
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002927 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002928 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002929 return true;
2930 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002931
Roland Levillain31dd3d62016-02-16 12:21:02 +00002932 // Try to statically evaluate `this` and return a HConstant
2933 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002934 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002935 HConstant* TryStaticEvaluation() const;
2936
2937 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002938 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2939 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002940 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2941 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002942
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002943 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002944
2945 private:
2946 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2947};
2948
Dave Allison20dfc792014-06-16 20:44:29 -07002949class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002950 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002951 HBinaryOperation(Primitive::Type result_type,
2952 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002953 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002954 SideEffects side_effects = SideEffects::None(),
2955 uint32_t dex_pc = kNoDexPc)
2956 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002957 SetRawInputAt(0, left);
2958 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002959 }
2960
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002961 HInstruction* GetLeft() const { return InputAt(0); }
2962 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002963 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002964
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002965 virtual bool IsCommutative() const { return false; }
2966
2967 // Put constant on the right.
2968 // Returns whether order is changed.
2969 bool OrderInputsWithConstantOnTheRight() {
2970 HInstruction* left = InputAt(0);
2971 HInstruction* right = InputAt(1);
2972 if (left->IsConstant() && !right->IsConstant()) {
2973 ReplaceInput(right, 0);
2974 ReplaceInput(left, 1);
2975 return true;
2976 }
2977 return false;
2978 }
2979
2980 // Order inputs by instruction id, but favor constant on the right side.
2981 // This helps GVN for commutative ops.
2982 void OrderInputs() {
2983 DCHECK(IsCommutative());
2984 HInstruction* left = InputAt(0);
2985 HInstruction* right = InputAt(1);
2986 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2987 return;
2988 }
2989 if (OrderInputsWithConstantOnTheRight()) {
2990 return;
2991 }
2992 // Order according to instruction id.
2993 if (left->GetId() > right->GetId()) {
2994 ReplaceInput(right, 0);
2995 ReplaceInput(left, 1);
2996 }
2997 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002998
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002999 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003000 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003001 return true;
3002 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003003
Roland Levillain31dd3d62016-02-16 12:21:02 +00003004 // Try to statically evaluate `this` and return a HConstant
3005 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003006 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003007 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003008
3009 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003010 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3011 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003012 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3013 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003014 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003015 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3016 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003017 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3018 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003019 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3020 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003021 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3023 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003024
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003025 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003026 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003027 HConstant* GetConstantRight() const;
3028
3029 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003030 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003031 HInstruction* GetLeastConstantLeft() const;
3032
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003033 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003034
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003035 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003036 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3037};
3038
Mark Mendellc4701932015-04-10 13:18:51 -04003039// The comparison bias applies for floating point operations and indicates how NaN
3040// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003041enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003042 kNoBias, // bias is not applicable (i.e. for long operation)
3043 kGtBias, // return 1 for NaN comparisons
3044 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003045 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003046};
3047
Roland Levillain31dd3d62016-02-16 12:21:02 +00003048std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3049
Dave Allison20dfc792014-06-16 20:44:29 -07003050class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003051 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003052 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003053 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3054 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3055 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003056
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003057 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003058 // `instruction`, and disregard moves in between.
3059 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003060
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003061 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003062
3063 virtual IfCondition GetCondition() const = 0;
3064
Mark Mendellc4701932015-04-10 13:18:51 -04003065 virtual IfCondition GetOppositeCondition() const = 0;
3066
Vladimir Markoa1de9182016-02-25 11:37:38 +00003067 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003068 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3069
Vladimir Markoa1de9182016-02-25 11:37:38 +00003070 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3071 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003072
Vladimir Marko372f10e2016-05-17 16:30:10 +01003073 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003074 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003075 }
3076
Roland Levillain4fa13f62015-07-06 18:11:54 +01003077 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003079 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003080 if (if_cond == kCondNE) {
3081 return true;
3082 } else if (if_cond == kCondEQ) {
3083 return false;
3084 }
3085 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003086 }
3087
3088 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003089 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003090 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003091 if (if_cond == kCondEQ) {
3092 return true;
3093 } else if (if_cond == kCondNE) {
3094 return false;
3095 }
3096 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003097 }
3098
Roland Levillain31dd3d62016-02-16 12:21:02 +00003099 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003100 // Needed if we merge a HCompare into a HCondition.
3101 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3102 static constexpr size_t kFieldComparisonBiasSize =
3103 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3104 static constexpr size_t kNumberOfConditionPackedBits =
3105 kFieldComparisonBias + kFieldComparisonBiasSize;
3106 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3107 using ComparisonBiasField =
3108 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3109
Roland Levillain31dd3d62016-02-16 12:21:02 +00003110 template <typename T>
3111 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3112
3113 template <typename T>
3114 int32_t CompareFP(T x, T y) const {
3115 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3116 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3117 // Handle the bias.
3118 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3119 }
3120
3121 // Return an integer constant containing the result of a condition evaluated at compile time.
3122 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3123 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3124 }
3125
Dave Allison20dfc792014-06-16 20:44:29 -07003126 private:
3127 DISALLOW_COPY_AND_ASSIGN(HCondition);
3128};
3129
3130// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003131class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003132 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003133 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3134 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003135
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003136 bool IsCommutative() const OVERRIDE { return true; }
3137
Vladimir Marko9e23df52015-11-10 17:14:35 +00003138 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3139 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003140 return MakeConstantCondition(true, GetDexPc());
3141 }
3142 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3143 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3144 }
3145 // In the following Evaluate methods, a HCompare instruction has
3146 // been merged into this HEqual instruction; evaluate it as
3147 // `Compare(x, y) == 0`.
3148 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3149 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3150 GetDexPc());
3151 }
3152 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3153 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3154 }
3155 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3156 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003157 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003158
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003159 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003160
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003161 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003162 return kCondEQ;
3163 }
3164
Mark Mendellc4701932015-04-10 13:18:51 -04003165 IfCondition GetOppositeCondition() const OVERRIDE {
3166 return kCondNE;
3167 }
3168
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003169 private:
Aart Bike9f37602015-10-09 11:15:55 -07003170 template <typename T> bool Compute(T x, T y) const { return x == y; }
3171
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003172 DISALLOW_COPY_AND_ASSIGN(HEqual);
3173};
3174
Vladimir Markofcb503c2016-05-18 12:48:17 +01003175class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003176 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003177 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3178 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003179
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003180 bool IsCommutative() const OVERRIDE { return true; }
3181
Vladimir Marko9e23df52015-11-10 17:14:35 +00003182 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3183 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003184 return MakeConstantCondition(false, GetDexPc());
3185 }
3186 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3187 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3188 }
3189 // In the following Evaluate methods, a HCompare instruction has
3190 // been merged into this HNotEqual instruction; evaluate it as
3191 // `Compare(x, y) != 0`.
3192 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3193 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3194 }
3195 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3196 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3197 }
3198 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3199 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003200 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003201
Dave Allison20dfc792014-06-16 20:44:29 -07003202 DECLARE_INSTRUCTION(NotEqual);
3203
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003204 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003205 return kCondNE;
3206 }
3207
Mark Mendellc4701932015-04-10 13:18:51 -04003208 IfCondition GetOppositeCondition() const OVERRIDE {
3209 return kCondEQ;
3210 }
3211
Dave Allison20dfc792014-06-16 20:44:29 -07003212 private:
Aart Bike9f37602015-10-09 11:15:55 -07003213 template <typename T> bool Compute(T x, T y) const { return x != y; }
3214
Dave Allison20dfc792014-06-16 20:44:29 -07003215 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3216};
3217
Vladimir Markofcb503c2016-05-18 12:48:17 +01003218class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003219 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003220 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3221 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003222
Roland Levillain9867bc72015-08-05 10:21:34 +01003223 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003224 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003225 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003226 // In the following Evaluate methods, a HCompare instruction has
3227 // been merged into this HLessThan instruction; evaluate it as
3228 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003229 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003230 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3231 }
3232 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3233 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3234 }
3235 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3236 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003237 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003238
Dave Allison20dfc792014-06-16 20:44:29 -07003239 DECLARE_INSTRUCTION(LessThan);
3240
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003241 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003242 return kCondLT;
3243 }
3244
Mark Mendellc4701932015-04-10 13:18:51 -04003245 IfCondition GetOppositeCondition() const OVERRIDE {
3246 return kCondGE;
3247 }
3248
Dave Allison20dfc792014-06-16 20:44:29 -07003249 private:
Aart Bike9f37602015-10-09 11:15:55 -07003250 template <typename T> bool Compute(T x, T y) const { return x < y; }
3251
Dave Allison20dfc792014-06-16 20:44:29 -07003252 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3253};
3254
Vladimir Markofcb503c2016-05-18 12:48:17 +01003255class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003256 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003257 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3258 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003259
Roland Levillain9867bc72015-08-05 10:21:34 +01003260 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003261 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003262 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003263 // In the following Evaluate methods, a HCompare instruction has
3264 // been merged into this HLessThanOrEqual instruction; evaluate it as
3265 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003266 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003267 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3268 }
3269 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3270 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3271 }
3272 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3273 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003274 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003275
Dave Allison20dfc792014-06-16 20:44:29 -07003276 DECLARE_INSTRUCTION(LessThanOrEqual);
3277
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003278 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003279 return kCondLE;
3280 }
3281
Mark Mendellc4701932015-04-10 13:18:51 -04003282 IfCondition GetOppositeCondition() const OVERRIDE {
3283 return kCondGT;
3284 }
3285
Dave Allison20dfc792014-06-16 20:44:29 -07003286 private:
Aart Bike9f37602015-10-09 11:15:55 -07003287 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3288
Dave Allison20dfc792014-06-16 20:44:29 -07003289 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3290};
3291
Vladimir Markofcb503c2016-05-18 12:48:17 +01003292class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003293 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003294 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3295 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003296
Roland Levillain9867bc72015-08-05 10:21:34 +01003297 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003298 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003299 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003300 // In the following Evaluate methods, a HCompare instruction has
3301 // been merged into this HGreaterThan instruction; evaluate it as
3302 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003303 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003304 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3305 }
3306 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3307 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3308 }
3309 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3310 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003311 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003312
Dave Allison20dfc792014-06-16 20:44:29 -07003313 DECLARE_INSTRUCTION(GreaterThan);
3314
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003315 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003316 return kCondGT;
3317 }
3318
Mark Mendellc4701932015-04-10 13:18:51 -04003319 IfCondition GetOppositeCondition() const OVERRIDE {
3320 return kCondLE;
3321 }
3322
Dave Allison20dfc792014-06-16 20:44:29 -07003323 private:
Aart Bike9f37602015-10-09 11:15:55 -07003324 template <typename T> bool Compute(T x, T y) const { return x > y; }
3325
Dave Allison20dfc792014-06-16 20:44:29 -07003326 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3327};
3328
Vladimir Markofcb503c2016-05-18 12:48:17 +01003329class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003330 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003331 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3332 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003333
Roland Levillain9867bc72015-08-05 10:21:34 +01003334 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003335 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003336 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003337 // In the following Evaluate methods, a HCompare instruction has
3338 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3339 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003340 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003341 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3342 }
3343 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3344 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3345 }
3346 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3347 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003348 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003349
Dave Allison20dfc792014-06-16 20:44:29 -07003350 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3351
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003352 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003353 return kCondGE;
3354 }
3355
Mark Mendellc4701932015-04-10 13:18:51 -04003356 IfCondition GetOppositeCondition() const OVERRIDE {
3357 return kCondLT;
3358 }
3359
Dave Allison20dfc792014-06-16 20:44:29 -07003360 private:
Aart Bike9f37602015-10-09 11:15:55 -07003361 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3362
Dave Allison20dfc792014-06-16 20:44:29 -07003363 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3364};
3365
Vladimir Markofcb503c2016-05-18 12:48:17 +01003366class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003367 public:
3368 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3369 : HCondition(first, second, dex_pc) {}
3370
3371 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003372 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003373 }
3374 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003375 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3376 }
3377 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3378 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3379 LOG(FATAL) << DebugName() << " is not defined for float values";
3380 UNREACHABLE();
3381 }
3382 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3383 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3384 LOG(FATAL) << DebugName() << " is not defined for double values";
3385 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003386 }
3387
3388 DECLARE_INSTRUCTION(Below);
3389
3390 IfCondition GetCondition() const OVERRIDE {
3391 return kCondB;
3392 }
3393
3394 IfCondition GetOppositeCondition() const OVERRIDE {
3395 return kCondAE;
3396 }
3397
3398 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003399 template <typename T> bool Compute(T x, T y) const {
3400 return MakeUnsigned(x) < MakeUnsigned(y);
3401 }
Aart Bike9f37602015-10-09 11:15:55 -07003402
3403 DISALLOW_COPY_AND_ASSIGN(HBelow);
3404};
3405
Vladimir Markofcb503c2016-05-18 12:48:17 +01003406class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003407 public:
3408 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3409 : HCondition(first, second, dex_pc) {}
3410
3411 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003412 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003413 }
3414 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003415 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3416 }
3417 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3418 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3419 LOG(FATAL) << DebugName() << " is not defined for float values";
3420 UNREACHABLE();
3421 }
3422 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3423 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3424 LOG(FATAL) << DebugName() << " is not defined for double values";
3425 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003426 }
3427
3428 DECLARE_INSTRUCTION(BelowOrEqual);
3429
3430 IfCondition GetCondition() const OVERRIDE {
3431 return kCondBE;
3432 }
3433
3434 IfCondition GetOppositeCondition() const OVERRIDE {
3435 return kCondA;
3436 }
3437
3438 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003439 template <typename T> bool Compute(T x, T y) const {
3440 return MakeUnsigned(x) <= MakeUnsigned(y);
3441 }
Aart Bike9f37602015-10-09 11:15:55 -07003442
3443 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3444};
3445
Vladimir Markofcb503c2016-05-18 12:48:17 +01003446class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003447 public:
3448 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3449 : HCondition(first, second, dex_pc) {}
3450
3451 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003452 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003453 }
3454 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003455 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3456 }
3457 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3458 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3459 LOG(FATAL) << DebugName() << " is not defined for float values";
3460 UNREACHABLE();
3461 }
3462 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3463 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3464 LOG(FATAL) << DebugName() << " is not defined for double values";
3465 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003466 }
3467
3468 DECLARE_INSTRUCTION(Above);
3469
3470 IfCondition GetCondition() const OVERRIDE {
3471 return kCondA;
3472 }
3473
3474 IfCondition GetOppositeCondition() const OVERRIDE {
3475 return kCondBE;
3476 }
3477
3478 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003479 template <typename T> bool Compute(T x, T y) const {
3480 return MakeUnsigned(x) > MakeUnsigned(y);
3481 }
Aart Bike9f37602015-10-09 11:15:55 -07003482
3483 DISALLOW_COPY_AND_ASSIGN(HAbove);
3484};
3485
Vladimir Markofcb503c2016-05-18 12:48:17 +01003486class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003487 public:
3488 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3489 : HCondition(first, second, dex_pc) {}
3490
3491 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003492 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003493 }
3494 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003495 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3496 }
3497 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3498 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3499 LOG(FATAL) << DebugName() << " is not defined for float values";
3500 UNREACHABLE();
3501 }
3502 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3503 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3504 LOG(FATAL) << DebugName() << " is not defined for double values";
3505 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003506 }
3507
3508 DECLARE_INSTRUCTION(AboveOrEqual);
3509
3510 IfCondition GetCondition() const OVERRIDE {
3511 return kCondAE;
3512 }
3513
3514 IfCondition GetOppositeCondition() const OVERRIDE {
3515 return kCondB;
3516 }
3517
3518 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003519 template <typename T> bool Compute(T x, T y) const {
3520 return MakeUnsigned(x) >= MakeUnsigned(y);
3521 }
Aart Bike9f37602015-10-09 11:15:55 -07003522
3523 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3524};
Dave Allison20dfc792014-06-16 20:44:29 -07003525
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003526// Instruction to check how two inputs compare to each other.
3527// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003528class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003529 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003530 // Note that `comparison_type` is the type of comparison performed
3531 // between the comparison's inputs, not the type of the instantiated
3532 // HCompare instruction (which is always Primitive::kPrimInt).
3533 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003534 HInstruction* first,
3535 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003536 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003537 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003538 : HBinaryOperation(Primitive::kPrimInt,
3539 first,
3540 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003541 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003542 dex_pc) {
3543 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003544 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3545 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003546 }
3547
Roland Levillain9867bc72015-08-05 10:21:34 +01003548 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003549 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3550
3551 template <typename T>
3552 int32_t ComputeFP(T x, T y) const {
3553 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3554 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3555 // Handle the bias.
3556 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3557 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003558
Roland Levillain9867bc72015-08-05 10:21:34 +01003559 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003560 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3561 // reach this code path when processing a freshly built HIR
3562 // graph. However HCompare integer instructions can be synthesized
3563 // by the instruction simplifier to implement IntegerCompare and
3564 // IntegerSignum intrinsics, so we have to handle this case.
3565 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003566 }
3567 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003568 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3569 }
3570 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3571 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3572 }
3573 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3574 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003575 }
3576
Vladimir Marko372f10e2016-05-17 16:30:10 +01003577 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003578 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003579 }
3580
Vladimir Markoa1de9182016-02-25 11:37:38 +00003581 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003582
Roland Levillain31dd3d62016-02-16 12:21:02 +00003583 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003584 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003585 bool IsGtBias() const {
3586 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003587 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003588 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003589
Roland Levillain1693a1f2016-03-15 14:57:31 +00003590 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3591 // Comparisons do not require a runtime call in any back end.
3592 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003593 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003594
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003595 DECLARE_INSTRUCTION(Compare);
3596
Roland Levillain31dd3d62016-02-16 12:21:02 +00003597 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003598 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3599 static constexpr size_t kFieldComparisonBiasSize =
3600 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3601 static constexpr size_t kNumberOfComparePackedBits =
3602 kFieldComparisonBias + kFieldComparisonBiasSize;
3603 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3604 using ComparisonBiasField =
3605 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3606
Roland Levillain31dd3d62016-02-16 12:21:02 +00003607 // Return an integer constant containing the result of a comparison evaluated at compile time.
3608 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3609 DCHECK(value == -1 || value == 0 || value == 1) << value;
3610 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3611 }
3612
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003613 private:
3614 DISALLOW_COPY_AND_ASSIGN(HCompare);
3615};
3616
Vladimir Markofcb503c2016-05-18 12:48:17 +01003617class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003618 public:
3619 HNewInstance(HInstruction* cls,
3620 HCurrentMethod* current_method,
3621 uint32_t dex_pc,
3622 uint16_t type_index,
3623 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003624 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003625 bool finalizable,
3626 QuickEntrypointEnum entrypoint)
3627 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3628 type_index_(type_index),
3629 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003630 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003631 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003632 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003633 SetRawInputAt(0, cls);
3634 SetRawInputAt(1, current_method);
3635 }
3636
3637 uint16_t GetTypeIndex() const { return type_index_; }
3638 const DexFile& GetDexFile() const { return dex_file_; }
3639
3640 // Calls runtime so needs an environment.
3641 bool NeedsEnvironment() const OVERRIDE { return true; }
3642
Mingyao Yang062157f2016-03-02 10:15:36 -08003643 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3644 bool CanThrow() const OVERRIDE { return true; }
3645
3646 // Needs to call into runtime to make sure it's instantiable/accessible.
3647 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003648
Vladimir Markoa1de9182016-02-25 11:37:38 +00003649 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003650
3651 bool CanBeNull() const OVERRIDE { return false; }
3652
3653 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3654
3655 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3656 entrypoint_ = entrypoint;
3657 }
3658
3659 bool IsStringAlloc() const;
3660
3661 DECLARE_INSTRUCTION(NewInstance);
3662
3663 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003664 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3665 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003666 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3667 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3668 "Too many packed fields.");
3669
David Brazdil6de19382016-01-08 17:37:10 +00003670 const uint16_t type_index_;
3671 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003672 QuickEntrypointEnum entrypoint_;
3673
3674 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3675};
3676
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003677enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003678#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3679 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003680#include "intrinsics_list.h"
3681 kNone,
3682 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3683#undef INTRINSICS_LIST
3684#undef OPTIMIZING_INTRINSICS
3685};
3686std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3687
Agi Csaki05f20562015-08-19 14:58:14 -07003688enum IntrinsicNeedsEnvironmentOrCache {
3689 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3690 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003691};
3692
Aart Bik5d75afe2015-12-14 11:57:01 -08003693enum IntrinsicSideEffects {
3694 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3695 kReadSideEffects, // Intrinsic may read heap memory.
3696 kWriteSideEffects, // Intrinsic may write heap memory.
3697 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3698};
3699
3700enum IntrinsicExceptions {
3701 kNoThrow, // Intrinsic does not throw any exceptions.
3702 kCanThrow // Intrinsic may throw exceptions.
3703};
3704
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003705class HInvoke : public HInstruction {
3706 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003707 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003708
Vladimir Marko372f10e2016-05-17 16:30:10 +01003709 using HInstruction::GetInputRecords; // Keep the const version visible.
3710 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3711 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3712 }
3713
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003714 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003715 SetRawInputAt(index, argument);
3716 }
3717
Roland Levillain3e3d7332015-04-28 11:00:54 +01003718 // Return the number of arguments. This number can be lower than
3719 // the number of inputs returned by InputCount(), as some invoke
3720 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3721 // inputs at the end of their list of inputs.
3722 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3723
Vladimir Markoa1de9182016-02-25 11:37:38 +00003724 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003725
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003726 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003727 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003728
Vladimir Markoa1de9182016-02-25 11:37:38 +00003729 InvokeType GetOriginalInvokeType() const {
3730 return GetPackedField<OriginalInvokeTypeField>();
3731 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003732
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003733 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003734 return intrinsic_;
3735 }
3736
Aart Bik5d75afe2015-12-14 11:57:01 -08003737 void SetIntrinsic(Intrinsics intrinsic,
3738 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3739 IntrinsicSideEffects side_effects,
3740 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003741
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003742 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003743 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003744 }
3745
Vladimir Markoa1de9182016-02-25 11:37:38 +00003746 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003747
3748 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3749
Vladimir Marko372f10e2016-05-17 16:30:10 +01003750 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003751 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3752 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003753
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003754 uint32_t* GetIntrinsicOptimizations() {
3755 return &intrinsic_optimizations_;
3756 }
3757
3758 const uint32_t* GetIntrinsicOptimizations() const {
3759 return &intrinsic_optimizations_;
3760 }
3761
3762 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3763
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003764 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003765
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003766 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003767 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3768 static constexpr size_t kFieldOriginalInvokeTypeSize =
3769 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3770 static constexpr size_t kFieldReturnType =
3771 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3772 static constexpr size_t kFieldReturnTypeSize =
3773 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3774 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3775 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3776 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3777 using OriginalInvokeTypeField =
3778 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3779 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3780
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003781 HInvoke(ArenaAllocator* arena,
3782 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003783 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003784 Primitive::Type return_type,
3785 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003786 uint32_t dex_method_index,
3787 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003788 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003789 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003790 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003791 inputs_(number_of_arguments + number_of_other_inputs,
3792 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003793 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003794 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003795 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003796 SetPackedField<ReturnTypeField>(return_type);
3797 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3798 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003799 }
3800
Vladimir Markoa1de9182016-02-25 11:37:38 +00003801 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003802
Roland Levillain3e3d7332015-04-28 11:00:54 +01003803 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003804 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003805 const uint32_t dex_method_index_;
3806 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003807
3808 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3809 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003810
3811 private:
3812 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3813};
3814
Vladimir Markofcb503c2016-05-18 12:48:17 +01003815class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003816 public:
3817 HInvokeUnresolved(ArenaAllocator* arena,
3818 uint32_t number_of_arguments,
3819 Primitive::Type return_type,
3820 uint32_t dex_pc,
3821 uint32_t dex_method_index,
3822 InvokeType invoke_type)
3823 : HInvoke(arena,
3824 number_of_arguments,
3825 0u /* number_of_other_inputs */,
3826 return_type,
3827 dex_pc,
3828 dex_method_index,
3829 invoke_type) {
3830 }
3831
3832 DECLARE_INSTRUCTION(InvokeUnresolved);
3833
3834 private:
3835 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3836};
3837
Vladimir Markofcb503c2016-05-18 12:48:17 +01003838class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003839 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003840 // Requirements of this method call regarding the class
3841 // initialization (clinit) check of its declaring class.
3842 enum class ClinitCheckRequirement {
3843 kNone, // Class already initialized.
3844 kExplicit, // Static call having explicit clinit check as last input.
3845 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003846 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003847 };
3848
Vladimir Marko58155012015-08-19 12:49:41 +00003849 // Determines how to load the target ArtMethod*.
3850 enum class MethodLoadKind {
3851 // Use a String init ArtMethod* loaded from Thread entrypoints.
3852 kStringInit,
3853
3854 // Use the method's own ArtMethod* loaded by the register allocator.
3855 kRecursive,
3856
3857 // Use ArtMethod* at a known address, embed the direct address in the code.
3858 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3859 kDirectAddress,
3860
3861 // Use ArtMethod* at an address that will be known at link time, embed the direct
3862 // address in the code. If the image is relocatable, emit .patch_oat entry.
3863 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3864 // the image relocatable or not.
3865 kDirectAddressWithFixup,
3866
Vladimir Markoa1de9182016-02-25 11:37:38 +00003867 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003868 // Used when we need to use the dex cache, for example for invoke-static that
3869 // may cause class initialization (the entry may point to a resolution method),
3870 // and we know that we can access the dex cache arrays using a PC-relative load.
3871 kDexCachePcRelative,
3872
3873 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3874 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3875 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3876 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3877 kDexCacheViaMethod,
3878 };
3879
3880 // Determines the location of the code pointer.
3881 enum class CodePtrLocation {
3882 // Recursive call, use local PC-relative call instruction.
3883 kCallSelf,
3884
3885 // Use PC-relative call instruction patched at link time.
3886 // Used for calls within an oat file, boot->boot or app->app.
3887 kCallPCRelative,
3888
3889 // Call to a known target address, embed the direct address in code.
3890 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3891 kCallDirect,
3892
3893 // Call to a target address that will be known at link time, embed the direct
3894 // address in code. If the image is relocatable, emit .patch_oat entry.
3895 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3896 // the image relocatable or not.
3897 kCallDirectWithFixup,
3898
3899 // Use code pointer from the ArtMethod*.
3900 // Used when we don't know the target code. This is also the last-resort-kind used when
3901 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3902 kCallArtMethod,
3903 };
3904
3905 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003906 MethodLoadKind method_load_kind;
3907 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003908 // The method load data holds
3909 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3910 // Note that there are multiple string init methods, each having its own offset.
3911 // - the method address for kDirectAddress
3912 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003913 uint64_t method_load_data;
3914 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003915 };
3916
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003917 HInvokeStaticOrDirect(ArenaAllocator* arena,
3918 uint32_t number_of_arguments,
3919 Primitive::Type return_type,
3920 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003921 uint32_t method_index,
3922 MethodReference target_method,
3923 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003924 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003925 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003926 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003927 : HInvoke(arena,
3928 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003929 // There is potentially one extra argument for the HCurrentMethod node, and
3930 // potentially one other if the clinit check is explicit, and potentially
3931 // one other if the method is a string factory.
3932 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003933 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003934 return_type,
3935 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003936 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003937 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003938 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003939 dispatch_info_(dispatch_info) {
3940 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3941 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3942 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003943
Vladimir Markodc151b22015-10-15 18:02:30 +01003944 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003945 bool had_current_method_input = HasCurrentMethodInput();
3946 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3947
3948 // Using the current method is the default and once we find a better
3949 // method load kind, we should not go back to using the current method.
3950 DCHECK(had_current_method_input || !needs_current_method_input);
3951
3952 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003953 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3954 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003955 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003956 dispatch_info_ = dispatch_info;
3957 }
3958
Vladimir Markoc53c0792015-11-19 15:48:33 +00003959 void AddSpecialInput(HInstruction* input) {
3960 // We allow only one special input.
3961 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3962 DCHECK(InputCount() == GetSpecialInputIndex() ||
3963 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3964 InsertInputAt(GetSpecialInputIndex(), input);
3965 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003966
Vladimir Marko372f10e2016-05-17 16:30:10 +01003967 using HInstruction::GetInputRecords; // Keep the const version visible.
3968 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3969 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3970 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3971 DCHECK(!input_records.empty());
3972 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3973 HInstruction* last_input = input_records.back().GetInstruction();
3974 // Note: `last_input` may be null during arguments setup.
3975 if (last_input != nullptr) {
3976 // `last_input` is the last input of a static invoke marked as having
3977 // an explicit clinit check. It must either be:
3978 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3979 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3980 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3981 }
3982 }
3983 return input_records;
3984 }
3985
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003986 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003987 // We access the method via the dex cache so we can't do an implicit null check.
3988 // TODO: for intrinsics we can generate implicit null checks.
3989 return false;
3990 }
3991
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003992 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003993 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003994 }
3995
Vladimir Markoc53c0792015-11-19 15:48:33 +00003996 // Get the index of the special input, if any.
3997 //
David Brazdil6de19382016-01-08 17:37:10 +00003998 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3999 // method pointer; otherwise there may be one platform-specific special input,
4000 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004001 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004002 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004003
Vladimir Markoa1de9182016-02-25 11:37:38 +00004004 InvokeType GetOptimizedInvokeType() const {
4005 return GetPackedField<OptimizedInvokeTypeField>();
4006 }
4007
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004008 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004010 }
4011
Vladimir Marko58155012015-08-19 12:49:41 +00004012 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4013 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4014 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004015 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004016 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004017 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004018 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004019 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4020 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004021 bool HasCurrentMethodInput() const {
4022 // This function can be called only after the invoke has been fully initialized by the builder.
4023 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004024 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004025 return true;
4026 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004027 DCHECK(InputCount() == GetSpecialInputIndex() ||
4028 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004029 return false;
4030 }
4031 }
Vladimir Marko58155012015-08-19 12:49:41 +00004032 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4033 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004034 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004035
4036 int32_t GetStringInitOffset() const {
4037 DCHECK(IsStringInit());
4038 return dispatch_info_.method_load_data;
4039 }
4040
4041 uint64_t GetMethodAddress() const {
4042 DCHECK(HasMethodAddress());
4043 return dispatch_info_.method_load_data;
4044 }
4045
4046 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004047 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004048 return dispatch_info_.method_load_data;
4049 }
4050
4051 uint64_t GetDirectCodePtr() const {
4052 DCHECK(HasDirectCodePtr());
4053 return dispatch_info_.direct_code_ptr;
4054 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004055
Vladimir Markoa1de9182016-02-25 11:37:38 +00004056 ClinitCheckRequirement GetClinitCheckRequirement() const {
4057 return GetPackedField<ClinitCheckRequirementField>();
4058 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004059
Roland Levillain4c0eb422015-04-24 16:43:49 +01004060 // Is this instruction a call to a static method?
4061 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004062 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004063 }
4064
Vladimir Markofbb184a2015-11-13 14:47:00 +00004065 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4066 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4067 // instruction; only relevant for static calls with explicit clinit check.
4068 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004069 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004070 size_t last_input_index = inputs_.size() - 1u;
4071 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004072 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004073 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004074 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004075 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004076 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004077 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004078 }
4079
4080 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004081 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004082 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004083 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004084 }
4085
4086 // Is this a call to a static method whose declaring class has an
4087 // implicit intialization check requirement?
4088 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004089 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004090 }
4091
Vladimir Markob554b5a2015-11-06 12:57:55 +00004092 // Does this method load kind need the current method as an input?
4093 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4094 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4095 }
4096
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004097 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004098
Roland Levillain4c0eb422015-04-24 16:43:49 +01004099 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004100 void InsertInputAt(size_t index, HInstruction* input);
4101 void RemoveInputAt(size_t index);
4102
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004103 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004104 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4105 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4106 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4107 static constexpr size_t kFieldClinitCheckRequirement =
4108 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4109 static constexpr size_t kFieldClinitCheckRequirementSize =
4110 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4111 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4112 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4113 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4114 "Too many packed fields.");
4115 using OptimizedInvokeTypeField =
4116 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4117 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4118 kFieldClinitCheckRequirement,
4119 kFieldClinitCheckRequirementSize>;
4120
Vladimir Marko58155012015-08-19 12:49:41 +00004121 // The target method may refer to different dex file or method index than the original
4122 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4123 // in derived class to increase visibility.
4124 MethodReference target_method_;
4125 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004126
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004127 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004128};
Vladimir Markof64242a2015-12-01 14:58:23 +00004129std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004130std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004131
Vladimir Markofcb503c2016-05-18 12:48:17 +01004132class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004133 public:
4134 HInvokeVirtual(ArenaAllocator* arena,
4135 uint32_t number_of_arguments,
4136 Primitive::Type return_type,
4137 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004138 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004139 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004140 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004141 vtable_index_(vtable_index) {}
4142
Calin Juravle641547a2015-04-21 22:08:51 +01004143 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004144 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004145 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004146 }
4147
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004148 uint32_t GetVTableIndex() const { return vtable_index_; }
4149
4150 DECLARE_INSTRUCTION(InvokeVirtual);
4151
4152 private:
4153 const uint32_t vtable_index_;
4154
4155 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4156};
4157
Vladimir Markofcb503c2016-05-18 12:48:17 +01004158class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004159 public:
4160 HInvokeInterface(ArenaAllocator* arena,
4161 uint32_t number_of_arguments,
4162 Primitive::Type return_type,
4163 uint32_t dex_pc,
4164 uint32_t dex_method_index,
4165 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004166 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004167 imt_index_(imt_index) {}
4168
Calin Juravle641547a2015-04-21 22:08:51 +01004169 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004170 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004171 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004172 }
4173
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004174 uint32_t GetImtIndex() const { return imt_index_; }
4175 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4176
4177 DECLARE_INSTRUCTION(InvokeInterface);
4178
4179 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004180 const uint32_t imt_index_;
4181
4182 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4183};
4184
Vladimir Markofcb503c2016-05-18 12:48:17 +01004185class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004186 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004187 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004188 : HUnaryOperation(result_type, input, dex_pc) {
4189 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4190 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004191
Roland Levillain9867bc72015-08-05 10:21:34 +01004192 template <typename T> T Compute(T x) const { return -x; }
4193
4194 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004196 }
4197 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004198 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004199 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004200 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4201 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4202 }
4203 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4204 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4205 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004206
Roland Levillain88cb1752014-10-20 16:36:47 +01004207 DECLARE_INSTRUCTION(Neg);
4208
4209 private:
4210 DISALLOW_COPY_AND_ASSIGN(HNeg);
4211};
4212
Vladimir Markofcb503c2016-05-18 12:48:17 +01004213class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004214 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004215 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004216 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004217 uint32_t dex_pc,
4218 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004219 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004220 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004221 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004222 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004223 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004224 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004225 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004226 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004227 }
4228
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004229 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004230 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004231
4232 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004233 bool NeedsEnvironment() const OVERRIDE { return true; }
4234
Mingyao Yang0c365e62015-03-31 15:09:29 -07004235 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4236 bool CanThrow() const OVERRIDE { return true; }
4237
Calin Juravle10e244f2015-01-26 18:54:32 +00004238 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004239
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004240 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4241
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004242 DECLARE_INSTRUCTION(NewArray);
4243
4244 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004245 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004246 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004247 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004248
4249 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4250};
4251
Vladimir Markofcb503c2016-05-18 12:48:17 +01004252class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004253 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004254 HAdd(Primitive::Type result_type,
4255 HInstruction* left,
4256 HInstruction* right,
4257 uint32_t dex_pc = kNoDexPc)
4258 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004259
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004260 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004261
Roland Levillain9867bc72015-08-05 10:21:34 +01004262 template <typename T> T Compute(T x, T y) const { return x + y; }
4263
4264 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004265 return GetBlock()->GetGraph()->GetIntConstant(
4266 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004267 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004268 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004269 return GetBlock()->GetGraph()->GetLongConstant(
4270 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004271 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004272 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4273 return GetBlock()->GetGraph()->GetFloatConstant(
4274 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4275 }
4276 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4277 return GetBlock()->GetGraph()->GetDoubleConstant(
4278 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4279 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004280
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004281 DECLARE_INSTRUCTION(Add);
4282
4283 private:
4284 DISALLOW_COPY_AND_ASSIGN(HAdd);
4285};
4286
Vladimir Markofcb503c2016-05-18 12:48:17 +01004287class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004288 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004289 HSub(Primitive::Type result_type,
4290 HInstruction* left,
4291 HInstruction* right,
4292 uint32_t dex_pc = kNoDexPc)
4293 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004294
Roland Levillain9867bc72015-08-05 10:21:34 +01004295 template <typename T> T Compute(T x, T y) const { return x - y; }
4296
4297 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004298 return GetBlock()->GetGraph()->GetIntConstant(
4299 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004300 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004301 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004302 return GetBlock()->GetGraph()->GetLongConstant(
4303 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004304 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004305 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4306 return GetBlock()->GetGraph()->GetFloatConstant(
4307 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4308 }
4309 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4310 return GetBlock()->GetGraph()->GetDoubleConstant(
4311 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4312 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004313
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004314 DECLARE_INSTRUCTION(Sub);
4315
4316 private:
4317 DISALLOW_COPY_AND_ASSIGN(HSub);
4318};
4319
Vladimir Markofcb503c2016-05-18 12:48:17 +01004320class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004321 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004322 HMul(Primitive::Type result_type,
4323 HInstruction* left,
4324 HInstruction* right,
4325 uint32_t dex_pc = kNoDexPc)
4326 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004327
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004328 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004329
Roland Levillain9867bc72015-08-05 10:21:34 +01004330 template <typename T> T Compute(T x, T y) const { return x * y; }
4331
4332 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004333 return GetBlock()->GetGraph()->GetIntConstant(
4334 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004335 }
4336 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004337 return GetBlock()->GetGraph()->GetLongConstant(
4338 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004339 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004340 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4341 return GetBlock()->GetGraph()->GetFloatConstant(
4342 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4343 }
4344 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4345 return GetBlock()->GetGraph()->GetDoubleConstant(
4346 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4347 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004348
4349 DECLARE_INSTRUCTION(Mul);
4350
4351 private:
4352 DISALLOW_COPY_AND_ASSIGN(HMul);
4353};
4354
Vladimir Markofcb503c2016-05-18 12:48:17 +01004355class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004356 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004357 HDiv(Primitive::Type result_type,
4358 HInstruction* left,
4359 HInstruction* right,
4360 uint32_t dex_pc)
4361 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004362
Roland Levillain9867bc72015-08-05 10:21:34 +01004363 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004364 T ComputeIntegral(T x, T y) const {
4365 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004366 // Our graph structure ensures we never have 0 for `y` during
4367 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004368 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004369 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004370 return (y == -1) ? -x : x / y;
4371 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004372
Roland Levillain31dd3d62016-02-16 12:21:02 +00004373 template <typename T>
4374 T ComputeFP(T x, T y) const {
4375 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4376 return x / y;
4377 }
4378
Roland Levillain9867bc72015-08-05 10:21:34 +01004379 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004380 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004381 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004382 }
4383 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004384 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004385 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4386 }
4387 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4388 return GetBlock()->GetGraph()->GetFloatConstant(
4389 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4390 }
4391 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4392 return GetBlock()->GetGraph()->GetDoubleConstant(
4393 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004394 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004395
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004396 static SideEffects SideEffectsForArchRuntimeCalls() {
4397 // The generated code can use a runtime call.
4398 return SideEffects::CanTriggerGC();
4399 }
4400
Calin Juravle7c4954d2014-10-28 16:57:40 +00004401 DECLARE_INSTRUCTION(Div);
4402
4403 private:
4404 DISALLOW_COPY_AND_ASSIGN(HDiv);
4405};
4406
Vladimir Markofcb503c2016-05-18 12:48:17 +01004407class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004408 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004409 HRem(Primitive::Type result_type,
4410 HInstruction* left,
4411 HInstruction* right,
4412 uint32_t dex_pc)
4413 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004414
Roland Levillain9867bc72015-08-05 10:21:34 +01004415 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004416 T ComputeIntegral(T x, T y) const {
4417 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004418 // Our graph structure ensures we never have 0 for `y` during
4419 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004420 DCHECK_NE(y, 0);
4421 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4422 return (y == -1) ? 0 : x % y;
4423 }
4424
Roland Levillain31dd3d62016-02-16 12:21:02 +00004425 template <typename T>
4426 T ComputeFP(T x, T y) const {
4427 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4428 return std::fmod(x, y);
4429 }
4430
Roland Levillain9867bc72015-08-05 10:21:34 +01004431 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004432 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004433 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004434 }
4435 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004436 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004437 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004438 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004439 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4440 return GetBlock()->GetGraph()->GetFloatConstant(
4441 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4442 }
4443 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4444 return GetBlock()->GetGraph()->GetDoubleConstant(
4445 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4446 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004447
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004448 static SideEffects SideEffectsForArchRuntimeCalls() {
4449 return SideEffects::CanTriggerGC();
4450 }
4451
Calin Juravlebacfec32014-11-14 15:54:36 +00004452 DECLARE_INSTRUCTION(Rem);
4453
4454 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004455 DISALLOW_COPY_AND_ASSIGN(HRem);
4456};
4457
Vladimir Markofcb503c2016-05-18 12:48:17 +01004458class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004459 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004460 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4461 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004462 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004463 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004464 SetRawInputAt(0, value);
4465 }
4466
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004467 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4468
Calin Juravled0d48522014-11-04 16:40:20 +00004469 bool CanBeMoved() const OVERRIDE { return true; }
4470
Vladimir Marko372f10e2016-05-17 16:30:10 +01004471 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004472 return true;
4473 }
4474
4475 bool NeedsEnvironment() const OVERRIDE { return true; }
4476 bool CanThrow() const OVERRIDE { return true; }
4477
Calin Juravled0d48522014-11-04 16:40:20 +00004478 DECLARE_INSTRUCTION(DivZeroCheck);
4479
4480 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004481 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4482};
4483
Vladimir Markofcb503c2016-05-18 12:48:17 +01004484class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004485 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004486 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004487 HInstruction* value,
4488 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004489 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004490 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4491 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4492 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004493 }
4494
Roland Levillain5b5b9312016-03-22 14:57:31 +00004495 template <typename T>
4496 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4497 return value << (distance & max_shift_distance);
4498 }
4499
4500 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004501 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004503 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004504 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004505 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004506 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004507 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004508 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4509 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4510 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4511 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004512 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004513 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4514 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004515 LOG(FATAL) << DebugName() << " is not defined for float values";
4516 UNREACHABLE();
4517 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004518 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4519 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004520 LOG(FATAL) << DebugName() << " is not defined for double values";
4521 UNREACHABLE();
4522 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004523
4524 DECLARE_INSTRUCTION(Shl);
4525
4526 private:
4527 DISALLOW_COPY_AND_ASSIGN(HShl);
4528};
4529
Vladimir Markofcb503c2016-05-18 12:48:17 +01004530class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004531 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004532 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004533 HInstruction* value,
4534 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004535 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004536 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4537 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4538 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004539 }
4540
Roland Levillain5b5b9312016-03-22 14:57:31 +00004541 template <typename T>
4542 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4543 return value >> (distance & max_shift_distance);
4544 }
4545
4546 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004549 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004550 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004551 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004552 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004553 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004554 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4555 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4556 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4557 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004558 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004559 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4560 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004561 LOG(FATAL) << DebugName() << " is not defined for float values";
4562 UNREACHABLE();
4563 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004564 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4565 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004566 LOG(FATAL) << DebugName() << " is not defined for double values";
4567 UNREACHABLE();
4568 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004569
4570 DECLARE_INSTRUCTION(Shr);
4571
4572 private:
4573 DISALLOW_COPY_AND_ASSIGN(HShr);
4574};
4575
Vladimir Markofcb503c2016-05-18 12:48:17 +01004576class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004577 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004578 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004579 HInstruction* value,
4580 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004581 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004582 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4583 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4584 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004585 }
4586
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 template <typename T>
4588 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4589 typedef typename std::make_unsigned<T>::type V;
4590 V ux = static_cast<V>(value);
4591 return static_cast<T>(ux >> (distance & max_shift_distance));
4592 }
4593
4594 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004595 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004596 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004597 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004598 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004599 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004600 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004601 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004602 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4603 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4604 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4605 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004606 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004607 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4608 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004609 LOG(FATAL) << DebugName() << " is not defined for float values";
4610 UNREACHABLE();
4611 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004612 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4613 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004614 LOG(FATAL) << DebugName() << " is not defined for double values";
4615 UNREACHABLE();
4616 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004617
4618 DECLARE_INSTRUCTION(UShr);
4619
4620 private:
4621 DISALLOW_COPY_AND_ASSIGN(HUShr);
4622};
4623
Vladimir Markofcb503c2016-05-18 12:48:17 +01004624class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004625 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004626 HAnd(Primitive::Type result_type,
4627 HInstruction* left,
4628 HInstruction* right,
4629 uint32_t dex_pc = kNoDexPc)
4630 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004631
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004632 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004633
Roland Levillaine53bd812016-02-24 14:54:18 +00004634 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004635
4636 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004637 return GetBlock()->GetGraph()->GetIntConstant(
4638 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004639 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004640 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004641 return GetBlock()->GetGraph()->GetLongConstant(
4642 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004643 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004644 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4645 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4646 LOG(FATAL) << DebugName() << " is not defined for float values";
4647 UNREACHABLE();
4648 }
4649 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4650 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4651 LOG(FATAL) << DebugName() << " is not defined for double values";
4652 UNREACHABLE();
4653 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004654
4655 DECLARE_INSTRUCTION(And);
4656
4657 private:
4658 DISALLOW_COPY_AND_ASSIGN(HAnd);
4659};
4660
Vladimir Markofcb503c2016-05-18 12:48:17 +01004661class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004662 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 HOr(Primitive::Type result_type,
4664 HInstruction* left,
4665 HInstruction* right,
4666 uint32_t dex_pc = kNoDexPc)
4667 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004668
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004669 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004670
Roland Levillaine53bd812016-02-24 14:54:18 +00004671 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004672
4673 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004674 return GetBlock()->GetGraph()->GetIntConstant(
4675 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004676 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004677 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004678 return GetBlock()->GetGraph()->GetLongConstant(
4679 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004680 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004681 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4682 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4683 LOG(FATAL) << DebugName() << " is not defined for float values";
4684 UNREACHABLE();
4685 }
4686 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4687 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4688 LOG(FATAL) << DebugName() << " is not defined for double values";
4689 UNREACHABLE();
4690 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004691
4692 DECLARE_INSTRUCTION(Or);
4693
4694 private:
4695 DISALLOW_COPY_AND_ASSIGN(HOr);
4696};
4697
Vladimir Markofcb503c2016-05-18 12:48:17 +01004698class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004699 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004700 HXor(Primitive::Type result_type,
4701 HInstruction* left,
4702 HInstruction* right,
4703 uint32_t dex_pc = kNoDexPc)
4704 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004705
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004706 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004707
Roland Levillaine53bd812016-02-24 14:54:18 +00004708 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004709
4710 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004711 return GetBlock()->GetGraph()->GetIntConstant(
4712 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004713 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004714 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004715 return GetBlock()->GetGraph()->GetLongConstant(
4716 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004717 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004718 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4719 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4720 LOG(FATAL) << DebugName() << " is not defined for float values";
4721 UNREACHABLE();
4722 }
4723 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4724 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4725 LOG(FATAL) << DebugName() << " is not defined for double values";
4726 UNREACHABLE();
4727 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004728
4729 DECLARE_INSTRUCTION(Xor);
4730
4731 private:
4732 DISALLOW_COPY_AND_ASSIGN(HXor);
4733};
4734
Vladimir Markofcb503c2016-05-18 12:48:17 +01004735class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004736 public:
4737 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004738 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004739 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4740 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004741 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004742
Roland Levillain5b5b9312016-03-22 14:57:31 +00004743 template <typename T>
4744 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4745 typedef typename std::make_unsigned<T>::type V;
4746 V ux = static_cast<V>(value);
4747 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004748 return static_cast<T>(ux);
4749 } else {
4750 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004751 return static_cast<T>(ux >> (distance & max_shift_value)) |
4752 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004753 }
4754 }
4755
Roland Levillain5b5b9312016-03-22 14:57:31 +00004756 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004757 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004758 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004759 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004760 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004761 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004762 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004763 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004764 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4765 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4766 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4767 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004768 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4770 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004771 LOG(FATAL) << DebugName() << " is not defined for float values";
4772 UNREACHABLE();
4773 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4775 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004776 LOG(FATAL) << DebugName() << " is not defined for double values";
4777 UNREACHABLE();
4778 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004779
4780 DECLARE_INSTRUCTION(Ror);
4781
4782 private:
4783 DISALLOW_COPY_AND_ASSIGN(HRor);
4784};
4785
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004786// The value of a parameter in this method. Its location depends on
4787// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004788class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004789 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004790 HParameterValue(const DexFile& dex_file,
4791 uint16_t type_index,
4792 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004793 Primitive::Type parameter_type,
4794 bool is_this = false)
4795 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004796 dex_file_(dex_file),
4797 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004798 index_(index) {
4799 SetPackedFlag<kFlagIsThis>(is_this);
4800 SetPackedFlag<kFlagCanBeNull>(!is_this);
4801 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004802
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004803 const DexFile& GetDexFile() const { return dex_file_; }
4804 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004805 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004806 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004807
Vladimir Markoa1de9182016-02-25 11:37:38 +00004808 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4809 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004810
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004811 DECLARE_INSTRUCTION(ParameterValue);
4812
4813 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004814 // Whether or not the parameter value corresponds to 'this' argument.
4815 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4816 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4817 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4818 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4819 "Too many packed fields.");
4820
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004821 const DexFile& dex_file_;
4822 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004823 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004824 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004825 const uint8_t index_;
4826
4827 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4828};
4829
Vladimir Markofcb503c2016-05-18 12:48:17 +01004830class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004831 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4833 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004834
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004835 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004836 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004837 return true;
4838 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004839
Roland Levillain9867bc72015-08-05 10:21:34 +01004840 template <typename T> T Compute(T x) const { return ~x; }
4841
4842 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004843 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004844 }
4845 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004846 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004847 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004848 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4849 LOG(FATAL) << DebugName() << " is not defined for float values";
4850 UNREACHABLE();
4851 }
4852 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4853 LOG(FATAL) << DebugName() << " is not defined for double values";
4854 UNREACHABLE();
4855 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004856
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004857 DECLARE_INSTRUCTION(Not);
4858
4859 private:
4860 DISALLOW_COPY_AND_ASSIGN(HNot);
4861};
4862
Vladimir Markofcb503c2016-05-18 12:48:17 +01004863class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004864 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004865 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4866 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004867
4868 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004869 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004870 return true;
4871 }
4872
Roland Levillain9867bc72015-08-05 10:21:34 +01004873 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004874 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004875 return !x;
4876 }
4877
Roland Levillain9867bc72015-08-05 10:21:34 +01004878 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004879 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004880 }
4881 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4882 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004883 UNREACHABLE();
4884 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004885 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4886 LOG(FATAL) << DebugName() << " is not defined for float values";
4887 UNREACHABLE();
4888 }
4889 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4890 LOG(FATAL) << DebugName() << " is not defined for double values";
4891 UNREACHABLE();
4892 }
David Brazdil66d126e2015-04-03 16:02:44 +01004893
4894 DECLARE_INSTRUCTION(BooleanNot);
4895
4896 private:
4897 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4898};
4899
Vladimir Markofcb503c2016-05-18 12:48:17 +01004900class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004901 public:
4902 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004903 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004904 : HExpression(result_type,
4905 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4906 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004907 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004908 // Invariant: We should never generate a conversion to a Boolean value.
4909 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004910 }
4911
4912 HInstruction* GetInput() const { return InputAt(0); }
4913 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4914 Primitive::Type GetResultType() const { return GetType(); }
4915
4916 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004917 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4918 return true;
4919 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004920
Mark Mendelle82549b2015-05-06 10:55:34 -04004921 // Try to statically evaluate the conversion and return a HConstant
4922 // containing the result. If the input cannot be converted, return nullptr.
4923 HConstant* TryStaticEvaluation() const;
4924
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004925 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4926 Primitive::Type result_type) {
4927 // Some architectures may not require the 'GC' side effects, but at this point
4928 // in the compilation process we do not know what architecture we will
4929 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004930 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4931 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004932 return SideEffects::CanTriggerGC();
4933 }
4934 return SideEffects::None();
4935 }
4936
Roland Levillaindff1f282014-11-05 14:15:05 +00004937 DECLARE_INSTRUCTION(TypeConversion);
4938
4939 private:
4940 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4941};
4942
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004943static constexpr uint32_t kNoRegNumber = -1;
4944
Vladimir Markofcb503c2016-05-18 12:48:17 +01004945class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004946 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004947 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4948 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004949 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004950 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004951 SetRawInputAt(0, value);
4952 }
4953
Calin Juravle10e244f2015-01-26 18:54:32 +00004954 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004955 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004956 return true;
4957 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004958
Calin Juravle10e244f2015-01-26 18:54:32 +00004959 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004960
Calin Juravle10e244f2015-01-26 18:54:32 +00004961 bool CanThrow() const OVERRIDE { return true; }
4962
4963 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004964
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004965
4966 DECLARE_INSTRUCTION(NullCheck);
4967
4968 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004969 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4970};
4971
4972class FieldInfo : public ValueObject {
4973 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004974 FieldInfo(MemberOffset field_offset,
4975 Primitive::Type field_type,
4976 bool is_volatile,
4977 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004978 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004979 const DexFile& dex_file,
4980 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004981 : field_offset_(field_offset),
4982 field_type_(field_type),
4983 is_volatile_(is_volatile),
4984 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004985 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004986 dex_file_(dex_file),
4987 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988
4989 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004990 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004991 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004992 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004993 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004994 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004995 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004996
4997 private:
4998 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004999 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005000 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005001 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005002 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005003 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005004 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005005};
5006
Vladimir Markofcb503c2016-05-18 12:48:17 +01005007class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005008 public:
5009 HInstanceFieldGet(HInstruction* value,
5010 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005011 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005012 bool is_volatile,
5013 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005014 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005015 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005016 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005017 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005018 : HExpression(field_type,
5019 SideEffects::FieldReadOfType(field_type, is_volatile),
5020 dex_pc),
5021 field_info_(field_offset,
5022 field_type,
5023 is_volatile,
5024 field_idx,
5025 declaring_class_def_index,
5026 dex_file,
5027 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005028 SetRawInputAt(0, value);
5029 }
5030
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005031 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005032
Vladimir Marko372f10e2016-05-17 16:30:10 +01005033 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5034 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005035 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005036 }
5037
Calin Juravle641547a2015-04-21 22:08:51 +01005038 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005039 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005040 }
5041
5042 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005043 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5044 }
5045
Calin Juravle52c48962014-12-16 17:02:57 +00005046 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005047 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005048 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005049 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005050
5051 DECLARE_INSTRUCTION(InstanceFieldGet);
5052
5053 private:
5054 const FieldInfo field_info_;
5055
5056 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5057};
5058
Vladimir Markofcb503c2016-05-18 12:48:17 +01005059class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005060 public:
5061 HInstanceFieldSet(HInstruction* object,
5062 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005063 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005064 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005065 bool is_volatile,
5066 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005067 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005068 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005069 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005070 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005071 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5072 dex_pc),
5073 field_info_(field_offset,
5074 field_type,
5075 is_volatile,
5076 field_idx,
5077 declaring_class_def_index,
5078 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005079 dex_cache) {
5080 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005081 SetRawInputAt(0, object);
5082 SetRawInputAt(1, value);
5083 }
5084
Calin Juravle641547a2015-04-21 22:08:51 +01005085 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005086 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005087 }
5088
Calin Juravle52c48962014-12-16 17:02:57 +00005089 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005090 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005091 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005092 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005093 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005094 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5095 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005096
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005097 DECLARE_INSTRUCTION(InstanceFieldSet);
5098
5099 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005100 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5101 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5102 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5103 "Too many packed fields.");
5104
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005105 const FieldInfo field_info_;
5106
5107 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5108};
5109
Vladimir Markofcb503c2016-05-18 12:48:17 +01005110class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005112 HArrayGet(HInstruction* array,
5113 HInstruction* index,
5114 Primitive::Type type,
5115 uint32_t dex_pc,
5116 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005117 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005118 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005119 SetRawInputAt(0, array);
5120 SetRawInputAt(1, index);
5121 }
5122
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005123 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005124 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005125 return true;
5126 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005127 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005128 // TODO: We can be smarter here.
5129 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5130 // which generates the implicit null check. There are cases when these can be removed
5131 // to produce better code. If we ever add optimizations to do so we should allow an
5132 // implicit check here (as long as the address falls in the first page).
5133 return false;
5134 }
5135
David Brazdil4833f5a2015-12-16 10:37:39 +00005136 bool IsEquivalentOf(HArrayGet* other) const {
5137 bool result = (GetDexPc() == other->GetDexPc());
5138 if (kIsDebugBuild && result) {
5139 DCHECK_EQ(GetBlock(), other->GetBlock());
5140 DCHECK_EQ(GetArray(), other->GetArray());
5141 DCHECK_EQ(GetIndex(), other->GetIndex());
5142 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005143 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005144 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005145 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5146 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005147 }
5148 }
5149 return result;
5150 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005151
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005152 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5153
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005154 HInstruction* GetArray() const { return InputAt(0); }
5155 HInstruction* GetIndex() const { return InputAt(1); }
5156
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005157 DECLARE_INSTRUCTION(ArrayGet);
5158
5159 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005160 // We treat a String as an array, creating the HArrayGet from String.charAt()
5161 // intrinsic in the instruction simplifier. We can always determine whether
5162 // a particular HArrayGet is actually a String.charAt() by looking at the type
5163 // of the input but that requires holding the mutator lock, so we prefer to use
5164 // a flag, so that code generators don't need to do the locking.
5165 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5166 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5167 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5168 "Too many packed fields.");
5169
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005170 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5171};
5172
Vladimir Markofcb503c2016-05-18 12:48:17 +01005173class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005174 public:
5175 HArraySet(HInstruction* array,
5176 HInstruction* index,
5177 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005178 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005179 uint32_t dex_pc)
5180 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005181 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5182 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5183 SetPackedFlag<kFlagValueCanBeNull>(true);
5184 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185 SetRawInputAt(0, array);
5186 SetRawInputAt(1, index);
5187 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005188 // Make a best guess now, may be refined during SSA building.
5189 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005190 }
5191
Calin Juravle77520bc2015-01-12 18:45:46 +00005192 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005193 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005194 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 }
5196
Mingyao Yang81014cb2015-06-02 03:16:27 -07005197 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005198 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005199
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005200 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005201 // TODO: Same as for ArrayGet.
5202 return false;
5203 }
5204
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005205 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005206 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005207 }
5208
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005209 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005210 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005211 }
5212
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005213 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005214 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005215 }
5216
Vladimir Markoa1de9182016-02-25 11:37:38 +00005217 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5218 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5219 bool StaticTypeOfArrayIsObjectArray() const {
5220 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5221 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005222
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005223 HInstruction* GetArray() const { return InputAt(0); }
5224 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005225 HInstruction* GetValue() const { return InputAt(2); }
5226
5227 Primitive::Type GetComponentType() const {
5228 // The Dex format does not type floating point index operations. Since the
5229 // `expected_component_type_` is set during building and can therefore not
5230 // be correct, we also check what is the value type. If it is a floating
5231 // point type, we must use that type.
5232 Primitive::Type value_type = GetValue()->GetType();
5233 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5234 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005235 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005236 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005237
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005238 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005239 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005240 }
5241
Aart Bik18b36ab2016-04-13 16:41:35 -07005242 void ComputeSideEffects() {
5243 Primitive::Type type = GetComponentType();
5244 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5245 SideEffectsForArchRuntimeCalls(type)));
5246 }
5247
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005248 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5249 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5250 }
5251
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 DECLARE_INSTRUCTION(ArraySet);
5253
5254 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005255 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5256 static constexpr size_t kFieldExpectedComponentTypeSize =
5257 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5258 static constexpr size_t kFlagNeedsTypeCheck =
5259 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5260 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005261 // Cached information for the reference_type_info_ so that codegen
5262 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005263 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5264 static constexpr size_t kNumberOfArraySetPackedBits =
5265 kFlagStaticTypeOfArrayIsObjectArray + 1;
5266 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5267 using ExpectedComponentTypeField =
5268 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269
5270 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5271};
5272
Vladimir Markofcb503c2016-05-18 12:48:17 +01005273class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005275 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005276 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005277 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005278 // Note that arrays do not change length, so the instruction does not
5279 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280 SetRawInputAt(0, array);
5281 }
5282
Calin Juravle77520bc2015-01-12 18:45:46 +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 }
Calin Juravle641547a2015-04-21 22:08:51 +01005287 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5288 return obj == InputAt(0);
5289 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005290
Vladimir Markodce016e2016-04-28 13:10:02 +01005291 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5292
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 DECLARE_INSTRUCTION(ArrayLength);
5294
5295 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005296 // We treat a String as an array, creating the HArrayLength from String.length()
5297 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5298 // determine whether a particular HArrayLength is actually a String.length() by
5299 // looking at the type of the input but that requires holding the mutator lock, so
5300 // we prefer to use a flag, so that code generators don't need to do the locking.
5301 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5302 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5303 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5304 "Too many packed fields.");
5305
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5307};
5308
Vladimir Markofcb503c2016-05-18 12:48:17 +01005309class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005310 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005311 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5312 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005313 HBoundsCheck(HInstruction* index,
5314 HInstruction* length,
5315 uint32_t dex_pc,
5316 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5317 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5318 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005319 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 SetRawInputAt(0, index);
5321 SetRawInputAt(1, length);
5322 }
5323
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005324 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005325 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005326 return true;
5327 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005328
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005329 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005330
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005331 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005332
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005333 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5334 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5335
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005336 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005337
5338 DECLARE_INSTRUCTION(BoundsCheck);
5339
5340 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005341 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5342 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5343 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5344 // so we need to create an HEnvironment which will be translated to an InlineInfo
5345 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5346 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5347 // from the invoke as we don't want to look again at the dex bytecode.
5348 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5349
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005350 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5351};
5352
Vladimir Markofcb503c2016-05-18 12:48:17 +01005353class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005354 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005355 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005356 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005357
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005358 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005359 return true;
5360 }
5361
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005362 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5363 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005364
5365 DECLARE_INSTRUCTION(SuspendCheck);
5366
5367 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005368 // Only used for code generation, in order to share the same slow path between back edges
5369 // of a same loop.
5370 SlowPathCode* slow_path_;
5371
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005372 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5373};
5374
David Srbecky0cf44932015-12-09 14:09:59 +00005375// Pseudo-instruction which provides the native debugger with mapping information.
5376// It ensures that we can generate line number and local variables at this point.
5377class HNativeDebugInfo : public HTemplateInstruction<0> {
5378 public:
5379 explicit HNativeDebugInfo(uint32_t dex_pc)
5380 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5381
5382 bool NeedsEnvironment() const OVERRIDE {
5383 return true;
5384 }
5385
5386 DECLARE_INSTRUCTION(NativeDebugInfo);
5387
5388 private:
5389 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5390};
5391
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005392/**
5393 * Instruction to load a Class object.
5394 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005395class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005396 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005397 // Determines how to load the Class.
5398 enum class LoadKind {
5399 // Use the Class* from the method's own ArtMethod*.
5400 kReferrersClass,
5401
5402 // Use boot image Class* address that will be known at link time.
5403 // Used for boot image classes referenced by boot image code in non-PIC mode.
5404 kBootImageLinkTimeAddress,
5405
5406 // Use PC-relative boot image Class* address that will be known at link time.
5407 // Used for boot image classes referenced by boot image code in PIC mode.
5408 kBootImageLinkTimePcRelative,
5409
5410 // Use a known boot image Class* address, embedded in the code by the codegen.
5411 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5412 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5413 // GetIncludePatchInformation().
5414 kBootImageAddress,
5415
5416 // Load from the resolved types array at an absolute address.
5417 // Used for classes outside the boot image referenced by JIT-compiled code.
5418 kDexCacheAddress,
5419
5420 // Load from resolved types array in the dex cache using a PC-relative load.
5421 // Used for classes outside boot image when we know that we can access
5422 // the dex cache arrays using a PC-relative load.
5423 kDexCachePcRelative,
5424
5425 // Load from resolved types array accessed through the class loaded from
5426 // the compiled method's own ArtMethod*. This is the default access type when
5427 // all other types are unavailable.
5428 kDexCacheViaMethod,
5429
5430 kLast = kDexCacheViaMethod
5431 };
5432
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005433 HLoadClass(HCurrentMethod* current_method,
5434 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005435 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005436 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005437 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005438 bool needs_access_check,
5439 bool is_in_dex_cache)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005440 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5441 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005442 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005443 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005444 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005445 // Referrers class should not need access check. We never inline unverified
5446 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005447 DCHECK(!is_referrers_class || !needs_access_check);
5448
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005449 SetPackedField<LoadKindField>(
5450 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005451 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5452 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5453 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005454 }
5455
5456 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5457 DCHECK(HasAddress(load_kind));
5458 load_data_.address = address;
5459 SetLoadKindInternal(load_kind);
5460 }
5461
5462 void SetLoadKindWithTypeReference(LoadKind load_kind,
5463 const DexFile& dex_file,
5464 uint32_t type_index) {
5465 DCHECK(HasTypeReference(load_kind));
5466 DCHECK(IsSameDexFile(dex_file_, dex_file));
5467 DCHECK_EQ(type_index_, type_index);
5468 SetLoadKindInternal(load_kind);
5469 }
5470
5471 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5472 const DexFile& dex_file,
5473 uint32_t element_index) {
5474 DCHECK(HasDexCacheReference(load_kind));
5475 DCHECK(IsSameDexFile(dex_file_, dex_file));
5476 load_data_.dex_cache_element_index = element_index;
5477 SetLoadKindInternal(load_kind);
5478 }
5479
5480 LoadKind GetLoadKind() const {
5481 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005482 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005483
5484 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005485
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005486 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005487
5488 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5489
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005490 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005491
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005492 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005493 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005494 }
5495
Calin Juravle0ba218d2015-05-19 18:46:01 +01005496 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005497 // The entrypoint the code generator is going to call does not do
5498 // clinit of the class.
5499 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005500 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005501 }
5502
5503 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005504 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005505 (!IsReferrersClass() && !IsInDexCache()) ||
5506 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005507 }
5508
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005509
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005510 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005511 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005512 }
5513
Calin Juravleacf735c2015-02-12 15:25:22 +00005514 ReferenceTypeInfo GetLoadedClassRTI() {
5515 return loaded_class_rti_;
5516 }
5517
5518 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5519 // Make sure we only set exact types (the loaded class should never be merged).
5520 DCHECK(rti.IsExact());
5521 loaded_class_rti_ = rti;
5522 }
5523
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005524 uint32_t GetTypeIndex() const { return type_index_; }
5525 const DexFile& GetDexFile() const { return dex_file_; }
5526
5527 uint32_t GetDexCacheElementOffset() const;
5528
5529 uint64_t GetAddress() const {
5530 DCHECK(HasAddress(GetLoadKind()));
5531 return load_data_.address;
5532 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005533
Vladimir Markoa1de9182016-02-25 11:37:38 +00005534 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005535
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005536 static SideEffects SideEffectsForArchRuntimeCalls() {
5537 return SideEffects::CanTriggerGC();
5538 }
5539
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005540 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005541 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5542 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5543 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005544
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005545 void MarkInDexCache() {
5546 SetPackedFlag<kFlagIsInDexCache>(true);
5547 DCHECK(!NeedsEnvironment());
5548 RemoveEnvironment();
5549 SetSideEffects(SideEffects::None());
5550 }
5551
5552 void AddSpecialInput(HInstruction* special_input);
5553
5554 using HInstruction::GetInputRecords; // Keep the const version visible.
5555 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5556 return ArrayRef<HUserRecord<HInstruction*>>(
5557 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5558 }
5559
5560 Primitive::Type GetType() const OVERRIDE {
5561 return Primitive::kPrimNot;
5562 }
5563
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005564 DECLARE_INSTRUCTION(LoadClass);
5565
5566 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005567 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005568 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005569 // Whether this instruction must generate the initialization check.
5570 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005571 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005572 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5573 static constexpr size_t kFieldLoadKindSize =
5574 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5575 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005576 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005577 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5578
5579 static bool HasTypeReference(LoadKind load_kind) {
5580 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5581 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5582 load_kind == LoadKind::kDexCacheViaMethod ||
5583 load_kind == LoadKind::kReferrersClass;
5584 }
5585
5586 static bool HasAddress(LoadKind load_kind) {
5587 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5588 }
5589
5590 static bool HasDexCacheReference(LoadKind load_kind) {
5591 return load_kind == LoadKind::kDexCachePcRelative;
5592 }
5593
5594 void SetLoadKindInternal(LoadKind load_kind);
5595
5596 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5597 // For other load kinds it's empty or possibly some architecture-specific instruction
5598 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5599 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005600
5601 const uint16_t type_index_;
5602 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005603
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005604 union {
5605 uint32_t dex_cache_element_index; // Only for dex cache reference.
5606 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5607 } load_data_;
5608
Calin Juravleacf735c2015-02-12 15:25:22 +00005609 ReferenceTypeInfo loaded_class_rti_;
5610
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005611 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5612};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005613std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5614
5615// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5616inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5617 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5618 return load_data_.dex_cache_element_index;
5619}
5620
5621// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5622inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
5623 // The special input is used for PC-relative loads on some architectures.
5624 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5625 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5626 DCHECK(special_input_.GetInstruction() == nullptr);
5627 special_input_ = HUserRecord<HInstruction*>(special_input);
5628 special_input->AddUseAt(this, 0);
5629}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005630
Vladimir Marko372f10e2016-05-17 16:30:10 +01005631class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005632 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005633 // Determines how to load the String.
5634 enum class LoadKind {
5635 // Use boot image String* address that will be known at link time.
5636 // Used for boot image strings referenced by boot image code in non-PIC mode.
5637 kBootImageLinkTimeAddress,
5638
5639 // Use PC-relative boot image String* address that will be known at link time.
5640 // Used for boot image strings referenced by boot image code in PIC mode.
5641 kBootImageLinkTimePcRelative,
5642
5643 // Use a known boot image String* address, embedded in the code by the codegen.
5644 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5645 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5646 // GetIncludePatchInformation().
5647 kBootImageAddress,
5648
5649 // Load from the resolved strings array at an absolute address.
5650 // Used for strings outside the boot image referenced by JIT-compiled code.
5651 kDexCacheAddress,
5652
5653 // Load from resolved strings array in the dex cache using a PC-relative load.
5654 // Used for strings outside boot image when we know that we can access
5655 // the dex cache arrays using a PC-relative load.
5656 kDexCachePcRelative,
5657
5658 // Load from resolved strings array accessed through the class loaded from
5659 // the compiled method's own ArtMethod*. This is the default access type when
5660 // all other types are unavailable.
5661 kDexCacheViaMethod,
5662
5663 kLast = kDexCacheViaMethod
5664 };
5665
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005666 HLoadString(HCurrentMethod* current_method,
5667 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005668 const DexFile& dex_file,
5669 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005670 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5671 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005672 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005673 SetPackedFlag<kFlagIsInDexCache>(false);
5674 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5675 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005676 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005677
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005678 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5679 DCHECK(HasAddress(load_kind));
5680 load_data_.address = address;
5681 SetLoadKindInternal(load_kind);
5682 }
5683
5684 void SetLoadKindWithStringReference(LoadKind load_kind,
5685 const DexFile& dex_file,
5686 uint32_t string_index) {
5687 DCHECK(HasStringReference(load_kind));
5688 load_data_.ref.dex_file = &dex_file;
5689 string_index_ = string_index;
5690 SetLoadKindInternal(load_kind);
5691 }
5692
5693 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5694 const DexFile& dex_file,
5695 uint32_t element_index) {
5696 DCHECK(HasDexCacheReference(load_kind));
5697 load_data_.ref.dex_file = &dex_file;
5698 load_data_.ref.dex_cache_element_index = element_index;
5699 SetLoadKindInternal(load_kind);
5700 }
5701
5702 LoadKind GetLoadKind() const {
5703 return GetPackedField<LoadKindField>();
5704 }
5705
5706 const DexFile& GetDexFile() const;
5707
5708 uint32_t GetStringIndex() const {
5709 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5710 return string_index_;
5711 }
5712
5713 uint32_t GetDexCacheElementOffset() const;
5714
5715 uint64_t GetAddress() const {
5716 DCHECK(HasAddress(GetLoadKind()));
5717 return load_data_.address;
5718 }
5719
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005720 bool CanBeMoved() const OVERRIDE { return true; }
5721
Vladimir Marko372f10e2016-05-17 16:30:10 +01005722 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005723
5724 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5725
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005726 // Will call the runtime if we need to load the string through
5727 // the dex cache and the string is not guaranteed to be there yet.
5728 bool NeedsEnvironment() const OVERRIDE {
5729 LoadKind load_kind = GetLoadKind();
5730 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5731 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5732 load_kind == LoadKind::kBootImageAddress) {
5733 return false;
5734 }
5735 return !IsInDexCache();
5736 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005737
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005738 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5739 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5740 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005741
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005742 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005743 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005744
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005745 static SideEffects SideEffectsForArchRuntimeCalls() {
5746 return SideEffects::CanTriggerGC();
5747 }
5748
Vladimir Markoa1de9182016-02-25 11:37:38 +00005749 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5750
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005751 void MarkInDexCache() {
5752 SetPackedFlag<kFlagIsInDexCache>(true);
5753 DCHECK(!NeedsEnvironment());
5754 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005755 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005756 }
5757
Vladimir Marko372f10e2016-05-17 16:30:10 +01005758 void AddSpecialInput(HInstruction* special_input);
5759
5760 using HInstruction::GetInputRecords; // Keep the const version visible.
5761 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5762 return ArrayRef<HUserRecord<HInstruction*>>(
5763 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005764 }
5765
Vladimir Marko372f10e2016-05-17 16:30:10 +01005766 Primitive::Type GetType() const OVERRIDE {
5767 return Primitive::kPrimNot;
5768 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005769
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005770 DECLARE_INSTRUCTION(LoadString);
5771
5772 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005773 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005774 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5775 static constexpr size_t kFieldLoadKindSize =
5776 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5777 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005778 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005779 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005780
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005781 static bool HasStringReference(LoadKind load_kind) {
5782 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5783 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5784 load_kind == LoadKind::kDexCacheViaMethod;
5785 }
5786
5787 static bool HasAddress(LoadKind load_kind) {
5788 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5789 }
5790
5791 static bool HasDexCacheReference(LoadKind load_kind) {
5792 return load_kind == LoadKind::kDexCachePcRelative;
5793 }
5794
5795 void SetLoadKindInternal(LoadKind load_kind);
5796
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005797 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5798 // For other load kinds it's empty or possibly some architecture-specific instruction
5799 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005800 HUserRecord<HInstruction*> special_input_;
5801
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005802 // String index serves also as the hash code and it's also needed for slow-paths,
5803 // so it must not be overwritten with other load data.
5804 uint32_t string_index_;
5805
5806 union {
5807 struct {
5808 const DexFile* dex_file; // For string reference and dex cache reference.
5809 uint32_t dex_cache_element_index; // Only for dex cache reference.
5810 } ref;
5811 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5812 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005813
5814 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5815};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005816std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5817
5818// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5819inline const DexFile& HLoadString::GetDexFile() const {
5820 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5821 << GetLoadKind();
5822 return *load_data_.ref.dex_file;
5823}
5824
5825// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5826inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5827 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5828 return load_data_.ref.dex_cache_element_index;
5829}
5830
5831// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5832inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5833 // The special input is used for PC-relative loads on some architectures.
5834 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5835 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005836 // HLoadString::GetInputRecords() returns an empty array at this point,
5837 // so use the GetInputRecords() from the base class to set the input record.
5838 DCHECK(special_input_.GetInstruction() == nullptr);
5839 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005840 special_input->AddUseAt(this, 0);
5841}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005842
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005843/**
5844 * Performs an initialization check on its Class object input.
5845 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005846class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005847 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005848 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005849 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005850 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005851 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5852 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005853 SetRawInputAt(0, constant);
5854 }
5855
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005856 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005857 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005858 return true;
5859 }
5860
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005861 bool NeedsEnvironment() const OVERRIDE {
5862 // May call runtime to initialize the class.
5863 return true;
5864 }
5865
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005866 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005867
5868 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5869
5870 DECLARE_INSTRUCTION(ClinitCheck);
5871
5872 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005873 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5874};
5875
Vladimir Markofcb503c2016-05-18 12:48:17 +01005876class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005877 public:
5878 HStaticFieldGet(HInstruction* cls,
5879 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005880 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005881 bool is_volatile,
5882 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005883 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005884 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005885 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005886 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005887 : HExpression(field_type,
5888 SideEffects::FieldReadOfType(field_type, is_volatile),
5889 dex_pc),
5890 field_info_(field_offset,
5891 field_type,
5892 is_volatile,
5893 field_idx,
5894 declaring_class_def_index,
5895 dex_file,
5896 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005897 SetRawInputAt(0, cls);
5898 }
5899
Calin Juravle52c48962014-12-16 17:02:57 +00005900
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005901 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005902
Vladimir Marko372f10e2016-05-17 16:30:10 +01005903 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5904 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005905 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005906 }
5907
5908 size_t ComputeHashCode() const OVERRIDE {
5909 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5910 }
5911
Calin Juravle52c48962014-12-16 17:02:57 +00005912 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005913 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5914 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005915 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005916
5917 DECLARE_INSTRUCTION(StaticFieldGet);
5918
5919 private:
5920 const FieldInfo field_info_;
5921
5922 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5923};
5924
Vladimir Markofcb503c2016-05-18 12:48:17 +01005925class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005926 public:
5927 HStaticFieldSet(HInstruction* cls,
5928 HInstruction* value,
5929 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005930 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005931 bool is_volatile,
5932 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005933 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005934 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005935 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005936 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005937 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5938 dex_pc),
5939 field_info_(field_offset,
5940 field_type,
5941 is_volatile,
5942 field_idx,
5943 declaring_class_def_index,
5944 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005945 dex_cache) {
5946 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005947 SetRawInputAt(0, cls);
5948 SetRawInputAt(1, value);
5949 }
5950
Calin Juravle52c48962014-12-16 17:02:57 +00005951 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005952 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5953 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005954 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005955
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005956 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005957 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5958 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005959
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005960 DECLARE_INSTRUCTION(StaticFieldSet);
5961
5962 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005963 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5964 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5965 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5966 "Too many packed fields.");
5967
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005968 const FieldInfo field_info_;
5969
5970 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5971};
5972
Vladimir Markofcb503c2016-05-18 12:48:17 +01005973class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005974 public:
5975 HUnresolvedInstanceFieldGet(HInstruction* obj,
5976 Primitive::Type field_type,
5977 uint32_t field_index,
5978 uint32_t dex_pc)
5979 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5980 field_index_(field_index) {
5981 SetRawInputAt(0, obj);
5982 }
5983
5984 bool NeedsEnvironment() const OVERRIDE { return true; }
5985 bool CanThrow() const OVERRIDE { return true; }
5986
5987 Primitive::Type GetFieldType() const { return GetType(); }
5988 uint32_t GetFieldIndex() const { return field_index_; }
5989
5990 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5991
5992 private:
5993 const uint32_t field_index_;
5994
5995 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5996};
5997
Vladimir Markofcb503c2016-05-18 12:48:17 +01005998class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005999 public:
6000 HUnresolvedInstanceFieldSet(HInstruction* obj,
6001 HInstruction* value,
6002 Primitive::Type field_type,
6003 uint32_t field_index,
6004 uint32_t dex_pc)
6005 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006006 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006007 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006008 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006009 SetRawInputAt(0, obj);
6010 SetRawInputAt(1, value);
6011 }
6012
6013 bool NeedsEnvironment() const OVERRIDE { return true; }
6014 bool CanThrow() const OVERRIDE { return true; }
6015
Vladimir Markoa1de9182016-02-25 11:37:38 +00006016 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006017 uint32_t GetFieldIndex() const { return field_index_; }
6018
6019 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6020
6021 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006022 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6023 static constexpr size_t kFieldFieldTypeSize =
6024 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6025 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6026 kFieldFieldType + kFieldFieldTypeSize;
6027 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6028 "Too many packed fields.");
6029 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6030
Calin Juravlee460d1d2015-09-29 04:52:17 +01006031 const uint32_t field_index_;
6032
6033 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6034};
6035
Vladimir Markofcb503c2016-05-18 12:48:17 +01006036class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006037 public:
6038 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6039 uint32_t field_index,
6040 uint32_t dex_pc)
6041 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6042 field_index_(field_index) {
6043 }
6044
6045 bool NeedsEnvironment() const OVERRIDE { return true; }
6046 bool CanThrow() const OVERRIDE { return true; }
6047
6048 Primitive::Type GetFieldType() const { return GetType(); }
6049 uint32_t GetFieldIndex() const { return field_index_; }
6050
6051 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6052
6053 private:
6054 const uint32_t field_index_;
6055
6056 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6057};
6058
Vladimir Markofcb503c2016-05-18 12:48:17 +01006059class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006060 public:
6061 HUnresolvedStaticFieldSet(HInstruction* value,
6062 Primitive::Type field_type,
6063 uint32_t field_index,
6064 uint32_t dex_pc)
6065 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006066 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006067 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006068 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006069 SetRawInputAt(0, value);
6070 }
6071
6072 bool NeedsEnvironment() const OVERRIDE { return true; }
6073 bool CanThrow() const OVERRIDE { return true; }
6074
Vladimir Markoa1de9182016-02-25 11:37:38 +00006075 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006076 uint32_t GetFieldIndex() const { return field_index_; }
6077
6078 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6079
6080 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006081 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6082 static constexpr size_t kFieldFieldTypeSize =
6083 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6084 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6085 kFieldFieldType + kFieldFieldTypeSize;
6086 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6087 "Too many packed fields.");
6088 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6089
Calin Juravlee460d1d2015-09-29 04:52:17 +01006090 const uint32_t field_index_;
6091
6092 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6093};
6094
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006095// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006096class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006097 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006098 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6099 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006100
David Brazdilbbd733e2015-08-18 17:48:17 +01006101 bool CanBeNull() const OVERRIDE { return false; }
6102
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006103 DECLARE_INSTRUCTION(LoadException);
6104
6105 private:
6106 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6107};
6108
David Brazdilcb1c0552015-08-04 16:22:25 +01006109// Implicit part of move-exception which clears thread-local exception storage.
6110// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006111class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006112 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006113 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6114 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006115
6116 DECLARE_INSTRUCTION(ClearException);
6117
6118 private:
6119 DISALLOW_COPY_AND_ASSIGN(HClearException);
6120};
6121
Vladimir Markofcb503c2016-05-18 12:48:17 +01006122class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006123 public:
6124 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006125 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006126 SetRawInputAt(0, exception);
6127 }
6128
6129 bool IsControlFlow() const OVERRIDE { return true; }
6130
6131 bool NeedsEnvironment() const OVERRIDE { return true; }
6132
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006133 bool CanThrow() const OVERRIDE { return true; }
6134
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006135
6136 DECLARE_INSTRUCTION(Throw);
6137
6138 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006139 DISALLOW_COPY_AND_ASSIGN(HThrow);
6140};
6141
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142/**
6143 * Implementation strategies for the code generator of a HInstanceOf
6144 * or `HCheckCast`.
6145 */
6146enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006147 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148 kExactCheck, // Can do a single class compare.
6149 kClassHierarchyCheck, // Can just walk the super class chain.
6150 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6151 kInterfaceCheck, // No optimization yet when checking against an interface.
6152 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006153 kArrayCheck, // No optimization yet when checking against a generic array.
6154 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006155};
6156
Roland Levillain86503782016-02-11 19:07:30 +00006157std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6158
Vladimir Markofcb503c2016-05-18 12:48:17 +01006159class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006160 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006161 HInstanceOf(HInstruction* object,
6162 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006163 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006164 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006165 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006167 dex_pc) {
6168 SetPackedField<TypeCheckKindField>(check_kind);
6169 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006170 SetRawInputAt(0, object);
6171 SetRawInputAt(1, constant);
6172 }
6173
6174 bool CanBeMoved() const OVERRIDE { return true; }
6175
Vladimir Marko372f10e2016-05-17 16:30:10 +01006176 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006177 return true;
6178 }
6179
6180 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006181 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006182 }
6183
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006184 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006185 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6186 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6187 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6188 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006189
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006190 static bool CanCallRuntime(TypeCheckKind check_kind) {
6191 // Mips currently does runtime calls for any other checks.
6192 return check_kind != TypeCheckKind::kExactCheck;
6193 }
6194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006195 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006196 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006197 }
6198
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006199 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006200
6201 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006202 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6203 static constexpr size_t kFieldTypeCheckKindSize =
6204 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6205 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6206 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6207 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6208 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006209
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006210 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6211};
6212
Vladimir Markofcb503c2016-05-18 12:48:17 +01006213class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006214 public:
David Brazdilf5552582015-12-27 13:36:12 +00006215 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006216 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006217 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6218 SetPackedFlag<kFlagUpperCanBeNull>(true);
6219 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006220 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006221 SetRawInputAt(0, input);
6222 }
6223
David Brazdilf5552582015-12-27 13:36:12 +00006224 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006225 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006226 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006227 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006228
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006229 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006230 DCHECK(GetUpperCanBeNull() || !can_be_null);
6231 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006232 }
6233
Vladimir Markoa1de9182016-02-25 11:37:38 +00006234 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006235
Calin Juravleb1498f62015-02-16 13:13:29 +00006236 DECLARE_INSTRUCTION(BoundType);
6237
6238 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006239 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6240 // is false then CanBeNull() cannot be true).
6241 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6242 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6243 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6244 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6245
Calin Juravleb1498f62015-02-16 13:13:29 +00006246 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006247 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6248 // It is used to bound the type in cases like:
6249 // if (x instanceof ClassX) {
6250 // // uper_bound_ will be ClassX
6251 // }
David Brazdilf5552582015-12-27 13:36:12 +00006252 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006253
6254 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6255};
6256
Vladimir Markofcb503c2016-05-18 12:48:17 +01006257class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006258 public:
6259 HCheckCast(HInstruction* object,
6260 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006261 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006262 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006263 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6264 SetPackedField<TypeCheckKindField>(check_kind);
6265 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006266 SetRawInputAt(0, object);
6267 SetRawInputAt(1, constant);
6268 }
6269
6270 bool CanBeMoved() const OVERRIDE { return true; }
6271
Vladimir Marko372f10e2016-05-17 16:30:10 +01006272 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006273 return true;
6274 }
6275
6276 bool NeedsEnvironment() const OVERRIDE {
6277 // Instruction may throw a CheckCastError.
6278 return true;
6279 }
6280
6281 bool CanThrow() const OVERRIDE { return true; }
6282
Vladimir Markoa1de9182016-02-25 11:37:38 +00006283 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6284 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6285 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6286 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006287
6288 DECLARE_INSTRUCTION(CheckCast);
6289
6290 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006291 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6292 static constexpr size_t kFieldTypeCheckKindSize =
6293 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6294 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6295 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6296 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6297 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006298
6299 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006300};
6301
Vladimir Markofcb503c2016-05-18 12:48:17 +01006302class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006303 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006304 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006305 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006306 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6307 SetPackedField<BarrierKindField>(barrier_kind);
6308 }
Calin Juravle27df7582015-04-17 19:12:31 +01006309
Vladimir Markoa1de9182016-02-25 11:37:38 +00006310 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006311
6312 DECLARE_INSTRUCTION(MemoryBarrier);
6313
6314 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006315 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6316 static constexpr size_t kFieldBarrierKindSize =
6317 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6318 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6319 kFieldBarrierKind + kFieldBarrierKindSize;
6320 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6321 "Too many packed fields.");
6322 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006323
6324 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6325};
6326
Vladimir Markofcb503c2016-05-18 12:48:17 +01006327class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006328 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006329 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006330 kEnter,
6331 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006332 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006333 };
6334
6335 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006336 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006337 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6338 dex_pc) {
6339 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006340 SetRawInputAt(0, object);
6341 }
6342
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006343 // Instruction may go into runtime, so we need an environment.
6344 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006345
6346 bool CanThrow() const OVERRIDE {
6347 // Verifier guarantees that monitor-exit cannot throw.
6348 // This is important because it allows the HGraphBuilder to remove
6349 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6350 return IsEnter();
6351 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006352
Vladimir Markoa1de9182016-02-25 11:37:38 +00006353 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6354 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006355
6356 DECLARE_INSTRUCTION(MonitorOperation);
6357
Calin Juravle52c48962014-12-16 17:02:57 +00006358 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006359 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6360 static constexpr size_t kFieldOperationKindSize =
6361 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6362 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6363 kFieldOperationKind + kFieldOperationKindSize;
6364 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6365 "Too many packed fields.");
6366 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006367
6368 private:
6369 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6370};
6371
Vladimir Markofcb503c2016-05-18 12:48:17 +01006372class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006373 public:
6374 HSelect(HInstruction* condition,
6375 HInstruction* true_value,
6376 HInstruction* false_value,
6377 uint32_t dex_pc)
6378 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6379 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6380
6381 // First input must be `true_value` or `false_value` to allow codegens to
6382 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6383 // that architectures which implement HSelect as a conditional move also
6384 // will not need to invert the condition.
6385 SetRawInputAt(0, false_value);
6386 SetRawInputAt(1, true_value);
6387 SetRawInputAt(2, condition);
6388 }
6389
6390 HInstruction* GetFalseValue() const { return InputAt(0); }
6391 HInstruction* GetTrueValue() const { return InputAt(1); }
6392 HInstruction* GetCondition() const { return InputAt(2); }
6393
6394 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006395 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6396 return true;
6397 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006398
6399 bool CanBeNull() const OVERRIDE {
6400 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6401 }
6402
6403 DECLARE_INSTRUCTION(Select);
6404
6405 private:
6406 DISALLOW_COPY_AND_ASSIGN(HSelect);
6407};
6408
Vladimir Markof9f64412015-09-02 14:05:49 +01006409class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006410 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006411 MoveOperands(Location source,
6412 Location destination,
6413 Primitive::Type type,
6414 HInstruction* instruction)
6415 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006416
6417 Location GetSource() const { return source_; }
6418 Location GetDestination() const { return destination_; }
6419
6420 void SetSource(Location value) { source_ = value; }
6421 void SetDestination(Location value) { destination_ = value; }
6422
6423 // The parallel move resolver marks moves as "in-progress" by clearing the
6424 // destination (but not the source).
6425 Location MarkPending() {
6426 DCHECK(!IsPending());
6427 Location dest = destination_;
6428 destination_ = Location::NoLocation();
6429 return dest;
6430 }
6431
6432 void ClearPending(Location dest) {
6433 DCHECK(IsPending());
6434 destination_ = dest;
6435 }
6436
6437 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006438 DCHECK(source_.IsValid() || destination_.IsInvalid());
6439 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006440 }
6441
6442 // True if this blocks a move from the given location.
6443 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006444 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006445 }
6446
6447 // A move is redundant if it's been eliminated, if its source and
6448 // destination are the same, or if its destination is unneeded.
6449 bool IsRedundant() const {
6450 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6451 }
6452
6453 // We clear both operands to indicate move that's been eliminated.
6454 void Eliminate() {
6455 source_ = destination_ = Location::NoLocation();
6456 }
6457
6458 bool IsEliminated() const {
6459 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6460 return source_.IsInvalid();
6461 }
6462
Alexey Frunze4dda3372015-06-01 18:31:49 -07006463 Primitive::Type GetType() const { return type_; }
6464
Nicolas Geoffray90218252015-04-15 11:56:51 +01006465 bool Is64BitMove() const {
6466 return Primitive::Is64BitType(type_);
6467 }
6468
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006469 HInstruction* GetInstruction() const { return instruction_; }
6470
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006471 private:
6472 Location source_;
6473 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006474 // The type this move is for.
6475 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006476 // The instruction this move is assocatied with. Null when this move is
6477 // for moving an input in the expected locations of user (including a phi user).
6478 // This is only used in debug mode, to ensure we do not connect interval siblings
6479 // in the same parallel move.
6480 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006481};
6482
Roland Levillainc9285912015-12-18 10:38:42 +00006483std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6484
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006485static constexpr size_t kDefaultNumberOfMoves = 4;
6486
Vladimir Markofcb503c2016-05-18 12:48:17 +01006487class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006488 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006489 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006490 : HTemplateInstruction(SideEffects::None(), dex_pc),
6491 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6492 moves_.reserve(kDefaultNumberOfMoves);
6493 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006494
Nicolas Geoffray90218252015-04-15 11:56:51 +01006495 void AddMove(Location source,
6496 Location destination,
6497 Primitive::Type type,
6498 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006499 DCHECK(source.IsValid());
6500 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006501 if (kIsDebugBuild) {
6502 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006503 for (const MoveOperands& move : moves_) {
6504 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006505 // Special case the situation where the move is for the spill slot
6506 // of the instruction.
6507 if ((GetPrevious() == instruction)
6508 || ((GetPrevious() == nullptr)
6509 && instruction->IsPhi()
6510 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006511 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006512 << "Doing parallel moves for the same instruction.";
6513 } else {
6514 DCHECK(false) << "Doing parallel moves for the same instruction.";
6515 }
6516 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006517 }
6518 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006519 for (const MoveOperands& move : moves_) {
6520 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006521 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006522 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006523 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006524 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006525 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006526 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006527 }
6528
Vladimir Marko225b6462015-09-28 12:17:40 +01006529 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006530 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006531 }
6532
Vladimir Marko225b6462015-09-28 12:17:40 +01006533 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006534
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006535 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006536
6537 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006538 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006539
6540 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6541};
6542
Mark Mendell0616ae02015-04-17 12:49:27 -04006543} // namespace art
6544
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006545#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6546#include "nodes_shared.h"
6547#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006548#ifdef ART_ENABLE_CODEGEN_arm
6549#include "nodes_arm.h"
6550#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006551#ifdef ART_ENABLE_CODEGEN_arm64
6552#include "nodes_arm64.h"
6553#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006554#ifdef ART_ENABLE_CODEGEN_x86
6555#include "nodes_x86.h"
6556#endif
6557
6558namespace art {
6559
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006560class HGraphVisitor : public ValueObject {
6561 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006562 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6563 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006564
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006565 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006566 virtual void VisitBasicBlock(HBasicBlock* block);
6567
Roland Levillain633021e2014-10-01 14:12:25 +01006568 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006569 void VisitInsertionOrder();
6570
Roland Levillain633021e2014-10-01 14:12:25 +01006571 // Visit the graph following dominator tree reverse post-order.
6572 void VisitReversePostOrder();
6573
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006574 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006575
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006576 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006577#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006578 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6579
6580 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6581
6582#undef DECLARE_VISIT_INSTRUCTION
6583
6584 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006585 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006586
6587 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6588};
6589
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006590class HGraphDelegateVisitor : public HGraphVisitor {
6591 public:
6592 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6593 virtual ~HGraphDelegateVisitor() {}
6594
6595 // Visit functions that delegate to to super class.
6596#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006597 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006598
6599 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6600
6601#undef DECLARE_VISIT_INSTRUCTION
6602
6603 private:
6604 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6605};
6606
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006607class HInsertionOrderIterator : public ValueObject {
6608 public:
6609 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6610
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006611 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006612 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006613 void Advance() { ++index_; }
6614
6615 private:
6616 const HGraph& graph_;
6617 size_t index_;
6618
6619 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6620};
6621
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006622class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006623 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006624 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6625 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006626 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006627 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006628
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006629 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6630 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006631 void Advance() { ++index_; }
6632
6633 private:
6634 const HGraph& graph_;
6635 size_t index_;
6636
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006637 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006638};
6639
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006640class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006641 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006642 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006643 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006644 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006645 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006646 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006647
6648 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006649 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006650 void Advance() { --index_; }
6651
6652 private:
6653 const HGraph& graph_;
6654 size_t index_;
6655
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006656 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006657};
6658
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006659class HLinearPostOrderIterator : public ValueObject {
6660 public:
6661 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006662 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006663
6664 bool Done() const { return index_ == 0; }
6665
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006666 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006667
6668 void Advance() {
6669 --index_;
6670 DCHECK_GE(index_, 0U);
6671 }
6672
6673 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006674 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006675 size_t index_;
6676
6677 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6678};
6679
6680class HLinearOrderIterator : public ValueObject {
6681 public:
6682 explicit HLinearOrderIterator(const HGraph& graph)
6683 : order_(graph.GetLinearOrder()), index_(0) {}
6684
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006685 bool Done() const { return index_ == order_.size(); }
6686 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006687 void Advance() { ++index_; }
6688
6689 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006690 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006691 size_t index_;
6692
6693 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6694};
6695
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006696// Iterator over the blocks that art part of the loop. Includes blocks part
6697// of an inner loop. The order in which the blocks are iterated is on their
6698// block id.
6699class HBlocksInLoopIterator : public ValueObject {
6700 public:
6701 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6702 : blocks_in_loop_(info.GetBlocks()),
6703 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6704 index_(0) {
6705 if (!blocks_in_loop_.IsBitSet(index_)) {
6706 Advance();
6707 }
6708 }
6709
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006710 bool Done() const { return index_ == blocks_.size(); }
6711 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006712 void Advance() {
6713 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006714 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006715 if (blocks_in_loop_.IsBitSet(index_)) {
6716 break;
6717 }
6718 }
6719 }
6720
6721 private:
6722 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006723 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006724 size_t index_;
6725
6726 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6727};
6728
Mingyao Yang3584bce2015-05-19 16:01:59 -07006729// Iterator over the blocks that art part of the loop. Includes blocks part
6730// of an inner loop. The order in which the blocks are iterated is reverse
6731// post order.
6732class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6733 public:
6734 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6735 : blocks_in_loop_(info.GetBlocks()),
6736 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6737 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006738 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006739 Advance();
6740 }
6741 }
6742
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006743 bool Done() const { return index_ == blocks_.size(); }
6744 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006745 void Advance() {
6746 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006747 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6748 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006749 break;
6750 }
6751 }
6752 }
6753
6754 private:
6755 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006756 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006757 size_t index_;
6758
6759 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6760};
6761
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006762inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006763 if (constant->IsIntConstant()) {
6764 return constant->AsIntConstant()->GetValue();
6765 } else if (constant->IsLongConstant()) {
6766 return constant->AsLongConstant()->GetValue();
6767 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006768 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006769 return 0;
6770 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006771}
6772
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006773#define INSTRUCTION_TYPE_CHECK(type, super) \
6774 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6775 inline const H##type* HInstruction::As##type() const { \
6776 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6777 } \
6778 inline H##type* HInstruction::As##type() { \
6779 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6780 }
6781
6782 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6783#undef INSTRUCTION_TYPE_CHECK
6784
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006785// Create space in `blocks` for adding `number_of_new_blocks` entries
6786// starting at location `at`. Blocks after `at` are moved accordingly.
6787inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6788 size_t number_of_new_blocks,
6789 size_t after) {
6790 DCHECK_LT(after, blocks->size());
6791 size_t old_size = blocks->size();
6792 size_t new_size = old_size + number_of_new_blocks;
6793 blocks->resize(new_size);
6794 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6795}
6796
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006797} // namespace art
6798
6799#endif // ART_COMPILER_OPTIMIZING_NODES_H_