blob: 38472cf8e8993f9ff89fb0eba42e851240b7c0d5 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010039#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
41namespace art {
42
David Brazdil1abb4192015-02-17 18:33:36 +000043class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Roland Levillain5b5b9312016-03-22 14:57:31 +000075// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
76static constexpr int32_t kMaxIntShiftDistance = 0x1f;
77// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
78static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000079
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070081static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010082
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010083static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
84
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060085static constexpr uint32_t kNoDexPc = -1;
86
Dave Allison20dfc792014-06-16 20:44:29 -070087enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070088 // All types.
89 kCondEQ, // ==
90 kCondNE, // !=
91 // Signed integers and floating-point numbers.
92 kCondLT, // <
93 kCondLE, // <=
94 kCondGT, // >
95 kCondGE, // >=
96 // Unsigned integers.
97 kCondB, // <
98 kCondBE, // <=
99 kCondA, // >
100 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700101};
102
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000103enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000104 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000105 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000106 kAnalysisFailThrowCatchLoop,
107 kAnalysisFailAmbiguousArrayOp,
108 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000109};
110
Vladimir Markof9f64412015-09-02 14:05:49 +0100111class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112 public:
113 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
114
115 void AddInstruction(HInstruction* instruction);
116 void RemoveInstruction(HInstruction* instruction);
117
David Brazdilc3d743f2015-04-22 13:40:50 +0100118 // Insert `instruction` before/after an existing instruction `cursor`.
119 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
120 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
121
Roland Levillain6b469232014-09-25 10:10:38 +0100122 // Return true if this list contains `instruction`.
123 bool Contains(HInstruction* instruction) const;
124
Roland Levillainccc07a92014-09-16 14:48:16 +0100125 // Return true if `instruction1` is found before `instruction2` in
126 // this instruction list and false otherwise. Abort if none
127 // of these instructions is found.
128 bool FoundBefore(const HInstruction* instruction1,
129 const HInstruction* instruction2) const;
130
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000131 bool IsEmpty() const { return first_instruction_ == nullptr; }
132 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
133
134 // Update the block of all instructions to be `block`.
135 void SetBlockOfInstructions(HBasicBlock* block) const;
136
137 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000138 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000139 void Add(const HInstructionList& instruction_list);
140
David Brazdil2d7352b2015-04-20 14:52:42 +0100141 // Return the number of instructions in the list. This is an expensive operation.
142 size_t CountSize() const;
143
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100144 private:
145 HInstruction* first_instruction_;
146 HInstruction* last_instruction_;
147
148 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000149 friend class HGraph;
150 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100151 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100152 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100153
154 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
155};
156
David Brazdil4833f5a2015-12-16 10:37:39 +0000157class ReferenceTypeInfo : ValueObject {
158 public:
159 typedef Handle<mirror::Class> TypeHandle;
160
Vladimir Markoa1de9182016-02-25 11:37:38 +0000161 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
162
163 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000164 return ReferenceTypeInfo(type_handle, is_exact);
165 }
166
167 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
168
Vladimir Markof39745e2016-01-26 12:16:55 +0000169 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000170 return handle.GetReference() != nullptr;
171 }
172
Vladimir Marko456307a2016-04-19 14:12:13 +0000173 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000174 return IsValidHandle(type_handle_);
175 }
176
177 bool IsExact() const { return is_exact_; }
178
179 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
180 DCHECK(IsValid());
181 return GetTypeHandle()->IsObjectClass();
182 }
183
184 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
185 DCHECK(IsValid());
186 return GetTypeHandle()->IsStringClass();
187 }
188
189 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
190 DCHECK(IsValid());
191 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
192 }
193
194 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
195 DCHECK(IsValid());
196 return GetTypeHandle()->IsInterface();
197 }
198
199 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
200 DCHECK(IsValid());
201 return GetTypeHandle()->IsArrayClass();
202 }
203
204 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
205 DCHECK(IsValid());
206 return GetTypeHandle()->IsPrimitiveArray();
207 }
208
209 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
210 DCHECK(IsValid());
211 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
212 }
213
214 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 if (!IsExact()) return false;
217 if (!IsArrayClass()) return false;
218 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
219 }
220
221 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
222 DCHECK(IsValid());
223 if (!IsExact()) return false;
224 if (!IsArrayClass()) return false;
225 if (!rti.IsArrayClass()) return false;
226 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
227 rti.GetTypeHandle()->GetComponentType());
228 }
229
230 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
231
232 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
233 DCHECK(IsValid());
234 DCHECK(rti.IsValid());
235 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
236 }
237
238 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
239 DCHECK(IsValid());
240 DCHECK(rti.IsValid());
241 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
242 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
243 }
244
245 // Returns true if the type information provide the same amount of details.
246 // Note that it does not mean that the instructions have the same actual type
247 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000248 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 if (!IsValid() && !rti.IsValid()) {
250 // Invalid types are equal.
251 return true;
252 }
253 if (!IsValid() || !rti.IsValid()) {
254 // One is valid, the other not.
255 return false;
256 }
257 return IsExact() == rti.IsExact()
258 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
259 }
260
261 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000262 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
263 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
264 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000265
266 // The class of the object.
267 TypeHandle type_handle_;
268 // Whether or not the type is exact or a superclass of the actual type.
269 // Whether or not we have any information about this type.
270 bool is_exact_;
271};
272
273std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
274
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000275// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100276class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100278 HGraph(ArenaAllocator* arena,
279 const DexFile& dex_file,
280 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100281 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100283 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100284 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000285 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100286 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000287 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100288 blocks_(arena->Adapter(kArenaAllocBlockList)),
289 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
290 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700291 entry_block_(nullptr),
292 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100293 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100294 number_of_vregs_(0),
295 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000296 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400297 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000298 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000299 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000300 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000301 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100302 dex_file_(dex_file),
303 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100304 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100305 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100306 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000308 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100309 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
310 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
311 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
312 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000313 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000314 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
315 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100316 blocks_.reserve(kDefaultNumberOfBlocks);
317 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000318
David Brazdilbadd8262016-02-02 16:28:56 +0000319 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
320 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
321
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000322 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100323 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
324
David Brazdil69ba7b72015-06-23 18:27:30 +0100325 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000326 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 HBasicBlock* GetEntryBlock() const { return entry_block_; }
329 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100330 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000331
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000332 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
333 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000334
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100336
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100337 void ComputeDominanceInformation();
338 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000339 void ClearLoopInformation();
340 void FindBackEdges(ArenaBitVector* visited);
341 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100342 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100343 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000344
David Brazdil4833f5a2015-12-16 10:37:39 +0000345 // Analyze all natural loops in this graph. Returns a code specifying that it
346 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000348 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349
David Brazdilffee3d32015-07-06 11:48:53 +0100350 // Iterate over blocks to compute try block membership. Needs reverse post
351 // order and loop information.
352 void ComputeTryBlockInformation();
353
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000354 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000355 // Returns the instruction to replace the invoke expression or null if the
356 // invoke is for a void method. Note that the caller is responsible for replacing
357 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000358 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000359
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000360 // Update the loop and try membership of `block`, which was spawned from `reference`.
361 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
362 // should be the new back edge.
363 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
364 HBasicBlock* reference,
365 bool replace_if_back_edge);
366
Mingyao Yang3584bce2015-05-19 16:01:59 -0700367 // Need to add a couple of blocks to test if the loop body is entered and
368 // put deoptimization instructions, etc.
369 void TransformLoopHeaderForBCE(HBasicBlock* header);
370
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000371 // Removes `block` from the graph. Assumes `block` has been disconnected from
372 // other blocks and has no instructions or phis.
373 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000374
David Brazdilfc6a86a2015-06-26 10:33:45 +0000375 // Splits the edge between `block` and `successor` while preserving the
376 // indices in the predecessor/successor lists. If there are multiple edges
377 // between the blocks, the lowest indices are used.
378 // Returns the new block which is empty and has the same dex pc as `successor`.
379 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
380
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100381 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
382 void SimplifyLoop(HBasicBlock* header);
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 int32_t GetNextInstructionId() {
385 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000386 return current_instruction_id_++;
387 }
388
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000389 int32_t GetCurrentInstructionId() const {
390 return current_instruction_id_;
391 }
392
393 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000394 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000395 current_instruction_id_ = id;
396 }
397
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100398 uint16_t GetMaximumNumberOfOutVRegs() const {
399 return maximum_number_of_out_vregs_;
400 }
401
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000402 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
403 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100404 }
405
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100406 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
407 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
408 }
409
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000410 void UpdateTemporariesVRegSlots(size_t slots) {
411 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000414 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100415 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000416 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100417 }
418
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100419 void SetNumberOfVRegs(uint16_t number_of_vregs) {
420 number_of_vregs_ = number_of_vregs;
421 }
422
423 uint16_t GetNumberOfVRegs() const {
424 return number_of_vregs_;
425 }
426
427 void SetNumberOfInVRegs(uint16_t value) {
428 number_of_in_vregs_ = value;
429 }
430
David Brazdildee58d62016-04-07 09:54:26 +0000431 uint16_t GetNumberOfInVRegs() const {
432 return number_of_in_vregs_;
433 }
434
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100435 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100437 return number_of_vregs_ - number_of_in_vregs_;
438 }
439
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100440 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100441 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100442 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100443
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100444 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100445 return linear_order_;
446 }
447
Mark Mendell1152c922015-04-24 17:06:35 -0400448 bool HasBoundsChecks() const {
449 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800450 }
451
Mark Mendell1152c922015-04-24 17:06:35 -0400452 void SetHasBoundsChecks(bool value) {
453 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800454 }
455
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100456 bool ShouldGenerateConstructorBarrier() const {
457 return should_generate_constructor_barrier_;
458 }
459
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000460 bool IsDebuggable() const { return debuggable_; }
461
David Brazdil8d5b8b22015-03-24 10:51:52 +0000462 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000463 // already, it is created and inserted into the graph. This method is only for
464 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600465 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000466
467 // TODO: This is problematic for the consistency of reference type propagation
468 // because it can be created anytime after the pass and thus it will be left
469 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600470 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000471
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600472 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
473 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000474 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600475 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
476 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600478 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
479 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000480 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600481 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
482 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000483 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000484
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100485 HCurrentMethod* GetCurrentMethod();
486
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100487 const DexFile& GetDexFile() const {
488 return dex_file_;
489 }
490
491 uint32_t GetMethodIdx() const {
492 return method_idx_;
493 }
494
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100495 InvokeType GetInvokeType() const {
496 return invoke_type_;
497 }
498
Mark Mendellc4701932015-04-10 13:18:51 -0400499 InstructionSet GetInstructionSet() const {
500 return instruction_set_;
501 }
502
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000503 bool IsCompilingOsr() const { return osr_; }
504
David Brazdil77a48ae2015-09-15 12:34:04 +0000505 bool HasTryCatch() const { return has_try_catch_; }
506 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100507
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000508 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
509 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
510
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100511 ArtMethod* GetArtMethod() const { return art_method_; }
512 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
513
Mark Mendellf6529172015-11-17 11:16:56 -0500514 // Returns an instruction with the opposite boolean value from 'cond'.
515 // The instruction has been inserted into the graph, either as a constant, or
516 // before cursor.
517 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
518
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000519 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
520
David Brazdil2d7352b2015-04-20 14:52:42 +0100521 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000522 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100523 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000524
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 template <class InstructionType, typename ValueType>
526 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600527 ArenaSafeMap<ValueType, InstructionType*>* cache,
528 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 // Try to find an existing constant of the given value.
530 InstructionType* constant = nullptr;
531 auto cached_constant = cache->find(value);
532 if (cached_constant != cache->end()) {
533 constant = cached_constant->second;
534 }
535
536 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100537 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000538 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000540 cache->Overwrite(value, constant);
541 InsertConstant(constant);
542 }
543 return constant;
544 }
545
David Brazdil8d5b8b22015-03-24 10:51:52 +0000546 void InsertConstant(HConstant* instruction);
547
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000548 // Cache a float constant into the graph. This method should only be
549 // called by the SsaBuilder when creating "equivalent" instructions.
550 void CacheFloatConstant(HFloatConstant* constant);
551
552 // See CacheFloatConstant comment.
553 void CacheDoubleConstant(HDoubleConstant* constant);
554
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000555 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000556
557 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100558 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100561 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000562
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100563 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100564 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100565
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000566 HBasicBlock* entry_block_;
567 HBasicBlock* exit_block_;
568
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100569 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100570 uint16_t maximum_number_of_out_vregs_;
571
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100572 // The number of virtual registers in this method. Contains the parameters.
573 uint16_t number_of_vregs_;
574
575 // The number of virtual registers used by parameters of this method.
576 uint16_t number_of_in_vregs_;
577
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000578 // Number of vreg size slots that the temporaries use (used in baseline compiler).
579 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100580
Mark Mendell1152c922015-04-24 17:06:35 -0400581 // Has bounds checks. We can totally skip BCE if it's false.
582 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800583
David Brazdil77a48ae2015-09-15 12:34:04 +0000584 // Flag whether there are any try/catch blocks in the graph. We will skip
585 // try/catch-related passes if false.
586 bool has_try_catch_;
587
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000588 // Flag whether there are any irreducible loops in the graph.
589 bool has_irreducible_loops_;
590
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000591 // Indicates whether the graph should be compiled in a way that
592 // ensures full debuggability. If false, we can apply more
593 // aggressive optimizations that may limit the level of debugging.
594 const bool debuggable_;
595
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000596 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000597 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000598
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100599 // The dex file from which the method is from.
600 const DexFile& dex_file_;
601
602 // The method index in the dex file.
603 const uint32_t method_idx_;
604
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100605 // If inlined, this encodes how the callee is being invoked.
606 const InvokeType invoke_type_;
607
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100608 // Whether the graph has been transformed to SSA form. Only used
609 // in debug mode to ensure we are not using properties only valid
610 // for non-SSA form (like the number of temporaries).
611 bool in_ssa_form_;
612
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100613 const bool should_generate_constructor_barrier_;
614
Mathieu Chartiere401d142015-04-22 13:56:20 -0700615 const InstructionSet instruction_set_;
616
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000618 HNullConstant* cached_null_constant_;
619 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000620 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000621 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000623
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100624 HCurrentMethod* cached_current_method_;
625
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100626 // The ArtMethod this graph is for. Note that for AOT, it may be null,
627 // for example for methods whose declaring class could not be resolved
628 // (such as when the superclass could not be found).
629 ArtMethod* art_method_;
630
David Brazdil4833f5a2015-12-16 10:37:39 +0000631 // Keep the RTI of inexact Object to avoid having to pass stack handle
632 // collection pointer to passes which may create NullConstant.
633 ReferenceTypeInfo inexact_object_rti_;
634
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000635 // Whether we are compiling this graph for on stack replacement: this will
636 // make all loops seen as irreducible and emit special stack maps to mark
637 // compiled code entries which the interpreter can directly jump to.
638 const bool osr_;
639
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100641 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000642 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000643 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644 DISALLOW_COPY_AND_ASSIGN(HGraph);
645};
646
Vladimir Markof9f64412015-09-02 14:05:49 +0100647class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648 public:
649 HLoopInformation(HBasicBlock* header, HGraph* graph)
650 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100651 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000652 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100653 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100654 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100655 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000656 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100657 back_edges_.reserve(kDefaultNumberOfBackEdges);
658 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000660 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100661 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000662
663 void Dump(std::ostream& os);
664
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100665 HBasicBlock* GetHeader() const {
666 return header_;
667 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000669 void SetHeader(HBasicBlock* block) {
670 header_ = block;
671 }
672
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100673 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
674 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
675 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
676
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100678 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000679 }
680
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100682 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100683 }
684
David Brazdil46e2a392015-03-16 17:31:52 +0000685 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100686 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100687 }
688
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000689 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100690 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000691 }
692
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100695 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100697 }
698
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100699 // Returns the lifetime position of the back edge that has the
700 // greatest lifetime position.
701 size_t GetLifetimeEnd() const;
702
703 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100704 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100705 }
706
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000707 // Finds blocks that are part of this loop.
708 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100709
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100710 // Returns whether this loop information contains `block`.
711 // Note that this loop information *must* be populated before entering this function.
712 bool Contains(const HBasicBlock& block) const;
713
714 // Returns whether this loop information is an inner loop of `other`.
715 // Note that `other` *must* be populated before entering this function.
716 bool IsIn(const HLoopInformation& other) const;
717
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800718 // Returns true if instruction is not defined within this loop.
719 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700720
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100721 const ArenaBitVector& GetBlocks() const { return blocks_; }
722
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000723 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000724 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000725
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000726 void ClearAllBlocks() {
727 blocks_.ClearAllBits();
728 }
729
David Brazdil3f4a5222016-05-06 12:46:21 +0100730 bool HasBackEdgeNotDominatedByHeader() const;
731
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100732 bool IsPopulated() const {
733 return blocks_.GetHighestBitSet() != -1;
734 }
735
Anton Shaminf89381f2016-05-16 16:44:13 +0600736 bool DominatesAllBackEdges(HBasicBlock* block);
737
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000738 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100739 // Internal recursive implementation of `Populate`.
740 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100741 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100742
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000743 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100744 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000745 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100746 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100747 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100748 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000749
750 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
751};
752
David Brazdilec16f792015-08-19 15:04:01 +0100753// Stores try/catch information for basic blocks.
754// Note that HGraph is constructed so that catch blocks cannot simultaneously
755// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100756class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100757 public:
758 // Try block information constructor.
759 explicit TryCatchInformation(const HTryBoundary& try_entry)
760 : try_entry_(&try_entry),
761 catch_dex_file_(nullptr),
762 catch_type_index_(DexFile::kDexNoIndex16) {
763 DCHECK(try_entry_ != nullptr);
764 }
765
766 // Catch block information constructor.
767 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
768 : try_entry_(nullptr),
769 catch_dex_file_(&dex_file),
770 catch_type_index_(catch_type_index) {}
771
772 bool IsTryBlock() const { return try_entry_ != nullptr; }
773
774 const HTryBoundary& GetTryEntry() const {
775 DCHECK(IsTryBlock());
776 return *try_entry_;
777 }
778
779 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
780
781 bool IsCatchAllTypeIndex() const {
782 DCHECK(IsCatchBlock());
783 return catch_type_index_ == DexFile::kDexNoIndex16;
784 }
785
786 uint16_t GetCatchTypeIndex() const {
787 DCHECK(IsCatchBlock());
788 return catch_type_index_;
789 }
790
791 const DexFile& GetCatchDexFile() const {
792 DCHECK(IsCatchBlock());
793 return *catch_dex_file_;
794 }
795
796 private:
797 // One of possibly several TryBoundary instructions entering the block's try.
798 // Only set for try blocks.
799 const HTryBoundary* try_entry_;
800
801 // Exception type information. Only set for catch blocks.
802 const DexFile* catch_dex_file_;
803 const uint16_t catch_type_index_;
804};
805
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100806static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100807static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100808
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000809// A block in a method. Contains the list of instructions represented
810// as a double linked list. Each block knows its predecessors and
811// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100812
Vladimir Markof9f64412015-09-02 14:05:49 +0100813class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600815 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000816 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000817 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
818 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000819 loop_information_(nullptr),
820 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000821 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100822 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100823 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100824 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000825 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000826 try_catch_information_(nullptr) {
827 predecessors_.reserve(kDefaultNumberOfPredecessors);
828 successors_.reserve(kDefaultNumberOfSuccessors);
829 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
830 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000831
Vladimir Marko60584552015-09-03 13:35:12 +0000832 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100833 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000834 }
835
Vladimir Marko60584552015-09-03 13:35:12 +0000836 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100837 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838 }
839
David Brazdild26a4112015-11-10 11:07:31 +0000840 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
841 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
842
Vladimir Marko60584552015-09-03 13:35:12 +0000843 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
844 return ContainsElement(successors_, block, start_from);
845 }
846
847 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100848 return dominated_blocks_;
849 }
850
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100851 bool IsEntryBlock() const {
852 return graph_->GetEntryBlock() == this;
853 }
854
855 bool IsExitBlock() const {
856 return graph_->GetExitBlock() == this;
857 }
858
David Brazdil46e2a392015-03-16 17:31:52 +0000859 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000860 bool IsSingleTryBoundary() const;
861
862 // Returns true if this block emits nothing but a jump.
863 bool IsSingleJump() const {
864 HLoopInformation* loop_info = GetLoopInformation();
865 return (IsSingleGoto() || IsSingleTryBoundary())
866 // Back edges generate a suspend check.
867 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
868 }
David Brazdil46e2a392015-03-16 17:31:52 +0000869
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000870 void AddBackEdge(HBasicBlock* back_edge) {
871 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000872 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000873 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100874 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000875 loop_information_->AddBackEdge(back_edge);
876 }
877
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000878 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000879 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000880
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100881 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000882 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600883 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000885 HBasicBlock* GetDominator() const { return dominator_; }
886 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000887 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
888
889 void RemoveDominatedBlock(HBasicBlock* block) {
890 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100891 }
Vladimir Marko60584552015-09-03 13:35:12 +0000892
893 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
894 ReplaceElement(dominated_blocks_, existing, new_block);
895 }
896
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100897 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000898
899 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100900 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000901 }
902
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100903 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
904 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100905 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100906 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100907 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
908 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000910 HInstruction* GetFirstInstructionDisregardMoves() const;
911
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000912 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000913 successors_.push_back(block);
914 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000915 }
916
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100917 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
918 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100919 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000920 new_block->predecessors_.push_back(this);
921 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000922 }
923
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000924 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
925 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000926 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000927 new_block->successors_.push_back(this);
928 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000929 }
930
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100931 // Insert `this` between `predecessor` and `successor. This method
932 // preserves the indicies, and will update the first edge found between
933 // `predecessor` and `successor`.
934 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
935 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100936 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000937 successor->predecessors_[predecessor_index] = this;
938 predecessor->successors_[successor_index] = this;
939 successors_.push_back(successor);
940 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100941 }
942
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100943 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000944 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100945 }
946
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000947 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000948 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000949 }
950
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100951 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000952 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100953 }
954
955 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000956 predecessors_.push_back(block);
957 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100958 }
959
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100960 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000961 DCHECK_EQ(predecessors_.size(), 2u);
962 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100963 }
964
David Brazdil769c9e52015-04-27 13:54:09 +0100965 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000966 DCHECK_EQ(successors_.size(), 2u);
967 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100968 }
969
David Brazdilfc6a86a2015-06-26 10:33:45 +0000970 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000971 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100972 }
973
David Brazdilfc6a86a2015-06-26 10:33:45 +0000974 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000975 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100976 }
977
David Brazdilfc6a86a2015-06-26 10:33:45 +0000978 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000979 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100980 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000981 }
982
983 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000984 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100985 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000986 }
987
988 // Returns whether the first occurrence of `predecessor` in the list of
989 // predecessors is at index `idx`.
990 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100991 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000992 return GetPredecessorIndexOf(predecessor) == idx;
993 }
994
David Brazdild7558da2015-09-22 13:04:14 +0100995 // Create a new block between this block and its predecessors. The new block
996 // is added to the graph, all predecessor edges are relinked to it and an edge
997 // is created to `this`. Returns the new empty block. Reverse post order or
998 // loop and try/catch information are not updated.
999 HBasicBlock* CreateImmediateDominator();
1000
David Brazdilfc6a86a2015-06-26 10:33:45 +00001001 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001002 // created, latter block. Note that this method will add the block to the
1003 // graph, create a Goto at the end of the former block and will create an edge
1004 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001005 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001006 HBasicBlock* SplitBefore(HInstruction* cursor);
1007
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001008 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001009 // created block. Note that this method just updates raw block information,
1010 // like predecessors, successors, dominators, and instruction list. It does not
1011 // update the graph, reverse post order, loop information, nor make sure the
1012 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001013 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1014
1015 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1016 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001017
1018 // Merge `other` at the end of `this`. Successors and dominated blocks of
1019 // `other` are changed to be successors and dominated blocks of `this`. Note
1020 // that this method does not update the graph, reverse post order, loop
1021 // information, nor make sure the blocks are consistent (for example ending
1022 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001023 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001024
1025 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1026 // of `this` are moved to `other`.
1027 // Note that this method does not update the graph, reverse post order, loop
1028 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001029 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001030 void ReplaceWith(HBasicBlock* other);
1031
David Brazdil2d7352b2015-04-20 14:52:42 +01001032 // Merge `other` at the end of `this`. This method updates loops, reverse post
1033 // order, links to predecessors, successors, dominators and deletes the block
1034 // from the graph. The two blocks must be successive, i.e. `this` the only
1035 // predecessor of `other` and vice versa.
1036 void MergeWith(HBasicBlock* other);
1037
1038 // Disconnects `this` from all its predecessors, successors and dominator,
1039 // removes it from all loops it is included in and eventually from the graph.
1040 // The block must not dominate any other block. Predecessors and successors
1041 // are safely updated.
1042 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001043
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001044 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001045 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001046 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001047 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001048 // Replace instruction `initial` with `replacement` within this block.
1049 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1050 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001051 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001052 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001053 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001054 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1055 // instruction list. With 'ensure_safety' set to true, it verifies that the
1056 // instruction is not in use and removes it from the use lists of its inputs.
1057 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1058 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001059 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001060
1061 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001062 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063 }
1064
Roland Levillain6b879dd2014-09-22 17:13:44 +01001065 bool IsLoopPreHeaderFirstPredecessor() const {
1066 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001067 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001068 }
1069
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001070 bool IsFirstPredecessorBackEdge() const {
1071 DCHECK(IsLoopHeader());
1072 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1073 }
1074
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075 HLoopInformation* GetLoopInformation() const {
1076 return loop_information_;
1077 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001078
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001079 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001080 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001081 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001082 void SetInLoop(HLoopInformation* info) {
1083 if (IsLoopHeader()) {
1084 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001085 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001086 loop_information_ = info;
1087 } else if (loop_information_->Contains(*info->GetHeader())) {
1088 // Block is currently part of an outer loop. Make it part of this inner loop.
1089 // Note that a non loop header having a loop information means this loop information
1090 // has already been populated
1091 loop_information_ = info;
1092 } else {
1093 // Block is part of an inner loop. Do not update the loop information.
1094 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1095 // at this point, because this method is being called while populating `info`.
1096 }
1097 }
1098
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001099 // Raw update of the loop information.
1100 void SetLoopInformation(HLoopInformation* info) {
1101 loop_information_ = info;
1102 }
1103
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001104 bool IsInLoop() const { return loop_information_ != nullptr; }
1105
David Brazdilec16f792015-08-19 15:04:01 +01001106 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1107
1108 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1109 try_catch_information_ = try_catch_information;
1110 }
1111
1112 bool IsTryBlock() const {
1113 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1114 }
1115
1116 bool IsCatchBlock() const {
1117 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1118 }
David Brazdilffee3d32015-07-06 11:48:53 +01001119
1120 // Returns the try entry that this block's successors should have. They will
1121 // be in the same try, unless the block ends in a try boundary. In that case,
1122 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001123 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001124
David Brazdild7558da2015-09-22 13:04:14 +01001125 bool HasThrowingInstructions() const;
1126
David Brazdila4b8c212015-05-07 09:59:30 +01001127 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001128 bool Dominates(HBasicBlock* block) const;
1129
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001130 size_t GetLifetimeStart() const { return lifetime_start_; }
1131 size_t GetLifetimeEnd() const { return lifetime_end_; }
1132
1133 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1134 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1135
David Brazdil8d5b8b22015-03-24 10:51:52 +00001136 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001137 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001138 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001139 bool HasSinglePhi() const;
1140
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001141 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001142 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001143 ArenaVector<HBasicBlock*> predecessors_;
1144 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001145 HInstructionList instructions_;
1146 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001147 HLoopInformation* loop_information_;
1148 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001149 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001150 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001151 // The dex program counter of the first instruction of this block.
1152 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001153 size_t lifetime_start_;
1154 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001155 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001156
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001157 friend class HGraph;
1158 friend class HInstruction;
1159
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001160 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1161};
1162
David Brazdilb2bd1c52015-03-25 11:17:37 +00001163// Iterates over the LoopInformation of all loops which contain 'block'
1164// from the innermost to the outermost.
1165class HLoopInformationOutwardIterator : public ValueObject {
1166 public:
1167 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1168 : current_(block.GetLoopInformation()) {}
1169
1170 bool Done() const { return current_ == nullptr; }
1171
1172 void Advance() {
1173 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001174 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001175 }
1176
1177 HLoopInformation* Current() const {
1178 DCHECK(!Done());
1179 return current_;
1180 }
1181
1182 private:
1183 HLoopInformation* current_;
1184
1185 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1186};
1187
Alexandre Ramesef20f712015-06-09 10:29:30 +01001188#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001189 M(Above, Condition) \
1190 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001191 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001192 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(ArrayGet, Instruction) \
1194 M(ArrayLength, Instruction) \
1195 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001196 M(Below, Condition) \
1197 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001198 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001200 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001201 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001202 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001203 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001204 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001205 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001206 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001207 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001208 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001209 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001211 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001212 M(Exit, Instruction) \
1213 M(FloatConstant, Constant) \
1214 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001215 M(GreaterThan, Condition) \
1216 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001217 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001218 M(InstanceFieldGet, Instruction) \
1219 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001220 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001222 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001223 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001224 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001226 M(LessThan, Condition) \
1227 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001228 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001229 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001230 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001231 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001232 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001233 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001234 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001235 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001236 M(Neg, UnaryOperation) \
1237 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001239 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001240 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001241 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001242 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001243 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001244 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001245 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001246 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001247 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001248 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 M(Return, Instruction) \
1250 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001251 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001252 M(Shl, BinaryOperation) \
1253 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001254 M(StaticFieldGet, Instruction) \
1255 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001256 M(UnresolvedInstanceFieldGet, Instruction) \
1257 M(UnresolvedInstanceFieldSet, Instruction) \
1258 M(UnresolvedStaticFieldGet, Instruction) \
1259 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001260 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001262 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001263 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001264 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001265 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001266 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001267 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001268
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001269/*
1270 * Instructions, shared across several (not all) architectures.
1271 */
1272#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1273#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1274#else
1275#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001276 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001277 M(MultiplyAccumulate, Instruction)
1278#endif
1279
Vladimir Markob4536b72015-11-24 13:45:23 +00001280#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001281#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001282#else
1283#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1284 M(ArmDexCacheArraysBase, Instruction)
1285#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001286
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001287#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001288#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001289#else
1290#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001291 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001292 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001293#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001294
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001295#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1296
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001297#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1298
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001299#ifndef ART_ENABLE_CODEGEN_x86
1300#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1301#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001302#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1303 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001304 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001305 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001306 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001307#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001308
1309#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1310
1311#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1312 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001313 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001314 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1315 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001317 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001318 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1319 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1320
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001321#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1322 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001323 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001324 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001325 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001326 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001327
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001328#define FOR_EACH_INSTRUCTION(M) \
1329 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1330 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1331
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001332#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001333FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1334#undef FORWARD_DECLARATION
1335
Roland Levillainccc07a92014-09-16 14:48:16 +01001336#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001337 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001338 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001339 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001340 return other->Is##type(); \
1341 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001342 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001343
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001344#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1345 bool Is##type() const { return As##type() != nullptr; } \
1346 const H##type* As##type() const { return this; } \
1347 H##type* As##type() { return this; }
1348
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001349template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001350class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001351 public:
David Brazdiled596192015-01-23 10:39:45 +00001352 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001354 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001355
Vladimir Marko46817b82016-03-29 12:21:58 +01001356 // Hook for the IntrusiveForwardList<>.
1357 // TODO: Hide this better.
1358 IntrusiveForwardListHook hook;
1359
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001360 private:
David Brazdiled596192015-01-23 10:39:45 +00001361 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001362 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001363
1364 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001365 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001366
Vladimir Marko46817b82016-03-29 12:21:58 +01001367 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001368
1369 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1370};
1371
David Brazdiled596192015-01-23 10:39:45 +00001372template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001373using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001374
David Brazdil1abb4192015-02-17 18:33:36 +00001375// This class is used by HEnvironment and HInstruction classes to record the
1376// instructions they use and pointers to the corresponding HUseListNodes kept
1377// by the used instructions.
1378template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001379class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001380 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001381 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1382 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001383
Vladimir Marko46817b82016-03-29 12:21:58 +01001384 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1385 : HUserRecord(old_record.instruction_, before_use_node) {}
1386 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1387 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001388 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001389 }
1390
1391 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001392 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1393 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001394
1395 private:
1396 // Instruction used by the user.
1397 HInstruction* instruction_;
1398
Vladimir Marko46817b82016-03-29 12:21:58 +01001399 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1400 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001401};
1402
Aart Bik854a02b2015-07-14 16:07:00 -07001403/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001404 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001405 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001406 * For write/read dependences on fields/arrays, the dependence analysis uses
1407 * type disambiguation (e.g. a float field write cannot modify the value of an
1408 * integer field read) and the access type (e.g. a reference array write cannot
1409 * modify the value of a reference field read [although it may modify the
1410 * reference fetch prior to reading the field, which is represented by its own
1411 * write/read dependence]). The analysis makes conservative points-to
1412 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1413 * the same, and any reference read depends on any reference read without
1414 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001415 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001416 * The internal representation uses 38-bit and is described in the table below.
1417 * The first line indicates the side effect, and for field/array accesses the
1418 * second line indicates the type of the access (in the order of the
1419 * Primitive::Type enum).
1420 * The two numbered lines below indicate the bit position in the bitfield (read
1421 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001422 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001423 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1424 * +-------------+---------+---------+--------------+---------+---------+
1425 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1426 * | 3 |333333322|222222221| 1 |111111110|000000000|
1427 * | 7 |654321098|765432109| 8 |765432109|876543210|
1428 *
1429 * Note that, to ease the implementation, 'changes' bits are least significant
1430 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001431 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001432class SideEffects : public ValueObject {
1433 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001434 SideEffects() : flags_(0) {}
1435
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001436 static SideEffects None() {
1437 return SideEffects(0);
1438 }
1439
1440 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001441 return SideEffects(kAllChangeBits | kAllDependOnBits);
1442 }
1443
1444 static SideEffects AllChanges() {
1445 return SideEffects(kAllChangeBits);
1446 }
1447
1448 static SideEffects AllDependencies() {
1449 return SideEffects(kAllDependOnBits);
1450 }
1451
1452 static SideEffects AllExceptGCDependency() {
1453 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1454 }
1455
1456 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001457 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001458 }
1459
Aart Bik34c3ba92015-07-20 14:08:59 -07001460 static SideEffects AllWrites() {
1461 return SideEffects(kAllWrites);
1462 }
1463
1464 static SideEffects AllReads() {
1465 return SideEffects(kAllReads);
1466 }
1467
1468 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1469 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001470 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001471 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001472 }
1473
Aart Bik854a02b2015-07-14 16:07:00 -07001474 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001475 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001476 }
1477
Aart Bik34c3ba92015-07-20 14:08:59 -07001478 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1479 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001480 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001481 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001482 }
1483
1484 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001485 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001486 }
1487
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001488 static SideEffects CanTriggerGC() {
1489 return SideEffects(1ULL << kCanTriggerGCBit);
1490 }
1491
1492 static SideEffects DependsOnGC() {
1493 return SideEffects(1ULL << kDependsOnGCBit);
1494 }
1495
Aart Bik854a02b2015-07-14 16:07:00 -07001496 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001497 SideEffects Union(SideEffects other) const {
1498 return SideEffects(flags_ | other.flags_);
1499 }
1500
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001501 SideEffects Exclusion(SideEffects other) const {
1502 return SideEffects(flags_ & ~other.flags_);
1503 }
1504
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001505 void Add(SideEffects other) {
1506 flags_ |= other.flags_;
1507 }
1508
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001509 bool Includes(SideEffects other) const {
1510 return (other.flags_ & flags_) == other.flags_;
1511 }
1512
1513 bool HasSideEffects() const {
1514 return (flags_ & kAllChangeBits);
1515 }
1516
1517 bool HasDependencies() const {
1518 return (flags_ & kAllDependOnBits);
1519 }
1520
1521 // Returns true if there are no side effects or dependencies.
1522 bool DoesNothing() const {
1523 return flags_ == 0;
1524 }
1525
Aart Bik854a02b2015-07-14 16:07:00 -07001526 // Returns true if something is written.
1527 bool DoesAnyWrite() const {
1528 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001529 }
1530
Aart Bik854a02b2015-07-14 16:07:00 -07001531 // Returns true if something is read.
1532 bool DoesAnyRead() const {
1533 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001534 }
1535
Aart Bik854a02b2015-07-14 16:07:00 -07001536 // Returns true if potentially everything is written and read
1537 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001538 bool DoesAllReadWrite() const {
1539 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1540 }
1541
Aart Bik854a02b2015-07-14 16:07:00 -07001542 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001543 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001544 }
1545
Roland Levillain0d5a2812015-11-13 10:07:31 +00001546 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001547 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001548 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1549 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001550 }
1551
1552 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001554 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001555 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001556 for (int s = kLastBit; s >= 0; s--) {
1557 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1558 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1559 // This is a bit for the GC side effect.
1560 if (current_bit_is_set) {
1561 flags += "GC";
1562 }
Aart Bik854a02b2015-07-14 16:07:00 -07001563 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001564 } else {
1565 // This is a bit for the array/field analysis.
1566 // The underscore character stands for the 'can trigger GC' bit.
1567 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1568 if (current_bit_is_set) {
1569 flags += kDebug[s];
1570 }
1571 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1572 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1573 flags += "|";
1574 }
1575 }
Aart Bik854a02b2015-07-14 16:07:00 -07001576 }
1577 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001578 }
1579
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001580 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001581
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001582 private:
1583 static constexpr int kFieldArrayAnalysisBits = 9;
1584
1585 static constexpr int kFieldWriteOffset = 0;
1586 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1587 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1588 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1589
1590 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1591
1592 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1593 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1594 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1595 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1596
1597 static constexpr int kLastBit = kDependsOnGCBit;
1598 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1599
1600 // Aliases.
1601
1602 static_assert(kChangeBits == kDependOnBits,
1603 "the 'change' bits should match the 'depend on' bits.");
1604
1605 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1606 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1607 static constexpr uint64_t kAllWrites =
1608 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1609 static constexpr uint64_t kAllReads =
1610 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001611
Aart Bik854a02b2015-07-14 16:07:00 -07001612 // Translates type to bit flag.
1613 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1614 CHECK_NE(type, Primitive::kPrimVoid);
1615 const uint64_t one = 1;
1616 const int shift = type; // 0-based consecutive enum
1617 DCHECK_LE(kFieldWriteOffset, shift);
1618 DCHECK_LT(shift, kArrayWriteOffset);
1619 return one << (type + offset);
1620 }
1621
1622 // Private constructor on direct flags value.
1623 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1624
1625 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001626};
1627
David Brazdiled596192015-01-23 10:39:45 +00001628// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001629class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001630 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001631 HEnvironment(ArenaAllocator* arena,
1632 size_t number_of_vregs,
1633 const DexFile& dex_file,
1634 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001635 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001636 InvokeType invoke_type,
1637 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001638 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1639 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001640 parent_(nullptr),
1641 dex_file_(dex_file),
1642 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001643 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001644 invoke_type_(invoke_type),
1645 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001646 }
1647
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001648 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001649 : HEnvironment(arena,
1650 to_copy.Size(),
1651 to_copy.GetDexFile(),
1652 to_copy.GetMethodIdx(),
1653 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001654 to_copy.GetInvokeType(),
1655 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001656
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001657 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001658 if (parent_ != nullptr) {
1659 parent_->SetAndCopyParentChain(allocator, parent);
1660 } else {
1661 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1662 parent_->CopyFrom(parent);
1663 if (parent->GetParent() != nullptr) {
1664 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1665 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001666 }
David Brazdiled596192015-01-23 10:39:45 +00001667 }
1668
Vladimir Marko71bf8092015-09-15 15:33:14 +01001669 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001670 void CopyFrom(HEnvironment* environment);
1671
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001672 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1673 // input to the loop phi instead. This is for inserting instructions that
1674 // require an environment (like HDeoptimization) in the loop pre-header.
1675 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001676
1677 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001678 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001679 }
1680
1681 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001682 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001683 }
1684
David Brazdil1abb4192015-02-17 18:33:36 +00001685 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001686
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001687 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001688
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001689 HEnvironment* GetParent() const { return parent_; }
1690
1691 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001692 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001693 }
1694
1695 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001696 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001697 }
1698
1699 uint32_t GetDexPc() const {
1700 return dex_pc_;
1701 }
1702
1703 uint32_t GetMethodIdx() const {
1704 return method_idx_;
1705 }
1706
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001707 InvokeType GetInvokeType() const {
1708 return invoke_type_;
1709 }
1710
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001711 const DexFile& GetDexFile() const {
1712 return dex_file_;
1713 }
1714
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001715 HInstruction* GetHolder() const {
1716 return holder_;
1717 }
1718
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001719
1720 bool IsFromInlinedInvoke() const {
1721 return GetParent() != nullptr;
1722 }
1723
David Brazdiled596192015-01-23 10:39:45 +00001724 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001725 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1726 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001727 HEnvironment* parent_;
1728 const DexFile& dex_file_;
1729 const uint32_t method_idx_;
1730 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001731 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001732
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001733 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001734 HInstruction* const holder_;
1735
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001736 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001737
1738 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1739};
1740
Vladimir Markof9f64412015-09-02 14:05:49 +01001741class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001742 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001743 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001744 : previous_(nullptr),
1745 next_(nullptr),
1746 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001747 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001748 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001749 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001750 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001751 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001752 locations_(nullptr),
1753 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001754 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001755 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001756 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1757 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1758 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001759
Dave Allison20dfc792014-06-16 20:44:29 -07001760 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001761
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001762#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001763 enum InstructionKind {
1764 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1765 };
1766#undef DECLARE_KIND
1767
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001768 HInstruction* GetNext() const { return next_; }
1769 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001770
Calin Juravle77520bc2015-01-12 18:45:46 +00001771 HInstruction* GetNextDisregardingMoves() const;
1772 HInstruction* GetPreviousDisregardingMoves() const;
1773
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001774 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001775 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001776 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001777 bool IsInBlock() const { return block_ != nullptr; }
1778 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001779 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1780 bool IsIrreducibleLoopHeaderPhi() const {
1781 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1782 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001783
Roland Levillain6b879dd2014-09-22 17:13:44 +01001784 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001785 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001786
1787 virtual void Accept(HGraphVisitor* visitor) = 0;
1788 virtual const char* DebugName() const = 0;
1789
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001790 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001791 void SetRawInputAt(size_t index, HInstruction* input) {
1792 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1793 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001794
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001795 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001796
1797 uint32_t GetDexPc() const { return dex_pc_; }
1798
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001799 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001800
Roland Levillaine161a2a2014-10-03 12:45:18 +01001801 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001802 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001803
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001804 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001805 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001806
Calin Juravle10e244f2015-01-26 18:54:32 +00001807 // Does not apply for all instructions, but having this at top level greatly
1808 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001809 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001810 virtual bool CanBeNull() const {
1811 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1812 return true;
1813 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001814
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001815 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001816 return false;
1817 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001818
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001819 virtual bool IsActualObject() const {
1820 return GetType() == Primitive::kPrimNot;
1821 }
1822
Calin Juravle2e768302015-07-28 14:41:11 +00001823 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001824
Calin Juravle61d544b2015-02-23 16:46:57 +00001825 ReferenceTypeInfo GetReferenceTypeInfo() const {
1826 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001827 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001828 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001829 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001830
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001831 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001832 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001833 // Note: fixup_end remains valid across push_front().
1834 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1835 HUseListNode<HInstruction*>* new_node =
1836 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1837 uses_.push_front(*new_node);
1838 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001839 }
1840
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001841 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001842 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001843 // Note: env_fixup_end remains valid across push_front().
1844 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1845 HUseListNode<HEnvironment*>* new_node =
1846 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1847 env_uses_.push_front(*new_node);
1848 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001849 }
1850
David Brazdil1abb4192015-02-17 18:33:36 +00001851 void RemoveAsUserOfInput(size_t input) {
1852 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001853 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1854 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1855 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001856 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001857
David Brazdil1abb4192015-02-17 18:33:36 +00001858 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1859 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001860
Vladimir Marko46817b82016-03-29 12:21:58 +01001861 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1862 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1863 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001864 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001865 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001866 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001867
Roland Levillain6c82d402014-10-13 16:10:27 +01001868 // Does this instruction strictly dominate `other_instruction`?
1869 // Returns false if this instruction and `other_instruction` are the same.
1870 // Aborts if this instruction and `other_instruction` are both phis.
1871 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001872
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001873 int GetId() const { return id_; }
1874 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001875
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001876 int GetSsaIndex() const { return ssa_index_; }
1877 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1878 bool HasSsaIndex() const { return ssa_index_ != -1; }
1879
1880 bool HasEnvironment() const { return environment_ != nullptr; }
1881 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001882 // Set the `environment_` field. Raw because this method does not
1883 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001884 void SetRawEnvironment(HEnvironment* environment) {
1885 DCHECK(environment_ == nullptr);
1886 DCHECK_EQ(environment->GetHolder(), this);
1887 environment_ = environment;
1888 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001889
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001890 void RemoveEnvironment();
1891
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001892 // Set the environment of this instruction, copying it from `environment`. While
1893 // copying, the uses lists are being updated.
1894 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001895 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001896 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001897 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001898 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001899 if (environment->GetParent() != nullptr) {
1900 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1901 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001902 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001903
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001904 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1905 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001906 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001907 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001908 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001909 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001910 if (environment->GetParent() != nullptr) {
1911 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1912 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001913 }
1914
Nicolas Geoffray39468442014-09-02 15:17:15 +01001915 // Returns the number of entries in the environment. Typically, that is the
1916 // number of dex registers in a method. It could be more in case of inlining.
1917 size_t EnvironmentSize() const;
1918
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001919 LocationSummary* GetLocations() const { return locations_; }
1920 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001921
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001923 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001924
Alexandre Rames188d4312015-04-09 18:30:21 +01001925 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1926 // uses of this instruction by `other` are *not* updated.
1927 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1928 ReplaceWith(other);
1929 other->ReplaceInput(this, use_index);
1930 }
1931
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001932 // Move `this` instruction before `cursor`.
1933 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001934
Vladimir Markofb337ea2015-11-25 15:25:10 +00001935 // Move `this` before its first user and out of any loops. If there is no
1936 // out-of-loop user that dominates all other users, move the instruction
1937 // to the end of the out-of-loop common dominator of the user's blocks.
1938 //
1939 // This can be used only on non-throwing instructions with no side effects that
1940 // have at least one use but no environment uses.
1941 void MoveBeforeFirstUserAndOutOfLoops();
1942
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001943#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001944 bool Is##type() const; \
1945 const H##type* As##type() const; \
1946 H##type* As##type();
1947
1948 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1949#undef INSTRUCTION_TYPE_CHECK
1950
1951#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001952 bool Is##type() const { return (As##type() != nullptr); } \
1953 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001954 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001955 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001956#undef INSTRUCTION_TYPE_CHECK
1957
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001958 // Returns whether the instruction can be moved within the graph.
1959 virtual bool CanBeMoved() const { return false; }
1960
1961 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001962 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001963 return false;
1964 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001965
1966 // Returns whether any data encoded in the two instructions is equal.
1967 // This method does not look at the inputs. Both instructions must be
1968 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001969 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001970 return false;
1971 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001972
1973 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001974 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001975 // 2) Their inputs are identical.
1976 bool Equals(HInstruction* other) const;
1977
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
1979 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
1980 // the virtual function because the __attribute__((__pure__)) doesn't really
1981 // apply the strong requirement for virtual functions, preventing optimizations.
1982 InstructionKind GetKind() const PURE;
1983 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001984
1985 virtual size_t ComputeHashCode() const {
1986 size_t result = GetKind();
1987 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1988 result = (result * 31) + InputAt(i)->GetId();
1989 }
1990 return result;
1991 }
1992
1993 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001994 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001995 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001996
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001997 size_t GetLifetimePosition() const { return lifetime_position_; }
1998 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1999 LiveInterval* GetLiveInterval() const { return live_interval_; }
2000 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2001 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2002
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002003 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2004
2005 // Returns whether the code generation of the instruction will require to have access
2006 // to the current method. Such instructions are:
2007 // (1): Instructions that require an environment, as calling the runtime requires
2008 // to walk the stack and have the current method stored at a specific stack address.
2009 // (2): Object literals like classes and strings, that are loaded from the dex cache
2010 // fields of the current method.
2011 bool NeedsCurrentMethod() const {
2012 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2013 }
2014
Vladimir Markodc151b22015-10-15 18:02:30 +01002015 // Returns whether the code generation of the instruction will require to have access
2016 // to the dex cache of the current method's declaring class via the current method.
2017 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002018
Mark Mendellc4701932015-04-10 13:18:51 -04002019 // Does this instruction have any use in an environment before
2020 // control flow hits 'other'?
2021 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2022
2023 // Remove all references to environment uses of this instruction.
2024 // The caller must ensure that this is safe to do.
2025 void RemoveEnvironmentUsers();
2026
Vladimir Markoa1de9182016-02-25 11:37:38 +00002027 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2028 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002029
David Brazdil1abb4192015-02-17 18:33:36 +00002030 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002031 // If set, the machine code for this instruction is assumed to be generated by
2032 // its users. Used by liveness analysis to compute use positions accordingly.
2033 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2034 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2035 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2036 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2037
David Brazdil1abb4192015-02-17 18:33:36 +00002038 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2039 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2040
Vladimir Markoa1de9182016-02-25 11:37:38 +00002041 uint32_t GetPackedFields() const {
2042 return packed_fields_;
2043 }
2044
2045 template <size_t flag>
2046 bool GetPackedFlag() const {
2047 return (packed_fields_ & (1u << flag)) != 0u;
2048 }
2049
2050 template <size_t flag>
2051 void SetPackedFlag(bool value = true) {
2052 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2053 }
2054
2055 template <typename BitFieldType>
2056 typename BitFieldType::value_type GetPackedField() const {
2057 return BitFieldType::Decode(packed_fields_);
2058 }
2059
2060 template <typename BitFieldType>
2061 void SetPackedField(typename BitFieldType::value_type value) {
2062 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2063 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2064 }
2065
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002066 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002067 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2068 auto before_use_node = uses_.before_begin();
2069 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2070 HInstruction* user = use_node->GetUser();
2071 size_t input_index = use_node->GetIndex();
2072 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2073 before_use_node = use_node;
2074 }
2075 }
2076
2077 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2078 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2079 if (next != uses_.end()) {
2080 HInstruction* next_user = next->GetUser();
2081 size_t next_index = next->GetIndex();
2082 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2083 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2084 }
2085 }
2086
2087 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2088 auto before_env_use_node = env_uses_.before_begin();
2089 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2090 HEnvironment* user = env_use_node->GetUser();
2091 size_t input_index = env_use_node->GetIndex();
2092 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2093 before_env_use_node = env_use_node;
2094 }
2095 }
2096
2097 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2098 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2099 if (next != env_uses_.end()) {
2100 HEnvironment* next_user = next->GetUser();
2101 size_t next_index = next->GetIndex();
2102 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2103 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2104 }
2105 }
David Brazdil1abb4192015-02-17 18:33:36 +00002106
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002107 HInstruction* previous_;
2108 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002109 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002110 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002111
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002112 // An instruction gets an id when it is added to the graph.
2113 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002114 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002115 int id_;
2116
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002117 // When doing liveness analysis, instructions that have uses get an SSA index.
2118 int ssa_index_;
2119
Vladimir Markoa1de9182016-02-25 11:37:38 +00002120 // Packed fields.
2121 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002122
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002123 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002124 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002125
2126 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002127 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002128
Nicolas Geoffray39468442014-09-02 15:17:15 +01002129 // The environment associated with this instruction. Not null if the instruction
2130 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002131 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002132
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002133 // Set by the code generator.
2134 LocationSummary* locations_;
2135
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002136 // Set by the liveness analysis.
2137 LiveInterval* live_interval_;
2138
2139 // Set by the liveness analysis, this is the position in a linear
2140 // order of blocks where this instruction's live interval start.
2141 size_t lifetime_position_;
2142
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002143 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002144
Vladimir Markoa1de9182016-02-25 11:37:38 +00002145 // The reference handle part of the reference type info.
2146 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002147 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002148 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002149
David Brazdil1abb4192015-02-17 18:33:36 +00002150 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002152 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002153 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002154 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002155
2156 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2157};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002158std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160class HInputIterator : public ValueObject {
2161 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002162 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002163
2164 bool Done() const { return index_ == instruction_->InputCount(); }
2165 HInstruction* Current() const { return instruction_->InputAt(index_); }
2166 void Advance() { index_++; }
2167
2168 private:
2169 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002170 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002171
2172 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2173};
2174
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175class HInstructionIterator : public ValueObject {
2176 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002177 explicit HInstructionIterator(const HInstructionList& instructions)
2178 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002179 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002180 }
2181
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002182 bool Done() const { return instruction_ == nullptr; }
2183 HInstruction* Current() const { return instruction_; }
2184 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002186 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002187 }
2188
2189 private:
2190 HInstruction* instruction_;
2191 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002192
2193 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002194};
2195
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002196class HBackwardInstructionIterator : public ValueObject {
2197 public:
2198 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2199 : instruction_(instructions.last_instruction_) {
2200 next_ = Done() ? nullptr : instruction_->GetPrevious();
2201 }
2202
2203 bool Done() const { return instruction_ == nullptr; }
2204 HInstruction* Current() const { return instruction_; }
2205 void Advance() {
2206 instruction_ = next_;
2207 next_ = Done() ? nullptr : instruction_->GetPrevious();
2208 }
2209
2210 private:
2211 HInstruction* instruction_;
2212 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002213
2214 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002215};
2216
Vladimir Markof9f64412015-09-02 14:05:49 +01002217template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002218class HTemplateInstruction: public HInstruction {
2219 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002220 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002221 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002222 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002223
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002224 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002227 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2228 DCHECK_LT(i, N);
2229 return inputs_[i];
2230 }
David Brazdil1abb4192015-02-17 18:33:36 +00002231
2232 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002233 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002234 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002235 }
2236
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002237 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002238 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002239
2240 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241};
2242
Vladimir Markof9f64412015-09-02 14:05:49 +01002243// HTemplateInstruction specialization for N=0.
2244template<>
2245class HTemplateInstruction<0>: public HInstruction {
2246 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002247 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002248 : HInstruction(side_effects, dex_pc) {}
2249
Vladimir Markof9f64412015-09-02 14:05:49 +01002250 virtual ~HTemplateInstruction() {}
2251
2252 size_t InputCount() const OVERRIDE { return 0; }
2253
2254 protected:
2255 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2256 LOG(FATAL) << "Unreachable";
2257 UNREACHABLE();
2258 }
2259
2260 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2261 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2262 LOG(FATAL) << "Unreachable";
2263 UNREACHABLE();
2264 }
2265
2266 private:
2267 friend class SsaBuilder;
2268};
2269
Dave Allison20dfc792014-06-16 20:44:29 -07002270template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002271class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002272 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002273 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002274 : HTemplateInstruction<N>(side_effects, dex_pc) {
2275 this->template SetPackedField<TypeField>(type);
2276 }
Dave Allison20dfc792014-06-16 20:44:29 -07002277 virtual ~HExpression() {}
2278
Vladimir Markoa1de9182016-02-25 11:37:38 +00002279 Primitive::Type GetType() const OVERRIDE {
2280 return TypeField::Decode(this->GetPackedFields());
2281 }
Dave Allison20dfc792014-06-16 20:44:29 -07002282
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002283 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002284 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2285 static constexpr size_t kFieldTypeSize =
2286 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2287 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2288 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2289 "Too many packed fields.");
2290 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002291};
2292
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002293// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2294// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002295class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002296 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002297 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2298 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002299
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002300 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002302 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303
2304 private:
2305 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2306};
2307
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002308// Represents dex's RETURN opcodes. A HReturn is a control flow
2309// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002310class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002311 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2313 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002314 SetRawInputAt(0, value);
2315 }
2316
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002317 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002318
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002319 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002320
2321 private:
2322 DISALLOW_COPY_AND_ASSIGN(HReturn);
2323};
2324
Vladimir Markofcb503c2016-05-18 12:48:17 +01002325class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002326 public:
2327 HPhi(ArenaAllocator* arena,
2328 uint32_t reg_number,
2329 size_t number_of_inputs,
2330 Primitive::Type type,
2331 uint32_t dex_pc = kNoDexPc)
2332 : HInstruction(SideEffects::None(), dex_pc),
2333 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2334 reg_number_(reg_number) {
2335 SetPackedField<TypeField>(ToPhiType(type));
2336 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2337 // Phis are constructed live and marked dead if conflicting or unused.
2338 // Individual steps of SsaBuilder should assume that if a phi has been
2339 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2340 SetPackedFlag<kFlagIsLive>(true);
2341 SetPackedFlag<kFlagCanBeNull>(true);
2342 }
2343
2344 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2345 static Primitive::Type ToPhiType(Primitive::Type type) {
2346 return Primitive::PrimitiveKind(type);
2347 }
2348
2349 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2350
2351 size_t InputCount() const OVERRIDE { return inputs_.size(); }
2352
2353 void AddInput(HInstruction* input);
2354 void RemoveInputAt(size_t index);
2355
2356 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2357 void SetType(Primitive::Type new_type) {
2358 // Make sure that only valid type changes occur. The following are allowed:
2359 // (1) int -> float/ref (primitive type propagation),
2360 // (2) long -> double (primitive type propagation).
2361 DCHECK(GetType() == new_type ||
2362 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2363 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2364 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2365 SetPackedField<TypeField>(new_type);
2366 }
2367
2368 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2369 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2370
2371 uint32_t GetRegNumber() const { return reg_number_; }
2372
2373 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2374 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2375 bool IsDead() const { return !IsLive(); }
2376 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2377
2378 bool IsVRegEquivalentOf(HInstruction* other) const {
2379 return other != nullptr
2380 && other->IsPhi()
2381 && other->AsPhi()->GetBlock() == GetBlock()
2382 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2383 }
2384
2385 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2386 // An equivalent phi is a phi having the same dex register and type.
2387 // It assumes that phis with the same dex register are adjacent.
2388 HPhi* GetNextEquivalentPhiWithSameType() {
2389 HInstruction* next = GetNext();
2390 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2391 if (next->GetType() == GetType()) {
2392 return next->AsPhi();
2393 }
2394 next = next->GetNext();
2395 }
2396 return nullptr;
2397 }
2398
2399 DECLARE_INSTRUCTION(Phi);
2400
2401 protected:
2402 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
2403 return inputs_[index];
2404 }
2405
2406 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2407 inputs_[index] = input;
2408 }
2409
2410 private:
2411 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2412 static constexpr size_t kFieldTypeSize =
2413 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2414 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2415 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2416 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2417 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2418 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2419
2420 ArenaVector<HUserRecord<HInstruction*> > inputs_;
2421 const uint32_t reg_number_;
2422
2423 DISALLOW_COPY_AND_ASSIGN(HPhi);
2424};
2425
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002426// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002427// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002428// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002429class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002430 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002431 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002432
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002433 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002434
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002435 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002436
2437 private:
2438 DISALLOW_COPY_AND_ASSIGN(HExit);
2439};
2440
2441// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002442class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002443 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002444 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002445
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002446 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002447
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002448 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002449 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002450 }
2451
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002452 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002453
2454 private:
2455 DISALLOW_COPY_AND_ASSIGN(HGoto);
2456};
2457
Roland Levillain9867bc72015-08-05 10:21:34 +01002458class HConstant : public HExpression<0> {
2459 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002460 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2461 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002462
2463 bool CanBeMoved() const OVERRIDE { return true; }
2464
Roland Levillain1a653882016-03-18 18:05:57 +00002465 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002466 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002467 // Is this constant 0 in the arithmetic sense?
2468 virtual bool IsArithmeticZero() const { return false; }
2469 // Is this constant a 0-bit pattern?
2470 virtual bool IsZeroBitPattern() const { return false; }
2471 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002472 virtual bool IsOne() const { return false; }
2473
David Brazdil77a48ae2015-09-15 12:34:04 +00002474 virtual uint64_t GetValueAsUint64() const = 0;
2475
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002476 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002477
2478 private:
2479 DISALLOW_COPY_AND_ASSIGN(HConstant);
2480};
2481
Vladimir Markofcb503c2016-05-18 12:48:17 +01002482class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002483 public:
2484 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2485 return true;
2486 }
2487
David Brazdil77a48ae2015-09-15 12:34:04 +00002488 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2489
Roland Levillain9867bc72015-08-05 10:21:34 +01002490 size_t ComputeHashCode() const OVERRIDE { return 0; }
2491
Roland Levillain1a653882016-03-18 18:05:57 +00002492 // The null constant representation is a 0-bit pattern.
2493 virtual bool IsZeroBitPattern() const { return true; }
2494
Roland Levillain9867bc72015-08-05 10:21:34 +01002495 DECLARE_INSTRUCTION(NullConstant);
2496
2497 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002498 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002499
2500 friend class HGraph;
2501 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2502};
2503
2504// Constants of the type int. Those can be from Dex instructions, or
2505// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002506class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002507 public:
2508 int32_t GetValue() const { return value_; }
2509
David Brazdil9f389d42015-10-01 14:32:56 +01002510 uint64_t GetValueAsUint64() const OVERRIDE {
2511 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2512 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002513
Roland Levillain9867bc72015-08-05 10:21:34 +01002514 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002515 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002516 return other->AsIntConstant()->value_ == value_;
2517 }
2518
2519 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2520
2521 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002522 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2523 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002524 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2525
Roland Levillain1a653882016-03-18 18:05:57 +00002526 // Integer constants are used to encode Boolean values as well,
2527 // where 1 means true and 0 means false.
2528 bool IsTrue() const { return GetValue() == 1; }
2529 bool IsFalse() const { return GetValue() == 0; }
2530
Roland Levillain9867bc72015-08-05 10:21:34 +01002531 DECLARE_INSTRUCTION(IntConstant);
2532
2533 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002534 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2535 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2536 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2537 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002538
2539 const int32_t value_;
2540
2541 friend class HGraph;
2542 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2543 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2544 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2545};
2546
Vladimir Markofcb503c2016-05-18 12:48:17 +01002547class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002548 public:
2549 int64_t GetValue() const { return value_; }
2550
David Brazdil77a48ae2015-09-15 12:34:04 +00002551 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2552
Roland Levillain9867bc72015-08-05 10:21:34 +01002553 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002554 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002555 return other->AsLongConstant()->value_ == value_;
2556 }
2557
2558 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2559
2560 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002561 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2562 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002563 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2564
2565 DECLARE_INSTRUCTION(LongConstant);
2566
2567 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002568 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2569 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002570
2571 const int64_t value_;
2572
2573 friend class HGraph;
2574 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2575};
Dave Allison20dfc792014-06-16 20:44:29 -07002576
Vladimir Markofcb503c2016-05-18 12:48:17 +01002577class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002578 public:
2579 float GetValue() const { return value_; }
2580
2581 uint64_t GetValueAsUint64() const OVERRIDE {
2582 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2583 }
2584
2585 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2586 DCHECK(other->IsFloatConstant()) << other->DebugName();
2587 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2588 }
2589
2590 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2591
2592 bool IsMinusOne() const OVERRIDE {
2593 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2594 }
Roland Levillain1a653882016-03-18 18:05:57 +00002595 bool IsArithmeticZero() const OVERRIDE {
2596 return std::fpclassify(value_) == FP_ZERO;
2597 }
2598 bool IsArithmeticPositiveZero() const {
2599 return IsArithmeticZero() && !std::signbit(value_);
2600 }
2601 bool IsArithmeticNegativeZero() const {
2602 return IsArithmeticZero() && std::signbit(value_);
2603 }
2604 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002605 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002606 }
2607 bool IsOne() const OVERRIDE {
2608 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2609 }
2610 bool IsNaN() const {
2611 return std::isnan(value_);
2612 }
2613
2614 DECLARE_INSTRUCTION(FloatConstant);
2615
2616 private:
2617 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2618 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2619 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2620 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2621
2622 const float value_;
2623
2624 // Only the SsaBuilder and HGraph can create floating-point constants.
2625 friend class SsaBuilder;
2626 friend class HGraph;
2627 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2628};
2629
Vladimir Markofcb503c2016-05-18 12:48:17 +01002630class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002631 public:
2632 double GetValue() const { return value_; }
2633
2634 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2635
2636 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2637 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2638 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2639 }
2640
2641 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2642
2643 bool IsMinusOne() const OVERRIDE {
2644 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2645 }
Roland Levillain1a653882016-03-18 18:05:57 +00002646 bool IsArithmeticZero() const OVERRIDE {
2647 return std::fpclassify(value_) == FP_ZERO;
2648 }
2649 bool IsArithmeticPositiveZero() const {
2650 return IsArithmeticZero() && !std::signbit(value_);
2651 }
2652 bool IsArithmeticNegativeZero() const {
2653 return IsArithmeticZero() && std::signbit(value_);
2654 }
2655 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002656 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002657 }
2658 bool IsOne() const OVERRIDE {
2659 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2660 }
2661 bool IsNaN() const {
2662 return std::isnan(value_);
2663 }
2664
2665 DECLARE_INSTRUCTION(DoubleConstant);
2666
2667 private:
2668 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2669 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2670 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2671 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2672
2673 const double value_;
2674
2675 // Only the SsaBuilder and HGraph can create floating-point constants.
2676 friend class SsaBuilder;
2677 friend class HGraph;
2678 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2679};
2680
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002681// Conditional branch. A block ending with an HIf instruction must have
2682// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002683class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002684 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002685 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2686 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002687 SetRawInputAt(0, input);
2688 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002689
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002690 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002691
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002692 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002693 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002694 }
2695
2696 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002697 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002698 }
2699
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002700 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002701
2702 private:
2703 DISALLOW_COPY_AND_ASSIGN(HIf);
2704};
2705
David Brazdilfc6a86a2015-06-26 10:33:45 +00002706
2707// Abstract instruction which marks the beginning and/or end of a try block and
2708// links it to the respective exception handlers. Behaves the same as a Goto in
2709// non-exceptional control flow.
2710// Normal-flow successor is stored at index zero, exception handlers under
2711// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002712class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002713 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002714 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002715 kEntry,
2716 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002717 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002718 };
2719
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002721 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2722 SetPackedField<BoundaryKindField>(kind);
2723 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002724
2725 bool IsControlFlow() const OVERRIDE { return true; }
2726
2727 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002728 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002729
David Brazdild26a4112015-11-10 11:07:31 +00002730 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2731 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2732 }
2733
David Brazdilfc6a86a2015-06-26 10:33:45 +00002734 // Returns whether `handler` is among its exception handlers (non-zero index
2735 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002736 bool HasExceptionHandler(const HBasicBlock& handler) const {
2737 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002738 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002739 }
2740
2741 // If not present already, adds `handler` to its block's list of exception
2742 // handlers.
2743 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002744 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002745 GetBlock()->AddSuccessor(handler);
2746 }
2747 }
2748
Vladimir Markoa1de9182016-02-25 11:37:38 +00002749 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2750 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002751
David Brazdilffee3d32015-07-06 11:48:53 +01002752 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2753
David Brazdilfc6a86a2015-06-26 10:33:45 +00002754 DECLARE_INSTRUCTION(TryBoundary);
2755
2756 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002757 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2758 static constexpr size_t kFieldBoundaryKindSize =
2759 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2760 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2761 kFieldBoundaryKind + kFieldBoundaryKindSize;
2762 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2763 "Too many packed fields.");
2764 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002765
2766 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2767};
2768
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002769// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002770class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002771 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002772 // We set CanTriggerGC to prevent any intermediate address to be live
2773 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002774 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002775 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002776 SetRawInputAt(0, cond);
2777 }
2778
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002779 bool CanBeMoved() const OVERRIDE { return true; }
2780 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2781 return true;
2782 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002783 bool NeedsEnvironment() const OVERRIDE { return true; }
2784 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002785
2786 DECLARE_INSTRUCTION(Deoptimize);
2787
2788 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002789 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2790};
2791
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002792// Represents the ArtMethod that was passed as a first argument to
2793// the method. It is used by instructions that depend on it, like
2794// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002795class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002796 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002797 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2798 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002799
2800 DECLARE_INSTRUCTION(CurrentMethod);
2801
2802 private:
2803 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2804};
2805
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002806// Fetches an ArtMethod from the virtual table or the interface method table
2807// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002808class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002809 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002810 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002811 kVTable,
2812 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002813 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002814 };
2815 HClassTableGet(HInstruction* cls,
2816 Primitive::Type type,
2817 TableKind kind,
2818 size_t index,
2819 uint32_t dex_pc)
2820 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002821 index_(index) {
2822 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002823 SetRawInputAt(0, cls);
2824 }
2825
2826 bool CanBeMoved() const OVERRIDE { return true; }
2827 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2828 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002829 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002830 }
2831
Vladimir Markoa1de9182016-02-25 11:37:38 +00002832 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002833 size_t GetIndex() const { return index_; }
2834
2835 DECLARE_INSTRUCTION(ClassTableGet);
2836
2837 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002838 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2839 static constexpr size_t kFieldTableKindSize =
2840 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2841 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2842 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2843 "Too many packed fields.");
2844 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2845
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002846 // The index of the ArtMethod in the table.
2847 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002848
2849 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2850};
2851
Mark Mendellfe57faa2015-09-18 09:26:15 -04002852// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2853// have one successor for each entry in the switch table, and the final successor
2854// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002855class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002856 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002857 HPackedSwitch(int32_t start_value,
2858 uint32_t num_entries,
2859 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002860 uint32_t dex_pc = kNoDexPc)
2861 : HTemplateInstruction(SideEffects::None(), dex_pc),
2862 start_value_(start_value),
2863 num_entries_(num_entries) {
2864 SetRawInputAt(0, input);
2865 }
2866
2867 bool IsControlFlow() const OVERRIDE { return true; }
2868
2869 int32_t GetStartValue() const { return start_value_; }
2870
Vladimir Marko211c2112015-09-24 16:52:33 +01002871 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002872
2873 HBasicBlock* GetDefaultBlock() const {
2874 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002875 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002876 }
2877 DECLARE_INSTRUCTION(PackedSwitch);
2878
2879 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002880 const int32_t start_value_;
2881 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002882
2883 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2884};
2885
Roland Levillain88cb1752014-10-20 16:36:47 +01002886class HUnaryOperation : public HExpression<1> {
2887 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002888 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2889 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002890 SetRawInputAt(0, input);
2891 }
2892
2893 HInstruction* GetInput() const { return InputAt(0); }
2894 Primitive::Type GetResultType() const { return GetType(); }
2895
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002896 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002897 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002898 return true;
2899 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002900
Roland Levillain31dd3d62016-02-16 12:21:02 +00002901 // Try to statically evaluate `this` and return a HConstant
2902 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002903 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002904 HConstant* TryStaticEvaluation() const;
2905
2906 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002907 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2908 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002909 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2910 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002911
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002912 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002913
2914 private:
2915 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2916};
2917
Dave Allison20dfc792014-06-16 20:44:29 -07002918class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002919 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002920 HBinaryOperation(Primitive::Type result_type,
2921 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002922 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002923 SideEffects side_effects = SideEffects::None(),
2924 uint32_t dex_pc = kNoDexPc)
2925 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002926 SetRawInputAt(0, left);
2927 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002928 }
2929
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002930 HInstruction* GetLeft() const { return InputAt(0); }
2931 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002932 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002933
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002934 virtual bool IsCommutative() const { return false; }
2935
2936 // Put constant on the right.
2937 // Returns whether order is changed.
2938 bool OrderInputsWithConstantOnTheRight() {
2939 HInstruction* left = InputAt(0);
2940 HInstruction* right = InputAt(1);
2941 if (left->IsConstant() && !right->IsConstant()) {
2942 ReplaceInput(right, 0);
2943 ReplaceInput(left, 1);
2944 return true;
2945 }
2946 return false;
2947 }
2948
2949 // Order inputs by instruction id, but favor constant on the right side.
2950 // This helps GVN for commutative ops.
2951 void OrderInputs() {
2952 DCHECK(IsCommutative());
2953 HInstruction* left = InputAt(0);
2954 HInstruction* right = InputAt(1);
2955 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2956 return;
2957 }
2958 if (OrderInputsWithConstantOnTheRight()) {
2959 return;
2960 }
2961 // Order according to instruction id.
2962 if (left->GetId() > right->GetId()) {
2963 ReplaceInput(right, 0);
2964 ReplaceInput(left, 1);
2965 }
2966 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002967
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002968 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002969 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002970 return true;
2971 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002972
Roland Levillain31dd3d62016-02-16 12:21:02 +00002973 // Try to statically evaluate `this` and return a HConstant
2974 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002975 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002976 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002977
2978 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002979 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2980 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002981 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2982 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002983 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002984 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2985 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002986 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2987 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002988 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2989 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002990 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002991 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2992 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002993
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002994 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002995 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002996 HConstant* GetConstantRight() const;
2997
2998 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002999 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003000 HInstruction* GetLeastConstantLeft() const;
3001
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003002 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003003
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003004 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003005 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3006};
3007
Mark Mendellc4701932015-04-10 13:18:51 -04003008// The comparison bias applies for floating point operations and indicates how NaN
3009// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003010enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003011 kNoBias, // bias is not applicable (i.e. for long operation)
3012 kGtBias, // return 1 for NaN comparisons
3013 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003014 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003015};
3016
Roland Levillain31dd3d62016-02-16 12:21:02 +00003017std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3018
Dave Allison20dfc792014-06-16 20:44:29 -07003019class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003020 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003021 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003022 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3023 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3024 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003025
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003026 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003027 // `instruction`, and disregard moves in between.
3028 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003029
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003030 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003031
3032 virtual IfCondition GetCondition() const = 0;
3033
Mark Mendellc4701932015-04-10 13:18:51 -04003034 virtual IfCondition GetOppositeCondition() const = 0;
3035
Vladimir Markoa1de9182016-02-25 11:37:38 +00003036 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003037 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3038
Vladimir Markoa1de9182016-02-25 11:37:38 +00003039 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3040 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003041
3042 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003043 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003044 }
3045
Roland Levillain4fa13f62015-07-06 18:11:54 +01003046 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003047 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003048 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003049 if (if_cond == kCondNE) {
3050 return true;
3051 } else if (if_cond == kCondEQ) {
3052 return false;
3053 }
3054 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003055 }
3056
3057 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003058 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003059 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003060 if (if_cond == kCondEQ) {
3061 return true;
3062 } else if (if_cond == kCondNE) {
3063 return false;
3064 }
3065 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003066 }
3067
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003069 // Needed if we merge a HCompare into a HCondition.
3070 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3071 static constexpr size_t kFieldComparisonBiasSize =
3072 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3073 static constexpr size_t kNumberOfConditionPackedBits =
3074 kFieldComparisonBias + kFieldComparisonBiasSize;
3075 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3076 using ComparisonBiasField =
3077 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3078
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079 template <typename T>
3080 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3081
3082 template <typename T>
3083 int32_t CompareFP(T x, T y) const {
3084 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3085 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3086 // Handle the bias.
3087 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3088 }
3089
3090 // Return an integer constant containing the result of a condition evaluated at compile time.
3091 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3092 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3093 }
3094
Dave Allison20dfc792014-06-16 20:44:29 -07003095 private:
3096 DISALLOW_COPY_AND_ASSIGN(HCondition);
3097};
3098
3099// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003100class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003101 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003102 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3103 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003104
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003105 bool IsCommutative() const OVERRIDE { return true; }
3106
Vladimir Marko9e23df52015-11-10 17:14:35 +00003107 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3108 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003109 return MakeConstantCondition(true, GetDexPc());
3110 }
3111 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3112 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3113 }
3114 // In the following Evaluate methods, a HCompare instruction has
3115 // been merged into this HEqual instruction; evaluate it as
3116 // `Compare(x, y) == 0`.
3117 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3118 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3119 GetDexPc());
3120 }
3121 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3122 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3123 }
3124 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3125 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003126 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003127
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003128 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003129
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003130 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003131 return kCondEQ;
3132 }
3133
Mark Mendellc4701932015-04-10 13:18:51 -04003134 IfCondition GetOppositeCondition() const OVERRIDE {
3135 return kCondNE;
3136 }
3137
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003138 private:
Aart Bike9f37602015-10-09 11:15:55 -07003139 template <typename T> bool Compute(T x, T y) const { return x == y; }
3140
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003141 DISALLOW_COPY_AND_ASSIGN(HEqual);
3142};
3143
Vladimir Markofcb503c2016-05-18 12:48:17 +01003144class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003145 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003146 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3147 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003148
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003149 bool IsCommutative() const OVERRIDE { return true; }
3150
Vladimir Marko9e23df52015-11-10 17:14:35 +00003151 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3152 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003153 return MakeConstantCondition(false, GetDexPc());
3154 }
3155 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3156 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3157 }
3158 // In the following Evaluate methods, a HCompare instruction has
3159 // been merged into this HNotEqual instruction; evaluate it as
3160 // `Compare(x, y) != 0`.
3161 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3162 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3163 }
3164 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3165 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3166 }
3167 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3168 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003169 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003170
Dave Allison20dfc792014-06-16 20:44:29 -07003171 DECLARE_INSTRUCTION(NotEqual);
3172
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003173 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003174 return kCondNE;
3175 }
3176
Mark Mendellc4701932015-04-10 13:18:51 -04003177 IfCondition GetOppositeCondition() const OVERRIDE {
3178 return kCondEQ;
3179 }
3180
Dave Allison20dfc792014-06-16 20:44:29 -07003181 private:
Aart Bike9f37602015-10-09 11:15:55 -07003182 template <typename T> bool Compute(T x, T y) const { return x != y; }
3183
Dave Allison20dfc792014-06-16 20:44:29 -07003184 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3185};
3186
Vladimir Markofcb503c2016-05-18 12:48:17 +01003187class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003188 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003189 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3190 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003191
Roland Levillain9867bc72015-08-05 10:21:34 +01003192 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003193 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003194 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003195 // In the following Evaluate methods, a HCompare instruction has
3196 // been merged into this HLessThan instruction; evaluate it as
3197 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003198 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003199 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3200 }
3201 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3202 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3203 }
3204 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3205 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003206 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003207
Dave Allison20dfc792014-06-16 20:44:29 -07003208 DECLARE_INSTRUCTION(LessThan);
3209
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003210 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003211 return kCondLT;
3212 }
3213
Mark Mendellc4701932015-04-10 13:18:51 -04003214 IfCondition GetOppositeCondition() const OVERRIDE {
3215 return kCondGE;
3216 }
3217
Dave Allison20dfc792014-06-16 20:44:29 -07003218 private:
Aart Bike9f37602015-10-09 11:15:55 -07003219 template <typename T> bool Compute(T x, T y) const { return x < y; }
3220
Dave Allison20dfc792014-06-16 20:44:29 -07003221 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3222};
3223
Vladimir Markofcb503c2016-05-18 12:48:17 +01003224class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003225 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003226 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3227 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003228
Roland Levillain9867bc72015-08-05 10:21:34 +01003229 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003230 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003231 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003232 // In the following Evaluate methods, a HCompare instruction has
3233 // been merged into this HLessThanOrEqual instruction; evaluate it as
3234 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003235 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003236 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3237 }
3238 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3239 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3240 }
3241 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3242 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003243 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003244
Dave Allison20dfc792014-06-16 20:44:29 -07003245 DECLARE_INSTRUCTION(LessThanOrEqual);
3246
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003247 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003248 return kCondLE;
3249 }
3250
Mark Mendellc4701932015-04-10 13:18:51 -04003251 IfCondition GetOppositeCondition() const OVERRIDE {
3252 return kCondGT;
3253 }
3254
Dave Allison20dfc792014-06-16 20:44:29 -07003255 private:
Aart Bike9f37602015-10-09 11:15:55 -07003256 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3257
Dave Allison20dfc792014-06-16 20:44:29 -07003258 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3259};
3260
Vladimir Markofcb503c2016-05-18 12:48:17 +01003261class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003263 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3264 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003265
Roland Levillain9867bc72015-08-05 10:21:34 +01003266 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003267 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003268 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003269 // In the following Evaluate methods, a HCompare instruction has
3270 // been merged into this HGreaterThan instruction; evaluate it as
3271 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003272 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003273 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3274 }
3275 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3276 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3277 }
3278 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3279 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003280 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003281
Dave Allison20dfc792014-06-16 20:44:29 -07003282 DECLARE_INSTRUCTION(GreaterThan);
3283
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003284 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003285 return kCondGT;
3286 }
3287
Mark Mendellc4701932015-04-10 13:18:51 -04003288 IfCondition GetOppositeCondition() const OVERRIDE {
3289 return kCondLE;
3290 }
3291
Dave Allison20dfc792014-06-16 20:44:29 -07003292 private:
Aart Bike9f37602015-10-09 11:15:55 -07003293 template <typename T> bool Compute(T x, T y) const { return x > y; }
3294
Dave Allison20dfc792014-06-16 20:44:29 -07003295 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3296};
3297
Vladimir Markofcb503c2016-05-18 12:48:17 +01003298class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003299 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003300 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3301 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003302
Roland Levillain9867bc72015-08-05 10:21:34 +01003303 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003304 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003305 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003306 // In the following Evaluate methods, a HCompare instruction has
3307 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3308 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003309 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003310 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3311 }
3312 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3313 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3314 }
3315 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3316 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003317 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003318
Dave Allison20dfc792014-06-16 20:44:29 -07003319 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003321 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003322 return kCondGE;
3323 }
3324
Mark Mendellc4701932015-04-10 13:18:51 -04003325 IfCondition GetOppositeCondition() const OVERRIDE {
3326 return kCondLT;
3327 }
3328
Dave Allison20dfc792014-06-16 20:44:29 -07003329 private:
Aart Bike9f37602015-10-09 11:15:55 -07003330 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3331
Dave Allison20dfc792014-06-16 20:44:29 -07003332 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3333};
3334
Vladimir Markofcb503c2016-05-18 12:48:17 +01003335class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003336 public:
3337 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3338 : HCondition(first, second, dex_pc) {}
3339
3340 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003341 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003342 }
3343 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003344 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3345 }
3346 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3347 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3348 LOG(FATAL) << DebugName() << " is not defined for float values";
3349 UNREACHABLE();
3350 }
3351 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3352 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3353 LOG(FATAL) << DebugName() << " is not defined for double values";
3354 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003355 }
3356
3357 DECLARE_INSTRUCTION(Below);
3358
3359 IfCondition GetCondition() const OVERRIDE {
3360 return kCondB;
3361 }
3362
3363 IfCondition GetOppositeCondition() const OVERRIDE {
3364 return kCondAE;
3365 }
3366
3367 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003368 template <typename T> bool Compute(T x, T y) const {
3369 return MakeUnsigned(x) < MakeUnsigned(y);
3370 }
Aart Bike9f37602015-10-09 11:15:55 -07003371
3372 DISALLOW_COPY_AND_ASSIGN(HBelow);
3373};
3374
Vladimir Markofcb503c2016-05-18 12:48:17 +01003375class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003376 public:
3377 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3378 : HCondition(first, second, dex_pc) {}
3379
3380 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003381 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003382 }
3383 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003384 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3385 }
3386 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3387 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3388 LOG(FATAL) << DebugName() << " is not defined for float values";
3389 UNREACHABLE();
3390 }
3391 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3392 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3393 LOG(FATAL) << DebugName() << " is not defined for double values";
3394 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003395 }
3396
3397 DECLARE_INSTRUCTION(BelowOrEqual);
3398
3399 IfCondition GetCondition() const OVERRIDE {
3400 return kCondBE;
3401 }
3402
3403 IfCondition GetOppositeCondition() const OVERRIDE {
3404 return kCondA;
3405 }
3406
3407 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003408 template <typename T> bool Compute(T x, T y) const {
3409 return MakeUnsigned(x) <= MakeUnsigned(y);
3410 }
Aart Bike9f37602015-10-09 11:15:55 -07003411
3412 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3413};
3414
Vladimir Markofcb503c2016-05-18 12:48:17 +01003415class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003416 public:
3417 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3418 : HCondition(first, second, dex_pc) {}
3419
3420 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003421 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003422 }
3423 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3425 }
3426 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3427 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3428 LOG(FATAL) << DebugName() << " is not defined for float values";
3429 UNREACHABLE();
3430 }
3431 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3432 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3433 LOG(FATAL) << DebugName() << " is not defined for double values";
3434 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003435 }
3436
3437 DECLARE_INSTRUCTION(Above);
3438
3439 IfCondition GetCondition() const OVERRIDE {
3440 return kCondA;
3441 }
3442
3443 IfCondition GetOppositeCondition() const OVERRIDE {
3444 return kCondBE;
3445 }
3446
3447 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003448 template <typename T> bool Compute(T x, T y) const {
3449 return MakeUnsigned(x) > MakeUnsigned(y);
3450 }
Aart Bike9f37602015-10-09 11:15:55 -07003451
3452 DISALLOW_COPY_AND_ASSIGN(HAbove);
3453};
3454
Vladimir Markofcb503c2016-05-18 12:48:17 +01003455class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003456 public:
3457 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3458 : HCondition(first, second, dex_pc) {}
3459
3460 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003461 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003462 }
3463 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003464 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3465 }
3466 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3467 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3468 LOG(FATAL) << DebugName() << " is not defined for float values";
3469 UNREACHABLE();
3470 }
3471 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3472 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3473 LOG(FATAL) << DebugName() << " is not defined for double values";
3474 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003475 }
3476
3477 DECLARE_INSTRUCTION(AboveOrEqual);
3478
3479 IfCondition GetCondition() const OVERRIDE {
3480 return kCondAE;
3481 }
3482
3483 IfCondition GetOppositeCondition() const OVERRIDE {
3484 return kCondB;
3485 }
3486
3487 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003488 template <typename T> bool Compute(T x, T y) const {
3489 return MakeUnsigned(x) >= MakeUnsigned(y);
3490 }
Aart Bike9f37602015-10-09 11:15:55 -07003491
3492 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3493};
Dave Allison20dfc792014-06-16 20:44:29 -07003494
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003495// Instruction to check how two inputs compare to each other.
3496// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003497class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003498 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003499 // Note that `comparison_type` is the type of comparison performed
3500 // between the comparison's inputs, not the type of the instantiated
3501 // HCompare instruction (which is always Primitive::kPrimInt).
3502 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003503 HInstruction* first,
3504 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003505 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003506 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003507 : HBinaryOperation(Primitive::kPrimInt,
3508 first,
3509 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003510 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003511 dex_pc) {
3512 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003513 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3514 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003515 }
3516
Roland Levillain9867bc72015-08-05 10:21:34 +01003517 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003518 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3519
3520 template <typename T>
3521 int32_t ComputeFP(T x, T y) const {
3522 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3523 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3524 // Handle the bias.
3525 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3526 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003527
Roland Levillain9867bc72015-08-05 10:21:34 +01003528 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003529 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3530 // reach this code path when processing a freshly built HIR
3531 // graph. However HCompare integer instructions can be synthesized
3532 // by the instruction simplifier to implement IntegerCompare and
3533 // IntegerSignum intrinsics, so we have to handle this case.
3534 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003535 }
3536 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003537 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3538 }
3539 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3540 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3541 }
3542 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3543 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003544 }
3545
Calin Juravleddb7df22014-11-25 20:56:51 +00003546 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003547 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003548 }
3549
Vladimir Markoa1de9182016-02-25 11:37:38 +00003550 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003551
Roland Levillain31dd3d62016-02-16 12:21:02 +00003552 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003553 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003554 bool IsGtBias() const {
3555 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003556 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003557 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003558
Roland Levillain1693a1f2016-03-15 14:57:31 +00003559 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3560 // Comparisons do not require a runtime call in any back end.
3561 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003562 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003563
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003564 DECLARE_INSTRUCTION(Compare);
3565
Roland Levillain31dd3d62016-02-16 12:21:02 +00003566 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003567 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3568 static constexpr size_t kFieldComparisonBiasSize =
3569 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3570 static constexpr size_t kNumberOfComparePackedBits =
3571 kFieldComparisonBias + kFieldComparisonBiasSize;
3572 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3573 using ComparisonBiasField =
3574 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3575
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 // Return an integer constant containing the result of a comparison evaluated at compile time.
3577 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3578 DCHECK(value == -1 || value == 0 || value == 1) << value;
3579 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3580 }
3581
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003582 private:
3583 DISALLOW_COPY_AND_ASSIGN(HCompare);
3584};
3585
Vladimir Markofcb503c2016-05-18 12:48:17 +01003586class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003587 public:
3588 HNewInstance(HInstruction* cls,
3589 HCurrentMethod* current_method,
3590 uint32_t dex_pc,
3591 uint16_t type_index,
3592 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003593 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003594 bool finalizable,
3595 QuickEntrypointEnum entrypoint)
3596 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3597 type_index_(type_index),
3598 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003599 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003600 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003601 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003602 SetRawInputAt(0, cls);
3603 SetRawInputAt(1, current_method);
3604 }
3605
3606 uint16_t GetTypeIndex() const { return type_index_; }
3607 const DexFile& GetDexFile() const { return dex_file_; }
3608
3609 // Calls runtime so needs an environment.
3610 bool NeedsEnvironment() const OVERRIDE { return true; }
3611
Mingyao Yang062157f2016-03-02 10:15:36 -08003612 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3613 bool CanThrow() const OVERRIDE { return true; }
3614
3615 // Needs to call into runtime to make sure it's instantiable/accessible.
3616 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003617
Vladimir Markoa1de9182016-02-25 11:37:38 +00003618 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003619
3620 bool CanBeNull() const OVERRIDE { return false; }
3621
3622 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3623
3624 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3625 entrypoint_ = entrypoint;
3626 }
3627
3628 bool IsStringAlloc() const;
3629
3630 DECLARE_INSTRUCTION(NewInstance);
3631
3632 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003633 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3634 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003635 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3636 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3637 "Too many packed fields.");
3638
David Brazdil6de19382016-01-08 17:37:10 +00003639 const uint16_t type_index_;
3640 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003641 QuickEntrypointEnum entrypoint_;
3642
3643 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3644};
3645
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003646enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003647#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3648 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003649#include "intrinsics_list.h"
3650 kNone,
3651 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3652#undef INTRINSICS_LIST
3653#undef OPTIMIZING_INTRINSICS
3654};
3655std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3656
Agi Csaki05f20562015-08-19 14:58:14 -07003657enum IntrinsicNeedsEnvironmentOrCache {
3658 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3659 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003660};
3661
Aart Bik5d75afe2015-12-14 11:57:01 -08003662enum IntrinsicSideEffects {
3663 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3664 kReadSideEffects, // Intrinsic may read heap memory.
3665 kWriteSideEffects, // Intrinsic may write heap memory.
3666 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3667};
3668
3669enum IntrinsicExceptions {
3670 kNoThrow, // Intrinsic does not throw any exceptions.
3671 kCanThrow // Intrinsic may throw exceptions.
3672};
3673
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003674class HInvoke : public HInstruction {
3675 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003676 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003677
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003678 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003679
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003680 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003681 SetRawInputAt(index, argument);
3682 }
3683
Roland Levillain3e3d7332015-04-28 11:00:54 +01003684 // Return the number of arguments. This number can be lower than
3685 // the number of inputs returned by InputCount(), as some invoke
3686 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3687 // inputs at the end of their list of inputs.
3688 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3689
Vladimir Markoa1de9182016-02-25 11:37:38 +00003690 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003691
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003692 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003693 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003694
Vladimir Markoa1de9182016-02-25 11:37:38 +00003695 InvokeType GetOriginalInvokeType() const {
3696 return GetPackedField<OriginalInvokeTypeField>();
3697 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003698
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003699 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003700 return intrinsic_;
3701 }
3702
Aart Bik5d75afe2015-12-14 11:57:01 -08003703 void SetIntrinsic(Intrinsics intrinsic,
3704 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3705 IntrinsicSideEffects side_effects,
3706 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003707
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003708 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003709 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003710 }
3711
Vladimir Markoa1de9182016-02-25 11:37:38 +00003712 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003713
3714 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3715
3716 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3717 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3718 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003719
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003720 uint32_t* GetIntrinsicOptimizations() {
3721 return &intrinsic_optimizations_;
3722 }
3723
3724 const uint32_t* GetIntrinsicOptimizations() const {
3725 return &intrinsic_optimizations_;
3726 }
3727
3728 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3729
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003730 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003731
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003732 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003733 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3734 static constexpr size_t kFieldOriginalInvokeTypeSize =
3735 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3736 static constexpr size_t kFieldReturnType =
3737 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3738 static constexpr size_t kFieldReturnTypeSize =
3739 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3740 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3741 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3742 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3743 using OriginalInvokeTypeField =
3744 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3745 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3746
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003747 HInvoke(ArenaAllocator* arena,
3748 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003749 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003750 Primitive::Type return_type,
3751 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003752 uint32_t dex_method_index,
3753 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003754 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003755 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003756 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003757 inputs_(number_of_arguments + number_of_other_inputs,
3758 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003759 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003760 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003761 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003762 SetPackedField<ReturnTypeField>(return_type);
3763 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3764 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003765 }
3766
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003767 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003768 return inputs_[index];
3769 }
3770
David Brazdil1abb4192015-02-17 18:33:36 +00003771 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003772 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003773 }
3774
Vladimir Markoa1de9182016-02-25 11:37:38 +00003775 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003776
Roland Levillain3e3d7332015-04-28 11:00:54 +01003777 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003778 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003779 const uint32_t dex_method_index_;
3780 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003781
3782 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3783 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003784
3785 private:
3786 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3787};
3788
Vladimir Markofcb503c2016-05-18 12:48:17 +01003789class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003790 public:
3791 HInvokeUnresolved(ArenaAllocator* arena,
3792 uint32_t number_of_arguments,
3793 Primitive::Type return_type,
3794 uint32_t dex_pc,
3795 uint32_t dex_method_index,
3796 InvokeType invoke_type)
3797 : HInvoke(arena,
3798 number_of_arguments,
3799 0u /* number_of_other_inputs */,
3800 return_type,
3801 dex_pc,
3802 dex_method_index,
3803 invoke_type) {
3804 }
3805
3806 DECLARE_INSTRUCTION(InvokeUnresolved);
3807
3808 private:
3809 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3810};
3811
Vladimir Markofcb503c2016-05-18 12:48:17 +01003812class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003813 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003814 // Requirements of this method call regarding the class
3815 // initialization (clinit) check of its declaring class.
3816 enum class ClinitCheckRequirement {
3817 kNone, // Class already initialized.
3818 kExplicit, // Static call having explicit clinit check as last input.
3819 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003820 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003821 };
3822
Vladimir Marko58155012015-08-19 12:49:41 +00003823 // Determines how to load the target ArtMethod*.
3824 enum class MethodLoadKind {
3825 // Use a String init ArtMethod* loaded from Thread entrypoints.
3826 kStringInit,
3827
3828 // Use the method's own ArtMethod* loaded by the register allocator.
3829 kRecursive,
3830
3831 // Use ArtMethod* at a known address, embed the direct address in the code.
3832 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3833 kDirectAddress,
3834
3835 // Use ArtMethod* at an address that will be known at link time, embed the direct
3836 // address in the code. If the image is relocatable, emit .patch_oat entry.
3837 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3838 // the image relocatable or not.
3839 kDirectAddressWithFixup,
3840
Vladimir Markoa1de9182016-02-25 11:37:38 +00003841 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003842 // Used when we need to use the dex cache, for example for invoke-static that
3843 // may cause class initialization (the entry may point to a resolution method),
3844 // and we know that we can access the dex cache arrays using a PC-relative load.
3845 kDexCachePcRelative,
3846
3847 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3848 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3849 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3850 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3851 kDexCacheViaMethod,
3852 };
3853
3854 // Determines the location of the code pointer.
3855 enum class CodePtrLocation {
3856 // Recursive call, use local PC-relative call instruction.
3857 kCallSelf,
3858
3859 // Use PC-relative call instruction patched at link time.
3860 // Used for calls within an oat file, boot->boot or app->app.
3861 kCallPCRelative,
3862
3863 // Call to a known target address, embed the direct address in code.
3864 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3865 kCallDirect,
3866
3867 // Call to a target address that will be known at link time, embed the direct
3868 // address in code. If the image is relocatable, emit .patch_oat entry.
3869 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3870 // the image relocatable or not.
3871 kCallDirectWithFixup,
3872
3873 // Use code pointer from the ArtMethod*.
3874 // Used when we don't know the target code. This is also the last-resort-kind used when
3875 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3876 kCallArtMethod,
3877 };
3878
3879 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003880 MethodLoadKind method_load_kind;
3881 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003882 // The method load data holds
3883 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3884 // Note that there are multiple string init methods, each having its own offset.
3885 // - the method address for kDirectAddress
3886 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003887 uint64_t method_load_data;
3888 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003889 };
3890
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003891 HInvokeStaticOrDirect(ArenaAllocator* arena,
3892 uint32_t number_of_arguments,
3893 Primitive::Type return_type,
3894 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003895 uint32_t method_index,
3896 MethodReference target_method,
3897 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003898 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003899 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003900 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003901 : HInvoke(arena,
3902 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003903 // There is potentially one extra argument for the HCurrentMethod node, and
3904 // potentially one other if the clinit check is explicit, and potentially
3905 // one other if the method is a string factory.
3906 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003907 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003908 return_type,
3909 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003910 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003911 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003912 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003913 dispatch_info_(dispatch_info) {
3914 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3915 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3916 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003917
Vladimir Markodc151b22015-10-15 18:02:30 +01003918 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003919 bool had_current_method_input = HasCurrentMethodInput();
3920 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3921
3922 // Using the current method is the default and once we find a better
3923 // method load kind, we should not go back to using the current method.
3924 DCHECK(had_current_method_input || !needs_current_method_input);
3925
3926 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003927 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3928 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003929 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003930 dispatch_info_ = dispatch_info;
3931 }
3932
Vladimir Markoc53c0792015-11-19 15:48:33 +00003933 void AddSpecialInput(HInstruction* input) {
3934 // We allow only one special input.
3935 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3936 DCHECK(InputCount() == GetSpecialInputIndex() ||
3937 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3938 InsertInputAt(GetSpecialInputIndex(), input);
3939 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003940
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003941 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003942 // We access the method via the dex cache so we can't do an implicit null check.
3943 // TODO: for intrinsics we can generate implicit null checks.
3944 return false;
3945 }
3946
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003947 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003948 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003949 }
3950
Vladimir Markoc53c0792015-11-19 15:48:33 +00003951 // Get the index of the special input, if any.
3952 //
David Brazdil6de19382016-01-08 17:37:10 +00003953 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3954 // method pointer; otherwise there may be one platform-specific special input,
3955 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003956 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003957 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003958
Vladimir Markoa1de9182016-02-25 11:37:38 +00003959 InvokeType GetOptimizedInvokeType() const {
3960 return GetPackedField<OptimizedInvokeTypeField>();
3961 }
3962
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003963 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003964 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003965 }
3966
Vladimir Marko58155012015-08-19 12:49:41 +00003967 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3968 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3969 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003970 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003971 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003972 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003973 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003974 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3975 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003976 bool HasCurrentMethodInput() const {
3977 // This function can be called only after the invoke has been fully initialized by the builder.
3978 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003979 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003980 return true;
3981 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003982 DCHECK(InputCount() == GetSpecialInputIndex() ||
3983 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003984 return false;
3985 }
3986 }
Vladimir Marko58155012015-08-19 12:49:41 +00003987 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3988 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003989 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003990
3991 int32_t GetStringInitOffset() const {
3992 DCHECK(IsStringInit());
3993 return dispatch_info_.method_load_data;
3994 }
3995
3996 uint64_t GetMethodAddress() const {
3997 DCHECK(HasMethodAddress());
3998 return dispatch_info_.method_load_data;
3999 }
4000
4001 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004002 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004003 return dispatch_info_.method_load_data;
4004 }
4005
4006 uint64_t GetDirectCodePtr() const {
4007 DCHECK(HasDirectCodePtr());
4008 return dispatch_info_.direct_code_ptr;
4009 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004010
Vladimir Markoa1de9182016-02-25 11:37:38 +00004011 ClinitCheckRequirement GetClinitCheckRequirement() const {
4012 return GetPackedField<ClinitCheckRequirementField>();
4013 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004014
Roland Levillain4c0eb422015-04-24 16:43:49 +01004015 // Is this instruction a call to a static method?
4016 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004017 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004018 }
4019
Vladimir Markofbb184a2015-11-13 14:47:00 +00004020 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4021 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4022 // instruction; only relevant for static calls with explicit clinit check.
4023 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004024 DCHECK(IsStaticWithExplicitClinitCheck());
4025 size_t last_input_index = InputCount() - 1;
4026 HInstruction* last_input = InputAt(last_input_index);
4027 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004028 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004029 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004030 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004031 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004032 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004033 }
4034
4035 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004036 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004037 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004038 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004039 }
4040
4041 // Is this a call to a static method whose declaring class has an
4042 // implicit intialization check requirement?
4043 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004044 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004045 }
4046
Vladimir Markob554b5a2015-11-06 12:57:55 +00004047 // Does this method load kind need the current method as an input?
4048 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4049 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4050 }
4051
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004052 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004053
Roland Levillain4c0eb422015-04-24 16:43:49 +01004054 protected:
4055 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4056 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4057 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4058 HInstruction* input = input_record.GetInstruction();
4059 // `input` is the last input of a static invoke marked as having
4060 // an explicit clinit check. It must either be:
4061 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4062 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4063 DCHECK(input != nullptr);
4064 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4065 }
4066 return input_record;
4067 }
4068
Vladimir Markoc53c0792015-11-19 15:48:33 +00004069 void InsertInputAt(size_t index, HInstruction* input);
4070 void RemoveInputAt(size_t index);
4071
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004072 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004073 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4074 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4075 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4076 static constexpr size_t kFieldClinitCheckRequirement =
4077 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4078 static constexpr size_t kFieldClinitCheckRequirementSize =
4079 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4080 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4081 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4082 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4083 "Too many packed fields.");
4084 using OptimizedInvokeTypeField =
4085 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4086 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4087 kFieldClinitCheckRequirement,
4088 kFieldClinitCheckRequirementSize>;
4089
Vladimir Marko58155012015-08-19 12:49:41 +00004090 // The target method may refer to different dex file or method index than the original
4091 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4092 // in derived class to increase visibility.
4093 MethodReference target_method_;
4094 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004095
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004096 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004097};
Vladimir Markof64242a2015-12-01 14:58:23 +00004098std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004099std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004100
Vladimir Markofcb503c2016-05-18 12:48:17 +01004101class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004102 public:
4103 HInvokeVirtual(ArenaAllocator* arena,
4104 uint32_t number_of_arguments,
4105 Primitive::Type return_type,
4106 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004107 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004108 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004109 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004110 vtable_index_(vtable_index) {}
4111
Calin Juravle641547a2015-04-21 22:08:51 +01004112 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004113 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004114 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004115 }
4116
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004117 uint32_t GetVTableIndex() const { return vtable_index_; }
4118
4119 DECLARE_INSTRUCTION(InvokeVirtual);
4120
4121 private:
4122 const uint32_t vtable_index_;
4123
4124 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4125};
4126
Vladimir Markofcb503c2016-05-18 12:48:17 +01004127class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004128 public:
4129 HInvokeInterface(ArenaAllocator* arena,
4130 uint32_t number_of_arguments,
4131 Primitive::Type return_type,
4132 uint32_t dex_pc,
4133 uint32_t dex_method_index,
4134 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004135 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004136 imt_index_(imt_index) {}
4137
Calin Juravle641547a2015-04-21 22:08:51 +01004138 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004139 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004140 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004141 }
4142
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004143 uint32_t GetImtIndex() const { return imt_index_; }
4144 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4145
4146 DECLARE_INSTRUCTION(InvokeInterface);
4147
4148 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004149 const uint32_t imt_index_;
4150
4151 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4152};
4153
Vladimir Markofcb503c2016-05-18 12:48:17 +01004154class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004155 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004156 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004157 : HUnaryOperation(result_type, input, dex_pc) {
4158 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4159 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004160
Roland Levillain9867bc72015-08-05 10:21:34 +01004161 template <typename T> T Compute(T x) const { return -x; }
4162
4163 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004164 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004165 }
4166 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004167 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004168 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004169 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4170 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4171 }
4172 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4173 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4174 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004175
Roland Levillain88cb1752014-10-20 16:36:47 +01004176 DECLARE_INSTRUCTION(Neg);
4177
4178 private:
4179 DISALLOW_COPY_AND_ASSIGN(HNeg);
4180};
4181
Vladimir Markofcb503c2016-05-18 12:48:17 +01004182class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004183 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004184 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004185 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004186 uint32_t dex_pc,
4187 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004188 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004189 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004190 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004191 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004192 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004193 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004194 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004195 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004196 }
4197
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004198 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004199 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004200
4201 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004202 bool NeedsEnvironment() const OVERRIDE { return true; }
4203
Mingyao Yang0c365e62015-03-31 15:09:29 -07004204 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4205 bool CanThrow() const OVERRIDE { return true; }
4206
Calin Juravle10e244f2015-01-26 18:54:32 +00004207 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004208
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004209 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4210
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004211 DECLARE_INSTRUCTION(NewArray);
4212
4213 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004214 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004215 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004216 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004217
4218 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4219};
4220
Vladimir Markofcb503c2016-05-18 12:48:17 +01004221class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004222 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004223 HAdd(Primitive::Type result_type,
4224 HInstruction* left,
4225 HInstruction* right,
4226 uint32_t dex_pc = kNoDexPc)
4227 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004228
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004229 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004230
Roland Levillain9867bc72015-08-05 10:21:34 +01004231 template <typename T> T Compute(T x, T y) const { return x + y; }
4232
4233 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004234 return GetBlock()->GetGraph()->GetIntConstant(
4235 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004236 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004237 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004238 return GetBlock()->GetGraph()->GetLongConstant(
4239 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004240 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004241 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4242 return GetBlock()->GetGraph()->GetFloatConstant(
4243 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4244 }
4245 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4246 return GetBlock()->GetGraph()->GetDoubleConstant(
4247 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4248 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004249
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004250 DECLARE_INSTRUCTION(Add);
4251
4252 private:
4253 DISALLOW_COPY_AND_ASSIGN(HAdd);
4254};
4255
Vladimir Markofcb503c2016-05-18 12:48:17 +01004256class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004257 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004258 HSub(Primitive::Type result_type,
4259 HInstruction* left,
4260 HInstruction* right,
4261 uint32_t dex_pc = kNoDexPc)
4262 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004263
Roland Levillain9867bc72015-08-05 10:21:34 +01004264 template <typename T> T Compute(T x, T y) const { return x - y; }
4265
4266 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004267 return GetBlock()->GetGraph()->GetIntConstant(
4268 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004269 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004270 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004271 return GetBlock()->GetGraph()->GetLongConstant(
4272 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004273 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004274 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4275 return GetBlock()->GetGraph()->GetFloatConstant(
4276 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4277 }
4278 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4279 return GetBlock()->GetGraph()->GetDoubleConstant(
4280 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4281 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004282
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004283 DECLARE_INSTRUCTION(Sub);
4284
4285 private:
4286 DISALLOW_COPY_AND_ASSIGN(HSub);
4287};
4288
Vladimir Markofcb503c2016-05-18 12:48:17 +01004289class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004290 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 HMul(Primitive::Type result_type,
4292 HInstruction* left,
4293 HInstruction* right,
4294 uint32_t dex_pc = kNoDexPc)
4295 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004296
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004297 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004298
Roland Levillain9867bc72015-08-05 10:21:34 +01004299 template <typename T> T Compute(T x, T y) const { return x * y; }
4300
4301 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004302 return GetBlock()->GetGraph()->GetIntConstant(
4303 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004304 }
4305 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004306 return GetBlock()->GetGraph()->GetLongConstant(
4307 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004308 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004309 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4310 return GetBlock()->GetGraph()->GetFloatConstant(
4311 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4312 }
4313 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4314 return GetBlock()->GetGraph()->GetDoubleConstant(
4315 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4316 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004317
4318 DECLARE_INSTRUCTION(Mul);
4319
4320 private:
4321 DISALLOW_COPY_AND_ASSIGN(HMul);
4322};
4323
Vladimir Markofcb503c2016-05-18 12:48:17 +01004324class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004325 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004326 HDiv(Primitive::Type result_type,
4327 HInstruction* left,
4328 HInstruction* right,
4329 uint32_t dex_pc)
4330 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004331
Roland Levillain9867bc72015-08-05 10:21:34 +01004332 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004333 T ComputeIntegral(T x, T y) const {
4334 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004335 // Our graph structure ensures we never have 0 for `y` during
4336 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004337 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004338 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004339 return (y == -1) ? -x : x / y;
4340 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004341
Roland Levillain31dd3d62016-02-16 12:21:02 +00004342 template <typename T>
4343 T ComputeFP(T x, T y) const {
4344 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4345 return x / y;
4346 }
4347
Roland Levillain9867bc72015-08-05 10:21:34 +01004348 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004349 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004350 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004351 }
4352 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004353 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004354 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4355 }
4356 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4357 return GetBlock()->GetGraph()->GetFloatConstant(
4358 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4359 }
4360 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4361 return GetBlock()->GetGraph()->GetDoubleConstant(
4362 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004363 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004364
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004365 static SideEffects SideEffectsForArchRuntimeCalls() {
4366 // The generated code can use a runtime call.
4367 return SideEffects::CanTriggerGC();
4368 }
4369
Calin Juravle7c4954d2014-10-28 16:57:40 +00004370 DECLARE_INSTRUCTION(Div);
4371
4372 private:
4373 DISALLOW_COPY_AND_ASSIGN(HDiv);
4374};
4375
Vladimir Markofcb503c2016-05-18 12:48:17 +01004376class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004377 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004378 HRem(Primitive::Type result_type,
4379 HInstruction* left,
4380 HInstruction* right,
4381 uint32_t dex_pc)
4382 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004383
Roland Levillain9867bc72015-08-05 10:21:34 +01004384 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004385 T ComputeIntegral(T x, T y) const {
4386 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004387 // Our graph structure ensures we never have 0 for `y` during
4388 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004389 DCHECK_NE(y, 0);
4390 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4391 return (y == -1) ? 0 : x % y;
4392 }
4393
Roland Levillain31dd3d62016-02-16 12:21:02 +00004394 template <typename T>
4395 T ComputeFP(T x, T y) const {
4396 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4397 return std::fmod(x, y);
4398 }
4399
Roland Levillain9867bc72015-08-05 10:21:34 +01004400 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004401 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004402 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004403 }
4404 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004405 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004406 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004407 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004408 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4409 return GetBlock()->GetGraph()->GetFloatConstant(
4410 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4411 }
4412 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4413 return GetBlock()->GetGraph()->GetDoubleConstant(
4414 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4415 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004416
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004417 static SideEffects SideEffectsForArchRuntimeCalls() {
4418 return SideEffects::CanTriggerGC();
4419 }
4420
Calin Juravlebacfec32014-11-14 15:54:36 +00004421 DECLARE_INSTRUCTION(Rem);
4422
4423 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004424 DISALLOW_COPY_AND_ASSIGN(HRem);
4425};
4426
Vladimir Markofcb503c2016-05-18 12:48:17 +01004427class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004428 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004429 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4430 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004431 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004432 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004433 SetRawInputAt(0, value);
4434 }
4435
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004436 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4437
Calin Juravled0d48522014-11-04 16:40:20 +00004438 bool CanBeMoved() const OVERRIDE { return true; }
4439
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004440 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004441 return true;
4442 }
4443
4444 bool NeedsEnvironment() const OVERRIDE { return true; }
4445 bool CanThrow() const OVERRIDE { return true; }
4446
Calin Juravled0d48522014-11-04 16:40:20 +00004447 DECLARE_INSTRUCTION(DivZeroCheck);
4448
4449 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004450 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4451};
4452
Vladimir Markofcb503c2016-05-18 12:48:17 +01004453class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004455 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004456 HInstruction* value,
4457 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004458 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004459 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4460 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4461 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004462 }
4463
Roland Levillain5b5b9312016-03-22 14:57:31 +00004464 template <typename T>
4465 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4466 return value << (distance & max_shift_distance);
4467 }
4468
4469 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004470 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004471 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004472 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004473 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004474 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004475 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004476 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004477 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4478 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4479 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4480 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004481 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004482 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4483 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004484 LOG(FATAL) << DebugName() << " is not defined for float values";
4485 UNREACHABLE();
4486 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004487 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4488 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004489 LOG(FATAL) << DebugName() << " is not defined for double values";
4490 UNREACHABLE();
4491 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004492
4493 DECLARE_INSTRUCTION(Shl);
4494
4495 private:
4496 DISALLOW_COPY_AND_ASSIGN(HShl);
4497};
4498
Vladimir Markofcb503c2016-05-18 12:48:17 +01004499class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004500 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004501 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 HInstruction* value,
4503 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004504 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004505 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4506 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4507 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 }
4509
Roland Levillain5b5b9312016-03-22 14:57:31 +00004510 template <typename T>
4511 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4512 return value >> (distance & max_shift_distance);
4513 }
4514
4515 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004517 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004519 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004520 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004521 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004522 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004523 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4524 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4525 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4526 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004527 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004528 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4529 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004530 LOG(FATAL) << DebugName() << " is not defined for float values";
4531 UNREACHABLE();
4532 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004533 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4534 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004535 LOG(FATAL) << DebugName() << " is not defined for double values";
4536 UNREACHABLE();
4537 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004538
4539 DECLARE_INSTRUCTION(Shr);
4540
4541 private:
4542 DISALLOW_COPY_AND_ASSIGN(HShr);
4543};
4544
Vladimir Markofcb503c2016-05-18 12:48:17 +01004545class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004546 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004547 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 HInstruction* value,
4549 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004550 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004551 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4552 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4553 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004554 }
4555
Roland Levillain5b5b9312016-03-22 14:57:31 +00004556 template <typename T>
4557 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4558 typedef typename std::make_unsigned<T>::type V;
4559 V ux = static_cast<V>(value);
4560 return static_cast<T>(ux >> (distance & max_shift_distance));
4561 }
4562
4563 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004564 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004565 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004566 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004567 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004568 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004569 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004570 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004571 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4572 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4573 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4574 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004575 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004576 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4577 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004578 LOG(FATAL) << DebugName() << " is not defined for float values";
4579 UNREACHABLE();
4580 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004581 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4582 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004583 LOG(FATAL) << DebugName() << " is not defined for double values";
4584 UNREACHABLE();
4585 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004586
4587 DECLARE_INSTRUCTION(UShr);
4588
4589 private:
4590 DISALLOW_COPY_AND_ASSIGN(HUShr);
4591};
4592
Vladimir Markofcb503c2016-05-18 12:48:17 +01004593class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004594 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004595 HAnd(Primitive::Type result_type,
4596 HInstruction* left,
4597 HInstruction* right,
4598 uint32_t dex_pc = kNoDexPc)
4599 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004600
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004601 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004602
Roland Levillaine53bd812016-02-24 14:54:18 +00004603 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004604
4605 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004606 return GetBlock()->GetGraph()->GetIntConstant(
4607 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004608 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004609 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004610 return GetBlock()->GetGraph()->GetLongConstant(
4611 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004612 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004613 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4614 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4615 LOG(FATAL) << DebugName() << " is not defined for float values";
4616 UNREACHABLE();
4617 }
4618 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4619 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4620 LOG(FATAL) << DebugName() << " is not defined for double values";
4621 UNREACHABLE();
4622 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004623
4624 DECLARE_INSTRUCTION(And);
4625
4626 private:
4627 DISALLOW_COPY_AND_ASSIGN(HAnd);
4628};
4629
Vladimir Markofcb503c2016-05-18 12:48:17 +01004630class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004631 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004632 HOr(Primitive::Type result_type,
4633 HInstruction* left,
4634 HInstruction* right,
4635 uint32_t dex_pc = kNoDexPc)
4636 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004637
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004638 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004639
Roland Levillaine53bd812016-02-24 14:54:18 +00004640 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004641
4642 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004643 return GetBlock()->GetGraph()->GetIntConstant(
4644 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004646 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004647 return GetBlock()->GetGraph()->GetLongConstant(
4648 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004649 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004650 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4651 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4652 LOG(FATAL) << DebugName() << " is not defined for float values";
4653 UNREACHABLE();
4654 }
4655 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4656 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4657 LOG(FATAL) << DebugName() << " is not defined for double values";
4658 UNREACHABLE();
4659 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004660
4661 DECLARE_INSTRUCTION(Or);
4662
4663 private:
4664 DISALLOW_COPY_AND_ASSIGN(HOr);
4665};
4666
Vladimir Markofcb503c2016-05-18 12:48:17 +01004667class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 HXor(Primitive::Type result_type,
4670 HInstruction* left,
4671 HInstruction* right,
4672 uint32_t dex_pc = kNoDexPc)
4673 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004674
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004675 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004676
Roland Levillaine53bd812016-02-24 14:54:18 +00004677 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004678
4679 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 return GetBlock()->GetGraph()->GetIntConstant(
4681 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004682 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004683 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004684 return GetBlock()->GetGraph()->GetLongConstant(
4685 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004686 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004687 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4688 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4689 LOG(FATAL) << DebugName() << " is not defined for float values";
4690 UNREACHABLE();
4691 }
4692 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4693 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4694 LOG(FATAL) << DebugName() << " is not defined for double values";
4695 UNREACHABLE();
4696 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004697
4698 DECLARE_INSTRUCTION(Xor);
4699
4700 private:
4701 DISALLOW_COPY_AND_ASSIGN(HXor);
4702};
4703
Vladimir Markofcb503c2016-05-18 12:48:17 +01004704class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004705 public:
4706 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004707 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004708 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4709 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004710 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004711
Roland Levillain5b5b9312016-03-22 14:57:31 +00004712 template <typename T>
4713 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4714 typedef typename std::make_unsigned<T>::type V;
4715 V ux = static_cast<V>(value);
4716 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004717 return static_cast<T>(ux);
4718 } else {
4719 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004720 return static_cast<T>(ux >> (distance & max_shift_value)) |
4721 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004722 }
4723 }
4724
Roland Levillain5b5b9312016-03-22 14:57:31 +00004725 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004726 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004727 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004728 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004729 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004730 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004731 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004732 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004733 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4734 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4735 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4736 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004738 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4739 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004740 LOG(FATAL) << DebugName() << " is not defined for float values";
4741 UNREACHABLE();
4742 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004743 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4744 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004745 LOG(FATAL) << DebugName() << " is not defined for double values";
4746 UNREACHABLE();
4747 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004748
4749 DECLARE_INSTRUCTION(Ror);
4750
4751 private:
4752 DISALLOW_COPY_AND_ASSIGN(HRor);
4753};
4754
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004755// The value of a parameter in this method. Its location depends on
4756// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004757class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004758 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004759 HParameterValue(const DexFile& dex_file,
4760 uint16_t type_index,
4761 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004762 Primitive::Type parameter_type,
4763 bool is_this = false)
4764 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004765 dex_file_(dex_file),
4766 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004767 index_(index) {
4768 SetPackedFlag<kFlagIsThis>(is_this);
4769 SetPackedFlag<kFlagCanBeNull>(!is_this);
4770 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004771
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004772 const DexFile& GetDexFile() const { return dex_file_; }
4773 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004774 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004775 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004776
Vladimir Markoa1de9182016-02-25 11:37:38 +00004777 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4778 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004779
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004780 DECLARE_INSTRUCTION(ParameterValue);
4781
4782 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004783 // Whether or not the parameter value corresponds to 'this' argument.
4784 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4785 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4786 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4787 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4788 "Too many packed fields.");
4789
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004790 const DexFile& dex_file_;
4791 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004792 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004793 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004794 const uint8_t index_;
4795
4796 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4797};
4798
Vladimir Markofcb503c2016-05-18 12:48:17 +01004799class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004800 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4802 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004803
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004804 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004805 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004806 return true;
4807 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004808
Roland Levillain9867bc72015-08-05 10:21:34 +01004809 template <typename T> T Compute(T x) const { return ~x; }
4810
4811 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004812 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004813 }
4814 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004815 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004816 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004817 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4818 LOG(FATAL) << DebugName() << " is not defined for float values";
4819 UNREACHABLE();
4820 }
4821 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4822 LOG(FATAL) << DebugName() << " is not defined for double values";
4823 UNREACHABLE();
4824 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004825
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004826 DECLARE_INSTRUCTION(Not);
4827
4828 private:
4829 DISALLOW_COPY_AND_ASSIGN(HNot);
4830};
4831
Vladimir Markofcb503c2016-05-18 12:48:17 +01004832class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004833 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004834 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4835 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004836
4837 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004838 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004839 return true;
4840 }
4841
Roland Levillain9867bc72015-08-05 10:21:34 +01004842 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004843 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004844 return !x;
4845 }
4846
Roland Levillain9867bc72015-08-05 10:21:34 +01004847 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004849 }
4850 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4851 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004852 UNREACHABLE();
4853 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004854 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4855 LOG(FATAL) << DebugName() << " is not defined for float values";
4856 UNREACHABLE();
4857 }
4858 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4859 LOG(FATAL) << DebugName() << " is not defined for double values";
4860 UNREACHABLE();
4861 }
David Brazdil66d126e2015-04-03 16:02:44 +01004862
4863 DECLARE_INSTRUCTION(BooleanNot);
4864
4865 private:
4866 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4867};
4868
Vladimir Markofcb503c2016-05-18 12:48:17 +01004869class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004870 public:
4871 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004872 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004873 : HExpression(result_type,
4874 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4875 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004876 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004877 // Invariant: We should never generate a conversion to a Boolean value.
4878 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004879 }
4880
4881 HInstruction* GetInput() const { return InputAt(0); }
4882 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4883 Primitive::Type GetResultType() const { return GetType(); }
4884
4885 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004886 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004887
Mark Mendelle82549b2015-05-06 10:55:34 -04004888 // Try to statically evaluate the conversion and return a HConstant
4889 // containing the result. If the input cannot be converted, return nullptr.
4890 HConstant* TryStaticEvaluation() const;
4891
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004892 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4893 Primitive::Type result_type) {
4894 // Some architectures may not require the 'GC' side effects, but at this point
4895 // in the compilation process we do not know what architecture we will
4896 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004897 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4898 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004899 return SideEffects::CanTriggerGC();
4900 }
4901 return SideEffects::None();
4902 }
4903
Roland Levillaindff1f282014-11-05 14:15:05 +00004904 DECLARE_INSTRUCTION(TypeConversion);
4905
4906 private:
4907 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4908};
4909
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004910static constexpr uint32_t kNoRegNumber = -1;
4911
Vladimir Markofcb503c2016-05-18 12:48:17 +01004912class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004913 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004914 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4915 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004916 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004917 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004918 SetRawInputAt(0, value);
4919 }
4920
Calin Juravle10e244f2015-01-26 18:54:32 +00004921 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004922 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004923 return true;
4924 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004925
Calin Juravle10e244f2015-01-26 18:54:32 +00004926 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004927
Calin Juravle10e244f2015-01-26 18:54:32 +00004928 bool CanThrow() const OVERRIDE { return true; }
4929
4930 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004931
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004932
4933 DECLARE_INSTRUCTION(NullCheck);
4934
4935 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004936 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4937};
4938
4939class FieldInfo : public ValueObject {
4940 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004941 FieldInfo(MemberOffset field_offset,
4942 Primitive::Type field_type,
4943 bool is_volatile,
4944 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004945 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004946 const DexFile& dex_file,
4947 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004948 : field_offset_(field_offset),
4949 field_type_(field_type),
4950 is_volatile_(is_volatile),
4951 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004952 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004953 dex_file_(dex_file),
4954 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004955
4956 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004957 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004958 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004959 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004960 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004961 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004962 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004963
4964 private:
4965 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004966 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004967 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004968 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004969 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004970 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004971 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004972};
4973
Vladimir Markofcb503c2016-05-18 12:48:17 +01004974class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004975 public:
4976 HInstanceFieldGet(HInstruction* value,
4977 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004978 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004979 bool is_volatile,
4980 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004981 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004982 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004983 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004984 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004985 : HExpression(field_type,
4986 SideEffects::FieldReadOfType(field_type, is_volatile),
4987 dex_pc),
4988 field_info_(field_offset,
4989 field_type,
4990 is_volatile,
4991 field_idx,
4992 declaring_class_def_index,
4993 dex_file,
4994 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004995 SetRawInputAt(0, value);
4996 }
4997
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004998 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004999
5000 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5001 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5002 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005003 }
5004
Calin Juravle641547a2015-04-21 22:08:51 +01005005 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5006 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005007 }
5008
5009 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005010 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5011 }
5012
Calin Juravle52c48962014-12-16 17:02:57 +00005013 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005014 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005015 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005016 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005017
5018 DECLARE_INSTRUCTION(InstanceFieldGet);
5019
5020 private:
5021 const FieldInfo field_info_;
5022
5023 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5024};
5025
Vladimir Markofcb503c2016-05-18 12:48:17 +01005026class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005027 public:
5028 HInstanceFieldSet(HInstruction* object,
5029 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005030 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005031 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005032 bool is_volatile,
5033 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005034 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005035 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005036 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005037 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005038 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5039 dex_pc),
5040 field_info_(field_offset,
5041 field_type,
5042 is_volatile,
5043 field_idx,
5044 declaring_class_def_index,
5045 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005046 dex_cache) {
5047 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005048 SetRawInputAt(0, object);
5049 SetRawInputAt(1, value);
5050 }
5051
Calin Juravle641547a2015-04-21 22:08:51 +01005052 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5053 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005054 }
5055
Calin Juravle52c48962014-12-16 17:02:57 +00005056 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005058 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005059 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005060 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005061 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5062 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005063
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005064 DECLARE_INSTRUCTION(InstanceFieldSet);
5065
5066 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005067 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5068 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5069 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5070 "Too many packed fields.");
5071
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005072 const FieldInfo field_info_;
5073
5074 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5075};
5076
Vladimir Markofcb503c2016-05-18 12:48:17 +01005077class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078 public:
Aart Bik18b36ab2016-04-13 16:41:35 -07005079 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type, uint32_t dex_pc)
5080 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 SetRawInputAt(0, array);
5082 SetRawInputAt(1, index);
5083 }
5084
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005085 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005086 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005087 return true;
5088 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005089 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005090 // TODO: We can be smarter here.
5091 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5092 // which generates the implicit null check. There are cases when these can be removed
5093 // to produce better code. If we ever add optimizations to do so we should allow an
5094 // implicit check here (as long as the address falls in the first page).
5095 return false;
5096 }
5097
David Brazdil4833f5a2015-12-16 10:37:39 +00005098 bool IsEquivalentOf(HArrayGet* other) const {
5099 bool result = (GetDexPc() == other->GetDexPc());
5100 if (kIsDebugBuild && result) {
5101 DCHECK_EQ(GetBlock(), other->GetBlock());
5102 DCHECK_EQ(GetArray(), other->GetArray());
5103 DCHECK_EQ(GetIndex(), other->GetIndex());
5104 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005105 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005106 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005107 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5108 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005109 }
5110 }
5111 return result;
5112 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005113
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005114 HInstruction* GetArray() const { return InputAt(0); }
5115 HInstruction* GetIndex() const { return InputAt(1); }
5116
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005117 DECLARE_INSTRUCTION(ArrayGet);
5118
5119 private:
5120 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5121};
5122
Vladimir Markofcb503c2016-05-18 12:48:17 +01005123class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 public:
5125 HArraySet(HInstruction* array,
5126 HInstruction* index,
5127 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005128 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005129 uint32_t dex_pc)
5130 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005131 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5132 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5133 SetPackedFlag<kFlagValueCanBeNull>(true);
5134 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005135 SetRawInputAt(0, array);
5136 SetRawInputAt(1, index);
5137 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005138 // Make a best guess now, may be refined during SSA building.
5139 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005140 }
5141
Calin Juravle77520bc2015-01-12 18:45:46 +00005142 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005143 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005144 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005145 }
5146
Mingyao Yang81014cb2015-06-02 03:16:27 -07005147 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005148 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005149
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005150 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005151 // TODO: Same as for ArrayGet.
5152 return false;
5153 }
5154
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005155 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005156 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005157 }
5158
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005159 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005160 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005161 }
5162
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005163 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005164 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005165 }
5166
Vladimir Markoa1de9182016-02-25 11:37:38 +00005167 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5168 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5169 bool StaticTypeOfArrayIsObjectArray() const {
5170 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5171 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005172
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005173 HInstruction* GetArray() const { return InputAt(0); }
5174 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005175 HInstruction* GetValue() const { return InputAt(2); }
5176
5177 Primitive::Type GetComponentType() const {
5178 // The Dex format does not type floating point index operations. Since the
5179 // `expected_component_type_` is set during building and can therefore not
5180 // be correct, we also check what is the value type. If it is a floating
5181 // point type, we must use that type.
5182 Primitive::Type value_type = GetValue()->GetType();
5183 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5184 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005185 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005186 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005187
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005188 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005189 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005190 }
5191
Aart Bik18b36ab2016-04-13 16:41:35 -07005192 void ComputeSideEffects() {
5193 Primitive::Type type = GetComponentType();
5194 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5195 SideEffectsForArchRuntimeCalls(type)));
5196 }
5197
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005198 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5199 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5200 }
5201
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005202 DECLARE_INSTRUCTION(ArraySet);
5203
5204 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005205 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5206 static constexpr size_t kFieldExpectedComponentTypeSize =
5207 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5208 static constexpr size_t kFlagNeedsTypeCheck =
5209 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5210 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005211 // Cached information for the reference_type_info_ so that codegen
5212 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005213 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5214 static constexpr size_t kNumberOfArraySetPackedBits =
5215 kFlagStaticTypeOfArrayIsObjectArray + 1;
5216 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5217 using ExpectedComponentTypeField =
5218 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219
5220 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5221};
5222
Vladimir Markofcb503c2016-05-18 12:48:17 +01005223class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005225 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005226 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005227 // Note that arrays do not change length, so the instruction does not
5228 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229 SetRawInputAt(0, array);
5230 }
5231
Calin Juravle77520bc2015-01-12 18:45:46 +00005232 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005233 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005234 return true;
5235 }
Calin Juravle641547a2015-04-21 22:08:51 +01005236 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5237 return obj == InputAt(0);
5238 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005239
Vladimir Markodce016e2016-04-28 13:10:02 +01005240 void MarkAsStringLength() { SetPackedFlag<kFlagIsStringLength>(); }
5241 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5242
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005243 DECLARE_INSTRUCTION(ArrayLength);
5244
5245 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005246 // We treat a String as an array, creating the HArrayLength from String.length()
5247 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5248 // determine whether a particular HArrayLength is actually a String.length() by
5249 // looking at the type of the input but that requires holding the mutator lock, so
5250 // we prefer to use a flag, so that code generators don't need to do the locking.
5251 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5252 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5253 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5254 "Too many packed fields.");
5255
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5257};
5258
Vladimir Markofcb503c2016-05-18 12:48:17 +01005259class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005261 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5262 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005264 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005265 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005266 SetRawInputAt(0, index);
5267 SetRawInputAt(1, length);
5268 }
5269
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005270 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005271 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005272 return true;
5273 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005274
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005275 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005277 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005278
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005279 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280
5281 DECLARE_INSTRUCTION(BoundsCheck);
5282
5283 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5285};
5286
Vladimir Markofcb503c2016-05-18 12:48:17 +01005287class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005288 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005289 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005290 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005291
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005292 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005293 return true;
5294 }
5295
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005296 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5297 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005298
5299 DECLARE_INSTRUCTION(SuspendCheck);
5300
5301 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005302 // Only used for code generation, in order to share the same slow path between back edges
5303 // of a same loop.
5304 SlowPathCode* slow_path_;
5305
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005306 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5307};
5308
David Srbecky0cf44932015-12-09 14:09:59 +00005309// Pseudo-instruction which provides the native debugger with mapping information.
5310// It ensures that we can generate line number and local variables at this point.
5311class HNativeDebugInfo : public HTemplateInstruction<0> {
5312 public:
5313 explicit HNativeDebugInfo(uint32_t dex_pc)
5314 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5315
5316 bool NeedsEnvironment() const OVERRIDE {
5317 return true;
5318 }
5319
5320 DECLARE_INSTRUCTION(NativeDebugInfo);
5321
5322 private:
5323 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5324};
5325
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005326/**
5327 * Instruction to load a Class object.
5328 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005329class HLoadClass FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005330 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005331 HLoadClass(HCurrentMethod* current_method,
5332 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005333 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005334 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005335 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005336 bool needs_access_check,
5337 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005338 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005339 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005340 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005341 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005342 // Referrers class should not need access check. We never inline unverified
5343 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005344 DCHECK(!is_referrers_class || !needs_access_check);
5345
5346 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5347 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5348 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5349 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005350 SetRawInputAt(0, current_method);
5351 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005352
5353 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005354
5355 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005356 // Note that we don't need to test for generate_clinit_check_.
5357 // Whether or not we need to generate the clinit check is processed in
5358 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005359 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005360 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005361 }
5362
5363 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5364
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005365 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005366 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005367
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005368 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005369 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005370 }
5371
Calin Juravle0ba218d2015-05-19 18:46:01 +01005372 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005373 // The entrypoint the code generator is going to call does not do
5374 // clinit of the class.
5375 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005376 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005377 }
5378
5379 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005380 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005381 (!IsReferrersClass() && !IsInDexCache()) ||
5382 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005383 }
5384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005385
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005386 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005387 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005388 }
5389
Calin Juravleacf735c2015-02-12 15:25:22 +00005390 ReferenceTypeInfo GetLoadedClassRTI() {
5391 return loaded_class_rti_;
5392 }
5393
5394 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5395 // Make sure we only set exact types (the loaded class should never be merged).
5396 DCHECK(rti.IsExact());
5397 loaded_class_rti_ = rti;
5398 }
5399
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005400 const DexFile& GetDexFile() { return dex_file_; }
5401
Vladimir Markoa1de9182016-02-25 11:37:38 +00005402 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005403
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005404 static SideEffects SideEffectsForArchRuntimeCalls() {
5405 return SideEffects::CanTriggerGC();
5406 }
5407
Vladimir Markoa1de9182016-02-25 11:37:38 +00005408 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5409 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5410 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5411 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005412
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005413 DECLARE_INSTRUCTION(LoadClass);
5414
5415 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005416 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5417 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5418 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005419 // Whether this instruction must generate the initialization check.
5420 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005421 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5422 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5423 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5424
5425 const uint16_t type_index_;
5426 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005427
Calin Juravleacf735c2015-02-12 15:25:22 +00005428 ReferenceTypeInfo loaded_class_rti_;
5429
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005430 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5431};
5432
Vladimir Markofcb503c2016-05-18 12:48:17 +01005433class HLoadString FINAL : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005434 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005435 // Determines how to load the String.
5436 enum class LoadKind {
5437 // Use boot image String* address that will be known at link time.
5438 // Used for boot image strings referenced by boot image code in non-PIC mode.
5439 kBootImageLinkTimeAddress,
5440
5441 // Use PC-relative boot image String* address that will be known at link time.
5442 // Used for boot image strings referenced by boot image code in PIC mode.
5443 kBootImageLinkTimePcRelative,
5444
5445 // Use a known boot image String* address, embedded in the code by the codegen.
5446 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5447 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5448 // GetIncludePatchInformation().
5449 kBootImageAddress,
5450
5451 // Load from the resolved strings array at an absolute address.
5452 // Used for strings outside the boot image referenced by JIT-compiled code.
5453 kDexCacheAddress,
5454
5455 // Load from resolved strings array in the dex cache using a PC-relative load.
5456 // Used for strings outside boot image when we know that we can access
5457 // the dex cache arrays using a PC-relative load.
5458 kDexCachePcRelative,
5459
5460 // Load from resolved strings array accessed through the class loaded from
5461 // the compiled method's own ArtMethod*. This is the default access type when
5462 // all other types are unavailable.
5463 kDexCacheViaMethod,
5464
5465 kLast = kDexCacheViaMethod
5466 };
5467
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005468 HLoadString(HCurrentMethod* current_method,
5469 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005470 const DexFile& dex_file,
5471 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005472 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005473 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005474 SetPackedFlag<kFlagIsInDexCache>(false);
5475 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5476 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005477 SetRawInputAt(0, current_method);
5478 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005479
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005480 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5481 DCHECK(HasAddress(load_kind));
5482 load_data_.address = address;
5483 SetLoadKindInternal(load_kind);
5484 }
5485
5486 void SetLoadKindWithStringReference(LoadKind load_kind,
5487 const DexFile& dex_file,
5488 uint32_t string_index) {
5489 DCHECK(HasStringReference(load_kind));
5490 load_data_.ref.dex_file = &dex_file;
5491 string_index_ = string_index;
5492 SetLoadKindInternal(load_kind);
5493 }
5494
5495 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5496 const DexFile& dex_file,
5497 uint32_t element_index) {
5498 DCHECK(HasDexCacheReference(load_kind));
5499 load_data_.ref.dex_file = &dex_file;
5500 load_data_.ref.dex_cache_element_index = element_index;
5501 SetLoadKindInternal(load_kind);
5502 }
5503
5504 LoadKind GetLoadKind() const {
5505 return GetPackedField<LoadKindField>();
5506 }
5507
5508 const DexFile& GetDexFile() const;
5509
5510 uint32_t GetStringIndex() const {
5511 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5512 return string_index_;
5513 }
5514
5515 uint32_t GetDexCacheElementOffset() const;
5516
5517 uint64_t GetAddress() const {
5518 DCHECK(HasAddress(GetLoadKind()));
5519 return load_data_.address;
5520 }
5521
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005522 bool CanBeMoved() const OVERRIDE { return true; }
5523
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005524 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005525
5526 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5527
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005528 // Will call the runtime if we need to load the string through
5529 // the dex cache and the string is not guaranteed to be there yet.
5530 bool NeedsEnvironment() const OVERRIDE {
5531 LoadKind load_kind = GetLoadKind();
5532 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5533 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5534 load_kind == LoadKind::kBootImageAddress) {
5535 return false;
5536 }
5537 return !IsInDexCache();
5538 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005539
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005540 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5541 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5542 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005543
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005544 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005545 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005546
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005547 static SideEffects SideEffectsForArchRuntimeCalls() {
5548 return SideEffects::CanTriggerGC();
5549 }
5550
Vladimir Markoa1de9182016-02-25 11:37:38 +00005551 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5552
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005553 void MarkInDexCache() {
5554 SetPackedFlag<kFlagIsInDexCache>(true);
5555 DCHECK(!NeedsEnvironment());
5556 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005557 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005558 }
5559
5560 size_t InputCount() const OVERRIDE {
5561 return (InputAt(0) != nullptr) ? 1u : 0u;
5562 }
5563
5564 void AddSpecialInput(HInstruction* special_input);
5565
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005566 DECLARE_INSTRUCTION(LoadString);
5567
5568 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005569 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005570 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5571 static constexpr size_t kFieldLoadKindSize =
5572 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5573 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005574 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005575 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005576
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005577 static bool HasStringReference(LoadKind load_kind) {
5578 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5579 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5580 load_kind == LoadKind::kDexCacheViaMethod;
5581 }
5582
5583 static bool HasAddress(LoadKind load_kind) {
5584 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5585 }
5586
5587 static bool HasDexCacheReference(LoadKind load_kind) {
5588 return load_kind == LoadKind::kDexCachePcRelative;
5589 }
5590
5591 void SetLoadKindInternal(LoadKind load_kind);
5592
5593 // String index serves also as the hash code and it's also needed for slow-paths,
5594 // so it must not be overwritten with other load data.
5595 uint32_t string_index_;
5596
5597 union {
5598 struct {
5599 const DexFile* dex_file; // For string reference and dex cache reference.
5600 uint32_t dex_cache_element_index; // Only for dex cache reference.
5601 } ref;
5602 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5603 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005604
5605 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5606};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005607std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5608
5609// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5610inline const DexFile& HLoadString::GetDexFile() const {
5611 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5612 << GetLoadKind();
5613 return *load_data_.ref.dex_file;
5614}
5615
5616// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5617inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5618 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5619 return load_data_.ref.dex_cache_element_index;
5620}
5621
5622// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5623inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5624 // The special input is used for PC-relative loads on some architectures.
5625 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5626 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5627 DCHECK(InputAt(0) == nullptr);
5628 SetRawInputAt(0u, special_input);
5629 special_input->AddUseAt(this, 0);
5630}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005631
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005632/**
5633 * Performs an initialization check on its Class object input.
5634 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005635class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005636 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005637 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005638 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005639 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005640 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5641 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005642 SetRawInputAt(0, constant);
5643 }
5644
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005645 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005646 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005647 return true;
5648 }
5649
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005650 bool NeedsEnvironment() const OVERRIDE {
5651 // May call runtime to initialize the class.
5652 return true;
5653 }
5654
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005655 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005656
5657 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5658
5659 DECLARE_INSTRUCTION(ClinitCheck);
5660
5661 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005662 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5663};
5664
Vladimir Markofcb503c2016-05-18 12:48:17 +01005665class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005666 public:
5667 HStaticFieldGet(HInstruction* cls,
5668 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005669 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005670 bool is_volatile,
5671 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005672 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005673 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005674 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005675 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005676 : HExpression(field_type,
5677 SideEffects::FieldReadOfType(field_type, is_volatile),
5678 dex_pc),
5679 field_info_(field_offset,
5680 field_type,
5681 is_volatile,
5682 field_idx,
5683 declaring_class_def_index,
5684 dex_file,
5685 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005686 SetRawInputAt(0, cls);
5687 }
5688
Calin Juravle52c48962014-12-16 17:02:57 +00005689
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005690 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005691
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005692 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005693 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5694 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005695 }
5696
5697 size_t ComputeHashCode() const OVERRIDE {
5698 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5699 }
5700
Calin Juravle52c48962014-12-16 17:02:57 +00005701 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005702 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5703 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005704 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005705
5706 DECLARE_INSTRUCTION(StaticFieldGet);
5707
5708 private:
5709 const FieldInfo field_info_;
5710
5711 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5712};
5713
Vladimir Markofcb503c2016-05-18 12:48:17 +01005714class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005715 public:
5716 HStaticFieldSet(HInstruction* cls,
5717 HInstruction* value,
5718 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005719 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005720 bool is_volatile,
5721 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005722 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005723 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005724 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005725 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005726 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5727 dex_pc),
5728 field_info_(field_offset,
5729 field_type,
5730 is_volatile,
5731 field_idx,
5732 declaring_class_def_index,
5733 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005734 dex_cache) {
5735 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005736 SetRawInputAt(0, cls);
5737 SetRawInputAt(1, value);
5738 }
5739
Calin Juravle52c48962014-12-16 17:02:57 +00005740 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005741 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5742 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005743 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005744
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005745 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005746 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5747 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005748
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005749 DECLARE_INSTRUCTION(StaticFieldSet);
5750
5751 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005752 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5753 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5754 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5755 "Too many packed fields.");
5756
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005757 const FieldInfo field_info_;
5758
5759 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5760};
5761
Vladimir Markofcb503c2016-05-18 12:48:17 +01005762class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005763 public:
5764 HUnresolvedInstanceFieldGet(HInstruction* obj,
5765 Primitive::Type field_type,
5766 uint32_t field_index,
5767 uint32_t dex_pc)
5768 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5769 field_index_(field_index) {
5770 SetRawInputAt(0, obj);
5771 }
5772
5773 bool NeedsEnvironment() const OVERRIDE { return true; }
5774 bool CanThrow() const OVERRIDE { return true; }
5775
5776 Primitive::Type GetFieldType() const { return GetType(); }
5777 uint32_t GetFieldIndex() const { return field_index_; }
5778
5779 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5780
5781 private:
5782 const uint32_t field_index_;
5783
5784 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5785};
5786
Vladimir Markofcb503c2016-05-18 12:48:17 +01005787class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005788 public:
5789 HUnresolvedInstanceFieldSet(HInstruction* obj,
5790 HInstruction* value,
5791 Primitive::Type field_type,
5792 uint32_t field_index,
5793 uint32_t dex_pc)
5794 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005795 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005796 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005797 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005798 SetRawInputAt(0, obj);
5799 SetRawInputAt(1, value);
5800 }
5801
5802 bool NeedsEnvironment() const OVERRIDE { return true; }
5803 bool CanThrow() const OVERRIDE { return true; }
5804
Vladimir Markoa1de9182016-02-25 11:37:38 +00005805 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005806 uint32_t GetFieldIndex() const { return field_index_; }
5807
5808 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5809
5810 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005811 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5812 static constexpr size_t kFieldFieldTypeSize =
5813 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5814 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5815 kFieldFieldType + kFieldFieldTypeSize;
5816 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5817 "Too many packed fields.");
5818 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5819
Calin Juravlee460d1d2015-09-29 04:52:17 +01005820 const uint32_t field_index_;
5821
5822 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5823};
5824
Vladimir Markofcb503c2016-05-18 12:48:17 +01005825class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005826 public:
5827 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5828 uint32_t field_index,
5829 uint32_t dex_pc)
5830 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5831 field_index_(field_index) {
5832 }
5833
5834 bool NeedsEnvironment() const OVERRIDE { return true; }
5835 bool CanThrow() const OVERRIDE { return true; }
5836
5837 Primitive::Type GetFieldType() const { return GetType(); }
5838 uint32_t GetFieldIndex() const { return field_index_; }
5839
5840 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5841
5842 private:
5843 const uint32_t field_index_;
5844
5845 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5846};
5847
Vladimir Markofcb503c2016-05-18 12:48:17 +01005848class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005849 public:
5850 HUnresolvedStaticFieldSet(HInstruction* value,
5851 Primitive::Type field_type,
5852 uint32_t field_index,
5853 uint32_t dex_pc)
5854 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005855 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005856 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005857 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005858 SetRawInputAt(0, value);
5859 }
5860
5861 bool NeedsEnvironment() const OVERRIDE { return true; }
5862 bool CanThrow() const OVERRIDE { return true; }
5863
Vladimir Markoa1de9182016-02-25 11:37:38 +00005864 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005865 uint32_t GetFieldIndex() const { return field_index_; }
5866
5867 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5868
5869 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005870 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5871 static constexpr size_t kFieldFieldTypeSize =
5872 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5873 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5874 kFieldFieldType + kFieldFieldTypeSize;
5875 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5876 "Too many packed fields.");
5877 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5878
Calin Juravlee460d1d2015-09-29 04:52:17 +01005879 const uint32_t field_index_;
5880
5881 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5882};
5883
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005884// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005885class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005886 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005887 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5888 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005889
David Brazdilbbd733e2015-08-18 17:48:17 +01005890 bool CanBeNull() const OVERRIDE { return false; }
5891
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005892 DECLARE_INSTRUCTION(LoadException);
5893
5894 private:
5895 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5896};
5897
David Brazdilcb1c0552015-08-04 16:22:25 +01005898// Implicit part of move-exception which clears thread-local exception storage.
5899// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005900class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01005901 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005902 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5903 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005904
5905 DECLARE_INSTRUCTION(ClearException);
5906
5907 private:
5908 DISALLOW_COPY_AND_ASSIGN(HClearException);
5909};
5910
Vladimir Markofcb503c2016-05-18 12:48:17 +01005911class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005912 public:
5913 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005914 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005915 SetRawInputAt(0, exception);
5916 }
5917
5918 bool IsControlFlow() const OVERRIDE { return true; }
5919
5920 bool NeedsEnvironment() const OVERRIDE { return true; }
5921
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005922 bool CanThrow() const OVERRIDE { return true; }
5923
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005924
5925 DECLARE_INSTRUCTION(Throw);
5926
5927 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005928 DISALLOW_COPY_AND_ASSIGN(HThrow);
5929};
5930
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005931/**
5932 * Implementation strategies for the code generator of a HInstanceOf
5933 * or `HCheckCast`.
5934 */
5935enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005936 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005937 kExactCheck, // Can do a single class compare.
5938 kClassHierarchyCheck, // Can just walk the super class chain.
5939 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5940 kInterfaceCheck, // No optimization yet when checking against an interface.
5941 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005942 kArrayCheck, // No optimization yet when checking against a generic array.
5943 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005944};
5945
Roland Levillain86503782016-02-11 19:07:30 +00005946std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5947
Vladimir Markofcb503c2016-05-18 12:48:17 +01005948class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005949 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005950 HInstanceOf(HInstruction* object,
5951 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005952 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005953 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005954 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005955 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005956 dex_pc) {
5957 SetPackedField<TypeCheckKindField>(check_kind);
5958 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005959 SetRawInputAt(0, object);
5960 SetRawInputAt(1, constant);
5961 }
5962
5963 bool CanBeMoved() const OVERRIDE { return true; }
5964
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005965 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005966 return true;
5967 }
5968
5969 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005970 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005971 }
5972
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005973 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005974 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5975 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5976 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5977 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005978
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005979 static bool CanCallRuntime(TypeCheckKind check_kind) {
5980 // Mips currently does runtime calls for any other checks.
5981 return check_kind != TypeCheckKind::kExactCheck;
5982 }
5983
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005984 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005985 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005986 }
5987
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005988 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005989
5990 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005991 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5992 static constexpr size_t kFieldTypeCheckKindSize =
5993 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5994 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5995 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5996 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5997 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005998
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005999 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6000};
6001
Vladimir Markofcb503c2016-05-18 12:48:17 +01006002class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006003 public:
David Brazdilf5552582015-12-27 13:36:12 +00006004 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006005 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006006 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6007 SetPackedFlag<kFlagUpperCanBeNull>(true);
6008 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006009 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006010 SetRawInputAt(0, input);
6011 }
6012
David Brazdilf5552582015-12-27 13:36:12 +00006013 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006014 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006015 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006016 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006017
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006018 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006019 DCHECK(GetUpperCanBeNull() || !can_be_null);
6020 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006021 }
6022
Vladimir Markoa1de9182016-02-25 11:37:38 +00006023 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006024
Calin Juravleb1498f62015-02-16 13:13:29 +00006025 DECLARE_INSTRUCTION(BoundType);
6026
6027 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006028 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6029 // is false then CanBeNull() cannot be true).
6030 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6031 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6032 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6033 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6034
Calin Juravleb1498f62015-02-16 13:13:29 +00006035 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006036 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6037 // It is used to bound the type in cases like:
6038 // if (x instanceof ClassX) {
6039 // // uper_bound_ will be ClassX
6040 // }
David Brazdilf5552582015-12-27 13:36:12 +00006041 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006042
6043 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6044};
6045
Vladimir Markofcb503c2016-05-18 12:48:17 +01006046class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006047 public:
6048 HCheckCast(HInstruction* object,
6049 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006050 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006051 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006052 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6053 SetPackedField<TypeCheckKindField>(check_kind);
6054 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006055 SetRawInputAt(0, object);
6056 SetRawInputAt(1, constant);
6057 }
6058
6059 bool CanBeMoved() const OVERRIDE { return true; }
6060
6061 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6062 return true;
6063 }
6064
6065 bool NeedsEnvironment() const OVERRIDE {
6066 // Instruction may throw a CheckCastError.
6067 return true;
6068 }
6069
6070 bool CanThrow() const OVERRIDE { return true; }
6071
Vladimir Markoa1de9182016-02-25 11:37:38 +00006072 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6073 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6074 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6075 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006076
6077 DECLARE_INSTRUCTION(CheckCast);
6078
6079 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006080 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6081 static constexpr size_t kFieldTypeCheckKindSize =
6082 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6083 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6084 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6085 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6086 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006087
6088 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006089};
6090
Vladimir Markofcb503c2016-05-18 12:48:17 +01006091class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006092 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006093 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006094 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006095 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6096 SetPackedField<BarrierKindField>(barrier_kind);
6097 }
Calin Juravle27df7582015-04-17 19:12:31 +01006098
Vladimir Markoa1de9182016-02-25 11:37:38 +00006099 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006100
6101 DECLARE_INSTRUCTION(MemoryBarrier);
6102
6103 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006104 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6105 static constexpr size_t kFieldBarrierKindSize =
6106 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6107 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6108 kFieldBarrierKind + kFieldBarrierKindSize;
6109 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6110 "Too many packed fields.");
6111 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006112
6113 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6114};
6115
Vladimir Markofcb503c2016-05-18 12:48:17 +01006116class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006117 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006118 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006119 kEnter,
6120 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006121 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006122 };
6123
6124 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006125 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006126 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6127 dex_pc) {
6128 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006129 SetRawInputAt(0, object);
6130 }
6131
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006132 // Instruction may go into runtime, so we need an environment.
6133 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006134
6135 bool CanThrow() const OVERRIDE {
6136 // Verifier guarantees that monitor-exit cannot throw.
6137 // This is important because it allows the HGraphBuilder to remove
6138 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6139 return IsEnter();
6140 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006141
Vladimir Markoa1de9182016-02-25 11:37:38 +00006142 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6143 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006144
6145 DECLARE_INSTRUCTION(MonitorOperation);
6146
Calin Juravle52c48962014-12-16 17:02:57 +00006147 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006148 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6149 static constexpr size_t kFieldOperationKindSize =
6150 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6151 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6152 kFieldOperationKind + kFieldOperationKindSize;
6153 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6154 "Too many packed fields.");
6155 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006156
6157 private:
6158 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6159};
6160
Vladimir Markofcb503c2016-05-18 12:48:17 +01006161class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006162 public:
6163 HSelect(HInstruction* condition,
6164 HInstruction* true_value,
6165 HInstruction* false_value,
6166 uint32_t dex_pc)
6167 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6168 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6169
6170 // First input must be `true_value` or `false_value` to allow codegens to
6171 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6172 // that architectures which implement HSelect as a conditional move also
6173 // will not need to invert the condition.
6174 SetRawInputAt(0, false_value);
6175 SetRawInputAt(1, true_value);
6176 SetRawInputAt(2, condition);
6177 }
6178
6179 HInstruction* GetFalseValue() const { return InputAt(0); }
6180 HInstruction* GetTrueValue() const { return InputAt(1); }
6181 HInstruction* GetCondition() const { return InputAt(2); }
6182
6183 bool CanBeMoved() const OVERRIDE { return true; }
6184 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6185
6186 bool CanBeNull() const OVERRIDE {
6187 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6188 }
6189
6190 DECLARE_INSTRUCTION(Select);
6191
6192 private:
6193 DISALLOW_COPY_AND_ASSIGN(HSelect);
6194};
6195
Vladimir Markof9f64412015-09-02 14:05:49 +01006196class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006197 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006198 MoveOperands(Location source,
6199 Location destination,
6200 Primitive::Type type,
6201 HInstruction* instruction)
6202 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006203
6204 Location GetSource() const { return source_; }
6205 Location GetDestination() const { return destination_; }
6206
6207 void SetSource(Location value) { source_ = value; }
6208 void SetDestination(Location value) { destination_ = value; }
6209
6210 // The parallel move resolver marks moves as "in-progress" by clearing the
6211 // destination (but not the source).
6212 Location MarkPending() {
6213 DCHECK(!IsPending());
6214 Location dest = destination_;
6215 destination_ = Location::NoLocation();
6216 return dest;
6217 }
6218
6219 void ClearPending(Location dest) {
6220 DCHECK(IsPending());
6221 destination_ = dest;
6222 }
6223
6224 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006225 DCHECK(source_.IsValid() || destination_.IsInvalid());
6226 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006227 }
6228
6229 // True if this blocks a move from the given location.
6230 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006231 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006232 }
6233
6234 // A move is redundant if it's been eliminated, if its source and
6235 // destination are the same, or if its destination is unneeded.
6236 bool IsRedundant() const {
6237 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6238 }
6239
6240 // We clear both operands to indicate move that's been eliminated.
6241 void Eliminate() {
6242 source_ = destination_ = Location::NoLocation();
6243 }
6244
6245 bool IsEliminated() const {
6246 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6247 return source_.IsInvalid();
6248 }
6249
Alexey Frunze4dda3372015-06-01 18:31:49 -07006250 Primitive::Type GetType() const { return type_; }
6251
Nicolas Geoffray90218252015-04-15 11:56:51 +01006252 bool Is64BitMove() const {
6253 return Primitive::Is64BitType(type_);
6254 }
6255
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006256 HInstruction* GetInstruction() const { return instruction_; }
6257
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006258 private:
6259 Location source_;
6260 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006261 // The type this move is for.
6262 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006263 // The instruction this move is assocatied with. Null when this move is
6264 // for moving an input in the expected locations of user (including a phi user).
6265 // This is only used in debug mode, to ensure we do not connect interval siblings
6266 // in the same parallel move.
6267 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006268};
6269
Roland Levillainc9285912015-12-18 10:38:42 +00006270std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6271
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006272static constexpr size_t kDefaultNumberOfMoves = 4;
6273
Vladimir Markofcb503c2016-05-18 12:48:17 +01006274class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006275 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006276 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006277 : HTemplateInstruction(SideEffects::None(), dex_pc),
6278 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6279 moves_.reserve(kDefaultNumberOfMoves);
6280 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006281
Nicolas Geoffray90218252015-04-15 11:56:51 +01006282 void AddMove(Location source,
6283 Location destination,
6284 Primitive::Type type,
6285 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006286 DCHECK(source.IsValid());
6287 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006288 if (kIsDebugBuild) {
6289 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006290 for (const MoveOperands& move : moves_) {
6291 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006292 // Special case the situation where the move is for the spill slot
6293 // of the instruction.
6294 if ((GetPrevious() == instruction)
6295 || ((GetPrevious() == nullptr)
6296 && instruction->IsPhi()
6297 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006298 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006299 << "Doing parallel moves for the same instruction.";
6300 } else {
6301 DCHECK(false) << "Doing parallel moves for the same instruction.";
6302 }
6303 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006304 }
6305 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006306 for (const MoveOperands& move : moves_) {
6307 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006308 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006309 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006310 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006311 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006312 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006313 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006314 }
6315
Vladimir Marko225b6462015-09-28 12:17:40 +01006316 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006317 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006318 }
6319
Vladimir Marko225b6462015-09-28 12:17:40 +01006320 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006321
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006322 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006323
6324 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006325 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006326
6327 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6328};
6329
Mark Mendell0616ae02015-04-17 12:49:27 -04006330} // namespace art
6331
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006332#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6333#include "nodes_shared.h"
6334#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006335#ifdef ART_ENABLE_CODEGEN_arm
6336#include "nodes_arm.h"
6337#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006338#ifdef ART_ENABLE_CODEGEN_arm64
6339#include "nodes_arm64.h"
6340#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006341#ifdef ART_ENABLE_CODEGEN_x86
6342#include "nodes_x86.h"
6343#endif
6344
6345namespace art {
6346
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006347class HGraphVisitor : public ValueObject {
6348 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006349 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6350 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006351
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006352 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006353 virtual void VisitBasicBlock(HBasicBlock* block);
6354
Roland Levillain633021e2014-10-01 14:12:25 +01006355 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006356 void VisitInsertionOrder();
6357
Roland Levillain633021e2014-10-01 14:12:25 +01006358 // Visit the graph following dominator tree reverse post-order.
6359 void VisitReversePostOrder();
6360
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006361 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006362
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006363 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006364#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006365 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6366
6367 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6368
6369#undef DECLARE_VISIT_INSTRUCTION
6370
6371 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006372 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006373
6374 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6375};
6376
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006377class HGraphDelegateVisitor : public HGraphVisitor {
6378 public:
6379 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6380 virtual ~HGraphDelegateVisitor() {}
6381
6382 // Visit functions that delegate to to super class.
6383#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006384 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006385
6386 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6387
6388#undef DECLARE_VISIT_INSTRUCTION
6389
6390 private:
6391 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6392};
6393
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006394class HInsertionOrderIterator : public ValueObject {
6395 public:
6396 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6397
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006398 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006399 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006400 void Advance() { ++index_; }
6401
6402 private:
6403 const HGraph& graph_;
6404 size_t index_;
6405
6406 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6407};
6408
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006409class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006410 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006411 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6412 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006413 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006414 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006415
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006416 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6417 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006418 void Advance() { ++index_; }
6419
6420 private:
6421 const HGraph& graph_;
6422 size_t index_;
6423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006424 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006425};
6426
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006427class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006428 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006429 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006430 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006431 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006432 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006433 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006434
6435 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006436 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006437 void Advance() { --index_; }
6438
6439 private:
6440 const HGraph& graph_;
6441 size_t index_;
6442
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006443 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006444};
6445
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006446class HLinearPostOrderIterator : public ValueObject {
6447 public:
6448 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006449 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006450
6451 bool Done() const { return index_ == 0; }
6452
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006453 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006454
6455 void Advance() {
6456 --index_;
6457 DCHECK_GE(index_, 0U);
6458 }
6459
6460 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006461 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006462 size_t index_;
6463
6464 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6465};
6466
6467class HLinearOrderIterator : public ValueObject {
6468 public:
6469 explicit HLinearOrderIterator(const HGraph& graph)
6470 : order_(graph.GetLinearOrder()), index_(0) {}
6471
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006472 bool Done() const { return index_ == order_.size(); }
6473 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006474 void Advance() { ++index_; }
6475
6476 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006477 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006478 size_t index_;
6479
6480 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6481};
6482
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006483// Iterator over the blocks that art part of the loop. Includes blocks part
6484// of an inner loop. The order in which the blocks are iterated is on their
6485// block id.
6486class HBlocksInLoopIterator : public ValueObject {
6487 public:
6488 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6489 : blocks_in_loop_(info.GetBlocks()),
6490 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6491 index_(0) {
6492 if (!blocks_in_loop_.IsBitSet(index_)) {
6493 Advance();
6494 }
6495 }
6496
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006497 bool Done() const { return index_ == blocks_.size(); }
6498 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006499 void Advance() {
6500 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006501 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006502 if (blocks_in_loop_.IsBitSet(index_)) {
6503 break;
6504 }
6505 }
6506 }
6507
6508 private:
6509 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006510 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006511 size_t index_;
6512
6513 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6514};
6515
Mingyao Yang3584bce2015-05-19 16:01:59 -07006516// Iterator over the blocks that art part of the loop. Includes blocks part
6517// of an inner loop. The order in which the blocks are iterated is reverse
6518// post order.
6519class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6520 public:
6521 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6522 : blocks_in_loop_(info.GetBlocks()),
6523 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6524 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006525 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006526 Advance();
6527 }
6528 }
6529
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006530 bool Done() const { return index_ == blocks_.size(); }
6531 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006532 void Advance() {
6533 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006534 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6535 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006536 break;
6537 }
6538 }
6539 }
6540
6541 private:
6542 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006543 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006544 size_t index_;
6545
6546 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6547};
6548
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006549inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006550 if (constant->IsIntConstant()) {
6551 return constant->AsIntConstant()->GetValue();
6552 } else if (constant->IsLongConstant()) {
6553 return constant->AsLongConstant()->GetValue();
6554 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006555 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006556 return 0;
6557 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006558}
6559
Vladimir Marko58155012015-08-19 12:49:41 +00006560inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6561 // For the purposes of the compiler, the dex files must actually be the same object
6562 // if we want to safely treat them as the same. This is especially important for JIT
6563 // as custom class loaders can open the same underlying file (or memory) multiple
6564 // times and provide different class resolution but no two class loaders should ever
6565 // use the same DexFile object - doing so is an unsupported hack that can lead to
6566 // all sorts of weird failures.
6567 return &lhs == &rhs;
6568}
6569
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006570#define INSTRUCTION_TYPE_CHECK(type, super) \
6571 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6572 inline const H##type* HInstruction::As##type() const { \
6573 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6574 } \
6575 inline H##type* HInstruction::As##type() { \
6576 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6577 }
6578
6579 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6580#undef INSTRUCTION_TYPE_CHECK
6581
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006582// Create space in `blocks` for adding `number_of_new_blocks` entries
6583// starting at location `at`. Blocks after `at` are moved accordingly.
6584inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6585 size_t number_of_new_blocks,
6586 size_t after) {
6587 DCHECK_LT(after, blocks->size());
6588 size_t old_size = blocks->size();
6589 size_t new_size = old_size + number_of_new_blocks;
6590 blocks->resize(new_size);
6591 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6592}
6593
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006594} // namespace art
6595
6596#endif // ART_COMPILER_OPTIMIZING_NODES_H_