blob: 1c0ad6a6ccf3b0befe2da4cdfb62895a81b90e85 [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),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100654 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000655 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 back_edges_.reserve(kDefaultNumberOfBackEdges);
657 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000659 bool IsIrreducible() const { return irreducible_; }
660
661 void Dump(std::ostream& os);
662
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100663 HBasicBlock* GetHeader() const {
664 return header_;
665 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000666
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000667 void SetHeader(HBasicBlock* block) {
668 header_ = block;
669 }
670
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100671 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
672 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
673 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
674
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000675 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100676 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677 }
678
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100679 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100680 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 }
682
David Brazdil46e2a392015-03-16 17:31:52 +0000683 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100684 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 }
686
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000687 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100688 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000689 }
690
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100691 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100692
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100693 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100695 }
696
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100697 // Returns the lifetime position of the back edge that has the
698 // greatest lifetime position.
699 size_t GetLifetimeEnd() const;
700
701 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100703 }
704
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000705 // Finds blocks that are part of this loop.
706 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100707
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 // Returns whether this loop information contains `block`.
709 // Note that this loop information *must* be populated before entering this function.
710 bool Contains(const HBasicBlock& block) const;
711
712 // Returns whether this loop information is an inner loop of `other`.
713 // Note that `other` *must* be populated before entering this function.
714 bool IsIn(const HLoopInformation& other) const;
715
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800716 // Returns true if instruction is not defined within this loop.
717 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700718
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100719 const ArenaBitVector& GetBlocks() const { return blocks_; }
720
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000721 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000722 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000723
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000724 void ClearAllBlocks() {
725 blocks_.ClearAllBits();
726 }
727
David Brazdil3f4a5222016-05-06 12:46:21 +0100728 bool HasBackEdgeNotDominatedByHeader() const;
729
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000730 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100731 // Internal recursive implementation of `Populate`.
732 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100733 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100734
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100736 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000737 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100738 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100739 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000740
741 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
742};
743
David Brazdilec16f792015-08-19 15:04:01 +0100744// Stores try/catch information for basic blocks.
745// Note that HGraph is constructed so that catch blocks cannot simultaneously
746// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100747class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100748 public:
749 // Try block information constructor.
750 explicit TryCatchInformation(const HTryBoundary& try_entry)
751 : try_entry_(&try_entry),
752 catch_dex_file_(nullptr),
753 catch_type_index_(DexFile::kDexNoIndex16) {
754 DCHECK(try_entry_ != nullptr);
755 }
756
757 // Catch block information constructor.
758 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
759 : try_entry_(nullptr),
760 catch_dex_file_(&dex_file),
761 catch_type_index_(catch_type_index) {}
762
763 bool IsTryBlock() const { return try_entry_ != nullptr; }
764
765 const HTryBoundary& GetTryEntry() const {
766 DCHECK(IsTryBlock());
767 return *try_entry_;
768 }
769
770 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
771
772 bool IsCatchAllTypeIndex() const {
773 DCHECK(IsCatchBlock());
774 return catch_type_index_ == DexFile::kDexNoIndex16;
775 }
776
777 uint16_t GetCatchTypeIndex() const {
778 DCHECK(IsCatchBlock());
779 return catch_type_index_;
780 }
781
782 const DexFile& GetCatchDexFile() const {
783 DCHECK(IsCatchBlock());
784 return *catch_dex_file_;
785 }
786
787 private:
788 // One of possibly several TryBoundary instructions entering the block's try.
789 // Only set for try blocks.
790 const HTryBoundary* try_entry_;
791
792 // Exception type information. Only set for catch blocks.
793 const DexFile* catch_dex_file_;
794 const uint16_t catch_type_index_;
795};
796
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100797static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100798static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100799
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800// A block in a method. Contains the list of instructions represented
801// as a double linked list. Each block knows its predecessors and
802// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100803
Vladimir Markof9f64412015-09-02 14:05:49 +0100804class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600806 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000807 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000808 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
809 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000810 loop_information_(nullptr),
811 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000812 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100813 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100814 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100815 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000816 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000817 try_catch_information_(nullptr) {
818 predecessors_.reserve(kDefaultNumberOfPredecessors);
819 successors_.reserve(kDefaultNumberOfSuccessors);
820 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
821 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822
Vladimir Marko60584552015-09-03 13:35:12 +0000823 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100824 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000825 }
826
Vladimir Marko60584552015-09-03 13:35:12 +0000827 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100828 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829 }
830
David Brazdild26a4112015-11-10 11:07:31 +0000831 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
832 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
833
Vladimir Marko60584552015-09-03 13:35:12 +0000834 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
835 return ContainsElement(successors_, block, start_from);
836 }
837
838 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100839 return dominated_blocks_;
840 }
841
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100842 bool IsEntryBlock() const {
843 return graph_->GetEntryBlock() == this;
844 }
845
846 bool IsExitBlock() const {
847 return graph_->GetExitBlock() == this;
848 }
849
David Brazdil46e2a392015-03-16 17:31:52 +0000850 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000851 bool IsSingleTryBoundary() const;
852
853 // Returns true if this block emits nothing but a jump.
854 bool IsSingleJump() const {
855 HLoopInformation* loop_info = GetLoopInformation();
856 return (IsSingleGoto() || IsSingleTryBoundary())
857 // Back edges generate a suspend check.
858 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
859 }
David Brazdil46e2a392015-03-16 17:31:52 +0000860
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000861 void AddBackEdge(HBasicBlock* back_edge) {
862 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000863 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000864 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100865 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000866 loop_information_->AddBackEdge(back_edge);
867 }
868
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000869 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000870 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000871
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100872 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000873 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600874 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000875
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000876 HBasicBlock* GetDominator() const { return dominator_; }
877 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000878 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
879
880 void RemoveDominatedBlock(HBasicBlock* block) {
881 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100882 }
Vladimir Marko60584552015-09-03 13:35:12 +0000883
884 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
885 ReplaceElement(dominated_blocks_, existing, new_block);
886 }
887
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100888 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000889
890 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100891 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000892 }
893
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100894 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
895 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100896 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100897 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100898 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
899 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000901 HInstruction* GetFirstInstructionDisregardMoves() const;
902
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000904 successors_.push_back(block);
905 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 }
907
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100908 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
909 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100910 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000911 new_block->predecessors_.push_back(this);
912 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000913 }
914
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000915 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
916 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000917 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000918 new_block->successors_.push_back(this);
919 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000920 }
921
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100922 // Insert `this` between `predecessor` and `successor. This method
923 // preserves the indicies, and will update the first edge found between
924 // `predecessor` and `successor`.
925 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
926 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100927 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000928 successor->predecessors_[predecessor_index] = this;
929 predecessor->successors_[successor_index] = this;
930 successors_.push_back(successor);
931 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100932 }
933
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100934 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000935 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100936 }
937
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000938 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000939 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000940 }
941
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000943 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100944 }
945
946 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000947 predecessors_.push_back(block);
948 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100949 }
950
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100951 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000952 DCHECK_EQ(predecessors_.size(), 2u);
953 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100954 }
955
David Brazdil769c9e52015-04-27 13:54:09 +0100956 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000957 DCHECK_EQ(successors_.size(), 2u);
958 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100959 }
960
David Brazdilfc6a86a2015-06-26 10:33:45 +0000961 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000962 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100963 }
964
David Brazdilfc6a86a2015-06-26 10:33:45 +0000965 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000966 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100967 }
968
David Brazdilfc6a86a2015-06-26 10:33:45 +0000969 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000970 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100971 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 }
973
974 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000975 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100976 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000977 }
978
979 // Returns whether the first occurrence of `predecessor` in the list of
980 // predecessors is at index `idx`.
981 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100982 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000983 return GetPredecessorIndexOf(predecessor) == idx;
984 }
985
David Brazdild7558da2015-09-22 13:04:14 +0100986 // Create a new block between this block and its predecessors. The new block
987 // is added to the graph, all predecessor edges are relinked to it and an edge
988 // is created to `this`. Returns the new empty block. Reverse post order or
989 // loop and try/catch information are not updated.
990 HBasicBlock* CreateImmediateDominator();
991
David Brazdilfc6a86a2015-06-26 10:33:45 +0000992 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100993 // created, latter block. Note that this method will add the block to the
994 // graph, create a Goto at the end of the former block and will create an edge
995 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100996 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000997 HBasicBlock* SplitBefore(HInstruction* cursor);
998
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000999 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001000 // created block. Note that this method just updates raw block information,
1001 // like predecessors, successors, dominators, and instruction list. It does not
1002 // update the graph, reverse post order, loop information, nor make sure the
1003 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001004 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1005
1006 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1007 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001008
1009 // Merge `other` at the end of `this`. Successors and dominated blocks of
1010 // `other` are changed to be successors and dominated blocks of `this`. Note
1011 // that this method does not update the graph, reverse post order, loop
1012 // information, nor make sure the blocks are consistent (for example ending
1013 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001014 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001015
1016 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1017 // of `this` are moved to `other`.
1018 // Note that this method does not update the graph, reverse post order, loop
1019 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001020 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001021 void ReplaceWith(HBasicBlock* other);
1022
David Brazdil2d7352b2015-04-20 14:52:42 +01001023 // Merge `other` at the end of `this`. This method updates loops, reverse post
1024 // order, links to predecessors, successors, dominators and deletes the block
1025 // from the graph. The two blocks must be successive, i.e. `this` the only
1026 // predecessor of `other` and vice versa.
1027 void MergeWith(HBasicBlock* other);
1028
1029 // Disconnects `this` from all its predecessors, successors and dominator,
1030 // removes it from all loops it is included in and eventually from the graph.
1031 // The block must not dominate any other block. Predecessors and successors
1032 // are safely updated.
1033 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001034
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001036 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001037 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001038 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001039 // Replace instruction `initial` with `replacement` within this block.
1040 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1041 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001042 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001043 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001044 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001045 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1046 // instruction list. With 'ensure_safety' set to true, it verifies that the
1047 // instruction is not in use and removes it from the use lists of its inputs.
1048 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1049 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001050 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051
1052 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001053 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001054 }
1055
Roland Levillain6b879dd2014-09-22 17:13:44 +01001056 bool IsLoopPreHeaderFirstPredecessor() const {
1057 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001058 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001059 }
1060
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001061 bool IsFirstPredecessorBackEdge() const {
1062 DCHECK(IsLoopHeader());
1063 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1064 }
1065
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001066 HLoopInformation* GetLoopInformation() const {
1067 return loop_information_;
1068 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001071 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001073 void SetInLoop(HLoopInformation* info) {
1074 if (IsLoopHeader()) {
1075 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001076 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001077 loop_information_ = info;
1078 } else if (loop_information_->Contains(*info->GetHeader())) {
1079 // Block is currently part of an outer loop. Make it part of this inner loop.
1080 // Note that a non loop header having a loop information means this loop information
1081 // has already been populated
1082 loop_information_ = info;
1083 } else {
1084 // Block is part of an inner loop. Do not update the loop information.
1085 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1086 // at this point, because this method is being called while populating `info`.
1087 }
1088 }
1089
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001090 // Raw update of the loop information.
1091 void SetLoopInformation(HLoopInformation* info) {
1092 loop_information_ = info;
1093 }
1094
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001095 bool IsInLoop() const { return loop_information_ != nullptr; }
1096
David Brazdilec16f792015-08-19 15:04:01 +01001097 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1098
1099 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1100 try_catch_information_ = try_catch_information;
1101 }
1102
1103 bool IsTryBlock() const {
1104 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1105 }
1106
1107 bool IsCatchBlock() const {
1108 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1109 }
David Brazdilffee3d32015-07-06 11:48:53 +01001110
1111 // Returns the try entry that this block's successors should have. They will
1112 // be in the same try, unless the block ends in a try boundary. In that case,
1113 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001114 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001115
David Brazdild7558da2015-09-22 13:04:14 +01001116 bool HasThrowingInstructions() const;
1117
David Brazdila4b8c212015-05-07 09:59:30 +01001118 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001119 bool Dominates(HBasicBlock* block) const;
1120
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001121 size_t GetLifetimeStart() const { return lifetime_start_; }
1122 size_t GetLifetimeEnd() const { return lifetime_end_; }
1123
1124 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1125 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1126
David Brazdil8d5b8b22015-03-24 10:51:52 +00001127 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001128 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001129 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001130 bool HasSinglePhi() const;
1131
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001132 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001133 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001134 ArenaVector<HBasicBlock*> predecessors_;
1135 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001136 HInstructionList instructions_;
1137 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001138 HLoopInformation* loop_information_;
1139 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001140 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001141 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001142 // The dex program counter of the first instruction of this block.
1143 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001144 size_t lifetime_start_;
1145 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001146 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001147
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001148 friend class HGraph;
1149 friend class HInstruction;
1150
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001151 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1152};
1153
David Brazdilb2bd1c52015-03-25 11:17:37 +00001154// Iterates over the LoopInformation of all loops which contain 'block'
1155// from the innermost to the outermost.
1156class HLoopInformationOutwardIterator : public ValueObject {
1157 public:
1158 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1159 : current_(block.GetLoopInformation()) {}
1160
1161 bool Done() const { return current_ == nullptr; }
1162
1163 void Advance() {
1164 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001165 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001166 }
1167
1168 HLoopInformation* Current() const {
1169 DCHECK(!Done());
1170 return current_;
1171 }
1172
1173 private:
1174 HLoopInformation* current_;
1175
1176 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1177};
1178
Alexandre Ramesef20f712015-06-09 10:29:30 +01001179#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001180 M(Above, Condition) \
1181 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001183 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001184 M(ArrayGet, Instruction) \
1185 M(ArrayLength, Instruction) \
1186 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001187 M(Below, Condition) \
1188 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001189 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001190 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001191 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001192 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001193 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001194 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001195 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001196 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001197 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001198 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001200 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001202 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001203 M(Exit, Instruction) \
1204 M(FloatConstant, Constant) \
1205 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(GreaterThan, Condition) \
1207 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001209 M(InstanceFieldGet, Instruction) \
1210 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001211 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001212 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001213 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001214 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001215 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001216 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001217 M(LessThan, Condition) \
1218 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001219 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001220 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001221 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001222 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001223 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001224 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001226 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(Neg, UnaryOperation) \
1228 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001230 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001232 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001233 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001234 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001235 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001237 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001239 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(Return, Instruction) \
1241 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001242 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001243 M(Shl, BinaryOperation) \
1244 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001245 M(StaticFieldGet, Instruction) \
1246 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 M(UnresolvedInstanceFieldGet, Instruction) \
1248 M(UnresolvedInstanceFieldSet, Instruction) \
1249 M(UnresolvedStaticFieldGet, Instruction) \
1250 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001251 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001252 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001253 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001254 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001255 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001256 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001257 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001258 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001259
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001260/*
1261 * Instructions, shared across several (not all) architectures.
1262 */
1263#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1264#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1265#else
1266#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001267 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001268 M(MultiplyAccumulate, Instruction)
1269#endif
1270
Vladimir Markob4536b72015-11-24 13:45:23 +00001271#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001272#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001273#else
1274#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1275 M(ArmDexCacheArraysBase, Instruction)
1276#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001277
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001278#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001279#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001280#else
1281#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001282 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001283 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001284#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001285
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001286#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1287
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001288#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1289
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001290#ifndef ART_ENABLE_CODEGEN_x86
1291#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1292#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001293#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1294 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001295 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001296 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001297 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001298#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001299
1300#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1301
1302#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1303 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001304 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001305 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1306 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001307 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001308 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001309 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1310 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1311
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001312#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1313 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001314 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001315 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001316 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001317 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001318
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001319#define FOR_EACH_INSTRUCTION(M) \
1320 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1321 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1322
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001323#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001324FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1325#undef FORWARD_DECLARATION
1326
Roland Levillainccc07a92014-09-16 14:48:16 +01001327#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001328 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001329 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001330 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001331 return other->Is##type(); \
1332 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001333 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001334
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001335#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1336 bool Is##type() const { return As##type() != nullptr; } \
1337 const H##type* As##type() const { return this; } \
1338 H##type* As##type() { return this; }
1339
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001340template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001341class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001342 public:
David Brazdiled596192015-01-23 10:39:45 +00001343 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001344 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001345 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001346
Vladimir Marko46817b82016-03-29 12:21:58 +01001347 // Hook for the IntrusiveForwardList<>.
1348 // TODO: Hide this better.
1349 IntrusiveForwardListHook hook;
1350
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001351 private:
David Brazdiled596192015-01-23 10:39:45 +00001352 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001353 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001354
1355 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001356 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001357
Vladimir Marko46817b82016-03-29 12:21:58 +01001358 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001359
1360 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1361};
1362
David Brazdiled596192015-01-23 10:39:45 +00001363template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001364using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001365
David Brazdil1abb4192015-02-17 18:33:36 +00001366// This class is used by HEnvironment and HInstruction classes to record the
1367// instructions they use and pointers to the corresponding HUseListNodes kept
1368// by the used instructions.
1369template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001370class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001371 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001372 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1373 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001374
Vladimir Marko46817b82016-03-29 12:21:58 +01001375 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1376 : HUserRecord(old_record.instruction_, before_use_node) {}
1377 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1378 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001379 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001380 }
1381
1382 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001383 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1384 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001385
1386 private:
1387 // Instruction used by the user.
1388 HInstruction* instruction_;
1389
Vladimir Marko46817b82016-03-29 12:21:58 +01001390 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1391 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001392};
1393
Aart Bik854a02b2015-07-14 16:07:00 -07001394/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001395 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001396 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001397 * For write/read dependences on fields/arrays, the dependence analysis uses
1398 * type disambiguation (e.g. a float field write cannot modify the value of an
1399 * integer field read) and the access type (e.g. a reference array write cannot
1400 * modify the value of a reference field read [although it may modify the
1401 * reference fetch prior to reading the field, which is represented by its own
1402 * write/read dependence]). The analysis makes conservative points-to
1403 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1404 * the same, and any reference read depends on any reference read without
1405 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001406 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001407 * The internal representation uses 38-bit and is described in the table below.
1408 * The first line indicates the side effect, and for field/array accesses the
1409 * second line indicates the type of the access (in the order of the
1410 * Primitive::Type enum).
1411 * The two numbered lines below indicate the bit position in the bitfield (read
1412 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001413 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1415 * +-------------+---------+---------+--------------+---------+---------+
1416 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1417 * | 3 |333333322|222222221| 1 |111111110|000000000|
1418 * | 7 |654321098|765432109| 8 |765432109|876543210|
1419 *
1420 * Note that, to ease the implementation, 'changes' bits are least significant
1421 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001422 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001423class SideEffects : public ValueObject {
1424 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001425 SideEffects() : flags_(0) {}
1426
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001427 static SideEffects None() {
1428 return SideEffects(0);
1429 }
1430
1431 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001432 return SideEffects(kAllChangeBits | kAllDependOnBits);
1433 }
1434
1435 static SideEffects AllChanges() {
1436 return SideEffects(kAllChangeBits);
1437 }
1438
1439 static SideEffects AllDependencies() {
1440 return SideEffects(kAllDependOnBits);
1441 }
1442
1443 static SideEffects AllExceptGCDependency() {
1444 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1445 }
1446
1447 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001448 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001449 }
1450
Aart Bik34c3ba92015-07-20 14:08:59 -07001451 static SideEffects AllWrites() {
1452 return SideEffects(kAllWrites);
1453 }
1454
1455 static SideEffects AllReads() {
1456 return SideEffects(kAllReads);
1457 }
1458
1459 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1460 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001462 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001463 }
1464
Aart Bik854a02b2015-07-14 16:07:00 -07001465 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001466 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001467 }
1468
Aart Bik34c3ba92015-07-20 14:08:59 -07001469 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1470 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001471 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001472 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001473 }
1474
1475 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001476 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001477 }
1478
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001479 static SideEffects CanTriggerGC() {
1480 return SideEffects(1ULL << kCanTriggerGCBit);
1481 }
1482
1483 static SideEffects DependsOnGC() {
1484 return SideEffects(1ULL << kDependsOnGCBit);
1485 }
1486
Aart Bik854a02b2015-07-14 16:07:00 -07001487 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001488 SideEffects Union(SideEffects other) const {
1489 return SideEffects(flags_ | other.flags_);
1490 }
1491
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001492 SideEffects Exclusion(SideEffects other) const {
1493 return SideEffects(flags_ & ~other.flags_);
1494 }
1495
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001496 void Add(SideEffects other) {
1497 flags_ |= other.flags_;
1498 }
1499
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001500 bool Includes(SideEffects other) const {
1501 return (other.flags_ & flags_) == other.flags_;
1502 }
1503
1504 bool HasSideEffects() const {
1505 return (flags_ & kAllChangeBits);
1506 }
1507
1508 bool HasDependencies() const {
1509 return (flags_ & kAllDependOnBits);
1510 }
1511
1512 // Returns true if there are no side effects or dependencies.
1513 bool DoesNothing() const {
1514 return flags_ == 0;
1515 }
1516
Aart Bik854a02b2015-07-14 16:07:00 -07001517 // Returns true if something is written.
1518 bool DoesAnyWrite() const {
1519 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001520 }
1521
Aart Bik854a02b2015-07-14 16:07:00 -07001522 // Returns true if something is read.
1523 bool DoesAnyRead() const {
1524 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001525 }
1526
Aart Bik854a02b2015-07-14 16:07:00 -07001527 // Returns true if potentially everything is written and read
1528 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001529 bool DoesAllReadWrite() const {
1530 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1531 }
1532
Aart Bik854a02b2015-07-14 16:07:00 -07001533 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001534 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001535 }
1536
Roland Levillain0d5a2812015-11-13 10:07:31 +00001537 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001538 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001539 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1540 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001541 }
1542
1543 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001544 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001545 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001546 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 for (int s = kLastBit; s >= 0; s--) {
1548 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1549 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1550 // This is a bit for the GC side effect.
1551 if (current_bit_is_set) {
1552 flags += "GC";
1553 }
Aart Bik854a02b2015-07-14 16:07:00 -07001554 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001555 } else {
1556 // This is a bit for the array/field analysis.
1557 // The underscore character stands for the 'can trigger GC' bit.
1558 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1559 if (current_bit_is_set) {
1560 flags += kDebug[s];
1561 }
1562 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1563 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1564 flags += "|";
1565 }
1566 }
Aart Bik854a02b2015-07-14 16:07:00 -07001567 }
1568 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001569 }
1570
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001572
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001573 private:
1574 static constexpr int kFieldArrayAnalysisBits = 9;
1575
1576 static constexpr int kFieldWriteOffset = 0;
1577 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1578 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1579 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1580
1581 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1582
1583 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1584 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1585 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1586 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1587
1588 static constexpr int kLastBit = kDependsOnGCBit;
1589 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1590
1591 // Aliases.
1592
1593 static_assert(kChangeBits == kDependOnBits,
1594 "the 'change' bits should match the 'depend on' bits.");
1595
1596 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1597 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1598 static constexpr uint64_t kAllWrites =
1599 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1600 static constexpr uint64_t kAllReads =
1601 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001602
Aart Bik854a02b2015-07-14 16:07:00 -07001603 // Translates type to bit flag.
1604 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1605 CHECK_NE(type, Primitive::kPrimVoid);
1606 const uint64_t one = 1;
1607 const int shift = type; // 0-based consecutive enum
1608 DCHECK_LE(kFieldWriteOffset, shift);
1609 DCHECK_LT(shift, kArrayWriteOffset);
1610 return one << (type + offset);
1611 }
1612
1613 // Private constructor on direct flags value.
1614 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1615
1616 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001617};
1618
David Brazdiled596192015-01-23 10:39:45 +00001619// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001620class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001621 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001622 HEnvironment(ArenaAllocator* arena,
1623 size_t number_of_vregs,
1624 const DexFile& dex_file,
1625 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001626 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001627 InvokeType invoke_type,
1628 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001629 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1630 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001631 parent_(nullptr),
1632 dex_file_(dex_file),
1633 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001634 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001635 invoke_type_(invoke_type),
1636 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001637 }
1638
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001639 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001640 : HEnvironment(arena,
1641 to_copy.Size(),
1642 to_copy.GetDexFile(),
1643 to_copy.GetMethodIdx(),
1644 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001645 to_copy.GetInvokeType(),
1646 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001647
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001648 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001649 if (parent_ != nullptr) {
1650 parent_->SetAndCopyParentChain(allocator, parent);
1651 } else {
1652 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1653 parent_->CopyFrom(parent);
1654 if (parent->GetParent() != nullptr) {
1655 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1656 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001657 }
David Brazdiled596192015-01-23 10:39:45 +00001658 }
1659
Vladimir Marko71bf8092015-09-15 15:33:14 +01001660 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001661 void CopyFrom(HEnvironment* environment);
1662
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001663 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1664 // input to the loop phi instead. This is for inserting instructions that
1665 // require an environment (like HDeoptimization) in the loop pre-header.
1666 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001667
1668 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001669 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001670 }
1671
1672 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001673 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001674 }
1675
David Brazdil1abb4192015-02-17 18:33:36 +00001676 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001677
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001678 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001679
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001680 HEnvironment* GetParent() const { return parent_; }
1681
1682 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001683 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001684 }
1685
1686 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001687 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001688 }
1689
1690 uint32_t GetDexPc() const {
1691 return dex_pc_;
1692 }
1693
1694 uint32_t GetMethodIdx() const {
1695 return method_idx_;
1696 }
1697
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001698 InvokeType GetInvokeType() const {
1699 return invoke_type_;
1700 }
1701
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001702 const DexFile& GetDexFile() const {
1703 return dex_file_;
1704 }
1705
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001706 HInstruction* GetHolder() const {
1707 return holder_;
1708 }
1709
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001710
1711 bool IsFromInlinedInvoke() const {
1712 return GetParent() != nullptr;
1713 }
1714
David Brazdiled596192015-01-23 10:39:45 +00001715 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001716 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1717 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001718 HEnvironment* parent_;
1719 const DexFile& dex_file_;
1720 const uint32_t method_idx_;
1721 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001722 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001723
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001724 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001725 HInstruction* const holder_;
1726
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001727 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001728
1729 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1730};
1731
Vladimir Markof9f64412015-09-02 14:05:49 +01001732class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001733 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001734 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001735 : previous_(nullptr),
1736 next_(nullptr),
1737 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001738 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001739 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001740 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001741 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001742 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001743 locations_(nullptr),
1744 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001745 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001746 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001747 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1748 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1749 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001750
Dave Allison20dfc792014-06-16 20:44:29 -07001751 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001752
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001753#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001754 enum InstructionKind {
1755 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1756 };
1757#undef DECLARE_KIND
1758
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001759 HInstruction* GetNext() const { return next_; }
1760 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001761
Calin Juravle77520bc2015-01-12 18:45:46 +00001762 HInstruction* GetNextDisregardingMoves() const;
1763 HInstruction* GetPreviousDisregardingMoves() const;
1764
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001765 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001766 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001767 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001768 bool IsInBlock() const { return block_ != nullptr; }
1769 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001770 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1771 bool IsIrreducibleLoopHeaderPhi() const {
1772 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1773 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001774
Roland Levillain6b879dd2014-09-22 17:13:44 +01001775 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001776 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777
1778 virtual void Accept(HGraphVisitor* visitor) = 0;
1779 virtual const char* DebugName() const = 0;
1780
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001781 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001782 void SetRawInputAt(size_t index, HInstruction* input) {
1783 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1784 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001785
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001786 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001787
1788 uint32_t GetDexPc() const { return dex_pc_; }
1789
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001790 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001791
Roland Levillaine161a2a2014-10-03 12:45:18 +01001792 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001793 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001794
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001795 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001796 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001797
Calin Juravle10e244f2015-01-26 18:54:32 +00001798 // Does not apply for all instructions, but having this at top level greatly
1799 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001800 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001801 virtual bool CanBeNull() const {
1802 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1803 return true;
1804 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001805
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001806 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001807 return false;
1808 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001809
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001810 virtual bool IsActualObject() const {
1811 return GetType() == Primitive::kPrimNot;
1812 }
1813
Calin Juravle2e768302015-07-28 14:41:11 +00001814 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001815
Calin Juravle61d544b2015-02-23 16:46:57 +00001816 ReferenceTypeInfo GetReferenceTypeInfo() const {
1817 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001818 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001819 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001820 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001821
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001822 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001823 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001824 // Note: fixup_end remains valid across push_front().
1825 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1826 HUseListNode<HInstruction*>* new_node =
1827 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1828 uses_.push_front(*new_node);
1829 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001830 }
1831
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001832 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001833 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001834 // Note: env_fixup_end remains valid across push_front().
1835 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1836 HUseListNode<HEnvironment*>* new_node =
1837 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1838 env_uses_.push_front(*new_node);
1839 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001840 }
1841
David Brazdil1abb4192015-02-17 18:33:36 +00001842 void RemoveAsUserOfInput(size_t input) {
1843 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001844 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1845 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1846 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001847 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848
David Brazdil1abb4192015-02-17 18:33:36 +00001849 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1850 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851
Vladimir Marko46817b82016-03-29 12:21:58 +01001852 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1853 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1854 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001855 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001856 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001857 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858
Roland Levillain6c82d402014-10-13 16:10:27 +01001859 // Does this instruction strictly dominate `other_instruction`?
1860 // Returns false if this instruction and `other_instruction` are the same.
1861 // Aborts if this instruction and `other_instruction` are both phis.
1862 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001863
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864 int GetId() const { return id_; }
1865 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001866
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001867 int GetSsaIndex() const { return ssa_index_; }
1868 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1869 bool HasSsaIndex() const { return ssa_index_ != -1; }
1870
1871 bool HasEnvironment() const { return environment_ != nullptr; }
1872 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001873 // Set the `environment_` field. Raw because this method does not
1874 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001875 void SetRawEnvironment(HEnvironment* environment) {
1876 DCHECK(environment_ == nullptr);
1877 DCHECK_EQ(environment->GetHolder(), this);
1878 environment_ = environment;
1879 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001880
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001881 void RemoveEnvironment();
1882
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001883 // Set the environment of this instruction, copying it from `environment`. While
1884 // copying, the uses lists are being updated.
1885 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001886 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001887 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001888 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001889 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001890 if (environment->GetParent() != nullptr) {
1891 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1892 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001893 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001894
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001895 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1896 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001897 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001898 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001899 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001900 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001901 if (environment->GetParent() != nullptr) {
1902 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1903 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001904 }
1905
Nicolas Geoffray39468442014-09-02 15:17:15 +01001906 // Returns the number of entries in the environment. Typically, that is the
1907 // number of dex registers in a method. It could be more in case of inlining.
1908 size_t EnvironmentSize() const;
1909
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001910 LocationSummary* GetLocations() const { return locations_; }
1911 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001912
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001914 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001915
Alexandre Rames188d4312015-04-09 18:30:21 +01001916 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1917 // uses of this instruction by `other` are *not* updated.
1918 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1919 ReplaceWith(other);
1920 other->ReplaceInput(this, use_index);
1921 }
1922
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001923 // Move `this` instruction before `cursor`.
1924 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001925
Vladimir Markofb337ea2015-11-25 15:25:10 +00001926 // Move `this` before its first user and out of any loops. If there is no
1927 // out-of-loop user that dominates all other users, move the instruction
1928 // to the end of the out-of-loop common dominator of the user's blocks.
1929 //
1930 // This can be used only on non-throwing instructions with no side effects that
1931 // have at least one use but no environment uses.
1932 void MoveBeforeFirstUserAndOutOfLoops();
1933
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001934#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001935 bool Is##type() const; \
1936 const H##type* As##type() const; \
1937 H##type* As##type();
1938
1939 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1940#undef INSTRUCTION_TYPE_CHECK
1941
1942#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001943 bool Is##type() const { return (As##type() != nullptr); } \
1944 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001945 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001946 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001947#undef INSTRUCTION_TYPE_CHECK
1948
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001949 // Returns whether the instruction can be moved within the graph.
1950 virtual bool CanBeMoved() const { return false; }
1951
1952 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001953 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001954 return false;
1955 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001956
1957 // Returns whether any data encoded in the two instructions is equal.
1958 // This method does not look at the inputs. Both instructions must be
1959 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001960 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001961 return false;
1962 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001963
1964 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001965 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001966 // 2) Their inputs are identical.
1967 bool Equals(HInstruction* other) const;
1968
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
1970 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
1971 // the virtual function because the __attribute__((__pure__)) doesn't really
1972 // apply the strong requirement for virtual functions, preventing optimizations.
1973 InstructionKind GetKind() const PURE;
1974 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001975
1976 virtual size_t ComputeHashCode() const {
1977 size_t result = GetKind();
1978 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1979 result = (result * 31) + InputAt(i)->GetId();
1980 }
1981 return result;
1982 }
1983
1984 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001985 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001986 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001987
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001988 size_t GetLifetimePosition() const { return lifetime_position_; }
1989 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1990 LiveInterval* GetLiveInterval() const { return live_interval_; }
1991 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1992 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1993
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001994 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1995
1996 // Returns whether the code generation of the instruction will require to have access
1997 // to the current method. Such instructions are:
1998 // (1): Instructions that require an environment, as calling the runtime requires
1999 // to walk the stack and have the current method stored at a specific stack address.
2000 // (2): Object literals like classes and strings, that are loaded from the dex cache
2001 // fields of the current method.
2002 bool NeedsCurrentMethod() const {
2003 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2004 }
2005
Vladimir Markodc151b22015-10-15 18:02:30 +01002006 // Returns whether the code generation of the instruction will require to have access
2007 // to the dex cache of the current method's declaring class via the current method.
2008 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002009
Mark Mendellc4701932015-04-10 13:18:51 -04002010 // Does this instruction have any use in an environment before
2011 // control flow hits 'other'?
2012 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2013
2014 // Remove all references to environment uses of this instruction.
2015 // The caller must ensure that this is safe to do.
2016 void RemoveEnvironmentUsers();
2017
Vladimir Markoa1de9182016-02-25 11:37:38 +00002018 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2019 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002020
David Brazdil1abb4192015-02-17 18:33:36 +00002021 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002022 // If set, the machine code for this instruction is assumed to be generated by
2023 // its users. Used by liveness analysis to compute use positions accordingly.
2024 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2025 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2026 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2027 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2028
David Brazdil1abb4192015-02-17 18:33:36 +00002029 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2030 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2031
Vladimir Markoa1de9182016-02-25 11:37:38 +00002032 uint32_t GetPackedFields() const {
2033 return packed_fields_;
2034 }
2035
2036 template <size_t flag>
2037 bool GetPackedFlag() const {
2038 return (packed_fields_ & (1u << flag)) != 0u;
2039 }
2040
2041 template <size_t flag>
2042 void SetPackedFlag(bool value = true) {
2043 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2044 }
2045
2046 template <typename BitFieldType>
2047 typename BitFieldType::value_type GetPackedField() const {
2048 return BitFieldType::Decode(packed_fields_);
2049 }
2050
2051 template <typename BitFieldType>
2052 void SetPackedField(typename BitFieldType::value_type value) {
2053 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2054 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2055 }
2056
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002057 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002058 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2059 auto before_use_node = uses_.before_begin();
2060 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2061 HInstruction* user = use_node->GetUser();
2062 size_t input_index = use_node->GetIndex();
2063 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2064 before_use_node = use_node;
2065 }
2066 }
2067
2068 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2069 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2070 if (next != uses_.end()) {
2071 HInstruction* next_user = next->GetUser();
2072 size_t next_index = next->GetIndex();
2073 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2074 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2075 }
2076 }
2077
2078 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2079 auto before_env_use_node = env_uses_.before_begin();
2080 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2081 HEnvironment* user = env_use_node->GetUser();
2082 size_t input_index = env_use_node->GetIndex();
2083 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2084 before_env_use_node = env_use_node;
2085 }
2086 }
2087
2088 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2089 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2090 if (next != env_uses_.end()) {
2091 HEnvironment* next_user = next->GetUser();
2092 size_t next_index = next->GetIndex();
2093 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2094 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2095 }
2096 }
David Brazdil1abb4192015-02-17 18:33:36 +00002097
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098 HInstruction* previous_;
2099 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002100 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002101 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002102
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002103 // An instruction gets an id when it is added to the graph.
2104 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002105 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002106 int id_;
2107
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002108 // When doing liveness analysis, instructions that have uses get an SSA index.
2109 int ssa_index_;
2110
Vladimir Markoa1de9182016-02-25 11:37:38 +00002111 // Packed fields.
2112 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002113
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002114 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002115 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002116
2117 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002118 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002119
Nicolas Geoffray39468442014-09-02 15:17:15 +01002120 // The environment associated with this instruction. Not null if the instruction
2121 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002122 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002123
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002124 // Set by the code generator.
2125 LocationSummary* locations_;
2126
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002127 // Set by the liveness analysis.
2128 LiveInterval* live_interval_;
2129
2130 // Set by the liveness analysis, this is the position in a linear
2131 // order of blocks where this instruction's live interval start.
2132 size_t lifetime_position_;
2133
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002134 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002135
Vladimir Markoa1de9182016-02-25 11:37:38 +00002136 // The reference handle part of the reference type info.
2137 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002138 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002139 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002140
David Brazdil1abb4192015-02-17 18:33:36 +00002141 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002142 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002143 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002144 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002145 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146
2147 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2148};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002149std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002150
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002151class HInputIterator : public ValueObject {
2152 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002153 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002154
2155 bool Done() const { return index_ == instruction_->InputCount(); }
2156 HInstruction* Current() const { return instruction_->InputAt(index_); }
2157 void Advance() { index_++; }
2158
2159 private:
2160 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002161 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002162
2163 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2164};
2165
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002166class HInstructionIterator : public ValueObject {
2167 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002168 explicit HInstructionIterator(const HInstructionList& instructions)
2169 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002170 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002171 }
2172
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002173 bool Done() const { return instruction_ == nullptr; }
2174 HInstruction* Current() const { return instruction_; }
2175 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002177 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002178 }
2179
2180 private:
2181 HInstruction* instruction_;
2182 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002183
2184 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185};
2186
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002187class HBackwardInstructionIterator : public ValueObject {
2188 public:
2189 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2190 : instruction_(instructions.last_instruction_) {
2191 next_ = Done() ? nullptr : instruction_->GetPrevious();
2192 }
2193
2194 bool Done() const { return instruction_ == nullptr; }
2195 HInstruction* Current() const { return instruction_; }
2196 void Advance() {
2197 instruction_ = next_;
2198 next_ = Done() ? nullptr : instruction_->GetPrevious();
2199 }
2200
2201 private:
2202 HInstruction* instruction_;
2203 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002204
2205 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002206};
2207
Vladimir Markof9f64412015-09-02 14:05:49 +01002208template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209class HTemplateInstruction: public HInstruction {
2210 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002211 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002212 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002213 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002214
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002215 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002216
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002217 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002218 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2219 DCHECK_LT(i, N);
2220 return inputs_[i];
2221 }
David Brazdil1abb4192015-02-17 18:33:36 +00002222
2223 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002224 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002225 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226 }
2227
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002228 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002229 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002230
2231 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232};
2233
Vladimir Markof9f64412015-09-02 14:05:49 +01002234// HTemplateInstruction specialization for N=0.
2235template<>
2236class HTemplateInstruction<0>: public HInstruction {
2237 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002238 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002239 : HInstruction(side_effects, dex_pc) {}
2240
Vladimir Markof9f64412015-09-02 14:05:49 +01002241 virtual ~HTemplateInstruction() {}
2242
2243 size_t InputCount() const OVERRIDE { return 0; }
2244
2245 protected:
2246 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2247 LOG(FATAL) << "Unreachable";
2248 UNREACHABLE();
2249 }
2250
2251 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2252 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2253 LOG(FATAL) << "Unreachable";
2254 UNREACHABLE();
2255 }
2256
2257 private:
2258 friend class SsaBuilder;
2259};
2260
Dave Allison20dfc792014-06-16 20:44:29 -07002261template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002262class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002263 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002264 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002265 : HTemplateInstruction<N>(side_effects, dex_pc) {
2266 this->template SetPackedField<TypeField>(type);
2267 }
Dave Allison20dfc792014-06-16 20:44:29 -07002268 virtual ~HExpression() {}
2269
Vladimir Markoa1de9182016-02-25 11:37:38 +00002270 Primitive::Type GetType() const OVERRIDE {
2271 return TypeField::Decode(this->GetPackedFields());
2272 }
Dave Allison20dfc792014-06-16 20:44:29 -07002273
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002274 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002275 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2276 static constexpr size_t kFieldTypeSize =
2277 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2278 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2279 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2280 "Too many packed fields.");
2281 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002282};
2283
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002284// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2285// instruction that branches to the exit block.
2286class HReturnVoid : public HTemplateInstruction<0> {
2287 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002288 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2289 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002290
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002291 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002292
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002293 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002294
2295 private:
2296 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2297};
2298
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002299// Represents dex's RETURN opcodes. A HReturn is a control flow
2300// instruction that branches to the exit block.
2301class HReturn : public HTemplateInstruction<1> {
2302 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2304 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002305 SetRawInputAt(0, value);
2306 }
2307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002308 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002309
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002310 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002311
2312 private:
2313 DISALLOW_COPY_AND_ASSIGN(HReturn);
2314};
2315
David Brazdildee58d62016-04-07 09:54:26 +00002316class HPhi : public HInstruction {
2317 public:
2318 HPhi(ArenaAllocator* arena,
2319 uint32_t reg_number,
2320 size_t number_of_inputs,
2321 Primitive::Type type,
2322 uint32_t dex_pc = kNoDexPc)
2323 : HInstruction(SideEffects::None(), dex_pc),
2324 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2325 reg_number_(reg_number) {
2326 SetPackedField<TypeField>(ToPhiType(type));
2327 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2328 // Phis are constructed live and marked dead if conflicting or unused.
2329 // Individual steps of SsaBuilder should assume that if a phi has been
2330 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2331 SetPackedFlag<kFlagIsLive>(true);
2332 SetPackedFlag<kFlagCanBeNull>(true);
2333 }
2334
2335 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2336 static Primitive::Type ToPhiType(Primitive::Type type) {
2337 return Primitive::PrimitiveKind(type);
2338 }
2339
2340 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2341
2342 size_t InputCount() const OVERRIDE { return inputs_.size(); }
2343
2344 void AddInput(HInstruction* input);
2345 void RemoveInputAt(size_t index);
2346
2347 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2348 void SetType(Primitive::Type new_type) {
2349 // Make sure that only valid type changes occur. The following are allowed:
2350 // (1) int -> float/ref (primitive type propagation),
2351 // (2) long -> double (primitive type propagation).
2352 DCHECK(GetType() == new_type ||
2353 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2354 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2355 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2356 SetPackedField<TypeField>(new_type);
2357 }
2358
2359 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2360 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2361
2362 uint32_t GetRegNumber() const { return reg_number_; }
2363
2364 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2365 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2366 bool IsDead() const { return !IsLive(); }
2367 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2368
2369 bool IsVRegEquivalentOf(HInstruction* other) const {
2370 return other != nullptr
2371 && other->IsPhi()
2372 && other->AsPhi()->GetBlock() == GetBlock()
2373 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2374 }
2375
2376 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2377 // An equivalent phi is a phi having the same dex register and type.
2378 // It assumes that phis with the same dex register are adjacent.
2379 HPhi* GetNextEquivalentPhiWithSameType() {
2380 HInstruction* next = GetNext();
2381 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2382 if (next->GetType() == GetType()) {
2383 return next->AsPhi();
2384 }
2385 next = next->GetNext();
2386 }
2387 return nullptr;
2388 }
2389
2390 DECLARE_INSTRUCTION(Phi);
2391
2392 protected:
2393 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
2394 return inputs_[index];
2395 }
2396
2397 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2398 inputs_[index] = input;
2399 }
2400
2401 private:
2402 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2403 static constexpr size_t kFieldTypeSize =
2404 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2405 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2406 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2407 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2408 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2409 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2410
2411 ArenaVector<HUserRecord<HInstruction*> > inputs_;
2412 const uint32_t reg_number_;
2413
2414 DISALLOW_COPY_AND_ASSIGN(HPhi);
2415};
2416
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002417// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002418// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002419// exit block.
2420class HExit : public HTemplateInstruction<0> {
2421 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002422 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002423
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002424 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002425
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002426 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002427
2428 private:
2429 DISALLOW_COPY_AND_ASSIGN(HExit);
2430};
2431
2432// Jumps from one block to another.
2433class HGoto : public HTemplateInstruction<0> {
2434 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002435 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002436
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002437 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002438
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002439 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002440 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002441 }
2442
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002443 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002444
2445 private:
2446 DISALLOW_COPY_AND_ASSIGN(HGoto);
2447};
2448
Roland Levillain9867bc72015-08-05 10:21:34 +01002449class HConstant : public HExpression<0> {
2450 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002451 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2452 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002453
2454 bool CanBeMoved() const OVERRIDE { return true; }
2455
Roland Levillain1a653882016-03-18 18:05:57 +00002456 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002457 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002458 // Is this constant 0 in the arithmetic sense?
2459 virtual bool IsArithmeticZero() const { return false; }
2460 // Is this constant a 0-bit pattern?
2461 virtual bool IsZeroBitPattern() const { return false; }
2462 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002463 virtual bool IsOne() const { return false; }
2464
David Brazdil77a48ae2015-09-15 12:34:04 +00002465 virtual uint64_t GetValueAsUint64() const = 0;
2466
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002467 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002468
2469 private:
2470 DISALLOW_COPY_AND_ASSIGN(HConstant);
2471};
2472
2473class HNullConstant : public HConstant {
2474 public:
2475 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2476 return true;
2477 }
2478
David Brazdil77a48ae2015-09-15 12:34:04 +00002479 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2480
Roland Levillain9867bc72015-08-05 10:21:34 +01002481 size_t ComputeHashCode() const OVERRIDE { return 0; }
2482
Roland Levillain1a653882016-03-18 18:05:57 +00002483 // The null constant representation is a 0-bit pattern.
2484 virtual bool IsZeroBitPattern() const { return true; }
2485
Roland Levillain9867bc72015-08-05 10:21:34 +01002486 DECLARE_INSTRUCTION(NullConstant);
2487
2488 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002489 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002490
2491 friend class HGraph;
2492 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2493};
2494
2495// Constants of the type int. Those can be from Dex instructions, or
2496// synthesized (for example with the if-eqz instruction).
2497class HIntConstant : public HConstant {
2498 public:
2499 int32_t GetValue() const { return value_; }
2500
David Brazdil9f389d42015-10-01 14:32:56 +01002501 uint64_t GetValueAsUint64() const OVERRIDE {
2502 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2503 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002504
Roland Levillain9867bc72015-08-05 10:21:34 +01002505 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002506 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002507 return other->AsIntConstant()->value_ == value_;
2508 }
2509
2510 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2511
2512 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002513 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2514 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002515 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2516
Roland Levillain1a653882016-03-18 18:05:57 +00002517 // Integer constants are used to encode Boolean values as well,
2518 // where 1 means true and 0 means false.
2519 bool IsTrue() const { return GetValue() == 1; }
2520 bool IsFalse() const { return GetValue() == 0; }
2521
Roland Levillain9867bc72015-08-05 10:21:34 +01002522 DECLARE_INSTRUCTION(IntConstant);
2523
2524 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002525 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2526 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2527 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2528 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002529
2530 const int32_t value_;
2531
2532 friend class HGraph;
2533 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2534 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2535 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2536};
2537
2538class HLongConstant : public HConstant {
2539 public:
2540 int64_t GetValue() const { return value_; }
2541
David Brazdil77a48ae2015-09-15 12:34:04 +00002542 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2543
Roland Levillain9867bc72015-08-05 10:21:34 +01002544 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002545 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002546 return other->AsLongConstant()->value_ == value_;
2547 }
2548
2549 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2550
2551 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002552 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2553 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002554 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2555
2556 DECLARE_INSTRUCTION(LongConstant);
2557
2558 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002559 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2560 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002561
2562 const int64_t value_;
2563
2564 friend class HGraph;
2565 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2566};
Dave Allison20dfc792014-06-16 20:44:29 -07002567
Roland Levillain31dd3d62016-02-16 12:21:02 +00002568class HFloatConstant : public HConstant {
2569 public:
2570 float GetValue() const { return value_; }
2571
2572 uint64_t GetValueAsUint64() const OVERRIDE {
2573 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2574 }
2575
2576 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2577 DCHECK(other->IsFloatConstant()) << other->DebugName();
2578 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2579 }
2580
2581 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2582
2583 bool IsMinusOne() const OVERRIDE {
2584 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2585 }
Roland Levillain1a653882016-03-18 18:05:57 +00002586 bool IsArithmeticZero() const OVERRIDE {
2587 return std::fpclassify(value_) == FP_ZERO;
2588 }
2589 bool IsArithmeticPositiveZero() const {
2590 return IsArithmeticZero() && !std::signbit(value_);
2591 }
2592 bool IsArithmeticNegativeZero() const {
2593 return IsArithmeticZero() && std::signbit(value_);
2594 }
2595 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002596 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002597 }
2598 bool IsOne() const OVERRIDE {
2599 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2600 }
2601 bool IsNaN() const {
2602 return std::isnan(value_);
2603 }
2604
2605 DECLARE_INSTRUCTION(FloatConstant);
2606
2607 private:
2608 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2609 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2610 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2611 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2612
2613 const float value_;
2614
2615 // Only the SsaBuilder and HGraph can create floating-point constants.
2616 friend class SsaBuilder;
2617 friend class HGraph;
2618 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2619};
2620
2621class HDoubleConstant : public HConstant {
2622 public:
2623 double GetValue() const { return value_; }
2624
2625 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2626
2627 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2628 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2629 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2630 }
2631
2632 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2633
2634 bool IsMinusOne() const OVERRIDE {
2635 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2636 }
Roland Levillain1a653882016-03-18 18:05:57 +00002637 bool IsArithmeticZero() const OVERRIDE {
2638 return std::fpclassify(value_) == FP_ZERO;
2639 }
2640 bool IsArithmeticPositiveZero() const {
2641 return IsArithmeticZero() && !std::signbit(value_);
2642 }
2643 bool IsArithmeticNegativeZero() const {
2644 return IsArithmeticZero() && std::signbit(value_);
2645 }
2646 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002647 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002648 }
2649 bool IsOne() const OVERRIDE {
2650 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2651 }
2652 bool IsNaN() const {
2653 return std::isnan(value_);
2654 }
2655
2656 DECLARE_INSTRUCTION(DoubleConstant);
2657
2658 private:
2659 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2660 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2661 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2662 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2663
2664 const double value_;
2665
2666 // Only the SsaBuilder and HGraph can create floating-point constants.
2667 friend class SsaBuilder;
2668 friend class HGraph;
2669 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2670};
2671
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002672// Conditional branch. A block ending with an HIf instruction must have
2673// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002674class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002675 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002676 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2677 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002678 SetRawInputAt(0, input);
2679 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002680
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002681 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002682
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002683 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002684 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002685 }
2686
2687 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002688 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002689 }
2690
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002691 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002692
2693 private:
2694 DISALLOW_COPY_AND_ASSIGN(HIf);
2695};
2696
David Brazdilfc6a86a2015-06-26 10:33:45 +00002697
2698// Abstract instruction which marks the beginning and/or end of a try block and
2699// links it to the respective exception handlers. Behaves the same as a Goto in
2700// non-exceptional control flow.
2701// Normal-flow successor is stored at index zero, exception handlers under
2702// higher indices in no particular order.
2703class HTryBoundary : public HTemplateInstruction<0> {
2704 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002705 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002706 kEntry,
2707 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002708 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002709 };
2710
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002711 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002712 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2713 SetPackedField<BoundaryKindField>(kind);
2714 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002715
2716 bool IsControlFlow() const OVERRIDE { return true; }
2717
2718 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002719 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002720
David Brazdild26a4112015-11-10 11:07:31 +00002721 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2722 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2723 }
2724
David Brazdilfc6a86a2015-06-26 10:33:45 +00002725 // Returns whether `handler` is among its exception handlers (non-zero index
2726 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002727 bool HasExceptionHandler(const HBasicBlock& handler) const {
2728 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002729 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002730 }
2731
2732 // If not present already, adds `handler` to its block's list of exception
2733 // handlers.
2734 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002735 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002736 GetBlock()->AddSuccessor(handler);
2737 }
2738 }
2739
Vladimir Markoa1de9182016-02-25 11:37:38 +00002740 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2741 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002742
David Brazdilffee3d32015-07-06 11:48:53 +01002743 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2744
David Brazdilfc6a86a2015-06-26 10:33:45 +00002745 DECLARE_INSTRUCTION(TryBoundary);
2746
2747 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002748 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2749 static constexpr size_t kFieldBoundaryKindSize =
2750 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2751 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2752 kFieldBoundaryKind + kFieldBoundaryKindSize;
2753 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2754 "Too many packed fields.");
2755 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002756
2757 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2758};
2759
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002760// Deoptimize to interpreter, upon checking a condition.
2761class HDeoptimize : public HTemplateInstruction<1> {
2762 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002763 // We set CanTriggerGC to prevent any intermediate address to be live
2764 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002765 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002766 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002767 SetRawInputAt(0, cond);
2768 }
2769
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002770 bool CanBeMoved() const OVERRIDE { return true; }
2771 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2772 return true;
2773 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002774 bool NeedsEnvironment() const OVERRIDE { return true; }
2775 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002776
2777 DECLARE_INSTRUCTION(Deoptimize);
2778
2779 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002780 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2781};
2782
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002783// Represents the ArtMethod that was passed as a first argument to
2784// the method. It is used by instructions that depend on it, like
2785// instructions that work with the dex cache.
2786class HCurrentMethod : public HExpression<0> {
2787 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002788 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2789 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002790
2791 DECLARE_INSTRUCTION(CurrentMethod);
2792
2793 private:
2794 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2795};
2796
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002797// Fetches an ArtMethod from the virtual table or the interface method table
2798// of a class.
2799class HClassTableGet : public HExpression<1> {
2800 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002801 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002802 kVTable,
2803 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002804 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002805 };
2806 HClassTableGet(HInstruction* cls,
2807 Primitive::Type type,
2808 TableKind kind,
2809 size_t index,
2810 uint32_t dex_pc)
2811 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002812 index_(index) {
2813 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002814 SetRawInputAt(0, cls);
2815 }
2816
2817 bool CanBeMoved() const OVERRIDE { return true; }
2818 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2819 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002820 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002821 }
2822
Vladimir Markoa1de9182016-02-25 11:37:38 +00002823 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002824 size_t GetIndex() const { return index_; }
2825
2826 DECLARE_INSTRUCTION(ClassTableGet);
2827
2828 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002829 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2830 static constexpr size_t kFieldTableKindSize =
2831 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2832 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2833 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2834 "Too many packed fields.");
2835 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2836
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002837 // The index of the ArtMethod in the table.
2838 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002839
2840 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2841};
2842
Mark Mendellfe57faa2015-09-18 09:26:15 -04002843// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2844// have one successor for each entry in the switch table, and the final successor
2845// will be the block containing the next Dex opcode.
2846class HPackedSwitch : public HTemplateInstruction<1> {
2847 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002848 HPackedSwitch(int32_t start_value,
2849 uint32_t num_entries,
2850 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002851 uint32_t dex_pc = kNoDexPc)
2852 : HTemplateInstruction(SideEffects::None(), dex_pc),
2853 start_value_(start_value),
2854 num_entries_(num_entries) {
2855 SetRawInputAt(0, input);
2856 }
2857
2858 bool IsControlFlow() const OVERRIDE { return true; }
2859
2860 int32_t GetStartValue() const { return start_value_; }
2861
Vladimir Marko211c2112015-09-24 16:52:33 +01002862 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002863
2864 HBasicBlock* GetDefaultBlock() const {
2865 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002866 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002867 }
2868 DECLARE_INSTRUCTION(PackedSwitch);
2869
2870 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002871 const int32_t start_value_;
2872 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002873
2874 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2875};
2876
Roland Levillain88cb1752014-10-20 16:36:47 +01002877class HUnaryOperation : public HExpression<1> {
2878 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002879 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2880 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002881 SetRawInputAt(0, input);
2882 }
2883
2884 HInstruction* GetInput() const { return InputAt(0); }
2885 Primitive::Type GetResultType() const { return GetType(); }
2886
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002887 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002888 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002889 return true;
2890 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002891
Roland Levillain31dd3d62016-02-16 12:21:02 +00002892 // Try to statically evaluate `this` and return a HConstant
2893 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002894 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002895 HConstant* TryStaticEvaluation() const;
2896
2897 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002898 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2899 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002900 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2901 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002902
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002903 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002904
2905 private:
2906 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2907};
2908
Dave Allison20dfc792014-06-16 20:44:29 -07002909class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002910 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002911 HBinaryOperation(Primitive::Type result_type,
2912 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002913 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002914 SideEffects side_effects = SideEffects::None(),
2915 uint32_t dex_pc = kNoDexPc)
2916 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002917 SetRawInputAt(0, left);
2918 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002919 }
2920
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002921 HInstruction* GetLeft() const { return InputAt(0); }
2922 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002923 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002924
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002925 virtual bool IsCommutative() const { return false; }
2926
2927 // Put constant on the right.
2928 // Returns whether order is changed.
2929 bool OrderInputsWithConstantOnTheRight() {
2930 HInstruction* left = InputAt(0);
2931 HInstruction* right = InputAt(1);
2932 if (left->IsConstant() && !right->IsConstant()) {
2933 ReplaceInput(right, 0);
2934 ReplaceInput(left, 1);
2935 return true;
2936 }
2937 return false;
2938 }
2939
2940 // Order inputs by instruction id, but favor constant on the right side.
2941 // This helps GVN for commutative ops.
2942 void OrderInputs() {
2943 DCHECK(IsCommutative());
2944 HInstruction* left = InputAt(0);
2945 HInstruction* right = InputAt(1);
2946 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2947 return;
2948 }
2949 if (OrderInputsWithConstantOnTheRight()) {
2950 return;
2951 }
2952 // Order according to instruction id.
2953 if (left->GetId() > right->GetId()) {
2954 ReplaceInput(right, 0);
2955 ReplaceInput(left, 1);
2956 }
2957 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002959 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002960 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002961 return true;
2962 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002963
Roland Levillain31dd3d62016-02-16 12:21:02 +00002964 // Try to statically evaluate `this` and return a HConstant
2965 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002966 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002967 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002968
2969 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002970 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2971 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002972 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2973 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002974 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002975 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2976 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002977 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2978 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002979 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2980 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002981 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002982 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2983 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002984
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002985 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002986 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002987 HConstant* GetConstantRight() const;
2988
2989 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002990 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002991 HInstruction* GetLeastConstantLeft() const;
2992
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002993 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002994
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002995 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002996 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2997};
2998
Mark Mendellc4701932015-04-10 13:18:51 -04002999// The comparison bias applies for floating point operations and indicates how NaN
3000// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003001enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003002 kNoBias, // bias is not applicable (i.e. for long operation)
3003 kGtBias, // return 1 for NaN comparisons
3004 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003005 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003006};
3007
Roland Levillain31dd3d62016-02-16 12:21:02 +00003008std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3009
Dave Allison20dfc792014-06-16 20:44:29 -07003010class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003011 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003012 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003013 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3014 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3015 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003016
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003017 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003018 // `instruction`, and disregard moves in between.
3019 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003020
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003021 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003022
3023 virtual IfCondition GetCondition() const = 0;
3024
Mark Mendellc4701932015-04-10 13:18:51 -04003025 virtual IfCondition GetOppositeCondition() const = 0;
3026
Vladimir Markoa1de9182016-02-25 11:37:38 +00003027 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003028 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3029
Vladimir Markoa1de9182016-02-25 11:37:38 +00003030 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3031 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003032
3033 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003034 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003035 }
3036
Roland Levillain4fa13f62015-07-06 18:11:54 +01003037 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003038 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003039 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003040 if (if_cond == kCondNE) {
3041 return true;
3042 } else if (if_cond == kCondEQ) {
3043 return false;
3044 }
3045 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003046 }
3047
3048 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003049 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003050 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003051 if (if_cond == kCondEQ) {
3052 return true;
3053 } else if (if_cond == kCondNE) {
3054 return false;
3055 }
3056 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003057 }
3058
Roland Levillain31dd3d62016-02-16 12:21:02 +00003059 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003060 // Needed if we merge a HCompare into a HCondition.
3061 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3062 static constexpr size_t kFieldComparisonBiasSize =
3063 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3064 static constexpr size_t kNumberOfConditionPackedBits =
3065 kFieldComparisonBias + kFieldComparisonBiasSize;
3066 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3067 using ComparisonBiasField =
3068 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3069
Roland Levillain31dd3d62016-02-16 12:21:02 +00003070 template <typename T>
3071 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3072
3073 template <typename T>
3074 int32_t CompareFP(T x, T y) const {
3075 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3076 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3077 // Handle the bias.
3078 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3079 }
3080
3081 // Return an integer constant containing the result of a condition evaluated at compile time.
3082 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3083 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3084 }
3085
Dave Allison20dfc792014-06-16 20:44:29 -07003086 private:
3087 DISALLOW_COPY_AND_ASSIGN(HCondition);
3088};
3089
3090// Instruction to check if two inputs are equal to each other.
3091class HEqual : public HCondition {
3092 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003093 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3094 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003095
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003096 bool IsCommutative() const OVERRIDE { return true; }
3097
Vladimir Marko9e23df52015-11-10 17:14:35 +00003098 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3099 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003100 return MakeConstantCondition(true, GetDexPc());
3101 }
3102 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3103 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3104 }
3105 // In the following Evaluate methods, a HCompare instruction has
3106 // been merged into this HEqual instruction; evaluate it as
3107 // `Compare(x, y) == 0`.
3108 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3109 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3110 GetDexPc());
3111 }
3112 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3113 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3114 }
3115 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3116 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003117 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003118
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003119 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003120
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003121 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003122 return kCondEQ;
3123 }
3124
Mark Mendellc4701932015-04-10 13:18:51 -04003125 IfCondition GetOppositeCondition() const OVERRIDE {
3126 return kCondNE;
3127 }
3128
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003129 private:
Aart Bike9f37602015-10-09 11:15:55 -07003130 template <typename T> bool Compute(T x, T y) const { return x == y; }
3131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003132 DISALLOW_COPY_AND_ASSIGN(HEqual);
3133};
3134
Dave Allison20dfc792014-06-16 20:44:29 -07003135class HNotEqual : public HCondition {
3136 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003137 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3138 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003139
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003140 bool IsCommutative() const OVERRIDE { return true; }
3141
Vladimir Marko9e23df52015-11-10 17:14:35 +00003142 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3143 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003144 return MakeConstantCondition(false, GetDexPc());
3145 }
3146 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3147 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3148 }
3149 // In the following Evaluate methods, a HCompare instruction has
3150 // been merged into this HNotEqual instruction; evaluate it as
3151 // `Compare(x, y) != 0`.
3152 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3153 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3154 }
3155 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3156 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3157 }
3158 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3159 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003160 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003161
Dave Allison20dfc792014-06-16 20:44:29 -07003162 DECLARE_INSTRUCTION(NotEqual);
3163
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003164 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003165 return kCondNE;
3166 }
3167
Mark Mendellc4701932015-04-10 13:18:51 -04003168 IfCondition GetOppositeCondition() const OVERRIDE {
3169 return kCondEQ;
3170 }
3171
Dave Allison20dfc792014-06-16 20:44:29 -07003172 private:
Aart Bike9f37602015-10-09 11:15:55 -07003173 template <typename T> bool Compute(T x, T y) const { return x != y; }
3174
Dave Allison20dfc792014-06-16 20:44:29 -07003175 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3176};
3177
3178class HLessThan : public HCondition {
3179 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003180 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3181 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003182
Roland Levillain9867bc72015-08-05 10:21:34 +01003183 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003184 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003185 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003186 // In the following Evaluate methods, a HCompare instruction has
3187 // been merged into this HLessThan instruction; evaluate it as
3188 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003189 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003190 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3191 }
3192 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3193 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3194 }
3195 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3196 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003197 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003198
Dave Allison20dfc792014-06-16 20:44:29 -07003199 DECLARE_INSTRUCTION(LessThan);
3200
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003201 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003202 return kCondLT;
3203 }
3204
Mark Mendellc4701932015-04-10 13:18:51 -04003205 IfCondition GetOppositeCondition() const OVERRIDE {
3206 return kCondGE;
3207 }
3208
Dave Allison20dfc792014-06-16 20:44:29 -07003209 private:
Aart Bike9f37602015-10-09 11:15:55 -07003210 template <typename T> bool Compute(T x, T y) const { return x < y; }
3211
Dave Allison20dfc792014-06-16 20:44:29 -07003212 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3213};
3214
3215class HLessThanOrEqual : public HCondition {
3216 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003217 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3218 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003219
Roland Levillain9867bc72015-08-05 10:21:34 +01003220 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003221 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003222 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003223 // In the following Evaluate methods, a HCompare instruction has
3224 // been merged into this HLessThanOrEqual instruction; evaluate it as
3225 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003226 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003227 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3228 }
3229 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3230 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3231 }
3232 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3233 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003234 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003235
Dave Allison20dfc792014-06-16 20:44:29 -07003236 DECLARE_INSTRUCTION(LessThanOrEqual);
3237
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003238 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003239 return kCondLE;
3240 }
3241
Mark Mendellc4701932015-04-10 13:18:51 -04003242 IfCondition GetOppositeCondition() const OVERRIDE {
3243 return kCondGT;
3244 }
3245
Dave Allison20dfc792014-06-16 20:44:29 -07003246 private:
Aart Bike9f37602015-10-09 11:15:55 -07003247 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3248
Dave Allison20dfc792014-06-16 20:44:29 -07003249 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3250};
3251
3252class HGreaterThan : public HCondition {
3253 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003254 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3255 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003256
Roland Levillain9867bc72015-08-05 10:21:34 +01003257 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003258 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003259 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003260 // In the following Evaluate methods, a HCompare instruction has
3261 // been merged into this HGreaterThan instruction; evaluate it as
3262 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003263 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003264 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3265 }
3266 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3267 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3268 }
3269 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3270 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003271 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003272
Dave Allison20dfc792014-06-16 20:44:29 -07003273 DECLARE_INSTRUCTION(GreaterThan);
3274
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003275 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003276 return kCondGT;
3277 }
3278
Mark Mendellc4701932015-04-10 13:18:51 -04003279 IfCondition GetOppositeCondition() const OVERRIDE {
3280 return kCondLE;
3281 }
3282
Dave Allison20dfc792014-06-16 20:44:29 -07003283 private:
Aart Bike9f37602015-10-09 11:15:55 -07003284 template <typename T> bool Compute(T x, T y) const { return x > y; }
3285
Dave Allison20dfc792014-06-16 20:44:29 -07003286 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3287};
3288
3289class HGreaterThanOrEqual : public HCondition {
3290 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003291 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3292 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003293
Roland Levillain9867bc72015-08-05 10:21:34 +01003294 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003295 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003296 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003297 // In the following Evaluate methods, a HCompare instruction has
3298 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3299 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003300 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003301 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3302 }
3303 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3304 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3305 }
3306 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3307 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003308 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003309
Dave Allison20dfc792014-06-16 20:44:29 -07003310 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3311
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003312 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003313 return kCondGE;
3314 }
3315
Mark Mendellc4701932015-04-10 13:18:51 -04003316 IfCondition GetOppositeCondition() const OVERRIDE {
3317 return kCondLT;
3318 }
3319
Dave Allison20dfc792014-06-16 20:44:29 -07003320 private:
Aart Bike9f37602015-10-09 11:15:55 -07003321 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3322
Dave Allison20dfc792014-06-16 20:44:29 -07003323 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3324};
3325
Aart Bike9f37602015-10-09 11:15:55 -07003326class HBelow : public HCondition {
3327 public:
3328 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3329 : HCondition(first, second, dex_pc) {}
3330
3331 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003332 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003333 }
3334 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003335 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3336 }
3337 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3338 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3339 LOG(FATAL) << DebugName() << " is not defined for float values";
3340 UNREACHABLE();
3341 }
3342 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3343 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3344 LOG(FATAL) << DebugName() << " is not defined for double values";
3345 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003346 }
3347
3348 DECLARE_INSTRUCTION(Below);
3349
3350 IfCondition GetCondition() const OVERRIDE {
3351 return kCondB;
3352 }
3353
3354 IfCondition GetOppositeCondition() const OVERRIDE {
3355 return kCondAE;
3356 }
3357
3358 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003359 template <typename T> bool Compute(T x, T y) const {
3360 return MakeUnsigned(x) < MakeUnsigned(y);
3361 }
Aart Bike9f37602015-10-09 11:15:55 -07003362
3363 DISALLOW_COPY_AND_ASSIGN(HBelow);
3364};
3365
3366class HBelowOrEqual : public HCondition {
3367 public:
3368 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3369 : HCondition(first, second, dex_pc) {}
3370
3371 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003372 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003373 }
3374 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003375 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3376 }
3377 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3378 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3379 LOG(FATAL) << DebugName() << " is not defined for float values";
3380 UNREACHABLE();
3381 }
3382 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3383 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3384 LOG(FATAL) << DebugName() << " is not defined for double values";
3385 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003386 }
3387
3388 DECLARE_INSTRUCTION(BelowOrEqual);
3389
3390 IfCondition GetCondition() const OVERRIDE {
3391 return kCondBE;
3392 }
3393
3394 IfCondition GetOppositeCondition() const OVERRIDE {
3395 return kCondA;
3396 }
3397
3398 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003399 template <typename T> bool Compute(T x, T y) const {
3400 return MakeUnsigned(x) <= MakeUnsigned(y);
3401 }
Aart Bike9f37602015-10-09 11:15:55 -07003402
3403 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3404};
3405
3406class HAbove : public HCondition {
3407 public:
3408 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3409 : HCondition(first, second, dex_pc) {}
3410
3411 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003412 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003413 }
3414 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003415 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3416 }
3417 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3418 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3419 LOG(FATAL) << DebugName() << " is not defined for float values";
3420 UNREACHABLE();
3421 }
3422 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3423 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3424 LOG(FATAL) << DebugName() << " is not defined for double values";
3425 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003426 }
3427
3428 DECLARE_INSTRUCTION(Above);
3429
3430 IfCondition GetCondition() const OVERRIDE {
3431 return kCondA;
3432 }
3433
3434 IfCondition GetOppositeCondition() const OVERRIDE {
3435 return kCondBE;
3436 }
3437
3438 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003439 template <typename T> bool Compute(T x, T y) const {
3440 return MakeUnsigned(x) > MakeUnsigned(y);
3441 }
Aart Bike9f37602015-10-09 11:15:55 -07003442
3443 DISALLOW_COPY_AND_ASSIGN(HAbove);
3444};
3445
3446class HAboveOrEqual : public HCondition {
3447 public:
3448 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3449 : HCondition(first, second, dex_pc) {}
3450
3451 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003452 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003453 }
3454 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003455 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3456 }
3457 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3458 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3459 LOG(FATAL) << DebugName() << " is not defined for float values";
3460 UNREACHABLE();
3461 }
3462 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3463 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3464 LOG(FATAL) << DebugName() << " is not defined for double values";
3465 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003466 }
3467
3468 DECLARE_INSTRUCTION(AboveOrEqual);
3469
3470 IfCondition GetCondition() const OVERRIDE {
3471 return kCondAE;
3472 }
3473
3474 IfCondition GetOppositeCondition() const OVERRIDE {
3475 return kCondB;
3476 }
3477
3478 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003479 template <typename T> bool Compute(T x, T y) const {
3480 return MakeUnsigned(x) >= MakeUnsigned(y);
3481 }
Aart Bike9f37602015-10-09 11:15:55 -07003482
3483 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3484};
Dave Allison20dfc792014-06-16 20:44:29 -07003485
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003486// Instruction to check how two inputs compare to each other.
3487// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3488class HCompare : public HBinaryOperation {
3489 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003490 // Note that `comparison_type` is the type of comparison performed
3491 // between the comparison's inputs, not the type of the instantiated
3492 // HCompare instruction (which is always Primitive::kPrimInt).
3493 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003494 HInstruction* first,
3495 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003496 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003497 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003498 : HBinaryOperation(Primitive::kPrimInt,
3499 first,
3500 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003501 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003502 dex_pc) {
3503 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003504 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3505 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003506 }
3507
Roland Levillain9867bc72015-08-05 10:21:34 +01003508 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003509 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3510
3511 template <typename T>
3512 int32_t ComputeFP(T x, T y) const {
3513 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3514 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3515 // Handle the bias.
3516 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3517 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003518
Roland Levillain9867bc72015-08-05 10:21:34 +01003519 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003520 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3521 // reach this code path when processing a freshly built HIR
3522 // graph. However HCompare integer instructions can be synthesized
3523 // by the instruction simplifier to implement IntegerCompare and
3524 // IntegerSignum intrinsics, so we have to handle this case.
3525 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003526 }
3527 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003528 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3529 }
3530 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3531 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3532 }
3533 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3534 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003535 }
3536
Calin Juravleddb7df22014-11-25 20:56:51 +00003537 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003538 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003539 }
3540
Vladimir Markoa1de9182016-02-25 11:37:38 +00003541 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003542
Roland Levillain31dd3d62016-02-16 12:21:02 +00003543 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003544 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003545 bool IsGtBias() const {
3546 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003547 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003549
Roland Levillain1693a1f2016-03-15 14:57:31 +00003550 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3551 // Comparisons do not require a runtime call in any back end.
3552 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003553 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003554
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003555 DECLARE_INSTRUCTION(Compare);
3556
Roland Levillain31dd3d62016-02-16 12:21:02 +00003557 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003558 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3559 static constexpr size_t kFieldComparisonBiasSize =
3560 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3561 static constexpr size_t kNumberOfComparePackedBits =
3562 kFieldComparisonBias + kFieldComparisonBiasSize;
3563 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3564 using ComparisonBiasField =
3565 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3566
Roland Levillain31dd3d62016-02-16 12:21:02 +00003567 // Return an integer constant containing the result of a comparison evaluated at compile time.
3568 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3569 DCHECK(value == -1 || value == 0 || value == 1) << value;
3570 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3571 }
3572
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003573 private:
3574 DISALLOW_COPY_AND_ASSIGN(HCompare);
3575};
3576
David Brazdil6de19382016-01-08 17:37:10 +00003577class HNewInstance : public HExpression<2> {
3578 public:
3579 HNewInstance(HInstruction* cls,
3580 HCurrentMethod* current_method,
3581 uint32_t dex_pc,
3582 uint16_t type_index,
3583 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003584 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003585 bool finalizable,
3586 QuickEntrypointEnum entrypoint)
3587 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3588 type_index_(type_index),
3589 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003590 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003591 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003592 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003593 SetRawInputAt(0, cls);
3594 SetRawInputAt(1, current_method);
3595 }
3596
3597 uint16_t GetTypeIndex() const { return type_index_; }
3598 const DexFile& GetDexFile() const { return dex_file_; }
3599
3600 // Calls runtime so needs an environment.
3601 bool NeedsEnvironment() const OVERRIDE { return true; }
3602
Mingyao Yang062157f2016-03-02 10:15:36 -08003603 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3604 bool CanThrow() const OVERRIDE { return true; }
3605
3606 // Needs to call into runtime to make sure it's instantiable/accessible.
3607 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003608
Vladimir Markoa1de9182016-02-25 11:37:38 +00003609 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003610
3611 bool CanBeNull() const OVERRIDE { return false; }
3612
3613 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3614
3615 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3616 entrypoint_ = entrypoint;
3617 }
3618
3619 bool IsStringAlloc() const;
3620
3621 DECLARE_INSTRUCTION(NewInstance);
3622
3623 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003624 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3625 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003626 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3627 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3628 "Too many packed fields.");
3629
David Brazdil6de19382016-01-08 17:37:10 +00003630 const uint16_t type_index_;
3631 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003632 QuickEntrypointEnum entrypoint_;
3633
3634 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3635};
3636
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003637enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003638#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3639 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003640#include "intrinsics_list.h"
3641 kNone,
3642 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3643#undef INTRINSICS_LIST
3644#undef OPTIMIZING_INTRINSICS
3645};
3646std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3647
Agi Csaki05f20562015-08-19 14:58:14 -07003648enum IntrinsicNeedsEnvironmentOrCache {
3649 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3650 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003651};
3652
Aart Bik5d75afe2015-12-14 11:57:01 -08003653enum IntrinsicSideEffects {
3654 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3655 kReadSideEffects, // Intrinsic may read heap memory.
3656 kWriteSideEffects, // Intrinsic may write heap memory.
3657 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3658};
3659
3660enum IntrinsicExceptions {
3661 kNoThrow, // Intrinsic does not throw any exceptions.
3662 kCanThrow // Intrinsic may throw exceptions.
3663};
3664
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003665class HInvoke : public HInstruction {
3666 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003667 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003668
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003669 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003670
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003671 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003672 SetRawInputAt(index, argument);
3673 }
3674
Roland Levillain3e3d7332015-04-28 11:00:54 +01003675 // Return the number of arguments. This number can be lower than
3676 // the number of inputs returned by InputCount(), as some invoke
3677 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3678 // inputs at the end of their list of inputs.
3679 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3680
Vladimir Markoa1de9182016-02-25 11:37:38 +00003681 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003682
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003683 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003684 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003685
Vladimir Markoa1de9182016-02-25 11:37:38 +00003686 InvokeType GetOriginalInvokeType() const {
3687 return GetPackedField<OriginalInvokeTypeField>();
3688 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003689
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003690 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003691 return intrinsic_;
3692 }
3693
Aart Bik5d75afe2015-12-14 11:57:01 -08003694 void SetIntrinsic(Intrinsics intrinsic,
3695 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3696 IntrinsicSideEffects side_effects,
3697 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003698
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003699 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003700 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003701 }
3702
Vladimir Markoa1de9182016-02-25 11:37:38 +00003703 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003704
3705 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3706
3707 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3708 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3709 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003710
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003711 uint32_t* GetIntrinsicOptimizations() {
3712 return &intrinsic_optimizations_;
3713 }
3714
3715 const uint32_t* GetIntrinsicOptimizations() const {
3716 return &intrinsic_optimizations_;
3717 }
3718
3719 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3720
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003721 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003722
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003723 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003724 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3725 static constexpr size_t kFieldOriginalInvokeTypeSize =
3726 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3727 static constexpr size_t kFieldReturnType =
3728 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3729 static constexpr size_t kFieldReturnTypeSize =
3730 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3731 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3732 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3733 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3734 using OriginalInvokeTypeField =
3735 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3736 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3737
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003738 HInvoke(ArenaAllocator* arena,
3739 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003740 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003741 Primitive::Type return_type,
3742 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003743 uint32_t dex_method_index,
3744 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003745 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003746 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003747 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003748 inputs_(number_of_arguments + number_of_other_inputs,
3749 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003750 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003751 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003752 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003753 SetPackedField<ReturnTypeField>(return_type);
3754 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3755 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003756 }
3757
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003758 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003759 return inputs_[index];
3760 }
3761
David Brazdil1abb4192015-02-17 18:33:36 +00003762 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003763 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003764 }
3765
Vladimir Markoa1de9182016-02-25 11:37:38 +00003766 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003767
Roland Levillain3e3d7332015-04-28 11:00:54 +01003768 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003769 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003770 const uint32_t dex_method_index_;
3771 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003772
3773 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3774 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003775
3776 private:
3777 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3778};
3779
Calin Juravle175dc732015-08-25 15:42:32 +01003780class HInvokeUnresolved : public HInvoke {
3781 public:
3782 HInvokeUnresolved(ArenaAllocator* arena,
3783 uint32_t number_of_arguments,
3784 Primitive::Type return_type,
3785 uint32_t dex_pc,
3786 uint32_t dex_method_index,
3787 InvokeType invoke_type)
3788 : HInvoke(arena,
3789 number_of_arguments,
3790 0u /* number_of_other_inputs */,
3791 return_type,
3792 dex_pc,
3793 dex_method_index,
3794 invoke_type) {
3795 }
3796
3797 DECLARE_INSTRUCTION(InvokeUnresolved);
3798
3799 private:
3800 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3801};
3802
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003803class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003804 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003805 // Requirements of this method call regarding the class
3806 // initialization (clinit) check of its declaring class.
3807 enum class ClinitCheckRequirement {
3808 kNone, // Class already initialized.
3809 kExplicit, // Static call having explicit clinit check as last input.
3810 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003811 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003812 };
3813
Vladimir Marko58155012015-08-19 12:49:41 +00003814 // Determines how to load the target ArtMethod*.
3815 enum class MethodLoadKind {
3816 // Use a String init ArtMethod* loaded from Thread entrypoints.
3817 kStringInit,
3818
3819 // Use the method's own ArtMethod* loaded by the register allocator.
3820 kRecursive,
3821
3822 // Use ArtMethod* at a known address, embed the direct address in the code.
3823 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3824 kDirectAddress,
3825
3826 // Use ArtMethod* at an address that will be known at link time, embed the direct
3827 // address in the code. If the image is relocatable, emit .patch_oat entry.
3828 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3829 // the image relocatable or not.
3830 kDirectAddressWithFixup,
3831
Vladimir Markoa1de9182016-02-25 11:37:38 +00003832 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003833 // Used when we need to use the dex cache, for example for invoke-static that
3834 // may cause class initialization (the entry may point to a resolution method),
3835 // and we know that we can access the dex cache arrays using a PC-relative load.
3836 kDexCachePcRelative,
3837
3838 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3839 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3840 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3841 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3842 kDexCacheViaMethod,
3843 };
3844
3845 // Determines the location of the code pointer.
3846 enum class CodePtrLocation {
3847 // Recursive call, use local PC-relative call instruction.
3848 kCallSelf,
3849
3850 // Use PC-relative call instruction patched at link time.
3851 // Used for calls within an oat file, boot->boot or app->app.
3852 kCallPCRelative,
3853
3854 // Call to a known target address, embed the direct address in code.
3855 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3856 kCallDirect,
3857
3858 // Call to a target address that will be known at link time, embed the direct
3859 // address in code. If the image is relocatable, emit .patch_oat entry.
3860 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3861 // the image relocatable or not.
3862 kCallDirectWithFixup,
3863
3864 // Use code pointer from the ArtMethod*.
3865 // Used when we don't know the target code. This is also the last-resort-kind used when
3866 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3867 kCallArtMethod,
3868 };
3869
3870 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003871 MethodLoadKind method_load_kind;
3872 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003873 // The method load data holds
3874 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3875 // Note that there are multiple string init methods, each having its own offset.
3876 // - the method address for kDirectAddress
3877 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003878 uint64_t method_load_data;
3879 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003880 };
3881
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003882 HInvokeStaticOrDirect(ArenaAllocator* arena,
3883 uint32_t number_of_arguments,
3884 Primitive::Type return_type,
3885 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003886 uint32_t method_index,
3887 MethodReference target_method,
3888 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003889 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003890 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003891 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003892 : HInvoke(arena,
3893 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003894 // There is potentially one extra argument for the HCurrentMethod node, and
3895 // potentially one other if the clinit check is explicit, and potentially
3896 // one other if the method is a string factory.
3897 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003898 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003899 return_type,
3900 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003901 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003902 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003903 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003904 dispatch_info_(dispatch_info) {
3905 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3906 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3907 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003908
Vladimir Markodc151b22015-10-15 18:02:30 +01003909 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003910 bool had_current_method_input = HasCurrentMethodInput();
3911 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3912
3913 // Using the current method is the default and once we find a better
3914 // method load kind, we should not go back to using the current method.
3915 DCHECK(had_current_method_input || !needs_current_method_input);
3916
3917 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003918 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3919 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003920 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003921 dispatch_info_ = dispatch_info;
3922 }
3923
Vladimir Markoc53c0792015-11-19 15:48:33 +00003924 void AddSpecialInput(HInstruction* input) {
3925 // We allow only one special input.
3926 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3927 DCHECK(InputCount() == GetSpecialInputIndex() ||
3928 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3929 InsertInputAt(GetSpecialInputIndex(), input);
3930 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003931
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003932 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003933 // We access the method via the dex cache so we can't do an implicit null check.
3934 // TODO: for intrinsics we can generate implicit null checks.
3935 return false;
3936 }
3937
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003938 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003939 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003940 }
3941
Vladimir Markoc53c0792015-11-19 15:48:33 +00003942 // Get the index of the special input, if any.
3943 //
David Brazdil6de19382016-01-08 17:37:10 +00003944 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3945 // method pointer; otherwise there may be one platform-specific special input,
3946 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003947 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003948 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003949
Vladimir Markoa1de9182016-02-25 11:37:38 +00003950 InvokeType GetOptimizedInvokeType() const {
3951 return GetPackedField<OptimizedInvokeTypeField>();
3952 }
3953
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003954 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003955 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003956 }
3957
Vladimir Marko58155012015-08-19 12:49:41 +00003958 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3959 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3960 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003961 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003962 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003963 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003964 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003965 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3966 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003967 bool HasCurrentMethodInput() const {
3968 // This function can be called only after the invoke has been fully initialized by the builder.
3969 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003970 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003971 return true;
3972 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003973 DCHECK(InputCount() == GetSpecialInputIndex() ||
3974 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003975 return false;
3976 }
3977 }
Vladimir Marko58155012015-08-19 12:49:41 +00003978 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3979 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003980 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003981
3982 int32_t GetStringInitOffset() const {
3983 DCHECK(IsStringInit());
3984 return dispatch_info_.method_load_data;
3985 }
3986
3987 uint64_t GetMethodAddress() const {
3988 DCHECK(HasMethodAddress());
3989 return dispatch_info_.method_load_data;
3990 }
3991
3992 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003993 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003994 return dispatch_info_.method_load_data;
3995 }
3996
3997 uint64_t GetDirectCodePtr() const {
3998 DCHECK(HasDirectCodePtr());
3999 return dispatch_info_.direct_code_ptr;
4000 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004001
Vladimir Markoa1de9182016-02-25 11:37:38 +00004002 ClinitCheckRequirement GetClinitCheckRequirement() const {
4003 return GetPackedField<ClinitCheckRequirementField>();
4004 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004005
Roland Levillain4c0eb422015-04-24 16:43:49 +01004006 // Is this instruction a call to a static method?
4007 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004008 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004009 }
4010
Vladimir Markofbb184a2015-11-13 14:47:00 +00004011 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4012 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4013 // instruction; only relevant for static calls with explicit clinit check.
4014 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004015 DCHECK(IsStaticWithExplicitClinitCheck());
4016 size_t last_input_index = InputCount() - 1;
4017 HInstruction* last_input = InputAt(last_input_index);
4018 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004019 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004020 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004021 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004022 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004023 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004024 }
4025
4026 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004027 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004028 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004029 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004030 }
4031
4032 // Is this a call to a static method whose declaring class has an
4033 // implicit intialization check requirement?
4034 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004035 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004036 }
4037
Vladimir Markob554b5a2015-11-06 12:57:55 +00004038 // Does this method load kind need the current method as an input?
4039 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4040 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4041 }
4042
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004043 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004044
Roland Levillain4c0eb422015-04-24 16:43:49 +01004045 protected:
4046 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4047 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4048 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4049 HInstruction* input = input_record.GetInstruction();
4050 // `input` is the last input of a static invoke marked as having
4051 // an explicit clinit check. It must either be:
4052 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4053 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4054 DCHECK(input != nullptr);
4055 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4056 }
4057 return input_record;
4058 }
4059
Vladimir Markoc53c0792015-11-19 15:48:33 +00004060 void InsertInputAt(size_t index, HInstruction* input);
4061 void RemoveInputAt(size_t index);
4062
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004063 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004064 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4065 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4066 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4067 static constexpr size_t kFieldClinitCheckRequirement =
4068 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4069 static constexpr size_t kFieldClinitCheckRequirementSize =
4070 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4071 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4072 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4073 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4074 "Too many packed fields.");
4075 using OptimizedInvokeTypeField =
4076 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4077 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4078 kFieldClinitCheckRequirement,
4079 kFieldClinitCheckRequirementSize>;
4080
Vladimir Marko58155012015-08-19 12:49:41 +00004081 // The target method may refer to different dex file or method index than the original
4082 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4083 // in derived class to increase visibility.
4084 MethodReference target_method_;
4085 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004086
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004087 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004088};
Vladimir Markof64242a2015-12-01 14:58:23 +00004089std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004090std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004091
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004092class HInvokeVirtual : public HInvoke {
4093 public:
4094 HInvokeVirtual(ArenaAllocator* arena,
4095 uint32_t number_of_arguments,
4096 Primitive::Type return_type,
4097 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004098 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004099 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004100 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004101 vtable_index_(vtable_index) {}
4102
Calin Juravle641547a2015-04-21 22:08:51 +01004103 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004104 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004105 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004106 }
4107
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004108 uint32_t GetVTableIndex() const { return vtable_index_; }
4109
4110 DECLARE_INSTRUCTION(InvokeVirtual);
4111
4112 private:
4113 const uint32_t vtable_index_;
4114
4115 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4116};
4117
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004118class HInvokeInterface : public HInvoke {
4119 public:
4120 HInvokeInterface(ArenaAllocator* arena,
4121 uint32_t number_of_arguments,
4122 Primitive::Type return_type,
4123 uint32_t dex_pc,
4124 uint32_t dex_method_index,
4125 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004126 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004127 imt_index_(imt_index) {}
4128
Calin Juravle641547a2015-04-21 22:08:51 +01004129 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004130 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004131 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004132 }
4133
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004134 uint32_t GetImtIndex() const { return imt_index_; }
4135 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4136
4137 DECLARE_INSTRUCTION(InvokeInterface);
4138
4139 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004140 const uint32_t imt_index_;
4141
4142 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4143};
4144
Roland Levillain88cb1752014-10-20 16:36:47 +01004145class HNeg : public HUnaryOperation {
4146 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004148 : HUnaryOperation(result_type, input, dex_pc) {
4149 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4150 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004151
Roland Levillain9867bc72015-08-05 10:21:34 +01004152 template <typename T> T Compute(T x) const { return -x; }
4153
4154 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004155 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004156 }
4157 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004158 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004159 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004160 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4161 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4162 }
4163 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4164 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4165 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004166
Roland Levillain88cb1752014-10-20 16:36:47 +01004167 DECLARE_INSTRUCTION(Neg);
4168
4169 private:
4170 DISALLOW_COPY_AND_ASSIGN(HNeg);
4171};
4172
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004173class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004174 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004175 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004176 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004177 uint32_t dex_pc,
4178 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004179 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004180 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004181 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004182 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004183 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004184 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004185 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004186 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004187 }
4188
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004189 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004190 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004191
4192 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004193 bool NeedsEnvironment() const OVERRIDE { return true; }
4194
Mingyao Yang0c365e62015-03-31 15:09:29 -07004195 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4196 bool CanThrow() const OVERRIDE { return true; }
4197
Calin Juravle10e244f2015-01-26 18:54:32 +00004198 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004199
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004200 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4201
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004202 DECLARE_INSTRUCTION(NewArray);
4203
4204 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004205 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004206 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004207 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004208
4209 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4210};
4211
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004212class HAdd : public HBinaryOperation {
4213 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004214 HAdd(Primitive::Type result_type,
4215 HInstruction* left,
4216 HInstruction* right,
4217 uint32_t dex_pc = kNoDexPc)
4218 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004219
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004220 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004221
Roland Levillain9867bc72015-08-05 10:21:34 +01004222 template <typename T> T Compute(T x, T y) const { return x + y; }
4223
4224 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004225 return GetBlock()->GetGraph()->GetIntConstant(
4226 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004227 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004228 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004229 return GetBlock()->GetGraph()->GetLongConstant(
4230 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004231 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004232 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4233 return GetBlock()->GetGraph()->GetFloatConstant(
4234 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4235 }
4236 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4237 return GetBlock()->GetGraph()->GetDoubleConstant(
4238 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4239 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004240
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004241 DECLARE_INSTRUCTION(Add);
4242
4243 private:
4244 DISALLOW_COPY_AND_ASSIGN(HAdd);
4245};
4246
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004247class HSub : public HBinaryOperation {
4248 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004249 HSub(Primitive::Type result_type,
4250 HInstruction* left,
4251 HInstruction* right,
4252 uint32_t dex_pc = kNoDexPc)
4253 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004254
Roland Levillain9867bc72015-08-05 10:21:34 +01004255 template <typename T> T Compute(T x, T y) const { return x - y; }
4256
4257 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004258 return GetBlock()->GetGraph()->GetIntConstant(
4259 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004260 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004261 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004262 return GetBlock()->GetGraph()->GetLongConstant(
4263 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004264 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004265 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4266 return GetBlock()->GetGraph()->GetFloatConstant(
4267 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4268 }
4269 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4270 return GetBlock()->GetGraph()->GetDoubleConstant(
4271 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4272 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004273
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004274 DECLARE_INSTRUCTION(Sub);
4275
4276 private:
4277 DISALLOW_COPY_AND_ASSIGN(HSub);
4278};
4279
Calin Juravle34bacdf2014-10-07 20:23:36 +01004280class HMul : public HBinaryOperation {
4281 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004282 HMul(Primitive::Type result_type,
4283 HInstruction* left,
4284 HInstruction* right,
4285 uint32_t dex_pc = kNoDexPc)
4286 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004287
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004288 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004289
Roland Levillain9867bc72015-08-05 10:21:34 +01004290 template <typename T> T Compute(T x, T y) const { return x * y; }
4291
4292 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004293 return GetBlock()->GetGraph()->GetIntConstant(
4294 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004295 }
4296 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004297 return GetBlock()->GetGraph()->GetLongConstant(
4298 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004299 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004300 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4301 return GetBlock()->GetGraph()->GetFloatConstant(
4302 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4303 }
4304 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4305 return GetBlock()->GetGraph()->GetDoubleConstant(
4306 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4307 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004308
4309 DECLARE_INSTRUCTION(Mul);
4310
4311 private:
4312 DISALLOW_COPY_AND_ASSIGN(HMul);
4313};
4314
Calin Juravle7c4954d2014-10-28 16:57:40 +00004315class HDiv : public HBinaryOperation {
4316 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004317 HDiv(Primitive::Type result_type,
4318 HInstruction* left,
4319 HInstruction* right,
4320 uint32_t dex_pc)
4321 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004322
Roland Levillain9867bc72015-08-05 10:21:34 +01004323 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004324 T ComputeIntegral(T x, T y) const {
4325 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004326 // Our graph structure ensures we never have 0 for `y` during
4327 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004328 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004329 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004330 return (y == -1) ? -x : x / y;
4331 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004332
Roland Levillain31dd3d62016-02-16 12:21:02 +00004333 template <typename T>
4334 T ComputeFP(T x, T y) const {
4335 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4336 return x / y;
4337 }
4338
Roland Levillain9867bc72015-08-05 10:21:34 +01004339 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004340 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004341 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004342 }
4343 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004344 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004345 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4346 }
4347 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4348 return GetBlock()->GetGraph()->GetFloatConstant(
4349 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4350 }
4351 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4352 return GetBlock()->GetGraph()->GetDoubleConstant(
4353 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004354 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004355
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004356 static SideEffects SideEffectsForArchRuntimeCalls() {
4357 // The generated code can use a runtime call.
4358 return SideEffects::CanTriggerGC();
4359 }
4360
Calin Juravle7c4954d2014-10-28 16:57:40 +00004361 DECLARE_INSTRUCTION(Div);
4362
4363 private:
4364 DISALLOW_COPY_AND_ASSIGN(HDiv);
4365};
4366
Calin Juravlebacfec32014-11-14 15:54:36 +00004367class HRem : public HBinaryOperation {
4368 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004369 HRem(Primitive::Type result_type,
4370 HInstruction* left,
4371 HInstruction* right,
4372 uint32_t dex_pc)
4373 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004374
Roland Levillain9867bc72015-08-05 10:21:34 +01004375 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004376 T ComputeIntegral(T x, T y) const {
4377 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004378 // Our graph structure ensures we never have 0 for `y` during
4379 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004380 DCHECK_NE(y, 0);
4381 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4382 return (y == -1) ? 0 : x % y;
4383 }
4384
Roland Levillain31dd3d62016-02-16 12:21:02 +00004385 template <typename T>
4386 T ComputeFP(T x, T y) const {
4387 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4388 return std::fmod(x, y);
4389 }
4390
Roland Levillain9867bc72015-08-05 10:21:34 +01004391 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004392 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004393 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004394 }
4395 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004396 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004397 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004398 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004399 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4400 return GetBlock()->GetGraph()->GetFloatConstant(
4401 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4402 }
4403 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4404 return GetBlock()->GetGraph()->GetDoubleConstant(
4405 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4406 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004407
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004408 static SideEffects SideEffectsForArchRuntimeCalls() {
4409 return SideEffects::CanTriggerGC();
4410 }
4411
Calin Juravlebacfec32014-11-14 15:54:36 +00004412 DECLARE_INSTRUCTION(Rem);
4413
4414 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004415 DISALLOW_COPY_AND_ASSIGN(HRem);
4416};
4417
Calin Juravled0d48522014-11-04 16:40:20 +00004418class HDivZeroCheck : public HExpression<1> {
4419 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004420 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4421 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004422 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004423 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004424 SetRawInputAt(0, value);
4425 }
4426
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004427 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4428
Calin Juravled0d48522014-11-04 16:40:20 +00004429 bool CanBeMoved() const OVERRIDE { return true; }
4430
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004431 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004432 return true;
4433 }
4434
4435 bool NeedsEnvironment() const OVERRIDE { return true; }
4436 bool CanThrow() const OVERRIDE { return true; }
4437
Calin Juravled0d48522014-11-04 16:40:20 +00004438 DECLARE_INSTRUCTION(DivZeroCheck);
4439
4440 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004441 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4442};
4443
Calin Juravle9aec02f2014-11-18 23:06:35 +00004444class HShl : public HBinaryOperation {
4445 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004446 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004447 HInstruction* value,
4448 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004449 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004450 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4451 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4452 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004453 }
4454
Roland Levillain5b5b9312016-03-22 14:57:31 +00004455 template <typename T>
4456 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4457 return value << (distance & max_shift_distance);
4458 }
4459
4460 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004461 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004462 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004463 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004464 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004465 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004466 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004467 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004468 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4469 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4470 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4471 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004472 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004473 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4474 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004475 LOG(FATAL) << DebugName() << " is not defined for float values";
4476 UNREACHABLE();
4477 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004478 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4479 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004480 LOG(FATAL) << DebugName() << " is not defined for double values";
4481 UNREACHABLE();
4482 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004483
4484 DECLARE_INSTRUCTION(Shl);
4485
4486 private:
4487 DISALLOW_COPY_AND_ASSIGN(HShl);
4488};
4489
4490class HShr : public HBinaryOperation {
4491 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004492 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004493 HInstruction* value,
4494 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004495 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004496 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4497 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4498 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004499 }
4500
Roland Levillain5b5b9312016-03-22 14:57:31 +00004501 template <typename T>
4502 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4503 return value >> (distance & max_shift_distance);
4504 }
4505
4506 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004507 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004508 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004509 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004510 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004511 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004512 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004514 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4515 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4516 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4517 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004519 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4520 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004521 LOG(FATAL) << DebugName() << " is not defined for float values";
4522 UNREACHABLE();
4523 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004524 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4525 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004526 LOG(FATAL) << DebugName() << " is not defined for double values";
4527 UNREACHABLE();
4528 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004529
4530 DECLARE_INSTRUCTION(Shr);
4531
4532 private:
4533 DISALLOW_COPY_AND_ASSIGN(HShr);
4534};
4535
4536class HUShr : public HBinaryOperation {
4537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004538 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004539 HInstruction* value,
4540 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004542 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4543 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4544 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004545 }
4546
Roland Levillain5b5b9312016-03-22 14:57:31 +00004547 template <typename T>
4548 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4549 typedef typename std::make_unsigned<T>::type V;
4550 V ux = static_cast<V>(value);
4551 return static_cast<T>(ux >> (distance & max_shift_distance));
4552 }
4553
4554 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004555 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004556 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004557 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004558 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004559 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004560 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004561 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004562 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4563 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4564 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4565 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004566 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004567 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4568 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004569 LOG(FATAL) << DebugName() << " is not defined for float values";
4570 UNREACHABLE();
4571 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4573 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004574 LOG(FATAL) << DebugName() << " is not defined for double values";
4575 UNREACHABLE();
4576 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004577
4578 DECLARE_INSTRUCTION(UShr);
4579
4580 private:
4581 DISALLOW_COPY_AND_ASSIGN(HUShr);
4582};
4583
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004584class HAnd : public HBinaryOperation {
4585 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004586 HAnd(Primitive::Type result_type,
4587 HInstruction* left,
4588 HInstruction* right,
4589 uint32_t dex_pc = kNoDexPc)
4590 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004591
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004592 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004593
Roland Levillaine53bd812016-02-24 14:54:18 +00004594 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004595
4596 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004597 return GetBlock()->GetGraph()->GetIntConstant(
4598 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004599 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004600 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004601 return GetBlock()->GetGraph()->GetLongConstant(
4602 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004603 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004604 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4605 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4606 LOG(FATAL) << DebugName() << " is not defined for float values";
4607 UNREACHABLE();
4608 }
4609 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4610 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4611 LOG(FATAL) << DebugName() << " is not defined for double values";
4612 UNREACHABLE();
4613 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004614
4615 DECLARE_INSTRUCTION(And);
4616
4617 private:
4618 DISALLOW_COPY_AND_ASSIGN(HAnd);
4619};
4620
4621class HOr : public HBinaryOperation {
4622 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004623 HOr(Primitive::Type result_type,
4624 HInstruction* left,
4625 HInstruction* right,
4626 uint32_t dex_pc = kNoDexPc)
4627 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004628
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004629 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004630
Roland Levillaine53bd812016-02-24 14:54:18 +00004631 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004632
4633 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004634 return GetBlock()->GetGraph()->GetIntConstant(
4635 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004636 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004637 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004638 return GetBlock()->GetGraph()->GetLongConstant(
4639 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004640 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004641 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4642 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4643 LOG(FATAL) << DebugName() << " is not defined for float values";
4644 UNREACHABLE();
4645 }
4646 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4647 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4648 LOG(FATAL) << DebugName() << " is not defined for double values";
4649 UNREACHABLE();
4650 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004651
4652 DECLARE_INSTRUCTION(Or);
4653
4654 private:
4655 DISALLOW_COPY_AND_ASSIGN(HOr);
4656};
4657
4658class HXor : public HBinaryOperation {
4659 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004660 HXor(Primitive::Type result_type,
4661 HInstruction* left,
4662 HInstruction* right,
4663 uint32_t dex_pc = kNoDexPc)
4664 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004665
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004666 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004667
Roland Levillaine53bd812016-02-24 14:54:18 +00004668 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004669
4670 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 return GetBlock()->GetGraph()->GetIntConstant(
4672 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004673 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004674 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004675 return GetBlock()->GetGraph()->GetLongConstant(
4676 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004677 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004678 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4679 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4680 LOG(FATAL) << DebugName() << " is not defined for float values";
4681 UNREACHABLE();
4682 }
4683 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4684 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4685 LOG(FATAL) << DebugName() << " is not defined for double values";
4686 UNREACHABLE();
4687 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004688
4689 DECLARE_INSTRUCTION(Xor);
4690
4691 private:
4692 DISALLOW_COPY_AND_ASSIGN(HXor);
4693};
4694
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004695class HRor : public HBinaryOperation {
4696 public:
4697 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004698 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004699 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4700 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004701 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004702
Roland Levillain5b5b9312016-03-22 14:57:31 +00004703 template <typename T>
4704 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4705 typedef typename std::make_unsigned<T>::type V;
4706 V ux = static_cast<V>(value);
4707 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004708 return static_cast<T>(ux);
4709 } else {
4710 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004711 return static_cast<T>(ux >> (distance & max_shift_value)) |
4712 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004713 }
4714 }
4715
Roland Levillain5b5b9312016-03-22 14:57:31 +00004716 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004717 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004718 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004719 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004720 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004721 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004722 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004723 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004724 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4725 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4726 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4727 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004728 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004729 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4730 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004731 LOG(FATAL) << DebugName() << " is not defined for float values";
4732 UNREACHABLE();
4733 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004734 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4735 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004736 LOG(FATAL) << DebugName() << " is not defined for double values";
4737 UNREACHABLE();
4738 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004739
4740 DECLARE_INSTRUCTION(Ror);
4741
4742 private:
4743 DISALLOW_COPY_AND_ASSIGN(HRor);
4744};
4745
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004746// The value of a parameter in this method. Its location depends on
4747// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004748class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004749 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004750 HParameterValue(const DexFile& dex_file,
4751 uint16_t type_index,
4752 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004753 Primitive::Type parameter_type,
4754 bool is_this = false)
4755 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004756 dex_file_(dex_file),
4757 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004758 index_(index) {
4759 SetPackedFlag<kFlagIsThis>(is_this);
4760 SetPackedFlag<kFlagCanBeNull>(!is_this);
4761 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004762
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004763 const DexFile& GetDexFile() const { return dex_file_; }
4764 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004765 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004766 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004767
Vladimir Markoa1de9182016-02-25 11:37:38 +00004768 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4769 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004770
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004771 DECLARE_INSTRUCTION(ParameterValue);
4772
4773 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004774 // Whether or not the parameter value corresponds to 'this' argument.
4775 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4776 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4777 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4778 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4779 "Too many packed fields.");
4780
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004781 const DexFile& dex_file_;
4782 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004783 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004784 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004785 const uint8_t index_;
4786
4787 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4788};
4789
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004790class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004791 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004792 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4793 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004794
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004795 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004796 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004797 return true;
4798 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004799
Roland Levillain9867bc72015-08-05 10:21:34 +01004800 template <typename T> T Compute(T x) const { return ~x; }
4801
4802 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004803 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004804 }
4805 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004806 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004807 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004808 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4809 LOG(FATAL) << DebugName() << " is not defined for float values";
4810 UNREACHABLE();
4811 }
4812 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4813 LOG(FATAL) << DebugName() << " is not defined for double values";
4814 UNREACHABLE();
4815 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004816
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004817 DECLARE_INSTRUCTION(Not);
4818
4819 private:
4820 DISALLOW_COPY_AND_ASSIGN(HNot);
4821};
4822
David Brazdil66d126e2015-04-03 16:02:44 +01004823class HBooleanNot : public HUnaryOperation {
4824 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004825 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4826 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004827
4828 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004829 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004830 return true;
4831 }
4832
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004834 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004835 return !x;
4836 }
4837
Roland Levillain9867bc72015-08-05 10:21:34 +01004838 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004839 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004840 }
4841 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4842 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004843 UNREACHABLE();
4844 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004845 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4846 LOG(FATAL) << DebugName() << " is not defined for float values";
4847 UNREACHABLE();
4848 }
4849 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4850 LOG(FATAL) << DebugName() << " is not defined for double values";
4851 UNREACHABLE();
4852 }
David Brazdil66d126e2015-04-03 16:02:44 +01004853
4854 DECLARE_INSTRUCTION(BooleanNot);
4855
4856 private:
4857 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4858};
4859
Roland Levillaindff1f282014-11-05 14:15:05 +00004860class HTypeConversion : public HExpression<1> {
4861 public:
4862 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004863 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004864 : HExpression(result_type,
4865 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4866 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004867 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004868 // Invariant: We should never generate a conversion to a Boolean value.
4869 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004870 }
4871
4872 HInstruction* GetInput() const { return InputAt(0); }
4873 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4874 Primitive::Type GetResultType() const { return GetType(); }
4875
4876 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004877 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004878
Mark Mendelle82549b2015-05-06 10:55:34 -04004879 // Try to statically evaluate the conversion and return a HConstant
4880 // containing the result. If the input cannot be converted, return nullptr.
4881 HConstant* TryStaticEvaluation() const;
4882
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004883 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4884 Primitive::Type result_type) {
4885 // Some architectures may not require the 'GC' side effects, but at this point
4886 // in the compilation process we do not know what architecture we will
4887 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004888 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4889 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004890 return SideEffects::CanTriggerGC();
4891 }
4892 return SideEffects::None();
4893 }
4894
Roland Levillaindff1f282014-11-05 14:15:05 +00004895 DECLARE_INSTRUCTION(TypeConversion);
4896
4897 private:
4898 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4899};
4900
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004901static constexpr uint32_t kNoRegNumber = -1;
4902
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004903class HNullCheck : public HExpression<1> {
4904 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004905 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4906 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004907 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004908 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004909 SetRawInputAt(0, value);
4910 }
4911
Calin Juravle10e244f2015-01-26 18:54:32 +00004912 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004913 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004914 return true;
4915 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004916
Calin Juravle10e244f2015-01-26 18:54:32 +00004917 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004918
Calin Juravle10e244f2015-01-26 18:54:32 +00004919 bool CanThrow() const OVERRIDE { return true; }
4920
4921 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004922
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004923
4924 DECLARE_INSTRUCTION(NullCheck);
4925
4926 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004927 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4928};
4929
4930class FieldInfo : public ValueObject {
4931 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004932 FieldInfo(MemberOffset field_offset,
4933 Primitive::Type field_type,
4934 bool is_volatile,
4935 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004936 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004937 const DexFile& dex_file,
4938 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004939 : field_offset_(field_offset),
4940 field_type_(field_type),
4941 is_volatile_(is_volatile),
4942 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004943 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004944 dex_file_(dex_file),
4945 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004946
4947 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004948 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004949 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004950 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004951 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004952 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004953 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954
4955 private:
4956 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004957 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004958 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004959 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004960 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004961 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004962 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004963};
4964
4965class HInstanceFieldGet : public HExpression<1> {
4966 public:
4967 HInstanceFieldGet(HInstruction* value,
4968 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004969 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004970 bool is_volatile,
4971 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004972 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004973 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004974 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004975 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004976 : HExpression(field_type,
4977 SideEffects::FieldReadOfType(field_type, is_volatile),
4978 dex_pc),
4979 field_info_(field_offset,
4980 field_type,
4981 is_volatile,
4982 field_idx,
4983 declaring_class_def_index,
4984 dex_file,
4985 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004986 SetRawInputAt(0, value);
4987 }
4988
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004989 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004990
4991 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4992 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4993 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004994 }
4995
Calin Juravle641547a2015-04-21 22:08:51 +01004996 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4997 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004998 }
4999
5000 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005001 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5002 }
5003
Calin Juravle52c48962014-12-16 17:02:57 +00005004 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005005 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005006 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005007 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005008
5009 DECLARE_INSTRUCTION(InstanceFieldGet);
5010
5011 private:
5012 const FieldInfo field_info_;
5013
5014 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5015};
5016
5017class HInstanceFieldSet : public HTemplateInstruction<2> {
5018 public:
5019 HInstanceFieldSet(HInstruction* object,
5020 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005021 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005022 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005023 bool is_volatile,
5024 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005025 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005026 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005027 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005028 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005029 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5030 dex_pc),
5031 field_info_(field_offset,
5032 field_type,
5033 is_volatile,
5034 field_idx,
5035 declaring_class_def_index,
5036 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005037 dex_cache) {
5038 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005039 SetRawInputAt(0, object);
5040 SetRawInputAt(1, value);
5041 }
5042
Calin Juravle641547a2015-04-21 22:08:51 +01005043 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5044 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005045 }
5046
Calin Juravle52c48962014-12-16 17:02:57 +00005047 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005048 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005049 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005050 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005051 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005052 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5053 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005054
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055 DECLARE_INSTRUCTION(InstanceFieldSet);
5056
5057 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005058 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5059 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5060 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5061 "Too many packed fields.");
5062
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005063 const FieldInfo field_info_;
5064
5065 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5066};
5067
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068class HArrayGet : public HExpression<2> {
5069 public:
Aart Bik18b36ab2016-04-13 16:41:35 -07005070 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type, uint32_t dex_pc)
5071 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005072 SetRawInputAt(0, array);
5073 SetRawInputAt(1, index);
5074 }
5075
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005076 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005077 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005078 return true;
5079 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005080 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005081 // TODO: We can be smarter here.
5082 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5083 // which generates the implicit null check. There are cases when these can be removed
5084 // to produce better code. If we ever add optimizations to do so we should allow an
5085 // implicit check here (as long as the address falls in the first page).
5086 return false;
5087 }
5088
David Brazdil4833f5a2015-12-16 10:37:39 +00005089 bool IsEquivalentOf(HArrayGet* other) const {
5090 bool result = (GetDexPc() == other->GetDexPc());
5091 if (kIsDebugBuild && result) {
5092 DCHECK_EQ(GetBlock(), other->GetBlock());
5093 DCHECK_EQ(GetArray(), other->GetArray());
5094 DCHECK_EQ(GetIndex(), other->GetIndex());
5095 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005096 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005097 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005098 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5099 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005100 }
5101 }
5102 return result;
5103 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005104
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005105 HInstruction* GetArray() const { return InputAt(0); }
5106 HInstruction* GetIndex() const { return InputAt(1); }
5107
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 DECLARE_INSTRUCTION(ArrayGet);
5109
5110 private:
5111 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5112};
5113
5114class HArraySet : public HTemplateInstruction<3> {
5115 public:
5116 HArraySet(HInstruction* array,
5117 HInstruction* index,
5118 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005119 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005120 uint32_t dex_pc)
5121 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005122 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5123 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5124 SetPackedFlag<kFlagValueCanBeNull>(true);
5125 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 SetRawInputAt(0, array);
5127 SetRawInputAt(1, index);
5128 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005129 // Make a best guess now, may be refined during SSA building.
5130 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131 }
5132
Calin Juravle77520bc2015-01-12 18:45:46 +00005133 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005134 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005135 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 }
5137
Mingyao Yang81014cb2015-06-02 03:16:27 -07005138 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005139 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005140
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005141 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005142 // TODO: Same as for ArrayGet.
5143 return false;
5144 }
5145
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005146 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005147 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005148 }
5149
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005150 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005151 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005152 }
5153
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005154 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005155 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005156 }
5157
Vladimir Markoa1de9182016-02-25 11:37:38 +00005158 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5159 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5160 bool StaticTypeOfArrayIsObjectArray() const {
5161 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5162 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005163
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005164 HInstruction* GetArray() const { return InputAt(0); }
5165 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005166 HInstruction* GetValue() const { return InputAt(2); }
5167
5168 Primitive::Type GetComponentType() const {
5169 // The Dex format does not type floating point index operations. Since the
5170 // `expected_component_type_` is set during building and can therefore not
5171 // be correct, we also check what is the value type. If it is a floating
5172 // point type, we must use that type.
5173 Primitive::Type value_type = GetValue()->GetType();
5174 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5175 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005176 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005177 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005178
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005179 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005180 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005181 }
5182
Aart Bik18b36ab2016-04-13 16:41:35 -07005183 void ComputeSideEffects() {
5184 Primitive::Type type = GetComponentType();
5185 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5186 SideEffectsForArchRuntimeCalls(type)));
5187 }
5188
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005189 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5190 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5191 }
5192
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005193 DECLARE_INSTRUCTION(ArraySet);
5194
5195 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005196 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5197 static constexpr size_t kFieldExpectedComponentTypeSize =
5198 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5199 static constexpr size_t kFlagNeedsTypeCheck =
5200 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5201 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005202 // Cached information for the reference_type_info_ so that codegen
5203 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005204 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5205 static constexpr size_t kNumberOfArraySetPackedBits =
5206 kFlagStaticTypeOfArrayIsObjectArray + 1;
5207 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5208 using ExpectedComponentTypeField =
5209 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210
5211 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5212};
5213
5214class HArrayLength : public HExpression<1> {
5215 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005216 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005217 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005218 // Note that arrays do not change length, so the instruction does not
5219 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005220 SetRawInputAt(0, array);
5221 }
5222
Calin Juravle77520bc2015-01-12 18:45:46 +00005223 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005224 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005225 return true;
5226 }
Calin Juravle641547a2015-04-21 22:08:51 +01005227 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5228 return obj == InputAt(0);
5229 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005230
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 DECLARE_INSTRUCTION(ArrayLength);
5232
5233 private:
5234 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5235};
5236
5237class HBoundsCheck : public HExpression<2> {
5238 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005239 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5240 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005241 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005242 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005243 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005244 SetRawInputAt(0, index);
5245 SetRawInputAt(1, length);
5246 }
5247
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005248 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005249 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005250 return true;
5251 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005252
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005253 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005255 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005256
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005257 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258
5259 DECLARE_INSTRUCTION(BoundsCheck);
5260
5261 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005262 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5263};
5264
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005265class HSuspendCheck : public HTemplateInstruction<0> {
5266 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005267 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005268 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005269
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005270 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005271 return true;
5272 }
5273
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005274 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5275 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005276
5277 DECLARE_INSTRUCTION(SuspendCheck);
5278
5279 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005280 // Only used for code generation, in order to share the same slow path between back edges
5281 // of a same loop.
5282 SlowPathCode* slow_path_;
5283
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005284 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5285};
5286
David Srbecky0cf44932015-12-09 14:09:59 +00005287// Pseudo-instruction which provides the native debugger with mapping information.
5288// It ensures that we can generate line number and local variables at this point.
5289class HNativeDebugInfo : public HTemplateInstruction<0> {
5290 public:
5291 explicit HNativeDebugInfo(uint32_t dex_pc)
5292 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5293
5294 bool NeedsEnvironment() const OVERRIDE {
5295 return true;
5296 }
5297
5298 DECLARE_INSTRUCTION(NativeDebugInfo);
5299
5300 private:
5301 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5302};
5303
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005304/**
5305 * Instruction to load a Class object.
5306 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005307class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005308 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005309 HLoadClass(HCurrentMethod* current_method,
5310 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005311 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005312 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005313 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005314 bool needs_access_check,
5315 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005316 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005317 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005318 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005319 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005320 // Referrers class should not need access check. We never inline unverified
5321 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005322 DCHECK(!is_referrers_class || !needs_access_check);
5323
5324 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5325 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5326 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5327 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005328 SetRawInputAt(0, current_method);
5329 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005330
5331 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005332
5333 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005334 // Note that we don't need to test for generate_clinit_check_.
5335 // Whether or not we need to generate the clinit check is processed in
5336 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005337 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005338 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005339 }
5340
5341 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5342
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005343 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005344 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005345
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005346 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005347 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005348 }
5349
Calin Juravle0ba218d2015-05-19 18:46:01 +01005350 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005351 // The entrypoint the code generator is going to call does not do
5352 // clinit of the class.
5353 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005354 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005355 }
5356
5357 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005358 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005359 (!IsReferrersClass() && !IsInDexCache()) ||
5360 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005361 }
5362
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005363
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005364 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005365 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005366 }
5367
Calin Juravleacf735c2015-02-12 15:25:22 +00005368 ReferenceTypeInfo GetLoadedClassRTI() {
5369 return loaded_class_rti_;
5370 }
5371
5372 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5373 // Make sure we only set exact types (the loaded class should never be merged).
5374 DCHECK(rti.IsExact());
5375 loaded_class_rti_ = rti;
5376 }
5377
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005378 const DexFile& GetDexFile() { return dex_file_; }
5379
Vladimir Markoa1de9182016-02-25 11:37:38 +00005380 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005381
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005382 static SideEffects SideEffectsForArchRuntimeCalls() {
5383 return SideEffects::CanTriggerGC();
5384 }
5385
Vladimir Markoa1de9182016-02-25 11:37:38 +00005386 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5387 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5388 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5389 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005390
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005391 DECLARE_INSTRUCTION(LoadClass);
5392
5393 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005394 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5395 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5396 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005397 // Whether this instruction must generate the initialization check.
5398 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005399 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5400 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5401 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5402
5403 const uint16_t type_index_;
5404 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005405
Calin Juravleacf735c2015-02-12 15:25:22 +00005406 ReferenceTypeInfo loaded_class_rti_;
5407
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005408 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5409};
5410
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005411class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005412 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005413 // Determines how to load the String.
5414 enum class LoadKind {
5415 // Use boot image String* address that will be known at link time.
5416 // Used for boot image strings referenced by boot image code in non-PIC mode.
5417 kBootImageLinkTimeAddress,
5418
5419 // Use PC-relative boot image String* address that will be known at link time.
5420 // Used for boot image strings referenced by boot image code in PIC mode.
5421 kBootImageLinkTimePcRelative,
5422
5423 // Use a known boot image String* address, embedded in the code by the codegen.
5424 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5425 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5426 // GetIncludePatchInformation().
5427 kBootImageAddress,
5428
5429 // Load from the resolved strings array at an absolute address.
5430 // Used for strings outside the boot image referenced by JIT-compiled code.
5431 kDexCacheAddress,
5432
5433 // Load from resolved strings array in the dex cache using a PC-relative load.
5434 // Used for strings outside boot image when we know that we can access
5435 // the dex cache arrays using a PC-relative load.
5436 kDexCachePcRelative,
5437
5438 // Load from resolved strings array accessed through the class loaded from
5439 // the compiled method's own ArtMethod*. This is the default access type when
5440 // all other types are unavailable.
5441 kDexCacheViaMethod,
5442
5443 kLast = kDexCacheViaMethod
5444 };
5445
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005446 HLoadString(HCurrentMethod* current_method,
5447 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005448 const DexFile& dex_file,
5449 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005450 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005451 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005452 SetPackedFlag<kFlagIsInDexCache>(false);
5453 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5454 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005455 SetRawInputAt(0, current_method);
5456 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005457
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005458 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5459 DCHECK(HasAddress(load_kind));
5460 load_data_.address = address;
5461 SetLoadKindInternal(load_kind);
5462 }
5463
5464 void SetLoadKindWithStringReference(LoadKind load_kind,
5465 const DexFile& dex_file,
5466 uint32_t string_index) {
5467 DCHECK(HasStringReference(load_kind));
5468 load_data_.ref.dex_file = &dex_file;
5469 string_index_ = string_index;
5470 SetLoadKindInternal(load_kind);
5471 }
5472
5473 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5474 const DexFile& dex_file,
5475 uint32_t element_index) {
5476 DCHECK(HasDexCacheReference(load_kind));
5477 load_data_.ref.dex_file = &dex_file;
5478 load_data_.ref.dex_cache_element_index = element_index;
5479 SetLoadKindInternal(load_kind);
5480 }
5481
5482 LoadKind GetLoadKind() const {
5483 return GetPackedField<LoadKindField>();
5484 }
5485
5486 const DexFile& GetDexFile() const;
5487
5488 uint32_t GetStringIndex() const {
5489 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5490 return string_index_;
5491 }
5492
5493 uint32_t GetDexCacheElementOffset() const;
5494
5495 uint64_t GetAddress() const {
5496 DCHECK(HasAddress(GetLoadKind()));
5497 return load_data_.address;
5498 }
5499
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005500 bool CanBeMoved() const OVERRIDE { return true; }
5501
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005502 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005503
5504 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5505
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005506 // Will call the runtime if we need to load the string through
5507 // the dex cache and the string is not guaranteed to be there yet.
5508 bool NeedsEnvironment() const OVERRIDE {
5509 LoadKind load_kind = GetLoadKind();
5510 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5511 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5512 load_kind == LoadKind::kBootImageAddress) {
5513 return false;
5514 }
5515 return !IsInDexCache();
5516 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005517
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005518 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5519 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5520 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005521
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005522 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005523 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005524
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005525 static SideEffects SideEffectsForArchRuntimeCalls() {
5526 return SideEffects::CanTriggerGC();
5527 }
5528
Vladimir Markoa1de9182016-02-25 11:37:38 +00005529 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5530
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005531 void MarkInDexCache() {
5532 SetPackedFlag<kFlagIsInDexCache>(true);
5533 DCHECK(!NeedsEnvironment());
5534 RemoveEnvironment();
5535 }
5536
5537 size_t InputCount() const OVERRIDE {
5538 return (InputAt(0) != nullptr) ? 1u : 0u;
5539 }
5540
5541 void AddSpecialInput(HInstruction* special_input);
5542
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005543 DECLARE_INSTRUCTION(LoadString);
5544
5545 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005546 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005547 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5548 static constexpr size_t kFieldLoadKindSize =
5549 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5550 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005551 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005552 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005553
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005554 static bool HasStringReference(LoadKind load_kind) {
5555 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5556 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5557 load_kind == LoadKind::kDexCacheViaMethod;
5558 }
5559
5560 static bool HasAddress(LoadKind load_kind) {
5561 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5562 }
5563
5564 static bool HasDexCacheReference(LoadKind load_kind) {
5565 return load_kind == LoadKind::kDexCachePcRelative;
5566 }
5567
5568 void SetLoadKindInternal(LoadKind load_kind);
5569
5570 // String index serves also as the hash code and it's also needed for slow-paths,
5571 // so it must not be overwritten with other load data.
5572 uint32_t string_index_;
5573
5574 union {
5575 struct {
5576 const DexFile* dex_file; // For string reference and dex cache reference.
5577 uint32_t dex_cache_element_index; // Only for dex cache reference.
5578 } ref;
5579 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5580 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005581
5582 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5583};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005584std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5585
5586// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5587inline const DexFile& HLoadString::GetDexFile() const {
5588 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5589 << GetLoadKind();
5590 return *load_data_.ref.dex_file;
5591}
5592
5593// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5594inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5595 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5596 return load_data_.ref.dex_cache_element_index;
5597}
5598
5599// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5600inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5601 // The special input is used for PC-relative loads on some architectures.
5602 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5603 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5604 DCHECK(InputAt(0) == nullptr);
5605 SetRawInputAt(0u, special_input);
5606 special_input->AddUseAt(this, 0);
5607}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005608
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005609/**
5610 * Performs an initialization check on its Class object input.
5611 */
5612class HClinitCheck : public HExpression<1> {
5613 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005614 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005615 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005616 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005617 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5618 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005619 SetRawInputAt(0, constant);
5620 }
5621
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005622 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005623 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005624 return true;
5625 }
5626
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005627 bool NeedsEnvironment() const OVERRIDE {
5628 // May call runtime to initialize the class.
5629 return true;
5630 }
5631
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005632 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005633
5634 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5635
5636 DECLARE_INSTRUCTION(ClinitCheck);
5637
5638 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005639 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5640};
5641
5642class HStaticFieldGet : public HExpression<1> {
5643 public:
5644 HStaticFieldGet(HInstruction* cls,
5645 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005646 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005647 bool is_volatile,
5648 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005649 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005650 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005651 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005652 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005653 : HExpression(field_type,
5654 SideEffects::FieldReadOfType(field_type, is_volatile),
5655 dex_pc),
5656 field_info_(field_offset,
5657 field_type,
5658 is_volatile,
5659 field_idx,
5660 declaring_class_def_index,
5661 dex_file,
5662 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005663 SetRawInputAt(0, cls);
5664 }
5665
Calin Juravle52c48962014-12-16 17:02:57 +00005666
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005667 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005668
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005669 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005670 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5671 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005672 }
5673
5674 size_t ComputeHashCode() const OVERRIDE {
5675 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5676 }
5677
Calin Juravle52c48962014-12-16 17:02:57 +00005678 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005679 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5680 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005681 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005682
5683 DECLARE_INSTRUCTION(StaticFieldGet);
5684
5685 private:
5686 const FieldInfo field_info_;
5687
5688 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5689};
5690
5691class HStaticFieldSet : public HTemplateInstruction<2> {
5692 public:
5693 HStaticFieldSet(HInstruction* cls,
5694 HInstruction* value,
5695 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005696 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005697 bool is_volatile,
5698 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005699 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005700 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005701 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005702 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005703 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5704 dex_pc),
5705 field_info_(field_offset,
5706 field_type,
5707 is_volatile,
5708 field_idx,
5709 declaring_class_def_index,
5710 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005711 dex_cache) {
5712 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005713 SetRawInputAt(0, cls);
5714 SetRawInputAt(1, value);
5715 }
5716
Calin Juravle52c48962014-12-16 17:02:57 +00005717 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005718 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5719 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005720 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005721
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005722 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005723 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5724 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005725
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005726 DECLARE_INSTRUCTION(StaticFieldSet);
5727
5728 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005729 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5730 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5731 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5732 "Too many packed fields.");
5733
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005734 const FieldInfo field_info_;
5735
5736 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5737};
5738
Calin Juravlee460d1d2015-09-29 04:52:17 +01005739class HUnresolvedInstanceFieldGet : public HExpression<1> {
5740 public:
5741 HUnresolvedInstanceFieldGet(HInstruction* obj,
5742 Primitive::Type field_type,
5743 uint32_t field_index,
5744 uint32_t dex_pc)
5745 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5746 field_index_(field_index) {
5747 SetRawInputAt(0, obj);
5748 }
5749
5750 bool NeedsEnvironment() const OVERRIDE { return true; }
5751 bool CanThrow() const OVERRIDE { return true; }
5752
5753 Primitive::Type GetFieldType() const { return GetType(); }
5754 uint32_t GetFieldIndex() const { return field_index_; }
5755
5756 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5757
5758 private:
5759 const uint32_t field_index_;
5760
5761 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5762};
5763
5764class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5765 public:
5766 HUnresolvedInstanceFieldSet(HInstruction* obj,
5767 HInstruction* value,
5768 Primitive::Type field_type,
5769 uint32_t field_index,
5770 uint32_t dex_pc)
5771 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005772 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005773 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005774 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005775 SetRawInputAt(0, obj);
5776 SetRawInputAt(1, value);
5777 }
5778
5779 bool NeedsEnvironment() const OVERRIDE { return true; }
5780 bool CanThrow() const OVERRIDE { return true; }
5781
Vladimir Markoa1de9182016-02-25 11:37:38 +00005782 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005783 uint32_t GetFieldIndex() const { return field_index_; }
5784
5785 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5786
5787 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005788 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5789 static constexpr size_t kFieldFieldTypeSize =
5790 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5791 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5792 kFieldFieldType + kFieldFieldTypeSize;
5793 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5794 "Too many packed fields.");
5795 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5796
Calin Juravlee460d1d2015-09-29 04:52:17 +01005797 const uint32_t field_index_;
5798
5799 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5800};
5801
5802class HUnresolvedStaticFieldGet : public HExpression<0> {
5803 public:
5804 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5805 uint32_t field_index,
5806 uint32_t dex_pc)
5807 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5808 field_index_(field_index) {
5809 }
5810
5811 bool NeedsEnvironment() const OVERRIDE { return true; }
5812 bool CanThrow() const OVERRIDE { return true; }
5813
5814 Primitive::Type GetFieldType() const { return GetType(); }
5815 uint32_t GetFieldIndex() const { return field_index_; }
5816
5817 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5818
5819 private:
5820 const uint32_t field_index_;
5821
5822 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5823};
5824
5825class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5826 public:
5827 HUnresolvedStaticFieldSet(HInstruction* value,
5828 Primitive::Type field_type,
5829 uint32_t field_index,
5830 uint32_t dex_pc)
5831 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005832 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005833 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005834 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005835 SetRawInputAt(0, value);
5836 }
5837
5838 bool NeedsEnvironment() const OVERRIDE { return true; }
5839 bool CanThrow() const OVERRIDE { return true; }
5840
Vladimir Markoa1de9182016-02-25 11:37:38 +00005841 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005842 uint32_t GetFieldIndex() const { return field_index_; }
5843
5844 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5845
5846 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005847 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5848 static constexpr size_t kFieldFieldTypeSize =
5849 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5850 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5851 kFieldFieldType + kFieldFieldTypeSize;
5852 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5853 "Too many packed fields.");
5854 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5855
Calin Juravlee460d1d2015-09-29 04:52:17 +01005856 const uint32_t field_index_;
5857
5858 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5859};
5860
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005861// Implement the move-exception DEX instruction.
5862class HLoadException : public HExpression<0> {
5863 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005864 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5865 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005866
David Brazdilbbd733e2015-08-18 17:48:17 +01005867 bool CanBeNull() const OVERRIDE { return false; }
5868
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005869 DECLARE_INSTRUCTION(LoadException);
5870
5871 private:
5872 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5873};
5874
David Brazdilcb1c0552015-08-04 16:22:25 +01005875// Implicit part of move-exception which clears thread-local exception storage.
5876// Must not be removed because the runtime expects the TLS to get cleared.
5877class HClearException : public HTemplateInstruction<0> {
5878 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005879 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5880 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005881
5882 DECLARE_INSTRUCTION(ClearException);
5883
5884 private:
5885 DISALLOW_COPY_AND_ASSIGN(HClearException);
5886};
5887
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005888class HThrow : public HTemplateInstruction<1> {
5889 public:
5890 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005891 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005892 SetRawInputAt(0, exception);
5893 }
5894
5895 bool IsControlFlow() const OVERRIDE { return true; }
5896
5897 bool NeedsEnvironment() const OVERRIDE { return true; }
5898
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005899 bool CanThrow() const OVERRIDE { return true; }
5900
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005901
5902 DECLARE_INSTRUCTION(Throw);
5903
5904 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005905 DISALLOW_COPY_AND_ASSIGN(HThrow);
5906};
5907
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005908/**
5909 * Implementation strategies for the code generator of a HInstanceOf
5910 * or `HCheckCast`.
5911 */
5912enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005913 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005914 kExactCheck, // Can do a single class compare.
5915 kClassHierarchyCheck, // Can just walk the super class chain.
5916 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5917 kInterfaceCheck, // No optimization yet when checking against an interface.
5918 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005919 kArrayCheck, // No optimization yet when checking against a generic array.
5920 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005921};
5922
Roland Levillain86503782016-02-11 19:07:30 +00005923std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5924
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005925class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005926 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005927 HInstanceOf(HInstruction* object,
5928 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005930 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005931 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005932 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005933 dex_pc) {
5934 SetPackedField<TypeCheckKindField>(check_kind);
5935 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005936 SetRawInputAt(0, object);
5937 SetRawInputAt(1, constant);
5938 }
5939
5940 bool CanBeMoved() const OVERRIDE { return true; }
5941
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005942 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005943 return true;
5944 }
5945
5946 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005947 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005948 }
5949
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005950 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005951 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5952 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5953 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5954 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005955
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005956 static bool CanCallRuntime(TypeCheckKind check_kind) {
5957 // Mips currently does runtime calls for any other checks.
5958 return check_kind != TypeCheckKind::kExactCheck;
5959 }
5960
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005961 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005962 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005963 }
5964
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005965 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005966
5967 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005968 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5969 static constexpr size_t kFieldTypeCheckKindSize =
5970 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5971 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5972 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5973 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5974 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005975
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005976 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5977};
5978
Calin Juravleb1498f62015-02-16 13:13:29 +00005979class HBoundType : public HExpression<1> {
5980 public:
David Brazdilf5552582015-12-27 13:36:12 +00005981 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005982 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005983 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5984 SetPackedFlag<kFlagUpperCanBeNull>(true);
5985 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005986 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005987 SetRawInputAt(0, input);
5988 }
5989
David Brazdilf5552582015-12-27 13:36:12 +00005990 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005991 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005992 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005993 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005994
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005995 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005996 DCHECK(GetUpperCanBeNull() || !can_be_null);
5997 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005998 }
5999
Vladimir Markoa1de9182016-02-25 11:37:38 +00006000 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006001
Calin Juravleb1498f62015-02-16 13:13:29 +00006002 DECLARE_INSTRUCTION(BoundType);
6003
6004 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006005 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6006 // is false then CanBeNull() cannot be true).
6007 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6008 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6009 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6010 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6011
Calin Juravleb1498f62015-02-16 13:13:29 +00006012 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006013 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6014 // It is used to bound the type in cases like:
6015 // if (x instanceof ClassX) {
6016 // // uper_bound_ will be ClassX
6017 // }
David Brazdilf5552582015-12-27 13:36:12 +00006018 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006019
6020 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6021};
6022
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006023class HCheckCast : public HTemplateInstruction<2> {
6024 public:
6025 HCheckCast(HInstruction* object,
6026 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006027 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006028 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006029 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6030 SetPackedField<TypeCheckKindField>(check_kind);
6031 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006032 SetRawInputAt(0, object);
6033 SetRawInputAt(1, constant);
6034 }
6035
6036 bool CanBeMoved() const OVERRIDE { return true; }
6037
6038 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6039 return true;
6040 }
6041
6042 bool NeedsEnvironment() const OVERRIDE {
6043 // Instruction may throw a CheckCastError.
6044 return true;
6045 }
6046
6047 bool CanThrow() const OVERRIDE { return true; }
6048
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6050 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6051 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6052 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006053
6054 DECLARE_INSTRUCTION(CheckCast);
6055
6056 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006057 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6058 static constexpr size_t kFieldTypeCheckKindSize =
6059 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6060 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6061 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6062 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6063 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006064
6065 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006066};
6067
Calin Juravle27df7582015-04-17 19:12:31 +01006068class HMemoryBarrier : public HTemplateInstruction<0> {
6069 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006070 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006071 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006072 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6073 SetPackedField<BarrierKindField>(barrier_kind);
6074 }
Calin Juravle27df7582015-04-17 19:12:31 +01006075
Vladimir Markoa1de9182016-02-25 11:37:38 +00006076 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006077
6078 DECLARE_INSTRUCTION(MemoryBarrier);
6079
6080 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006081 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6082 static constexpr size_t kFieldBarrierKindSize =
6083 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6084 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6085 kFieldBarrierKind + kFieldBarrierKindSize;
6086 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6087 "Too many packed fields.");
6088 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006089
6090 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6091};
6092
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006093class HMonitorOperation : public HTemplateInstruction<1> {
6094 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006095 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006096 kEnter,
6097 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006098 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006099 };
6100
6101 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006102 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006103 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6104 dex_pc) {
6105 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006106 SetRawInputAt(0, object);
6107 }
6108
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006109 // Instruction may go into runtime, so we need an environment.
6110 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006111
6112 bool CanThrow() const OVERRIDE {
6113 // Verifier guarantees that monitor-exit cannot throw.
6114 // This is important because it allows the HGraphBuilder to remove
6115 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6116 return IsEnter();
6117 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006118
Vladimir Markoa1de9182016-02-25 11:37:38 +00006119 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6120 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006121
6122 DECLARE_INSTRUCTION(MonitorOperation);
6123
Calin Juravle52c48962014-12-16 17:02:57 +00006124 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006125 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6126 static constexpr size_t kFieldOperationKindSize =
6127 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6128 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6129 kFieldOperationKind + kFieldOperationKindSize;
6130 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6131 "Too many packed fields.");
6132 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006133
6134 private:
6135 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6136};
6137
David Brazdil74eb1b22015-12-14 11:44:01 +00006138class HSelect : public HExpression<3> {
6139 public:
6140 HSelect(HInstruction* condition,
6141 HInstruction* true_value,
6142 HInstruction* false_value,
6143 uint32_t dex_pc)
6144 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6145 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6146
6147 // First input must be `true_value` or `false_value` to allow codegens to
6148 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6149 // that architectures which implement HSelect as a conditional move also
6150 // will not need to invert the condition.
6151 SetRawInputAt(0, false_value);
6152 SetRawInputAt(1, true_value);
6153 SetRawInputAt(2, condition);
6154 }
6155
6156 HInstruction* GetFalseValue() const { return InputAt(0); }
6157 HInstruction* GetTrueValue() const { return InputAt(1); }
6158 HInstruction* GetCondition() const { return InputAt(2); }
6159
6160 bool CanBeMoved() const OVERRIDE { return true; }
6161 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6162
6163 bool CanBeNull() const OVERRIDE {
6164 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6165 }
6166
6167 DECLARE_INSTRUCTION(Select);
6168
6169 private:
6170 DISALLOW_COPY_AND_ASSIGN(HSelect);
6171};
6172
Vladimir Markof9f64412015-09-02 14:05:49 +01006173class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006174 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006175 MoveOperands(Location source,
6176 Location destination,
6177 Primitive::Type type,
6178 HInstruction* instruction)
6179 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006180
6181 Location GetSource() const { return source_; }
6182 Location GetDestination() const { return destination_; }
6183
6184 void SetSource(Location value) { source_ = value; }
6185 void SetDestination(Location value) { destination_ = value; }
6186
6187 // The parallel move resolver marks moves as "in-progress" by clearing the
6188 // destination (but not the source).
6189 Location MarkPending() {
6190 DCHECK(!IsPending());
6191 Location dest = destination_;
6192 destination_ = Location::NoLocation();
6193 return dest;
6194 }
6195
6196 void ClearPending(Location dest) {
6197 DCHECK(IsPending());
6198 destination_ = dest;
6199 }
6200
6201 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006202 DCHECK(source_.IsValid() || destination_.IsInvalid());
6203 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006204 }
6205
6206 // True if this blocks a move from the given location.
6207 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006208 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006209 }
6210
6211 // A move is redundant if it's been eliminated, if its source and
6212 // destination are the same, or if its destination is unneeded.
6213 bool IsRedundant() const {
6214 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6215 }
6216
6217 // We clear both operands to indicate move that's been eliminated.
6218 void Eliminate() {
6219 source_ = destination_ = Location::NoLocation();
6220 }
6221
6222 bool IsEliminated() const {
6223 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6224 return source_.IsInvalid();
6225 }
6226
Alexey Frunze4dda3372015-06-01 18:31:49 -07006227 Primitive::Type GetType() const { return type_; }
6228
Nicolas Geoffray90218252015-04-15 11:56:51 +01006229 bool Is64BitMove() const {
6230 return Primitive::Is64BitType(type_);
6231 }
6232
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006233 HInstruction* GetInstruction() const { return instruction_; }
6234
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006235 private:
6236 Location source_;
6237 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006238 // The type this move is for.
6239 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006240 // The instruction this move is assocatied with. Null when this move is
6241 // for moving an input in the expected locations of user (including a phi user).
6242 // This is only used in debug mode, to ensure we do not connect interval siblings
6243 // in the same parallel move.
6244 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006245};
6246
Roland Levillainc9285912015-12-18 10:38:42 +00006247std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6248
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006249static constexpr size_t kDefaultNumberOfMoves = 4;
6250
6251class HParallelMove : public HTemplateInstruction<0> {
6252 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006253 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006254 : HTemplateInstruction(SideEffects::None(), dex_pc),
6255 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6256 moves_.reserve(kDefaultNumberOfMoves);
6257 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006258
Nicolas Geoffray90218252015-04-15 11:56:51 +01006259 void AddMove(Location source,
6260 Location destination,
6261 Primitive::Type type,
6262 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006263 DCHECK(source.IsValid());
6264 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006265 if (kIsDebugBuild) {
6266 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006267 for (const MoveOperands& move : moves_) {
6268 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006269 // Special case the situation where the move is for the spill slot
6270 // of the instruction.
6271 if ((GetPrevious() == instruction)
6272 || ((GetPrevious() == nullptr)
6273 && instruction->IsPhi()
6274 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006275 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006276 << "Doing parallel moves for the same instruction.";
6277 } else {
6278 DCHECK(false) << "Doing parallel moves for the same instruction.";
6279 }
6280 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006281 }
6282 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006283 for (const MoveOperands& move : moves_) {
6284 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006285 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006286 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006287 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006288 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006289 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006290 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006291 }
6292
Vladimir Marko225b6462015-09-28 12:17:40 +01006293 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006294 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006295 }
6296
Vladimir Marko225b6462015-09-28 12:17:40 +01006297 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006298
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006299 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006300
6301 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006302 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006303
6304 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6305};
6306
Mark Mendell0616ae02015-04-17 12:49:27 -04006307} // namespace art
6308
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006309#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6310#include "nodes_shared.h"
6311#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006312#ifdef ART_ENABLE_CODEGEN_arm
6313#include "nodes_arm.h"
6314#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006315#ifdef ART_ENABLE_CODEGEN_arm64
6316#include "nodes_arm64.h"
6317#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006318#ifdef ART_ENABLE_CODEGEN_x86
6319#include "nodes_x86.h"
6320#endif
6321
6322namespace art {
6323
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006324class HGraphVisitor : public ValueObject {
6325 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006326 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6327 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006328
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006329 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006330 virtual void VisitBasicBlock(HBasicBlock* block);
6331
Roland Levillain633021e2014-10-01 14:12:25 +01006332 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006333 void VisitInsertionOrder();
6334
Roland Levillain633021e2014-10-01 14:12:25 +01006335 // Visit the graph following dominator tree reverse post-order.
6336 void VisitReversePostOrder();
6337
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006338 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006339
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006340 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006341#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006342 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6343
6344 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6345
6346#undef DECLARE_VISIT_INSTRUCTION
6347
6348 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006349 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006350
6351 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6352};
6353
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006354class HGraphDelegateVisitor : public HGraphVisitor {
6355 public:
6356 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6357 virtual ~HGraphDelegateVisitor() {}
6358
6359 // Visit functions that delegate to to super class.
6360#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006361 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006362
6363 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6364
6365#undef DECLARE_VISIT_INSTRUCTION
6366
6367 private:
6368 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6369};
6370
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006371class HInsertionOrderIterator : public ValueObject {
6372 public:
6373 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6374
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006375 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006376 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006377 void Advance() { ++index_; }
6378
6379 private:
6380 const HGraph& graph_;
6381 size_t index_;
6382
6383 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6384};
6385
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006386class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006387 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006388 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6389 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006390 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006391 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006392
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006393 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6394 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006395 void Advance() { ++index_; }
6396
6397 private:
6398 const HGraph& graph_;
6399 size_t index_;
6400
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006401 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006402};
6403
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006404class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006405 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006406 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006407 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006408 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006409 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006410 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006411
6412 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006413 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006414 void Advance() { --index_; }
6415
6416 private:
6417 const HGraph& graph_;
6418 size_t index_;
6419
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006420 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006421};
6422
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006423class HLinearPostOrderIterator : public ValueObject {
6424 public:
6425 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006426 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006427
6428 bool Done() const { return index_ == 0; }
6429
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006430 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006431
6432 void Advance() {
6433 --index_;
6434 DCHECK_GE(index_, 0U);
6435 }
6436
6437 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006438 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006439 size_t index_;
6440
6441 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6442};
6443
6444class HLinearOrderIterator : public ValueObject {
6445 public:
6446 explicit HLinearOrderIterator(const HGraph& graph)
6447 : order_(graph.GetLinearOrder()), index_(0) {}
6448
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006449 bool Done() const { return index_ == order_.size(); }
6450 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006451 void Advance() { ++index_; }
6452
6453 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006454 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006455 size_t index_;
6456
6457 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6458};
6459
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006460// Iterator over the blocks that art part of the loop. Includes blocks part
6461// of an inner loop. The order in which the blocks are iterated is on their
6462// block id.
6463class HBlocksInLoopIterator : public ValueObject {
6464 public:
6465 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6466 : blocks_in_loop_(info.GetBlocks()),
6467 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6468 index_(0) {
6469 if (!blocks_in_loop_.IsBitSet(index_)) {
6470 Advance();
6471 }
6472 }
6473
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006474 bool Done() const { return index_ == blocks_.size(); }
6475 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006476 void Advance() {
6477 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006478 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006479 if (blocks_in_loop_.IsBitSet(index_)) {
6480 break;
6481 }
6482 }
6483 }
6484
6485 private:
6486 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006487 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006488 size_t index_;
6489
6490 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6491};
6492
Mingyao Yang3584bce2015-05-19 16:01:59 -07006493// Iterator over the blocks that art part of the loop. Includes blocks part
6494// of an inner loop. The order in which the blocks are iterated is reverse
6495// post order.
6496class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6497 public:
6498 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6499 : blocks_in_loop_(info.GetBlocks()),
6500 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6501 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006502 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006503 Advance();
6504 }
6505 }
6506
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006507 bool Done() const { return index_ == blocks_.size(); }
6508 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006509 void Advance() {
6510 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006511 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6512 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006513 break;
6514 }
6515 }
6516 }
6517
6518 private:
6519 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006520 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006521 size_t index_;
6522
6523 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6524};
6525
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006526inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006527 if (constant->IsIntConstant()) {
6528 return constant->AsIntConstant()->GetValue();
6529 } else if (constant->IsLongConstant()) {
6530 return constant->AsLongConstant()->GetValue();
6531 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006532 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006533 return 0;
6534 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006535}
6536
Vladimir Marko58155012015-08-19 12:49:41 +00006537inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6538 // For the purposes of the compiler, the dex files must actually be the same object
6539 // if we want to safely treat them as the same. This is especially important for JIT
6540 // as custom class loaders can open the same underlying file (or memory) multiple
6541 // times and provide different class resolution but no two class loaders should ever
6542 // use the same DexFile object - doing so is an unsupported hack that can lead to
6543 // all sorts of weird failures.
6544 return &lhs == &rhs;
6545}
6546
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006547#define INSTRUCTION_TYPE_CHECK(type, super) \
6548 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6549 inline const H##type* HInstruction::As##type() const { \
6550 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6551 } \
6552 inline H##type* HInstruction::As##type() { \
6553 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6554 }
6555
6556 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6557#undef INSTRUCTION_TYPE_CHECK
6558
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006559// Create space in `blocks` for adding `number_of_new_blocks` entries
6560// starting at location `at`. Blocks after `at` are moved accordingly.
6561inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6562 size_t number_of_new_blocks,
6563 size_t after) {
6564 DCHECK_LT(after, blocks->size());
6565 size_t old_size = blocks->size();
6566 size_t new_size = old_size + number_of_new_blocks;
6567 blocks->resize(new_size);
6568 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6569}
6570
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006571} // namespace art
6572
6573#endif // ART_COMPILER_OPTIMIZING_NODES_H_