blob: b684cc697f56ab2aa206644537c14409adcc8ba7 [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"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000100enum GraphAnalysisResult {
David Brazdilbadd8262016-02-02 16:28:56 +0000101 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000102 kAnalysisFailThrowCatchLoop,
103 kAnalysisFailAmbiguousArrayOp,
104 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000105};
106
Vladimir Markof9f64412015-09-02 14:05:49 +0100107class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100108 public:
109 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
110
111 void AddInstruction(HInstruction* instruction);
112 void RemoveInstruction(HInstruction* instruction);
113
David Brazdilc3d743f2015-04-22 13:40:50 +0100114 // Insert `instruction` before/after an existing instruction `cursor`.
115 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
116 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
117
Roland Levillain6b469232014-09-25 10:10:38 +0100118 // Return true if this list contains `instruction`.
119 bool Contains(HInstruction* instruction) const;
120
Roland Levillainccc07a92014-09-16 14:48:16 +0100121 // Return true if `instruction1` is found before `instruction2` in
122 // this instruction list and false otherwise. Abort if none
123 // of these instructions is found.
124 bool FoundBefore(const HInstruction* instruction1,
125 const HInstruction* instruction2) const;
126
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000127 bool IsEmpty() const { return first_instruction_ == nullptr; }
128 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
129
130 // Update the block of all instructions to be `block`.
131 void SetBlockOfInstructions(HBasicBlock* block) const;
132
133 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000134 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000135 void Add(const HInstructionList& instruction_list);
136
David Brazdil2d7352b2015-04-20 14:52:42 +0100137 // Return the number of instructions in the list. This is an expensive operation.
138 size_t CountSize() const;
139
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 private:
141 HInstruction* first_instruction_;
142 HInstruction* last_instruction_;
143
144 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000145 friend class HGraph;
146 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100148 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100149
150 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
151};
152
David Brazdil4833f5a2015-12-16 10:37:39 +0000153class ReferenceTypeInfo : ValueObject {
154 public:
155 typedef Handle<mirror::Class> TypeHandle;
156
Vladimir Markoa1de9182016-02-25 11:37:38 +0000157 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
158
159 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000160 return ReferenceTypeInfo(type_handle, is_exact);
161 }
162
163 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
164
Vladimir Markof39745e2016-01-26 12:16:55 +0000165 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000166 return handle.GetReference() != nullptr;
167 }
168
169 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
170 return IsValidHandle(type_handle_);
171 }
172
173 bool IsExact() const { return is_exact_; }
174
175 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
176 DCHECK(IsValid());
177 return GetTypeHandle()->IsObjectClass();
178 }
179
180 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
181 DCHECK(IsValid());
182 return GetTypeHandle()->IsStringClass();
183 }
184
185 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
186 DCHECK(IsValid());
187 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
188 }
189
190 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
191 DCHECK(IsValid());
192 return GetTypeHandle()->IsInterface();
193 }
194
195 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
196 DCHECK(IsValid());
197 return GetTypeHandle()->IsArrayClass();
198 }
199
200 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
201 DCHECK(IsValid());
202 return GetTypeHandle()->IsPrimitiveArray();
203 }
204
205 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
206 DCHECK(IsValid());
207 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
208 }
209
210 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
211 DCHECK(IsValid());
212 if (!IsExact()) return false;
213 if (!IsArrayClass()) return false;
214 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
215 }
216
217 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
218 DCHECK(IsValid());
219 if (!IsExact()) return false;
220 if (!IsArrayClass()) return false;
221 if (!rti.IsArrayClass()) return false;
222 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
223 rti.GetTypeHandle()->GetComponentType());
224 }
225
226 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
227
228 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
229 DCHECK(IsValid());
230 DCHECK(rti.IsValid());
231 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
232 }
233
234 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
235 DCHECK(IsValid());
236 DCHECK(rti.IsValid());
237 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
238 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
239 }
240
241 // Returns true if the type information provide the same amount of details.
242 // Note that it does not mean that the instructions have the same actual type
243 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000244 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000245 if (!IsValid() && !rti.IsValid()) {
246 // Invalid types are equal.
247 return true;
248 }
249 if (!IsValid() || !rti.IsValid()) {
250 // One is valid, the other not.
251 return false;
252 }
253 return IsExact() == rti.IsExact()
254 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
255 }
256
257 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000258 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
259 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
260 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000261
262 // The class of the object.
263 TypeHandle type_handle_;
264 // Whether or not the type is exact or a superclass of the actual type.
265 // Whether or not we have any information about this type.
266 bool is_exact_;
267};
268
269std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
270
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100272class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000273 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100274 HGraph(ArenaAllocator* arena,
275 const DexFile& dex_file,
276 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100277 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100279 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100280 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000281 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100282 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000283 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100284 blocks_(arena->Adapter(kArenaAllocBlockList)),
285 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
286 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700287 entry_block_(nullptr),
288 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100289 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100290 number_of_vregs_(0),
291 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000292 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400293 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000294 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000295 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000296 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000297 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100298 dex_file_(dex_file),
299 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100300 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100301 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100302 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000304 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100305 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
306 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
307 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
308 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000309 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000310 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
311 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100312 blocks_.reserve(kDefaultNumberOfBlocks);
313 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314
David Brazdilbadd8262016-02-02 16:28:56 +0000315 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
316 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
317
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000318 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100319 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
320
David Brazdil69ba7b72015-06-23 18:27:30 +0100321 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000322 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100323
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000324 HBasicBlock* GetEntryBlock() const { return entry_block_; }
325 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100326 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
329 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000330
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000331 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100332
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100333 void ComputeDominanceInformation();
334 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000335 void ClearLoopInformation();
336 void FindBackEdges(ArenaBitVector* visited);
337 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100338 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100339 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000340
David Brazdil4833f5a2015-12-16 10:37:39 +0000341 // Analyze all natural loops in this graph. Returns a code specifying that it
342 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000343 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000344 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100345
David Brazdilffee3d32015-07-06 11:48:53 +0100346 // Iterate over blocks to compute try block membership. Needs reverse post
347 // order and loop information.
348 void ComputeTryBlockInformation();
349
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000350 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000351 // Returns the instruction to replace the invoke expression or null if the
352 // invoke is for a void method. Note that the caller is responsible for replacing
353 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000354 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000355
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000356 // Update the loop and try membership of `block`, which was spawned from `reference`.
357 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
358 // should be the new back edge.
359 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
360 HBasicBlock* reference,
361 bool replace_if_back_edge);
362
Mingyao Yang3584bce2015-05-19 16:01:59 -0700363 // Need to add a couple of blocks to test if the loop body is entered and
364 // put deoptimization instructions, etc.
365 void TransformLoopHeaderForBCE(HBasicBlock* header);
366
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000367 // Removes `block` from the graph. Assumes `block` has been disconnected from
368 // other blocks and has no instructions or phis.
369 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000370
David Brazdilfc6a86a2015-06-26 10:33:45 +0000371 // Splits the edge between `block` and `successor` while preserving the
372 // indices in the predecessor/successor lists. If there are multiple edges
373 // between the blocks, the lowest indices are used.
374 // Returns the new block which is empty and has the same dex pc as `successor`.
375 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
376
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100377 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
378 void SimplifyLoop(HBasicBlock* header);
379
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000380 int32_t GetNextInstructionId() {
381 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000382 return current_instruction_id_++;
383 }
384
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000385 int32_t GetCurrentInstructionId() const {
386 return current_instruction_id_;
387 }
388
389 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000390 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 current_instruction_id_ = id;
392 }
393
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100394 uint16_t GetMaximumNumberOfOutVRegs() const {
395 return maximum_number_of_out_vregs_;
396 }
397
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000398 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
399 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100400 }
401
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100402 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
403 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
404 }
405
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000406 void UpdateTemporariesVRegSlots(size_t slots) {
407 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100408 }
409
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000410 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100411 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000412 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 }
414
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100415 void SetNumberOfVRegs(uint16_t number_of_vregs) {
416 number_of_vregs_ = number_of_vregs;
417 }
418
419 uint16_t GetNumberOfVRegs() const {
420 return number_of_vregs_;
421 }
422
423 void SetNumberOfInVRegs(uint16_t value) {
424 number_of_in_vregs_ = value;
425 }
426
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100427 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100428 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100429 return number_of_vregs_ - number_of_in_vregs_;
430 }
431
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100432 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100433 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100434 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100435
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100436 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100437 return linear_order_;
438 }
439
Mark Mendell1152c922015-04-24 17:06:35 -0400440 bool HasBoundsChecks() const {
441 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800442 }
443
Mark Mendell1152c922015-04-24 17:06:35 -0400444 void SetHasBoundsChecks(bool value) {
445 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800446 }
447
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100448 bool ShouldGenerateConstructorBarrier() const {
449 return should_generate_constructor_barrier_;
450 }
451
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000452 bool IsDebuggable() const { return debuggable_; }
453
David Brazdil8d5b8b22015-03-24 10:51:52 +0000454 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000455 // already, it is created and inserted into the graph. This method is only for
456 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600457 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000458
459 // TODO: This is problematic for the consistency of reference type propagation
460 // because it can be created anytime after the pass and thus it will be left
461 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600462 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000463
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600464 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
465 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000466 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600467 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
468 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000469 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600470 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
471 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000472 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600473 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
474 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000475 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000476
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100477 HCurrentMethod* GetCurrentMethod();
478
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100479 const DexFile& GetDexFile() const {
480 return dex_file_;
481 }
482
483 uint32_t GetMethodIdx() const {
484 return method_idx_;
485 }
486
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100487 InvokeType GetInvokeType() const {
488 return invoke_type_;
489 }
490
Mark Mendellc4701932015-04-10 13:18:51 -0400491 InstructionSet GetInstructionSet() const {
492 return instruction_set_;
493 }
494
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000495 bool IsCompilingOsr() const { return osr_; }
496
David Brazdil77a48ae2015-09-15 12:34:04 +0000497 bool HasTryCatch() const { return has_try_catch_; }
498 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100499
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000500 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
501 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
502
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100503 ArtMethod* GetArtMethod() const { return art_method_; }
504 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
505
Mark Mendellf6529172015-11-17 11:16:56 -0500506 // Returns an instruction with the opposite boolean value from 'cond'.
507 // The instruction has been inserted into the graph, either as a constant, or
508 // before cursor.
509 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
510
David Brazdil2d7352b2015-04-20 14:52:42 +0100511 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000512 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100513 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000514
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000515 template <class InstructionType, typename ValueType>
516 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 ArenaSafeMap<ValueType, InstructionType*>* cache,
518 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000519 // Try to find an existing constant of the given value.
520 InstructionType* constant = nullptr;
521 auto cached_constant = cache->find(value);
522 if (cached_constant != cache->end()) {
523 constant = cached_constant->second;
524 }
525
526 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100527 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600529 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000530 cache->Overwrite(value, constant);
531 InsertConstant(constant);
532 }
533 return constant;
534 }
535
David Brazdil8d5b8b22015-03-24 10:51:52 +0000536 void InsertConstant(HConstant* instruction);
537
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000538 // Cache a float constant into the graph. This method should only be
539 // called by the SsaBuilder when creating "equivalent" instructions.
540 void CacheFloatConstant(HFloatConstant* constant);
541
542 // See CacheFloatConstant comment.
543 void CacheDoubleConstant(HDoubleConstant* constant);
544
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000545 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000546
547 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100548 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000549
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100550 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100551 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000552
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100553 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100554 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100555
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000556 HBasicBlock* entry_block_;
557 HBasicBlock* exit_block_;
558
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100559 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100560 uint16_t maximum_number_of_out_vregs_;
561
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100562 // The number of virtual registers in this method. Contains the parameters.
563 uint16_t number_of_vregs_;
564
565 // The number of virtual registers used by parameters of this method.
566 uint16_t number_of_in_vregs_;
567
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000568 // Number of vreg size slots that the temporaries use (used in baseline compiler).
569 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100570
Mark Mendell1152c922015-04-24 17:06:35 -0400571 // Has bounds checks. We can totally skip BCE if it's false.
572 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800573
David Brazdil77a48ae2015-09-15 12:34:04 +0000574 // Flag whether there are any try/catch blocks in the graph. We will skip
575 // try/catch-related passes if false.
576 bool has_try_catch_;
577
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000578 // Flag whether there are any irreducible loops in the graph.
579 bool has_irreducible_loops_;
580
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000581 // Indicates whether the graph should be compiled in a way that
582 // ensures full debuggability. If false, we can apply more
583 // aggressive optimizations that may limit the level of debugging.
584 const bool debuggable_;
585
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000586 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000587 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000588
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100589 // The dex file from which the method is from.
590 const DexFile& dex_file_;
591
592 // The method index in the dex file.
593 const uint32_t method_idx_;
594
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100595 // If inlined, this encodes how the callee is being invoked.
596 const InvokeType invoke_type_;
597
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100598 // Whether the graph has been transformed to SSA form. Only used
599 // in debug mode to ensure we are not using properties only valid
600 // for non-SSA form (like the number of temporaries).
601 bool in_ssa_form_;
602
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100603 const bool should_generate_constructor_barrier_;
604
Mathieu Chartiere401d142015-04-22 13:56:20 -0700605 const InstructionSet instruction_set_;
606
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000607 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000608 HNullConstant* cached_null_constant_;
609 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000610 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000611 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000612 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000613
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100614 HCurrentMethod* cached_current_method_;
615
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100616 // The ArtMethod this graph is for. Note that for AOT, it may be null,
617 // for example for methods whose declaring class could not be resolved
618 // (such as when the superclass could not be found).
619 ArtMethod* art_method_;
620
David Brazdil4833f5a2015-12-16 10:37:39 +0000621 // Keep the RTI of inexact Object to avoid having to pass stack handle
622 // collection pointer to passes which may create NullConstant.
623 ReferenceTypeInfo inexact_object_rti_;
624
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000625 // Whether we are compiling this graph for on stack replacement: this will
626 // make all loops seen as irreducible and emit special stack maps to mark
627 // compiled code entries which the interpreter can directly jump to.
628 const bool osr_;
629
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000630 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100631 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000632 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000633 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000634 DISALLOW_COPY_AND_ASSIGN(HGraph);
635};
636
Vladimir Markof9f64412015-09-02 14:05:49 +0100637class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000638 public:
639 HLoopInformation(HBasicBlock* header, HGraph* graph)
640 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100641 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000642 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100643 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100644 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100645 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
646 back_edges_.reserve(kDefaultNumberOfBackEdges);
647 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100648
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000649 bool IsIrreducible() const { return irreducible_; }
650
651 void Dump(std::ostream& os);
652
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 HBasicBlock* GetHeader() const {
654 return header_;
655 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000656
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000657 void SetHeader(HBasicBlock* block) {
658 header_ = block;
659 }
660
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100661 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
662 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
663 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
664
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000665 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100666 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000667 }
668
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100669 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100670 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100671 }
672
David Brazdil46e2a392015-03-16 17:31:52 +0000673 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100674 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100675 }
676
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000677 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100678 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000679 }
680
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100683 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100684 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100685 }
686
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100687 // Returns the lifetime position of the back edge that has the
688 // greatest lifetime position.
689 size_t GetLifetimeEnd() const;
690
691 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100692 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100693 }
694
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000695 // Finds blocks that are part of this loop.
696 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100697
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100698 // Returns whether this loop information contains `block`.
699 // Note that this loop information *must* be populated before entering this function.
700 bool Contains(const HBasicBlock& block) const;
701
702 // Returns whether this loop information is an inner loop of `other`.
703 // Note that `other` *must* be populated before entering this function.
704 bool IsIn(const HLoopInformation& other) const;
705
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800706 // Returns true if instruction is not defined within this loop.
707 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700708
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100709 const ArenaBitVector& GetBlocks() const { return blocks_; }
710
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000711 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000712 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000713
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000714 void ClearAllBlocks() {
715 blocks_.ClearAllBits();
716 }
717
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000718 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100719 // Internal recursive implementation of `Populate`.
720 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000721 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100722
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000723 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100724 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000725 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100726 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100727 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000728
729 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
730};
731
David Brazdilec16f792015-08-19 15:04:01 +0100732// Stores try/catch information for basic blocks.
733// Note that HGraph is constructed so that catch blocks cannot simultaneously
734// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100735class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100736 public:
737 // Try block information constructor.
738 explicit TryCatchInformation(const HTryBoundary& try_entry)
739 : try_entry_(&try_entry),
740 catch_dex_file_(nullptr),
741 catch_type_index_(DexFile::kDexNoIndex16) {
742 DCHECK(try_entry_ != nullptr);
743 }
744
745 // Catch block information constructor.
746 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
747 : try_entry_(nullptr),
748 catch_dex_file_(&dex_file),
749 catch_type_index_(catch_type_index) {}
750
751 bool IsTryBlock() const { return try_entry_ != nullptr; }
752
753 const HTryBoundary& GetTryEntry() const {
754 DCHECK(IsTryBlock());
755 return *try_entry_;
756 }
757
758 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
759
760 bool IsCatchAllTypeIndex() const {
761 DCHECK(IsCatchBlock());
762 return catch_type_index_ == DexFile::kDexNoIndex16;
763 }
764
765 uint16_t GetCatchTypeIndex() const {
766 DCHECK(IsCatchBlock());
767 return catch_type_index_;
768 }
769
770 const DexFile& GetCatchDexFile() const {
771 DCHECK(IsCatchBlock());
772 return *catch_dex_file_;
773 }
774
775 private:
776 // One of possibly several TryBoundary instructions entering the block's try.
777 // Only set for try blocks.
778 const HTryBoundary* try_entry_;
779
780 // Exception type information. Only set for catch blocks.
781 const DexFile* catch_dex_file_;
782 const uint16_t catch_type_index_;
783};
784
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100785static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100786static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100787
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000788// A block in a method. Contains the list of instructions represented
789// as a double linked list. Each block knows its predecessors and
790// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100791
Vladimir Markof9f64412015-09-02 14:05:49 +0100792class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000793 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600794 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000795 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000796 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
797 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 loop_information_(nullptr),
799 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000800 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100801 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100802 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100803 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000804 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000805 try_catch_information_(nullptr) {
806 predecessors_.reserve(kDefaultNumberOfPredecessors);
807 successors_.reserve(kDefaultNumberOfSuccessors);
808 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
809 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000810
Vladimir Marko60584552015-09-03 13:35:12 +0000811 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100812 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000813 }
814
Vladimir Marko60584552015-09-03 13:35:12 +0000815 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100816 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817 }
818
David Brazdild26a4112015-11-10 11:07:31 +0000819 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
820 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
821
Vladimir Marko60584552015-09-03 13:35:12 +0000822 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
823 return ContainsElement(successors_, block, start_from);
824 }
825
826 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100827 return dominated_blocks_;
828 }
829
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100830 bool IsEntryBlock() const {
831 return graph_->GetEntryBlock() == this;
832 }
833
834 bool IsExitBlock() const {
835 return graph_->GetExitBlock() == this;
836 }
837
David Brazdil46e2a392015-03-16 17:31:52 +0000838 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000839 bool IsSingleTryBoundary() const;
840
841 // Returns true if this block emits nothing but a jump.
842 bool IsSingleJump() const {
843 HLoopInformation* loop_info = GetLoopInformation();
844 return (IsSingleGoto() || IsSingleTryBoundary())
845 // Back edges generate a suspend check.
846 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
847 }
David Brazdil46e2a392015-03-16 17:31:52 +0000848
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000849 void AddBackEdge(HBasicBlock* back_edge) {
850 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000851 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000852 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100853 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000854 loop_information_->AddBackEdge(back_edge);
855 }
856
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000857 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000858 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000859
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100860 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000861 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600862 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000863
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000864 HBasicBlock* GetDominator() const { return dominator_; }
865 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000866 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
867
868 void RemoveDominatedBlock(HBasicBlock* block) {
869 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100870 }
Vladimir Marko60584552015-09-03 13:35:12 +0000871
872 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
873 ReplaceElement(dominated_blocks_, existing, new_block);
874 }
875
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100876 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000877
878 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100879 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000880 }
881
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100882 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
883 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100884 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100885 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100886 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
887 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000888
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000889 HInstruction* GetFirstInstructionDisregardMoves() const;
890
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000891 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000892 successors_.push_back(block);
893 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894 }
895
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100896 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
897 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100898 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000899 new_block->predecessors_.push_back(this);
900 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000901 }
902
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000903 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
904 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000905 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000906 new_block->successors_.push_back(this);
907 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000908 }
909
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100910 // Insert `this` between `predecessor` and `successor. This method
911 // preserves the indicies, and will update the first edge found between
912 // `predecessor` and `successor`.
913 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
914 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100915 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000916 successor->predecessors_[predecessor_index] = this;
917 predecessor->successors_[successor_index] = this;
918 successors_.push_back(successor);
919 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100920 }
921
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100922 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000923 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100924 }
925
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000926 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000927 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000928 }
929
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100930 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000931 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100932 }
933
934 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000935 predecessors_.push_back(block);
936 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100937 }
938
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100939 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000940 DCHECK_EQ(predecessors_.size(), 2u);
941 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100942 }
943
David Brazdil769c9e52015-04-27 13:54:09 +0100944 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000945 DCHECK_EQ(successors_.size(), 2u);
946 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100947 }
948
David Brazdilfc6a86a2015-06-26 10:33:45 +0000949 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100951 }
952
David Brazdilfc6a86a2015-06-26 10:33:45 +0000953 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000954 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100955 }
956
David Brazdilfc6a86a2015-06-26 10:33:45 +0000957 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000958 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100959 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000960 }
961
962 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000963 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100964 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000965 }
966
967 // Returns whether the first occurrence of `predecessor` in the list of
968 // predecessors is at index `idx`.
969 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100970 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000971 return GetPredecessorIndexOf(predecessor) == idx;
972 }
973
David Brazdild7558da2015-09-22 13:04:14 +0100974 // Create a new block between this block and its predecessors. The new block
975 // is added to the graph, all predecessor edges are relinked to it and an edge
976 // is created to `this`. Returns the new empty block. Reverse post order or
977 // loop and try/catch information are not updated.
978 HBasicBlock* CreateImmediateDominator();
979
David Brazdilfc6a86a2015-06-26 10:33:45 +0000980 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100981 // created, latter block. Note that this method will add the block to the
982 // graph, create a Goto at the end of the former block and will create an edge
983 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100984 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000985 HBasicBlock* SplitBefore(HInstruction* cursor);
986
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000987 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000988 // created block. Note that this method just updates raw block information,
989 // like predecessors, successors, dominators, and instruction list. It does not
990 // update the graph, reverse post order, loop information, nor make sure the
991 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000992 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
993
994 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
995 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000996
David Brazdil9bc43612015-11-05 21:25:24 +0000997 // Split catch block into two blocks after the original move-exception bytecode
998 // instruction, or at the beginning if not present. Returns the newly created,
999 // latter block, or nullptr if such block could not be created (must be dead
1000 // in that case). 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).
1004 HBasicBlock* SplitCatchBlockAfterMoveException();
1005
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001006 // Merge `other` at the end of `this`. Successors and dominated blocks of
1007 // `other` are changed to be successors and dominated blocks of `this`. Note
1008 // that this method does not update the graph, reverse post order, loop
1009 // information, nor make sure the blocks are consistent (for example ending
1010 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001011 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001012
1013 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1014 // of `this` are moved to `other`.
1015 // Note that this method does not update the graph, reverse post order, loop
1016 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001017 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001018 void ReplaceWith(HBasicBlock* other);
1019
David Brazdil2d7352b2015-04-20 14:52:42 +01001020 // Merge `other` at the end of `this`. This method updates loops, reverse post
1021 // order, links to predecessors, successors, dominators and deletes the block
1022 // from the graph. The two blocks must be successive, i.e. `this` the only
1023 // predecessor of `other` and vice versa.
1024 void MergeWith(HBasicBlock* other);
1025
1026 // Disconnects `this` from all its predecessors, successors and dominator,
1027 // removes it from all loops it is included in and eventually from the graph.
1028 // The block must not dominate any other block. Predecessors and successors
1029 // are safely updated.
1030 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001031
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001033 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001034 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001035 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001036 // Replace instruction `initial` with `replacement` within this block.
1037 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1038 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001039 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001040 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001041 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001042 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1043 // instruction list. With 'ensure_safety' set to true, it verifies that the
1044 // instruction is not in use and removes it from the use lists of its inputs.
1045 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1046 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001047 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001048
1049 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001050 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051 }
1052
Roland Levillain6b879dd2014-09-22 17:13:44 +01001053 bool IsLoopPreHeaderFirstPredecessor() const {
1054 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001055 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001056 }
1057
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001058 bool IsFirstPredecessorBackEdge() const {
1059 DCHECK(IsLoopHeader());
1060 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1061 }
1062
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063 HLoopInformation* GetLoopInformation() const {
1064 return loop_information_;
1065 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001066
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001067 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001068 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001069 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001070 void SetInLoop(HLoopInformation* info) {
1071 if (IsLoopHeader()) {
1072 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001073 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001074 loop_information_ = info;
1075 } else if (loop_information_->Contains(*info->GetHeader())) {
1076 // Block is currently part of an outer loop. Make it part of this inner loop.
1077 // Note that a non loop header having a loop information means this loop information
1078 // has already been populated
1079 loop_information_ = info;
1080 } else {
1081 // Block is part of an inner loop. Do not update the loop information.
1082 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1083 // at this point, because this method is being called while populating `info`.
1084 }
1085 }
1086
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001087 // Raw update of the loop information.
1088 void SetLoopInformation(HLoopInformation* info) {
1089 loop_information_ = info;
1090 }
1091
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001092 bool IsInLoop() const { return loop_information_ != nullptr; }
1093
David Brazdilec16f792015-08-19 15:04:01 +01001094 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1095
1096 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1097 try_catch_information_ = try_catch_information;
1098 }
1099
1100 bool IsTryBlock() const {
1101 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1102 }
1103
1104 bool IsCatchBlock() const {
1105 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1106 }
David Brazdilffee3d32015-07-06 11:48:53 +01001107
1108 // Returns the try entry that this block's successors should have. They will
1109 // be in the same try, unless the block ends in a try boundary. In that case,
1110 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001111 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001112
David Brazdild7558da2015-09-22 13:04:14 +01001113 bool HasThrowingInstructions() const;
1114
David Brazdila4b8c212015-05-07 09:59:30 +01001115 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001116 bool Dominates(HBasicBlock* block) const;
1117
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001118 size_t GetLifetimeStart() const { return lifetime_start_; }
1119 size_t GetLifetimeEnd() const { return lifetime_end_; }
1120
1121 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1122 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1123
David Brazdil8d5b8b22015-03-24 10:51:52 +00001124 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001125 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001126 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001127 bool HasSinglePhi() const;
1128
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001129 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001130 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001131 ArenaVector<HBasicBlock*> predecessors_;
1132 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133 HInstructionList instructions_;
1134 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001135 HLoopInformation* loop_information_;
1136 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001137 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001138 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001139 // The dex program counter of the first instruction of this block.
1140 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001141 size_t lifetime_start_;
1142 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001143 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001144
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001145 friend class HGraph;
1146 friend class HInstruction;
1147
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1149};
1150
David Brazdilb2bd1c52015-03-25 11:17:37 +00001151// Iterates over the LoopInformation of all loops which contain 'block'
1152// from the innermost to the outermost.
1153class HLoopInformationOutwardIterator : public ValueObject {
1154 public:
1155 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1156 : current_(block.GetLoopInformation()) {}
1157
1158 bool Done() const { return current_ == nullptr; }
1159
1160 void Advance() {
1161 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001162 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001163 }
1164
1165 HLoopInformation* Current() const {
1166 DCHECK(!Done());
1167 return current_;
1168 }
1169
1170 private:
1171 HLoopInformation* current_;
1172
1173 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1174};
1175
Alexandre Ramesef20f712015-06-09 10:29:30 +01001176#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001177 M(Above, Condition) \
1178 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001179 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001180 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001181 M(ArrayGet, Instruction) \
1182 M(ArrayLength, Instruction) \
1183 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001184 M(Below, Condition) \
1185 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001186 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001187 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001188 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001189 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001190 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001191 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001192 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001194 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001195 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001196 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001197 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001198 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001199 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001200 M(Exit, Instruction) \
1201 M(FloatConstant, Constant) \
1202 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001203 M(GreaterThan, Condition) \
1204 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001205 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001206 M(InstanceFieldGet, Instruction) \
1207 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001208 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001210 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001211 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001212 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001213 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001214 M(LessThan, Condition) \
1215 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001216 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001217 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001218 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001219 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001220 M(Local, Instruction) \
1221 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001222 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001223 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001225 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001226 M(Neg, UnaryOperation) \
1227 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001228 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001229 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001230 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001231 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001232 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001233 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001234 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001235 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001236 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001237 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001238 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001239 M(Return, Instruction) \
1240 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001241 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001242 M(Shl, BinaryOperation) \
1243 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001244 M(StaticFieldGet, Instruction) \
1245 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001246 M(UnresolvedInstanceFieldGet, Instruction) \
1247 M(UnresolvedInstanceFieldSet, Instruction) \
1248 M(UnresolvedStaticFieldGet, Instruction) \
1249 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001250 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001251 M(StoreLocal, Instruction) \
1252 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) \
1267 M(MultiplyAccumulate, Instruction)
1268#endif
1269
Vladimir Markob4536b72015-11-24 13:45:23 +00001270#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001271#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001272#else
1273#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1274 M(ArmDexCacheArraysBase, Instruction)
1275#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001276
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001277#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001278#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001279#else
1280#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001281 M(Arm64BitwiseNegatedRight, Instruction) \
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
David Brazdiled596192015-01-23 10:39:45 +00001340template <typename T> class HUseList;
1341
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001342template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001343class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001344 public:
David Brazdiled596192015-01-23 10:39:45 +00001345 HUseListNode* GetPrevious() const { return prev_; }
1346 HUseListNode* GetNext() const { return next_; }
1347 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001348 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001349 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001350
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001351 private:
David Brazdiled596192015-01-23 10:39:45 +00001352 HUseListNode(T user, size_t index)
1353 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1354
1355 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001356 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001357 HUseListNode<T>* prev_;
1358 HUseListNode<T>* next_;
1359
1360 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001361
1362 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1363};
1364
David Brazdiled596192015-01-23 10:39:45 +00001365template <typename T>
1366class HUseList : public ValueObject {
1367 public:
1368 HUseList() : first_(nullptr) {}
1369
1370 void Clear() {
1371 first_ = nullptr;
1372 }
1373
1374 // Adds a new entry at the beginning of the use list and returns
1375 // the newly created node.
1376 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001377 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001378 if (IsEmpty()) {
1379 first_ = new_node;
1380 } else {
1381 first_->prev_ = new_node;
1382 new_node->next_ = first_;
1383 first_ = new_node;
1384 }
1385 return new_node;
1386 }
1387
1388 HUseListNode<T>* GetFirst() const {
1389 return first_;
1390 }
1391
1392 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001393 DCHECK(node != nullptr);
1394 DCHECK(Contains(node));
1395
David Brazdiled596192015-01-23 10:39:45 +00001396 if (node->prev_ != nullptr) {
1397 node->prev_->next_ = node->next_;
1398 }
1399 if (node->next_ != nullptr) {
1400 node->next_->prev_ = node->prev_;
1401 }
1402 if (node == first_) {
1403 first_ = node->next_;
1404 }
1405 }
1406
David Brazdil1abb4192015-02-17 18:33:36 +00001407 bool Contains(const HUseListNode<T>* node) const {
1408 if (node == nullptr) {
1409 return false;
1410 }
1411 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1412 if (current == node) {
1413 return true;
1414 }
1415 }
1416 return false;
1417 }
1418
David Brazdiled596192015-01-23 10:39:45 +00001419 bool IsEmpty() const {
1420 return first_ == nullptr;
1421 }
1422
1423 bool HasOnlyOneUse() const {
1424 return first_ != nullptr && first_->next_ == nullptr;
1425 }
1426
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001427 size_t SizeSlow() const {
1428 size_t count = 0;
1429 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1430 ++count;
1431 }
1432 return count;
1433 }
1434
David Brazdiled596192015-01-23 10:39:45 +00001435 private:
1436 HUseListNode<T>* first_;
1437};
1438
1439template<typename T>
1440class HUseIterator : public ValueObject {
1441 public:
1442 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1443
1444 bool Done() const { return current_ == nullptr; }
1445
1446 void Advance() {
1447 DCHECK(!Done());
1448 current_ = current_->GetNext();
1449 }
1450
1451 HUseListNode<T>* Current() const {
1452 DCHECK(!Done());
1453 return current_;
1454 }
1455
1456 private:
1457 HUseListNode<T>* current_;
1458
1459 friend class HValue;
1460};
1461
David Brazdil1abb4192015-02-17 18:33:36 +00001462// This class is used by HEnvironment and HInstruction classes to record the
1463// instructions they use and pointers to the corresponding HUseListNodes kept
1464// by the used instructions.
1465template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001466class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001467 public:
1468 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1469 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1470
1471 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1472 : instruction_(old_record.instruction_), use_node_(use_node) {
1473 DCHECK(instruction_ != nullptr);
1474 DCHECK(use_node_ != nullptr);
1475 DCHECK(old_record.use_node_ == nullptr);
1476 }
1477
1478 HInstruction* GetInstruction() const { return instruction_; }
1479 HUseListNode<T>* GetUseNode() const { return use_node_; }
1480
1481 private:
1482 // Instruction used by the user.
1483 HInstruction* instruction_;
1484
1485 // Corresponding entry in the use list kept by 'instruction_'.
1486 HUseListNode<T>* use_node_;
1487};
1488
Aart Bik854a02b2015-07-14 16:07:00 -07001489/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001490 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001491 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001492 * For write/read dependences on fields/arrays, the dependence analysis uses
1493 * type disambiguation (e.g. a float field write cannot modify the value of an
1494 * integer field read) and the access type (e.g. a reference array write cannot
1495 * modify the value of a reference field read [although it may modify the
1496 * reference fetch prior to reading the field, which is represented by its own
1497 * write/read dependence]). The analysis makes conservative points-to
1498 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1499 * the same, and any reference read depends on any reference read without
1500 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001501 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001502 * The internal representation uses 38-bit and is described in the table below.
1503 * The first line indicates the side effect, and for field/array accesses the
1504 * second line indicates the type of the access (in the order of the
1505 * Primitive::Type enum).
1506 * The two numbered lines below indicate the bit position in the bitfield (read
1507 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001508 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001509 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1510 * +-------------+---------+---------+--------------+---------+---------+
1511 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1512 * | 3 |333333322|222222221| 1 |111111110|000000000|
1513 * | 7 |654321098|765432109| 8 |765432109|876543210|
1514 *
1515 * Note that, to ease the implementation, 'changes' bits are least significant
1516 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001517 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001518class SideEffects : public ValueObject {
1519 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001520 SideEffects() : flags_(0) {}
1521
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001522 static SideEffects None() {
1523 return SideEffects(0);
1524 }
1525
1526 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001527 return SideEffects(kAllChangeBits | kAllDependOnBits);
1528 }
1529
1530 static SideEffects AllChanges() {
1531 return SideEffects(kAllChangeBits);
1532 }
1533
1534 static SideEffects AllDependencies() {
1535 return SideEffects(kAllDependOnBits);
1536 }
1537
1538 static SideEffects AllExceptGCDependency() {
1539 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1540 }
1541
1542 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001543 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001544 }
1545
Aart Bik34c3ba92015-07-20 14:08:59 -07001546 static SideEffects AllWrites() {
1547 return SideEffects(kAllWrites);
1548 }
1549
1550 static SideEffects AllReads() {
1551 return SideEffects(kAllReads);
1552 }
1553
1554 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1555 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001556 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001557 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001558 }
1559
Aart Bik854a02b2015-07-14 16:07:00 -07001560 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1561 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001562 }
1563
Aart Bik34c3ba92015-07-20 14:08:59 -07001564 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1565 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001566 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001567 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001568 }
1569
1570 static SideEffects ArrayReadOfType(Primitive::Type type) {
1571 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1572 }
1573
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001574 static SideEffects CanTriggerGC() {
1575 return SideEffects(1ULL << kCanTriggerGCBit);
1576 }
1577
1578 static SideEffects DependsOnGC() {
1579 return SideEffects(1ULL << kDependsOnGCBit);
1580 }
1581
Aart Bik854a02b2015-07-14 16:07:00 -07001582 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001583 SideEffects Union(SideEffects other) const {
1584 return SideEffects(flags_ | other.flags_);
1585 }
1586
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001587 SideEffects Exclusion(SideEffects other) const {
1588 return SideEffects(flags_ & ~other.flags_);
1589 }
1590
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001591 void Add(SideEffects other) {
1592 flags_ |= other.flags_;
1593 }
1594
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 bool Includes(SideEffects other) const {
1596 return (other.flags_ & flags_) == other.flags_;
1597 }
1598
1599 bool HasSideEffects() const {
1600 return (flags_ & kAllChangeBits);
1601 }
1602
1603 bool HasDependencies() const {
1604 return (flags_ & kAllDependOnBits);
1605 }
1606
1607 // Returns true if there are no side effects or dependencies.
1608 bool DoesNothing() const {
1609 return flags_ == 0;
1610 }
1611
Aart Bik854a02b2015-07-14 16:07:00 -07001612 // Returns true if something is written.
1613 bool DoesAnyWrite() const {
1614 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001615 }
1616
Aart Bik854a02b2015-07-14 16:07:00 -07001617 // Returns true if something is read.
1618 bool DoesAnyRead() const {
1619 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001620 }
1621
Aart Bik854a02b2015-07-14 16:07:00 -07001622 // Returns true if potentially everything is written and read
1623 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001624 bool DoesAllReadWrite() const {
1625 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1626 }
1627
Aart Bik854a02b2015-07-14 16:07:00 -07001628 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001630 }
1631
Roland Levillain0d5a2812015-11-13 10:07:31 +00001632 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001633 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001634 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1635 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001636 }
1637
1638 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001639 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001640 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001641 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 for (int s = kLastBit; s >= 0; s--) {
1643 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1644 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1645 // This is a bit for the GC side effect.
1646 if (current_bit_is_set) {
1647 flags += "GC";
1648 }
Aart Bik854a02b2015-07-14 16:07:00 -07001649 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001650 } else {
1651 // This is a bit for the array/field analysis.
1652 // The underscore character stands for the 'can trigger GC' bit.
1653 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1654 if (current_bit_is_set) {
1655 flags += kDebug[s];
1656 }
1657 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1658 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1659 flags += "|";
1660 }
1661 }
Aart Bik854a02b2015-07-14 16:07:00 -07001662 }
1663 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001664 }
1665
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001666 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001667
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001668 private:
1669 static constexpr int kFieldArrayAnalysisBits = 9;
1670
1671 static constexpr int kFieldWriteOffset = 0;
1672 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1673 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1674 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1675
1676 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1677
1678 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1679 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1680 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1681 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1682
1683 static constexpr int kLastBit = kDependsOnGCBit;
1684 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1685
1686 // Aliases.
1687
1688 static_assert(kChangeBits == kDependOnBits,
1689 "the 'change' bits should match the 'depend on' bits.");
1690
1691 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1692 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1693 static constexpr uint64_t kAllWrites =
1694 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1695 static constexpr uint64_t kAllReads =
1696 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001697
Aart Bik854a02b2015-07-14 16:07:00 -07001698 // Work around the fact that HIR aliases I/F and J/D.
1699 // TODO: remove this interceptor once HIR types are clean
1700 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1701 switch (type) {
1702 case Primitive::kPrimInt:
1703 case Primitive::kPrimFloat:
1704 return TypeFlag(Primitive::kPrimInt, offset) |
1705 TypeFlag(Primitive::kPrimFloat, offset);
1706 case Primitive::kPrimLong:
1707 case Primitive::kPrimDouble:
1708 return TypeFlag(Primitive::kPrimLong, offset) |
1709 TypeFlag(Primitive::kPrimDouble, offset);
1710 default:
1711 return TypeFlag(type, offset);
1712 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001713 }
1714
Aart Bik854a02b2015-07-14 16:07:00 -07001715 // Translates type to bit flag.
1716 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1717 CHECK_NE(type, Primitive::kPrimVoid);
1718 const uint64_t one = 1;
1719 const int shift = type; // 0-based consecutive enum
1720 DCHECK_LE(kFieldWriteOffset, shift);
1721 DCHECK_LT(shift, kArrayWriteOffset);
1722 return one << (type + offset);
1723 }
1724
1725 // Private constructor on direct flags value.
1726 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1727
1728 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001729};
1730
David Brazdiled596192015-01-23 10:39:45 +00001731// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001732class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001733 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001734 HEnvironment(ArenaAllocator* arena,
1735 size_t number_of_vregs,
1736 const DexFile& dex_file,
1737 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001738 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001739 InvokeType invoke_type,
1740 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001741 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1742 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001743 parent_(nullptr),
1744 dex_file_(dex_file),
1745 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001746 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001747 invoke_type_(invoke_type),
1748 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001749 }
1750
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001751 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001752 : HEnvironment(arena,
1753 to_copy.Size(),
1754 to_copy.GetDexFile(),
1755 to_copy.GetMethodIdx(),
1756 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001757 to_copy.GetInvokeType(),
1758 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001759
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001760 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001761 if (parent_ != nullptr) {
1762 parent_->SetAndCopyParentChain(allocator, parent);
1763 } else {
1764 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1765 parent_->CopyFrom(parent);
1766 if (parent->GetParent() != nullptr) {
1767 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1768 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001769 }
David Brazdiled596192015-01-23 10:39:45 +00001770 }
1771
Vladimir Marko71bf8092015-09-15 15:33:14 +01001772 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001773 void CopyFrom(HEnvironment* environment);
1774
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001775 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1776 // input to the loop phi instead. This is for inserting instructions that
1777 // require an environment (like HDeoptimization) in the loop pre-header.
1778 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001779
1780 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001781 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001782 }
1783
1784 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001785 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001786 }
1787
David Brazdil1abb4192015-02-17 18:33:36 +00001788 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001789
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001790 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001791
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001792 HEnvironment* GetParent() const { return parent_; }
1793
1794 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001795 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001796 }
1797
1798 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001799 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001800 }
1801
1802 uint32_t GetDexPc() const {
1803 return dex_pc_;
1804 }
1805
1806 uint32_t GetMethodIdx() const {
1807 return method_idx_;
1808 }
1809
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001810 InvokeType GetInvokeType() const {
1811 return invoke_type_;
1812 }
1813
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001814 const DexFile& GetDexFile() const {
1815 return dex_file_;
1816 }
1817
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001818 HInstruction* GetHolder() const {
1819 return holder_;
1820 }
1821
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001822
1823 bool IsFromInlinedInvoke() const {
1824 return GetParent() != nullptr;
1825 }
1826
David Brazdiled596192015-01-23 10:39:45 +00001827 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001828 // Record instructions' use entries of this environment for constant-time removal.
1829 // It should only be called by HInstruction when a new environment use is added.
1830 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1831 DCHECK(env_use->GetUser() == this);
1832 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001833 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001834 }
David Brazdiled596192015-01-23 10:39:45 +00001835
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001836 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1837 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001838 HEnvironment* parent_;
1839 const DexFile& dex_file_;
1840 const uint32_t method_idx_;
1841 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001842 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001843
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001844 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001845 HInstruction* const holder_;
1846
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001847 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001848
1849 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1850};
1851
Vladimir Markof9f64412015-09-02 14:05:49 +01001852class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001853 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001854 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001855 : previous_(nullptr),
1856 next_(nullptr),
1857 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001858 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001859 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001860 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001861 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001862 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001863 locations_(nullptr),
1864 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001865 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001866 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001867 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1868 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1869 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870
Dave Allison20dfc792014-06-16 20:44:29 -07001871 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001872
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001873#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001874 enum InstructionKind {
1875 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1876 };
1877#undef DECLARE_KIND
1878
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001879 HInstruction* GetNext() const { return next_; }
1880 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001881
Calin Juravle77520bc2015-01-12 18:45:46 +00001882 HInstruction* GetNextDisregardingMoves() const;
1883 HInstruction* GetPreviousDisregardingMoves() const;
1884
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001885 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001886 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001887 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001888 bool IsInBlock() const { return block_ != nullptr; }
1889 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001890 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1891 bool IsIrreducibleLoopHeaderPhi() const {
1892 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1893 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001894
Roland Levillain6b879dd2014-09-22 17:13:44 +01001895 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001896 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001897
1898 virtual void Accept(HGraphVisitor* visitor) = 0;
1899 virtual const char* DebugName() const = 0;
1900
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001901 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001902 void SetRawInputAt(size_t index, HInstruction* input) {
1903 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1904 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001905
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001906 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001907
1908 uint32_t GetDexPc() const { return dex_pc_; }
1909
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001910 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001911
Roland Levillaine161a2a2014-10-03 12:45:18 +01001912 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001913 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001914
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001915 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001916 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001917
Calin Juravle10e244f2015-01-26 18:54:32 +00001918 // Does not apply for all instructions, but having this at top level greatly
1919 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001920 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001921 virtual bool CanBeNull() const {
1922 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1923 return true;
1924 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001925
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001926 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001927 return false;
1928 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001929
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001930 virtual bool IsActualObject() const {
1931 return GetType() == Primitive::kPrimNot;
1932 }
1933
Calin Juravle2e768302015-07-28 14:41:11 +00001934 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001935
Calin Juravle61d544b2015-02-23 16:46:57 +00001936 ReferenceTypeInfo GetReferenceTypeInfo() const {
1937 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001938 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
1939 GetPackedFlag<kFlagReferenceTypeIsExact>());;
Calin Juravle61d544b2015-02-23 16:46:57 +00001940 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001941
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001942 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001943 DCHECK(user != nullptr);
1944 HUseListNode<HInstruction*>* use =
1945 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1946 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001947 }
1948
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001949 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001950 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001951 HUseListNode<HEnvironment*>* env_use =
1952 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1953 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001954 }
1955
David Brazdil1abb4192015-02-17 18:33:36 +00001956 void RemoveAsUserOfInput(size_t input) {
1957 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1958 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1959 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960
David Brazdil1abb4192015-02-17 18:33:36 +00001961 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1962 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001963
David Brazdiled596192015-01-23 10:39:45 +00001964 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1965 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001966 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001967 bool HasOnlyOneNonEnvironmentUse() const {
1968 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1969 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001970
Roland Levillain6c82d402014-10-13 16:10:27 +01001971 // Does this instruction strictly dominate `other_instruction`?
1972 // Returns false if this instruction and `other_instruction` are the same.
1973 // Aborts if this instruction and `other_instruction` are both phis.
1974 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001975
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001976 int GetId() const { return id_; }
1977 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001978
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001979 int GetSsaIndex() const { return ssa_index_; }
1980 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1981 bool HasSsaIndex() const { return ssa_index_ != -1; }
1982
1983 bool HasEnvironment() const { return environment_ != nullptr; }
1984 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001985 // Set the `environment_` field. Raw because this method does not
1986 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001987 void SetRawEnvironment(HEnvironment* environment) {
1988 DCHECK(environment_ == nullptr);
1989 DCHECK_EQ(environment->GetHolder(), this);
1990 environment_ = environment;
1991 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001992
1993 // Set the environment of this instruction, copying it from `environment`. While
1994 // copying, the uses lists are being updated.
1995 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001996 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001997 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001998 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001999 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002000 if (environment->GetParent() != nullptr) {
2001 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2002 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002003 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002004
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002005 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2006 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002007 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002008 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002009 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002010 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002011 if (environment->GetParent() != nullptr) {
2012 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2013 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002014 }
2015
Nicolas Geoffray39468442014-09-02 15:17:15 +01002016 // Returns the number of entries in the environment. Typically, that is the
2017 // number of dex registers in a method. It could be more in case of inlining.
2018 size_t EnvironmentSize() const;
2019
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002020 LocationSummary* GetLocations() const { return locations_; }
2021 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002022
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002023 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002024 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002025
Alexandre Rames188d4312015-04-09 18:30:21 +01002026 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2027 // uses of this instruction by `other` are *not* updated.
2028 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2029 ReplaceWith(other);
2030 other->ReplaceInput(this, use_index);
2031 }
2032
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002033 // Move `this` instruction before `cursor`.
2034 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002035
Vladimir Markofb337ea2015-11-25 15:25:10 +00002036 // Move `this` before its first user and out of any loops. If there is no
2037 // out-of-loop user that dominates all other users, move the instruction
2038 // to the end of the out-of-loop common dominator of the user's blocks.
2039 //
2040 // This can be used only on non-throwing instructions with no side effects that
2041 // have at least one use but no environment uses.
2042 void MoveBeforeFirstUserAndOutOfLoops();
2043
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002044#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002045 bool Is##type() const; \
2046 const H##type* As##type() const; \
2047 H##type* As##type();
2048
2049 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2050#undef INSTRUCTION_TYPE_CHECK
2051
2052#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002053 bool Is##type() const { return (As##type() != nullptr); } \
2054 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002055 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002056 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002057#undef INSTRUCTION_TYPE_CHECK
2058
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002059 // Returns whether the instruction can be moved within the graph.
2060 virtual bool CanBeMoved() const { return false; }
2061
2062 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002063 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002064 return false;
2065 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002066
2067 // Returns whether any data encoded in the two instructions is equal.
2068 // This method does not look at the inputs. Both instructions must be
2069 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002070 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002071 return false;
2072 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002073
2074 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002075 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002076 // 2) Their inputs are identical.
2077 bool Equals(HInstruction* other) const;
2078
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002079 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2080 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2081 // the virtual function because the __attribute__((__pure__)) doesn't really
2082 // apply the strong requirement for virtual functions, preventing optimizations.
2083 InstructionKind GetKind() const PURE;
2084 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002085
2086 virtual size_t ComputeHashCode() const {
2087 size_t result = GetKind();
2088 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2089 result = (result * 31) + InputAt(i)->GetId();
2090 }
2091 return result;
2092 }
2093
2094 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002095 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002096 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002097
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002098 size_t GetLifetimePosition() const { return lifetime_position_; }
2099 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2100 LiveInterval* GetLiveInterval() const { return live_interval_; }
2101 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2102 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2103
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002104 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2105
2106 // Returns whether the code generation of the instruction will require to have access
2107 // to the current method. Such instructions are:
2108 // (1): Instructions that require an environment, as calling the runtime requires
2109 // to walk the stack and have the current method stored at a specific stack address.
2110 // (2): Object literals like classes and strings, that are loaded from the dex cache
2111 // fields of the current method.
2112 bool NeedsCurrentMethod() const {
2113 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2114 }
2115
Vladimir Markodc151b22015-10-15 18:02:30 +01002116 // Returns whether the code generation of the instruction will require to have access
2117 // to the dex cache of the current method's declaring class via the current method.
2118 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002119
Mark Mendellc4701932015-04-10 13:18:51 -04002120 // Does this instruction have any use in an environment before
2121 // control flow hits 'other'?
2122 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2123
2124 // Remove all references to environment uses of this instruction.
2125 // The caller must ensure that this is safe to do.
2126 void RemoveEnvironmentUsers();
2127
Vladimir Markoa1de9182016-02-25 11:37:38 +00002128 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2129 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002130
David Brazdil1abb4192015-02-17 18:33:36 +00002131 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002132 // If set, the machine code for this instruction is assumed to be generated by
2133 // its users. Used by liveness analysis to compute use positions accordingly.
2134 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2135 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2136 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2137 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2138
David Brazdil1abb4192015-02-17 18:33:36 +00002139 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2140 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2141
Vladimir Markoa1de9182016-02-25 11:37:38 +00002142 uint32_t GetPackedFields() const {
2143 return packed_fields_;
2144 }
2145
2146 template <size_t flag>
2147 bool GetPackedFlag() const {
2148 return (packed_fields_ & (1u << flag)) != 0u;
2149 }
2150
2151 template <size_t flag>
2152 void SetPackedFlag(bool value = true) {
2153 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2154 }
2155
2156 template <typename BitFieldType>
2157 typename BitFieldType::value_type GetPackedField() const {
2158 return BitFieldType::Decode(packed_fields_);
2159 }
2160
2161 template <typename BitFieldType>
2162 void SetPackedField(typename BitFieldType::value_type value) {
2163 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2164 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2165 }
2166
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002167 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002168 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2169
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170 HInstruction* previous_;
2171 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002172 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002173 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002174
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002175 // An instruction gets an id when it is added to the graph.
2176 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002177 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002178 int id_;
2179
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002180 // When doing liveness analysis, instructions that have uses get an SSA index.
2181 int ssa_index_;
2182
Vladimir Markoa1de9182016-02-25 11:37:38 +00002183 // Packed fields.
2184 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002185
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002186 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002187 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002188
2189 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002190 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002191
Nicolas Geoffray39468442014-09-02 15:17:15 +01002192 // The environment associated with this instruction. Not null if the instruction
2193 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002194 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002195
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002196 // Set by the code generator.
2197 LocationSummary* locations_;
2198
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002199 // Set by the liveness analysis.
2200 LiveInterval* live_interval_;
2201
2202 // Set by the liveness analysis, this is the position in a linear
2203 // order of blocks where this instruction's live interval start.
2204 size_t lifetime_position_;
2205
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002206 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002207
Vladimir Markoa1de9182016-02-25 11:37:38 +00002208 // The reference handle part of the reference type info.
2209 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002210 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002211 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002212
David Brazdil1abb4192015-02-17 18:33:36 +00002213 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002214 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002215 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002216 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002217 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002218
2219 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2220};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002221std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002222
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002223class HInputIterator : public ValueObject {
2224 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002225 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226
2227 bool Done() const { return index_ == instruction_->InputCount(); }
2228 HInstruction* Current() const { return instruction_->InputAt(index_); }
2229 void Advance() { index_++; }
2230
2231 private:
2232 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002233 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002234
2235 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2236};
2237
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002238class HInstructionIterator : public ValueObject {
2239 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002240 explicit HInstructionIterator(const HInstructionList& instructions)
2241 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002242 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002243 }
2244
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002245 bool Done() const { return instruction_ == nullptr; }
2246 HInstruction* Current() const { return instruction_; }
2247 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002248 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002249 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002250 }
2251
2252 private:
2253 HInstruction* instruction_;
2254 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002255
2256 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257};
2258
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002259class HBackwardInstructionIterator : public ValueObject {
2260 public:
2261 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2262 : instruction_(instructions.last_instruction_) {
2263 next_ = Done() ? nullptr : instruction_->GetPrevious();
2264 }
2265
2266 bool Done() const { return instruction_ == nullptr; }
2267 HInstruction* Current() const { return instruction_; }
2268 void Advance() {
2269 instruction_ = next_;
2270 next_ = Done() ? nullptr : instruction_->GetPrevious();
2271 }
2272
2273 private:
2274 HInstruction* instruction_;
2275 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002276
2277 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002278};
2279
Vladimir Markof9f64412015-09-02 14:05:49 +01002280template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002281class HTemplateInstruction: public HInstruction {
2282 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002283 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002284 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002285 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002286
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002287 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002288
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002289 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002290 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2291 DCHECK_LT(i, N);
2292 return inputs_[i];
2293 }
David Brazdil1abb4192015-02-17 18:33:36 +00002294
2295 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002296 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002297 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002298 }
2299
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002300 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002301 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002302
2303 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002304};
2305
Vladimir Markof9f64412015-09-02 14:05:49 +01002306// HTemplateInstruction specialization for N=0.
2307template<>
2308class HTemplateInstruction<0>: public HInstruction {
2309 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002310 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002311 : HInstruction(side_effects, dex_pc) {}
2312
Vladimir Markof9f64412015-09-02 14:05:49 +01002313 virtual ~HTemplateInstruction() {}
2314
2315 size_t InputCount() const OVERRIDE { return 0; }
2316
2317 protected:
2318 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2319 LOG(FATAL) << "Unreachable";
2320 UNREACHABLE();
2321 }
2322
2323 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2324 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2325 LOG(FATAL) << "Unreachable";
2326 UNREACHABLE();
2327 }
2328
2329 private:
2330 friend class SsaBuilder;
2331};
2332
Dave Allison20dfc792014-06-16 20:44:29 -07002333template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002334class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002335 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002336 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002337 : HTemplateInstruction<N>(side_effects, dex_pc) {
2338 this->template SetPackedField<TypeField>(type);
2339 }
Dave Allison20dfc792014-06-16 20:44:29 -07002340 virtual ~HExpression() {}
2341
Vladimir Markoa1de9182016-02-25 11:37:38 +00002342 Primitive::Type GetType() const OVERRIDE {
2343 return TypeField::Decode(this->GetPackedFields());
2344 }
Dave Allison20dfc792014-06-16 20:44:29 -07002345
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002346 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002347 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2348 static constexpr size_t kFieldTypeSize =
2349 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2350 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2351 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2352 "Too many packed fields.");
2353 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002354};
2355
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002356// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2357// instruction that branches to the exit block.
2358class HReturnVoid : public HTemplateInstruction<0> {
2359 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002360 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2361 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002362
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002363 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002364
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002365 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002366
2367 private:
2368 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2369};
2370
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002371// Represents dex's RETURN opcodes. A HReturn is a control flow
2372// instruction that branches to the exit block.
2373class HReturn : public HTemplateInstruction<1> {
2374 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002375 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2376 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002377 SetRawInputAt(0, value);
2378 }
2379
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002380 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002381
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002382 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002383
2384 private:
2385 DISALLOW_COPY_AND_ASSIGN(HReturn);
2386};
2387
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002388// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002389// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002390// exit block.
2391class HExit : public HTemplateInstruction<0> {
2392 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002393 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002394
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002395 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002396
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002397 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002398
2399 private:
2400 DISALLOW_COPY_AND_ASSIGN(HExit);
2401};
2402
2403// Jumps from one block to another.
2404class HGoto : public HTemplateInstruction<0> {
2405 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002406 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002407
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002408 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002409
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002410 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002411 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002412 }
2413
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002414 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002415
2416 private:
2417 DISALLOW_COPY_AND_ASSIGN(HGoto);
2418};
2419
Roland Levillain9867bc72015-08-05 10:21:34 +01002420class HConstant : public HExpression<0> {
2421 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002422 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2423 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002424
2425 bool CanBeMoved() const OVERRIDE { return true; }
2426
2427 virtual bool IsMinusOne() const { return false; }
2428 virtual bool IsZero() const { return false; }
2429 virtual bool IsOne() const { return false; }
2430
David Brazdil77a48ae2015-09-15 12:34:04 +00002431 virtual uint64_t GetValueAsUint64() const = 0;
2432
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002433 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002434
2435 private:
2436 DISALLOW_COPY_AND_ASSIGN(HConstant);
2437};
2438
2439class HNullConstant : public HConstant {
2440 public:
2441 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2442 return true;
2443 }
2444
David Brazdil77a48ae2015-09-15 12:34:04 +00002445 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2446
Roland Levillain9867bc72015-08-05 10:21:34 +01002447 size_t ComputeHashCode() const OVERRIDE { return 0; }
2448
2449 DECLARE_INSTRUCTION(NullConstant);
2450
2451 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002452 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002453
2454 friend class HGraph;
2455 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2456};
2457
2458// Constants of the type int. Those can be from Dex instructions, or
2459// synthesized (for example with the if-eqz instruction).
2460class HIntConstant : public HConstant {
2461 public:
2462 int32_t GetValue() const { return value_; }
2463
David Brazdil9f389d42015-10-01 14:32:56 +01002464 uint64_t GetValueAsUint64() const OVERRIDE {
2465 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2466 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002467
Roland Levillain9867bc72015-08-05 10:21:34 +01002468 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002469 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002470 return other->AsIntConstant()->value_ == value_;
2471 }
2472
2473 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2474
2475 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2476 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2477 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2478
2479 DECLARE_INSTRUCTION(IntConstant);
2480
2481 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002482 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2483 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2484 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2485 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002486
2487 const int32_t value_;
2488
2489 friend class HGraph;
2490 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2491 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2492 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2493};
2494
2495class HLongConstant : public HConstant {
2496 public:
2497 int64_t GetValue() const { return value_; }
2498
David Brazdil77a48ae2015-09-15 12:34:04 +00002499 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2500
Roland Levillain9867bc72015-08-05 10:21:34 +01002501 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002502 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002503 return other->AsLongConstant()->value_ == value_;
2504 }
2505
2506 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2507
2508 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2509 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2510 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2511
2512 DECLARE_INSTRUCTION(LongConstant);
2513
2514 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002515 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2516 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002517
2518 const int64_t value_;
2519
2520 friend class HGraph;
2521 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2522};
Dave Allison20dfc792014-06-16 20:44:29 -07002523
Roland Levillain31dd3d62016-02-16 12:21:02 +00002524class HFloatConstant : public HConstant {
2525 public:
2526 float GetValue() const { return value_; }
2527
2528 uint64_t GetValueAsUint64() const OVERRIDE {
2529 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2530 }
2531
2532 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2533 DCHECK(other->IsFloatConstant()) << other->DebugName();
2534 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2535 }
2536
2537 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2538
2539 bool IsMinusOne() const OVERRIDE {
2540 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2541 }
2542 bool IsZero() const OVERRIDE {
2543 return value_ == 0.0f;
2544 }
2545 bool IsOne() const OVERRIDE {
2546 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2547 }
2548 bool IsNaN() const {
2549 return std::isnan(value_);
2550 }
2551
2552 DECLARE_INSTRUCTION(FloatConstant);
2553
2554 private:
2555 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2556 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2557 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2558 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2559
2560 const float value_;
2561
2562 // Only the SsaBuilder and HGraph can create floating-point constants.
2563 friend class SsaBuilder;
2564 friend class HGraph;
2565 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2566};
2567
2568class HDoubleConstant : public HConstant {
2569 public:
2570 double GetValue() const { return value_; }
2571
2572 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2573
2574 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2575 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2576 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2577 }
2578
2579 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2580
2581 bool IsMinusOne() const OVERRIDE {
2582 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2583 }
2584 bool IsZero() const OVERRIDE {
2585 return value_ == 0.0;
2586 }
2587 bool IsOne() const OVERRIDE {
2588 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2589 }
2590 bool IsNaN() const {
2591 return std::isnan(value_);
2592 }
2593
2594 DECLARE_INSTRUCTION(DoubleConstant);
2595
2596 private:
2597 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2598 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2599 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2600 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2601
2602 const double value_;
2603
2604 // Only the SsaBuilder and HGraph can create floating-point constants.
2605 friend class SsaBuilder;
2606 friend class HGraph;
2607 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2608};
2609
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002610// Conditional branch. A block ending with an HIf instruction must have
2611// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002612class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002613 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002614 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2615 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002616 SetRawInputAt(0, input);
2617 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002618
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002619 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002620
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002621 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002622 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002623 }
2624
2625 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002626 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002627 }
2628
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002629 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002630
2631 private:
2632 DISALLOW_COPY_AND_ASSIGN(HIf);
2633};
2634
David Brazdilfc6a86a2015-06-26 10:33:45 +00002635
2636// Abstract instruction which marks the beginning and/or end of a try block and
2637// links it to the respective exception handlers. Behaves the same as a Goto in
2638// non-exceptional control flow.
2639// Normal-flow successor is stored at index zero, exception handlers under
2640// higher indices in no particular order.
2641class HTryBoundary : public HTemplateInstruction<0> {
2642 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002643 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002644 kEntry,
2645 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002646 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002647 };
2648
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002649 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002650 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2651 SetPackedField<BoundaryKindField>(kind);
2652 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002653
2654 bool IsControlFlow() const OVERRIDE { return true; }
2655
2656 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002657 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002658
David Brazdild26a4112015-11-10 11:07:31 +00002659 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2660 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2661 }
2662
David Brazdilfc6a86a2015-06-26 10:33:45 +00002663 // Returns whether `handler` is among its exception handlers (non-zero index
2664 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002665 bool HasExceptionHandler(const HBasicBlock& handler) const {
2666 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002667 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002668 }
2669
2670 // If not present already, adds `handler` to its block's list of exception
2671 // handlers.
2672 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002673 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002674 GetBlock()->AddSuccessor(handler);
2675 }
2676 }
2677
Vladimir Markoa1de9182016-02-25 11:37:38 +00002678 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2679 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002680
David Brazdilffee3d32015-07-06 11:48:53 +01002681 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2682
David Brazdilfc6a86a2015-06-26 10:33:45 +00002683 DECLARE_INSTRUCTION(TryBoundary);
2684
2685 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002686 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2687 static constexpr size_t kFieldBoundaryKindSize =
2688 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2689 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2690 kFieldBoundaryKind + kFieldBoundaryKindSize;
2691 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2692 "Too many packed fields.");
2693 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002694
2695 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2696};
2697
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002698// Deoptimize to interpreter, upon checking a condition.
2699class HDeoptimize : public HTemplateInstruction<1> {
2700 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002701 // We set CanTriggerGC to prevent any intermediate address to be live
2702 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002703 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002704 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002705 SetRawInputAt(0, cond);
2706 }
2707
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002708 bool CanBeMoved() const OVERRIDE { return true; }
2709 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2710 return true;
2711 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002712 bool NeedsEnvironment() const OVERRIDE { return true; }
2713 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002714
2715 DECLARE_INSTRUCTION(Deoptimize);
2716
2717 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002718 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2719};
2720
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002721// Represents the ArtMethod that was passed as a first argument to
2722// the method. It is used by instructions that depend on it, like
2723// instructions that work with the dex cache.
2724class HCurrentMethod : public HExpression<0> {
2725 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2727 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002728
2729 DECLARE_INSTRUCTION(CurrentMethod);
2730
2731 private:
2732 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2733};
2734
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002735// Fetches an ArtMethod from the virtual table or the interface method table
2736// of a class.
2737class HClassTableGet : public HExpression<1> {
2738 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002739 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002740 kVTable,
2741 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002742 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002743 };
2744 HClassTableGet(HInstruction* cls,
2745 Primitive::Type type,
2746 TableKind kind,
2747 size_t index,
2748 uint32_t dex_pc)
2749 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002750 index_(index) {
2751 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002752 SetRawInputAt(0, cls);
2753 }
2754
2755 bool CanBeMoved() const OVERRIDE { return true; }
2756 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2757 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002758 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002759 }
2760
Vladimir Markoa1de9182016-02-25 11:37:38 +00002761 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002762 size_t GetIndex() const { return index_; }
2763
2764 DECLARE_INSTRUCTION(ClassTableGet);
2765
2766 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002767 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2768 static constexpr size_t kFieldTableKindSize =
2769 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2770 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2771 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2772 "Too many packed fields.");
2773 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2774
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002775 // The index of the ArtMethod in the table.
2776 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002777
2778 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2779};
2780
Mark Mendellfe57faa2015-09-18 09:26:15 -04002781// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2782// have one successor for each entry in the switch table, and the final successor
2783// will be the block containing the next Dex opcode.
2784class HPackedSwitch : public HTemplateInstruction<1> {
2785 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002786 HPackedSwitch(int32_t start_value,
2787 uint32_t num_entries,
2788 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002789 uint32_t dex_pc = kNoDexPc)
2790 : HTemplateInstruction(SideEffects::None(), dex_pc),
2791 start_value_(start_value),
2792 num_entries_(num_entries) {
2793 SetRawInputAt(0, input);
2794 }
2795
2796 bool IsControlFlow() const OVERRIDE { return true; }
2797
2798 int32_t GetStartValue() const { return start_value_; }
2799
Vladimir Marko211c2112015-09-24 16:52:33 +01002800 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002801
2802 HBasicBlock* GetDefaultBlock() const {
2803 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002804 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002805 }
2806 DECLARE_INSTRUCTION(PackedSwitch);
2807
2808 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002809 const int32_t start_value_;
2810 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002811
2812 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2813};
2814
Roland Levillain88cb1752014-10-20 16:36:47 +01002815class HUnaryOperation : public HExpression<1> {
2816 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002817 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2818 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002819 SetRawInputAt(0, input);
2820 }
2821
2822 HInstruction* GetInput() const { return InputAt(0); }
2823 Primitive::Type GetResultType() const { return GetType(); }
2824
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002825 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002826 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002827 return true;
2828 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002829
Roland Levillain31dd3d62016-02-16 12:21:02 +00002830 // Try to statically evaluate `this` and return a HConstant
2831 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002832 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002833 HConstant* TryStaticEvaluation() const;
2834
2835 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002836 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2837 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002838 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2839 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002840
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002841 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002842
2843 private:
2844 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2845};
2846
Dave Allison20dfc792014-06-16 20:44:29 -07002847class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002848 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002849 HBinaryOperation(Primitive::Type result_type,
2850 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002851 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002852 SideEffects side_effects = SideEffects::None(),
2853 uint32_t dex_pc = kNoDexPc)
2854 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002855 SetRawInputAt(0, left);
2856 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002857 }
2858
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002859 HInstruction* GetLeft() const { return InputAt(0); }
2860 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002861 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002862
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002863 virtual bool IsCommutative() const { return false; }
2864
2865 // Put constant on the right.
2866 // Returns whether order is changed.
2867 bool OrderInputsWithConstantOnTheRight() {
2868 HInstruction* left = InputAt(0);
2869 HInstruction* right = InputAt(1);
2870 if (left->IsConstant() && !right->IsConstant()) {
2871 ReplaceInput(right, 0);
2872 ReplaceInput(left, 1);
2873 return true;
2874 }
2875 return false;
2876 }
2877
2878 // Order inputs by instruction id, but favor constant on the right side.
2879 // This helps GVN for commutative ops.
2880 void OrderInputs() {
2881 DCHECK(IsCommutative());
2882 HInstruction* left = InputAt(0);
2883 HInstruction* right = InputAt(1);
2884 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2885 return;
2886 }
2887 if (OrderInputsWithConstantOnTheRight()) {
2888 return;
2889 }
2890 // Order according to instruction id.
2891 if (left->GetId() > right->GetId()) {
2892 ReplaceInput(right, 0);
2893 ReplaceInput(left, 1);
2894 }
2895 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002896
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002897 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002898 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002899 return true;
2900 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002901
Roland Levillain31dd3d62016-02-16 12:21:02 +00002902 // Try to statically evaluate `this` and return a HConstant
2903 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002904 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002905 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002906
2907 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002908 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2909 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002910 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2911 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002912 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2914 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002915 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2916 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002917 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2918 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002919 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002920 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2921 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002922
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002923 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002924 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002925 HConstant* GetConstantRight() const;
2926
2927 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002928 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002929 HInstruction* GetLeastConstantLeft() const;
2930
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002931 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002932
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002933 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002934 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2935};
2936
Mark Mendellc4701932015-04-10 13:18:51 -04002937// The comparison bias applies for floating point operations and indicates how NaN
2938// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002939enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002940 kNoBias, // bias is not applicable (i.e. for long operation)
2941 kGtBias, // return 1 for NaN comparisons
2942 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00002943 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04002944};
2945
Roland Levillain31dd3d62016-02-16 12:21:02 +00002946std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2947
Dave Allison20dfc792014-06-16 20:44:29 -07002948class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002949 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002950 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002951 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
2952 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
2953 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002954
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002955 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002956 // `instruction`, and disregard moves in between.
2957 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002958
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002959 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002960
2961 virtual IfCondition GetCondition() const = 0;
2962
Mark Mendellc4701932015-04-10 13:18:51 -04002963 virtual IfCondition GetOppositeCondition() const = 0;
2964
Vladimir Markoa1de9182016-02-25 11:37:38 +00002965 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
2966 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
2967 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04002968
2969 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002970 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04002971 }
2972
Roland Levillain4fa13f62015-07-06 18:11:54 +01002973 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002974 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002975 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002976 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002977 }
2978
2979 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002980 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002981 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002982 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002983 }
2984
Roland Levillain31dd3d62016-02-16 12:21:02 +00002985 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002986 // Needed if we merge a HCompare into a HCondition.
2987 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
2988 static constexpr size_t kFieldComparisonBiasSize =
2989 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
2990 static constexpr size_t kNumberOfConditionPackedBits =
2991 kFieldComparisonBias + kFieldComparisonBiasSize;
2992 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2993 using ComparisonBiasField =
2994 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
2995
Roland Levillain31dd3d62016-02-16 12:21:02 +00002996 template <typename T>
2997 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
2998
2999 template <typename T>
3000 int32_t CompareFP(T x, T y) const {
3001 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3002 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3003 // Handle the bias.
3004 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3005 }
3006
3007 // Return an integer constant containing the result of a condition evaluated at compile time.
3008 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3009 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3010 }
3011
Dave Allison20dfc792014-06-16 20:44:29 -07003012 private:
3013 DISALLOW_COPY_AND_ASSIGN(HCondition);
3014};
3015
3016// Instruction to check if two inputs are equal to each other.
3017class HEqual : public HCondition {
3018 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003019 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3020 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003021
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003022 bool IsCommutative() const OVERRIDE { return true; }
3023
Vladimir Marko9e23df52015-11-10 17:14:35 +00003024 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3025 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003026 return MakeConstantCondition(true, GetDexPc());
3027 }
3028 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3029 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3030 }
3031 // In the following Evaluate methods, a HCompare instruction has
3032 // been merged into this HEqual instruction; evaluate it as
3033 // `Compare(x, y) == 0`.
3034 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3035 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3036 GetDexPc());
3037 }
3038 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3039 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3040 }
3041 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3042 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003043 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003044
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003045 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003046
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003047 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003048 return kCondEQ;
3049 }
3050
Mark Mendellc4701932015-04-10 13:18:51 -04003051 IfCondition GetOppositeCondition() const OVERRIDE {
3052 return kCondNE;
3053 }
3054
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003055 private:
Aart Bike9f37602015-10-09 11:15:55 -07003056 template <typename T> bool Compute(T x, T y) const { return x == y; }
3057
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003058 DISALLOW_COPY_AND_ASSIGN(HEqual);
3059};
3060
Dave Allison20dfc792014-06-16 20:44:29 -07003061class HNotEqual : public HCondition {
3062 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003063 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3064 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003065
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003066 bool IsCommutative() const OVERRIDE { return true; }
3067
Vladimir Marko9e23df52015-11-10 17:14:35 +00003068 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3069 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003070 return MakeConstantCondition(false, GetDexPc());
3071 }
3072 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3073 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3074 }
3075 // In the following Evaluate methods, a HCompare instruction has
3076 // been merged into this HNotEqual instruction; evaluate it as
3077 // `Compare(x, y) != 0`.
3078 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3079 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3080 }
3081 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3082 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3083 }
3084 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3085 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003086 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003087
Dave Allison20dfc792014-06-16 20:44:29 -07003088 DECLARE_INSTRUCTION(NotEqual);
3089
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003090 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003091 return kCondNE;
3092 }
3093
Mark Mendellc4701932015-04-10 13:18:51 -04003094 IfCondition GetOppositeCondition() const OVERRIDE {
3095 return kCondEQ;
3096 }
3097
Dave Allison20dfc792014-06-16 20:44:29 -07003098 private:
Aart Bike9f37602015-10-09 11:15:55 -07003099 template <typename T> bool Compute(T x, T y) const { return x != y; }
3100
Dave Allison20dfc792014-06-16 20:44:29 -07003101 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3102};
3103
3104class HLessThan : public HCondition {
3105 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003106 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3107 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003108
Roland Levillain9867bc72015-08-05 10:21:34 +01003109 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003110 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003111 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003112 // In the following Evaluate methods, a HCompare instruction has
3113 // been merged into this HLessThan instruction; evaluate it as
3114 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003115 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003116 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3117 }
3118 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3119 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3120 }
3121 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3122 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003123 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003124
Dave Allison20dfc792014-06-16 20:44:29 -07003125 DECLARE_INSTRUCTION(LessThan);
3126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003127 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003128 return kCondLT;
3129 }
3130
Mark Mendellc4701932015-04-10 13:18:51 -04003131 IfCondition GetOppositeCondition() const OVERRIDE {
3132 return kCondGE;
3133 }
3134
Dave Allison20dfc792014-06-16 20:44:29 -07003135 private:
Aart Bike9f37602015-10-09 11:15:55 -07003136 template <typename T> bool Compute(T x, T y) const { return x < y; }
3137
Dave Allison20dfc792014-06-16 20:44:29 -07003138 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3139};
3140
3141class HLessThanOrEqual : public HCondition {
3142 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003143 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3144 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003145
Roland Levillain9867bc72015-08-05 10:21:34 +01003146 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003147 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003148 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003149 // In the following Evaluate methods, a HCompare instruction has
3150 // been merged into this HLessThanOrEqual instruction; evaluate it as
3151 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003152 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003153 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());
Roland Levillain93445682014-10-06 19:24:02 +01003160 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003161
Dave Allison20dfc792014-06-16 20:44:29 -07003162 DECLARE_INSTRUCTION(LessThanOrEqual);
3163
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003164 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003165 return kCondLE;
3166 }
3167
Mark Mendellc4701932015-04-10 13:18:51 -04003168 IfCondition GetOppositeCondition() const OVERRIDE {
3169 return kCondGT;
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(HLessThanOrEqual);
3176};
3177
3178class HGreaterThan : public HCondition {
3179 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003180 HGreaterThan(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 HGreaterThan 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(GreaterThan);
3200
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003201 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003202 return kCondGT;
3203 }
3204
Mark Mendellc4701932015-04-10 13:18:51 -04003205 IfCondition GetOppositeCondition() const OVERRIDE {
3206 return kCondLE;
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(HGreaterThan);
3213};
3214
3215class HGreaterThanOrEqual : public HCondition {
3216 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003217 HGreaterThanOrEqual(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 HGreaterThanOrEqual 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(GreaterThanOrEqual);
3237
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003238 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003239 return kCondGE;
3240 }
3241
Mark Mendellc4701932015-04-10 13:18:51 -04003242 IfCondition GetOppositeCondition() const OVERRIDE {
3243 return kCondLT;
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(HGreaterThanOrEqual);
3250};
3251
Aart Bike9f37602015-10-09 11:15:55 -07003252class HBelow : public HCondition {
3253 public:
3254 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3255 : HCondition(first, second, dex_pc) {}
3256
3257 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003258 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003259 }
3260 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003261 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3262 }
3263 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3264 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3265 LOG(FATAL) << DebugName() << " is not defined for float values";
3266 UNREACHABLE();
3267 }
3268 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3269 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3270 LOG(FATAL) << DebugName() << " is not defined for double values";
3271 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003272 }
3273
3274 DECLARE_INSTRUCTION(Below);
3275
3276 IfCondition GetCondition() const OVERRIDE {
3277 return kCondB;
3278 }
3279
3280 IfCondition GetOppositeCondition() const OVERRIDE {
3281 return kCondAE;
3282 }
3283
3284 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003285 template <typename T> bool Compute(T x, T y) const {
3286 return MakeUnsigned(x) < MakeUnsigned(y);
3287 }
Aart Bike9f37602015-10-09 11:15:55 -07003288
3289 DISALLOW_COPY_AND_ASSIGN(HBelow);
3290};
3291
3292class HBelowOrEqual : public HCondition {
3293 public:
3294 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3295 : HCondition(first, second, dex_pc) {}
3296
3297 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003298 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003299 }
3300 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003301 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3302 }
3303 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3304 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3305 LOG(FATAL) << DebugName() << " is not defined for float values";
3306 UNREACHABLE();
3307 }
3308 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3309 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3310 LOG(FATAL) << DebugName() << " is not defined for double values";
3311 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003312 }
3313
3314 DECLARE_INSTRUCTION(BelowOrEqual);
3315
3316 IfCondition GetCondition() const OVERRIDE {
3317 return kCondBE;
3318 }
3319
3320 IfCondition GetOppositeCondition() const OVERRIDE {
3321 return kCondA;
3322 }
3323
3324 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003325 template <typename T> bool Compute(T x, T y) const {
3326 return MakeUnsigned(x) <= MakeUnsigned(y);
3327 }
Aart Bike9f37602015-10-09 11:15:55 -07003328
3329 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3330};
3331
3332class HAbove : public HCondition {
3333 public:
3334 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3335 : HCondition(first, second, dex_pc) {}
3336
3337 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003338 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003339 }
3340 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003341 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3342 }
3343 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3344 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3345 LOG(FATAL) << DebugName() << " is not defined for float values";
3346 UNREACHABLE();
3347 }
3348 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3349 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3350 LOG(FATAL) << DebugName() << " is not defined for double values";
3351 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003352 }
3353
3354 DECLARE_INSTRUCTION(Above);
3355
3356 IfCondition GetCondition() const OVERRIDE {
3357 return kCondA;
3358 }
3359
3360 IfCondition GetOppositeCondition() const OVERRIDE {
3361 return kCondBE;
3362 }
3363
3364 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003365 template <typename T> bool Compute(T x, T y) const {
3366 return MakeUnsigned(x) > MakeUnsigned(y);
3367 }
Aart Bike9f37602015-10-09 11:15:55 -07003368
3369 DISALLOW_COPY_AND_ASSIGN(HAbove);
3370};
3371
3372class HAboveOrEqual : public HCondition {
3373 public:
3374 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3375 : HCondition(first, second, dex_pc) {}
3376
3377 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003378 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003379 }
3380 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003381 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3382 }
3383 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3384 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3385 LOG(FATAL) << DebugName() << " is not defined for float values";
3386 UNREACHABLE();
3387 }
3388 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3389 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3390 LOG(FATAL) << DebugName() << " is not defined for double values";
3391 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003392 }
3393
3394 DECLARE_INSTRUCTION(AboveOrEqual);
3395
3396 IfCondition GetCondition() const OVERRIDE {
3397 return kCondAE;
3398 }
3399
3400 IfCondition GetOppositeCondition() const OVERRIDE {
3401 return kCondB;
3402 }
3403
3404 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003405 template <typename T> bool Compute(T x, T y) const {
3406 return MakeUnsigned(x) >= MakeUnsigned(y);
3407 }
Aart Bike9f37602015-10-09 11:15:55 -07003408
3409 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3410};
Dave Allison20dfc792014-06-16 20:44:29 -07003411
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003412// Instruction to check how two inputs compare to each other.
3413// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3414class HCompare : public HBinaryOperation {
3415 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003416 HCompare(Primitive::Type type,
3417 HInstruction* first,
3418 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003419 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003420 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003421 : HBinaryOperation(Primitive::kPrimInt,
3422 first,
3423 second,
3424 SideEffectsForArchRuntimeCalls(type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003425 dex_pc) {
3426 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003427 DCHECK_EQ(type, first->GetType());
3428 DCHECK_EQ(type, second->GetType());
3429 }
3430
Roland Levillain9867bc72015-08-05 10:21:34 +01003431 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003432 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3433
3434 template <typename T>
3435 int32_t ComputeFP(T x, T y) const {
3436 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3437 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3438 // Handle the bias.
3439 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3440 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003441
Roland Levillain9867bc72015-08-05 10:21:34 +01003442 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003443 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3444 // reach this code path when processing a freshly built HIR
3445 // graph. However HCompare integer instructions can be synthesized
3446 // by the instruction simplifier to implement IntegerCompare and
3447 // IntegerSignum intrinsics, so we have to handle this case.
3448 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003449 }
3450 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003451 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3452 }
3453 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3454 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3455 }
3456 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3457 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003458 }
3459
Calin Juravleddb7df22014-11-25 20:56:51 +00003460 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003461 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003462 }
3463
Vladimir Markoa1de9182016-02-25 11:37:38 +00003464 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003465
Roland Levillain31dd3d62016-02-16 12:21:02 +00003466 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003467 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003468 bool IsGtBias() const {
3469 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003470 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003472
3473 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3474 // MIPS64 uses a runtime call for FP comparisons.
3475 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3476 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003477
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003478 DECLARE_INSTRUCTION(Compare);
3479
Roland Levillain31dd3d62016-02-16 12:21:02 +00003480 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003481 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3482 static constexpr size_t kFieldComparisonBiasSize =
3483 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3484 static constexpr size_t kNumberOfComparePackedBits =
3485 kFieldComparisonBias + kFieldComparisonBiasSize;
3486 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3487 using ComparisonBiasField =
3488 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3489
Roland Levillain31dd3d62016-02-16 12:21:02 +00003490 // Return an integer constant containing the result of a comparison evaluated at compile time.
3491 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3492 DCHECK(value == -1 || value == 0 || value == 1) << value;
3493 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3494 }
3495
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003496 private:
3497 DISALLOW_COPY_AND_ASSIGN(HCompare);
3498};
3499
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003500// A local in the graph. Corresponds to a Dex register.
3501class HLocal : public HTemplateInstruction<0> {
3502 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003503 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003504 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003505
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003506 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003507
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003508 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003509
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003510 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003511 // The Dex register number.
3512 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003513
3514 DISALLOW_COPY_AND_ASSIGN(HLocal);
3515};
3516
3517// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003518class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003519 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003520 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3521 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003522 SetRawInputAt(0, local);
3523 }
3524
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003525 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3526
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003527 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003528
3529 private:
3530 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3531};
3532
3533// Store a value in a given local. This instruction has two inputs: the value
3534// and the local.
3535class HStoreLocal : public HTemplateInstruction<2> {
3536 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003537 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3538 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003539 SetRawInputAt(0, local);
3540 SetRawInputAt(1, value);
3541 }
3542
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003543 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3544
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003545 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003546
3547 private:
3548 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3549};
3550
David Brazdil6de19382016-01-08 17:37:10 +00003551class HNewInstance : public HExpression<2> {
3552 public:
3553 HNewInstance(HInstruction* cls,
3554 HCurrentMethod* current_method,
3555 uint32_t dex_pc,
3556 uint16_t type_index,
3557 const DexFile& dex_file,
3558 bool can_throw,
3559 bool finalizable,
3560 QuickEntrypointEnum entrypoint)
3561 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3562 type_index_(type_index),
3563 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003564 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003565 SetPackedFlag<kFlagCanThrow>(can_throw);
3566 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003567 SetRawInputAt(0, cls);
3568 SetRawInputAt(1, current_method);
3569 }
3570
3571 uint16_t GetTypeIndex() const { return type_index_; }
3572 const DexFile& GetDexFile() const { return dex_file_; }
3573
3574 // Calls runtime so needs an environment.
3575 bool NeedsEnvironment() const OVERRIDE { return true; }
3576
3577 // It may throw when called on type that's not instantiable/accessible.
3578 // It can throw OOME.
3579 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003580 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003581
Vladimir Markoa1de9182016-02-25 11:37:38 +00003582 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003583
3584 bool CanBeNull() const OVERRIDE { return false; }
3585
3586 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3587
3588 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3589 entrypoint_ = entrypoint;
3590 }
3591
3592 bool IsStringAlloc() const;
3593
3594 DECLARE_INSTRUCTION(NewInstance);
3595
3596 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003597 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3598 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3599 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3600 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3601 "Too many packed fields.");
3602
David Brazdil6de19382016-01-08 17:37:10 +00003603 const uint16_t type_index_;
3604 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003605 QuickEntrypointEnum entrypoint_;
3606
3607 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3608};
3609
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003610enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003611#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3612 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003613#include "intrinsics_list.h"
3614 kNone,
3615 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3616#undef INTRINSICS_LIST
3617#undef OPTIMIZING_INTRINSICS
3618};
3619std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3620
Agi Csaki05f20562015-08-19 14:58:14 -07003621enum IntrinsicNeedsEnvironmentOrCache {
3622 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3623 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003624};
3625
Aart Bik5d75afe2015-12-14 11:57:01 -08003626enum IntrinsicSideEffects {
3627 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3628 kReadSideEffects, // Intrinsic may read heap memory.
3629 kWriteSideEffects, // Intrinsic may write heap memory.
3630 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3631};
3632
3633enum IntrinsicExceptions {
3634 kNoThrow, // Intrinsic does not throw any exceptions.
3635 kCanThrow // Intrinsic may throw exceptions.
3636};
3637
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003638class HInvoke : public HInstruction {
3639 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003640 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003641
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003642 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003643
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003644 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003645 SetRawInputAt(index, argument);
3646 }
3647
Roland Levillain3e3d7332015-04-28 11:00:54 +01003648 // Return the number of arguments. This number can be lower than
3649 // the number of inputs returned by InputCount(), as some invoke
3650 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3651 // inputs at the end of their list of inputs.
3652 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3653
Vladimir Markoa1de9182016-02-25 11:37:38 +00003654 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003655
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003656 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003657 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003658
Vladimir Markoa1de9182016-02-25 11:37:38 +00003659 InvokeType GetOriginalInvokeType() const {
3660 return GetPackedField<OriginalInvokeTypeField>();
3661 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003662
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003663 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003664 return intrinsic_;
3665 }
3666
Aart Bik5d75afe2015-12-14 11:57:01 -08003667 void SetIntrinsic(Intrinsics intrinsic,
3668 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3669 IntrinsicSideEffects side_effects,
3670 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003671
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003672 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003673 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003674 }
3675
Vladimir Markoa1de9182016-02-25 11:37:38 +00003676 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003677
3678 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3679
3680 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3681 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3682 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003683
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003684 uint32_t* GetIntrinsicOptimizations() {
3685 return &intrinsic_optimizations_;
3686 }
3687
3688 const uint32_t* GetIntrinsicOptimizations() const {
3689 return &intrinsic_optimizations_;
3690 }
3691
3692 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3693
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003694 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003695
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003696 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003697 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3698 static constexpr size_t kFieldOriginalInvokeTypeSize =
3699 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3700 static constexpr size_t kFieldReturnType =
3701 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3702 static constexpr size_t kFieldReturnTypeSize =
3703 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3704 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3705 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3706 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3707 using OriginalInvokeTypeField =
3708 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3709 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3710
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003711 HInvoke(ArenaAllocator* arena,
3712 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003713 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003714 Primitive::Type return_type,
3715 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003716 uint32_t dex_method_index,
3717 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003718 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003719 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003720 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003721 inputs_(number_of_arguments + number_of_other_inputs,
3722 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003723 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003724 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003725 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003726 SetPackedField<ReturnTypeField>(return_type);
3727 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3728 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003729 }
3730
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003731 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003732 return inputs_[index];
3733 }
3734
David Brazdil1abb4192015-02-17 18:33:36 +00003735 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003736 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003737 }
3738
Vladimir Markoa1de9182016-02-25 11:37:38 +00003739 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003740
Roland Levillain3e3d7332015-04-28 11:00:54 +01003741 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003742 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003743 const uint32_t dex_method_index_;
3744 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003745
3746 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3747 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003748
3749 private:
3750 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3751};
3752
Calin Juravle175dc732015-08-25 15:42:32 +01003753class HInvokeUnresolved : public HInvoke {
3754 public:
3755 HInvokeUnresolved(ArenaAllocator* arena,
3756 uint32_t number_of_arguments,
3757 Primitive::Type return_type,
3758 uint32_t dex_pc,
3759 uint32_t dex_method_index,
3760 InvokeType invoke_type)
3761 : HInvoke(arena,
3762 number_of_arguments,
3763 0u /* number_of_other_inputs */,
3764 return_type,
3765 dex_pc,
3766 dex_method_index,
3767 invoke_type) {
3768 }
3769
3770 DECLARE_INSTRUCTION(InvokeUnresolved);
3771
3772 private:
3773 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3774};
3775
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003776class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003777 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003778 // Requirements of this method call regarding the class
3779 // initialization (clinit) check of its declaring class.
3780 enum class ClinitCheckRequirement {
3781 kNone, // Class already initialized.
3782 kExplicit, // Static call having explicit clinit check as last input.
3783 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003784 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003785 };
3786
Vladimir Marko58155012015-08-19 12:49:41 +00003787 // Determines how to load the target ArtMethod*.
3788 enum class MethodLoadKind {
3789 // Use a String init ArtMethod* loaded from Thread entrypoints.
3790 kStringInit,
3791
3792 // Use the method's own ArtMethod* loaded by the register allocator.
3793 kRecursive,
3794
3795 // Use ArtMethod* at a known address, embed the direct address in the code.
3796 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3797 kDirectAddress,
3798
3799 // Use ArtMethod* at an address that will be known at link time, embed the direct
3800 // address in the code. If the image is relocatable, emit .patch_oat entry.
3801 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3802 // the image relocatable or not.
3803 kDirectAddressWithFixup,
3804
Vladimir Markoa1de9182016-02-25 11:37:38 +00003805 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003806 // Used when we need to use the dex cache, for example for invoke-static that
3807 // may cause class initialization (the entry may point to a resolution method),
3808 // and we know that we can access the dex cache arrays using a PC-relative load.
3809 kDexCachePcRelative,
3810
3811 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3812 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3813 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3814 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3815 kDexCacheViaMethod,
3816 };
3817
3818 // Determines the location of the code pointer.
3819 enum class CodePtrLocation {
3820 // Recursive call, use local PC-relative call instruction.
3821 kCallSelf,
3822
3823 // Use PC-relative call instruction patched at link time.
3824 // Used for calls within an oat file, boot->boot or app->app.
3825 kCallPCRelative,
3826
3827 // Call to a known target address, embed the direct address in code.
3828 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3829 kCallDirect,
3830
3831 // Call to a target address that will be known at link time, embed the direct
3832 // address in code. If the image is relocatable, emit .patch_oat entry.
3833 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3834 // the image relocatable or not.
3835 kCallDirectWithFixup,
3836
3837 // Use code pointer from the ArtMethod*.
3838 // Used when we don't know the target code. This is also the last-resort-kind used when
3839 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3840 kCallArtMethod,
3841 };
3842
3843 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003844 MethodLoadKind method_load_kind;
3845 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003846 // The method load data holds
3847 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3848 // Note that there are multiple string init methods, each having its own offset.
3849 // - the method address for kDirectAddress
3850 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003851 uint64_t method_load_data;
3852 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003853 };
3854
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003855 HInvokeStaticOrDirect(ArenaAllocator* arena,
3856 uint32_t number_of_arguments,
3857 Primitive::Type return_type,
3858 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003859 uint32_t method_index,
3860 MethodReference target_method,
3861 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003862 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003863 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003864 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003865 : HInvoke(arena,
3866 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003867 // There is potentially one extra argument for the HCurrentMethod node, and
3868 // potentially one other if the clinit check is explicit, and potentially
3869 // one other if the method is a string factory.
3870 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3871 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3872 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003873 return_type,
3874 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003875 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003876 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003877 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003878 dispatch_info_(dispatch_info) {
3879 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3880 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3881 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003882
Vladimir Markodc151b22015-10-15 18:02:30 +01003883 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003884 bool had_current_method_input = HasCurrentMethodInput();
3885 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3886
3887 // Using the current method is the default and once we find a better
3888 // method load kind, we should not go back to using the current method.
3889 DCHECK(had_current_method_input || !needs_current_method_input);
3890
3891 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003892 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3893 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003894 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003895 dispatch_info_ = dispatch_info;
3896 }
3897
Vladimir Markoc53c0792015-11-19 15:48:33 +00003898 void AddSpecialInput(HInstruction* input) {
3899 // We allow only one special input.
3900 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3901 DCHECK(InputCount() == GetSpecialInputIndex() ||
3902 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3903 InsertInputAt(GetSpecialInputIndex(), input);
3904 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003905
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003906 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003907 // We access the method via the dex cache so we can't do an implicit null check.
3908 // TODO: for intrinsics we can generate implicit null checks.
3909 return false;
3910 }
3911
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003912 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003913 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003914 }
3915
Vladimir Markoc53c0792015-11-19 15:48:33 +00003916 // Get the index of the special input, if any.
3917 //
David Brazdil6de19382016-01-08 17:37:10 +00003918 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3919 // method pointer; otherwise there may be one platform-specific special input,
3920 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003921 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003922 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003923
Vladimir Markoa1de9182016-02-25 11:37:38 +00003924 InvokeType GetOptimizedInvokeType() const {
3925 return GetPackedField<OptimizedInvokeTypeField>();
3926 }
3927
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003928 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003929 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003930 }
3931
Vladimir Marko58155012015-08-19 12:49:41 +00003932 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3933 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3934 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003935 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003936 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003937 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003938 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003939 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3940 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003941 bool HasCurrentMethodInput() const {
3942 // This function can be called only after the invoke has been fully initialized by the builder.
3943 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003944 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003945 return true;
3946 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003947 DCHECK(InputCount() == GetSpecialInputIndex() ||
3948 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003949 return false;
3950 }
3951 }
Vladimir Marko58155012015-08-19 12:49:41 +00003952 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3953 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003954 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003955
3956 int32_t GetStringInitOffset() const {
3957 DCHECK(IsStringInit());
3958 return dispatch_info_.method_load_data;
3959 }
3960
3961 uint64_t GetMethodAddress() const {
3962 DCHECK(HasMethodAddress());
3963 return dispatch_info_.method_load_data;
3964 }
3965
3966 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003967 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003968 return dispatch_info_.method_load_data;
3969 }
3970
3971 uint64_t GetDirectCodePtr() const {
3972 DCHECK(HasDirectCodePtr());
3973 return dispatch_info_.direct_code_ptr;
3974 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003975
Vladimir Markoa1de9182016-02-25 11:37:38 +00003976 ClinitCheckRequirement GetClinitCheckRequirement() const {
3977 return GetPackedField<ClinitCheckRequirementField>();
3978 }
Calin Juravle68ad6492015-08-18 17:08:12 +01003979
Roland Levillain4c0eb422015-04-24 16:43:49 +01003980 // Is this instruction a call to a static method?
3981 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003982 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003983 }
3984
Vladimir Markofbb184a2015-11-13 14:47:00 +00003985 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3986 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3987 // instruction; only relevant for static calls with explicit clinit check.
3988 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003989 DCHECK(IsStaticWithExplicitClinitCheck());
3990 size_t last_input_index = InputCount() - 1;
3991 HInstruction* last_input = InputAt(last_input_index);
3992 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003993 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003994 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003995 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003996 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003997 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003998 }
3999
David Brazdilbc9ab162016-01-20 14:50:19 +00004000 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00004001 DCHECK(IsStringInit());
4002 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00004003 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00004004 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004005 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00004006 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004007 }
4008
Roland Levillain4c0eb422015-04-24 16:43:49 +01004009 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004010 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004011 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004012 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004013 }
4014
4015 // Is this a call to a static method whose declaring class has an
4016 // implicit intialization check requirement?
4017 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004018 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004019 }
4020
Vladimir Markob554b5a2015-11-06 12:57:55 +00004021 // Does this method load kind need the current method as an input?
4022 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4023 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4024 }
4025
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004026 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004027
Roland Levillain4c0eb422015-04-24 16:43:49 +01004028 protected:
4029 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4030 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4031 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4032 HInstruction* input = input_record.GetInstruction();
4033 // `input` is the last input of a static invoke marked as having
4034 // an explicit clinit check. It must either be:
4035 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4036 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4037 DCHECK(input != nullptr);
4038 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4039 }
4040 return input_record;
4041 }
4042
Vladimir Markoc53c0792015-11-19 15:48:33 +00004043 void InsertInputAt(size_t index, HInstruction* input);
4044 void RemoveInputAt(size_t index);
4045
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004046 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004047 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4048 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4049 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4050 static constexpr size_t kFieldClinitCheckRequirement =
4051 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4052 static constexpr size_t kFieldClinitCheckRequirementSize =
4053 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4054 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4055 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4056 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4057 "Too many packed fields.");
4058 using OptimizedInvokeTypeField =
4059 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4060 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4061 kFieldClinitCheckRequirement,
4062 kFieldClinitCheckRequirementSize>;
4063
Vladimir Marko58155012015-08-19 12:49:41 +00004064 // The target method may refer to different dex file or method index than the original
4065 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4066 // in derived class to increase visibility.
4067 MethodReference target_method_;
4068 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004069
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004070 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004071};
Vladimir Markof64242a2015-12-01 14:58:23 +00004072std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004073std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004074
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004075class HInvokeVirtual : public HInvoke {
4076 public:
4077 HInvokeVirtual(ArenaAllocator* arena,
4078 uint32_t number_of_arguments,
4079 Primitive::Type return_type,
4080 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004081 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004082 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004083 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004084 vtable_index_(vtable_index) {}
4085
Calin Juravle641547a2015-04-21 22:08:51 +01004086 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004087 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004088 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004089 }
4090
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004091 uint32_t GetVTableIndex() const { return vtable_index_; }
4092
4093 DECLARE_INSTRUCTION(InvokeVirtual);
4094
4095 private:
4096 const uint32_t vtable_index_;
4097
4098 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4099};
4100
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004101class HInvokeInterface : public HInvoke {
4102 public:
4103 HInvokeInterface(ArenaAllocator* arena,
4104 uint32_t number_of_arguments,
4105 Primitive::Type return_type,
4106 uint32_t dex_pc,
4107 uint32_t dex_method_index,
4108 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004109 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004110 imt_index_(imt_index) {}
4111
Calin Juravle641547a2015-04-21 22:08:51 +01004112 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004113 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004114 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004115 }
4116
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004117 uint32_t GetImtIndex() const { return imt_index_; }
4118 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4119
4120 DECLARE_INSTRUCTION(InvokeInterface);
4121
4122 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004123 const uint32_t imt_index_;
4124
4125 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4126};
4127
Roland Levillain88cb1752014-10-20 16:36:47 +01004128class HNeg : public HUnaryOperation {
4129 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004130 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4131 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004132
Roland Levillain9867bc72015-08-05 10:21:34 +01004133 template <typename T> T Compute(T x) const { return -x; }
4134
4135 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004136 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004137 }
4138 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004139 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004140 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004141 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4142 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4143 }
4144 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4145 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4146 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004147
Roland Levillain88cb1752014-10-20 16:36:47 +01004148 DECLARE_INSTRUCTION(Neg);
4149
4150 private:
4151 DISALLOW_COPY_AND_ASSIGN(HNeg);
4152};
4153
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004154class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004155 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004156 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004157 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004158 uint32_t dex_pc,
4159 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004160 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004161 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004162 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004163 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004164 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004165 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004166 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004167 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004168 }
4169
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004170 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004171 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004172
4173 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004174 bool NeedsEnvironment() const OVERRIDE { return true; }
4175
Mingyao Yang0c365e62015-03-31 15:09:29 -07004176 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4177 bool CanThrow() const OVERRIDE { return true; }
4178
Calin Juravle10e244f2015-01-26 18:54:32 +00004179 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004180
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004181 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4182
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004183 DECLARE_INSTRUCTION(NewArray);
4184
4185 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004186 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004187 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004188 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004189
4190 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4191};
4192
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004193class HAdd : public HBinaryOperation {
4194 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 HAdd(Primitive::Type result_type,
4196 HInstruction* left,
4197 HInstruction* right,
4198 uint32_t dex_pc = kNoDexPc)
4199 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004200
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004201 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004202
Roland Levillain9867bc72015-08-05 10:21:34 +01004203 template <typename T> T Compute(T x, T y) const { return x + y; }
4204
4205 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004206 return GetBlock()->GetGraph()->GetIntConstant(
4207 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004208 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004209 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 return GetBlock()->GetGraph()->GetLongConstant(
4211 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004212 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004213 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4214 return GetBlock()->GetGraph()->GetFloatConstant(
4215 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4216 }
4217 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4218 return GetBlock()->GetGraph()->GetDoubleConstant(
4219 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4220 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004221
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004222 DECLARE_INSTRUCTION(Add);
4223
4224 private:
4225 DISALLOW_COPY_AND_ASSIGN(HAdd);
4226};
4227
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004228class HSub : public HBinaryOperation {
4229 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 HSub(Primitive::Type result_type,
4231 HInstruction* left,
4232 HInstruction* right,
4233 uint32_t dex_pc = kNoDexPc)
4234 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004235
Roland Levillain9867bc72015-08-05 10:21:34 +01004236 template <typename T> T Compute(T x, T y) const { return x - y; }
4237
4238 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004239 return GetBlock()->GetGraph()->GetIntConstant(
4240 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004241 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004242 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 return GetBlock()->GetGraph()->GetLongConstant(
4244 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004245 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004246 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4247 return GetBlock()->GetGraph()->GetFloatConstant(
4248 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4249 }
4250 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4251 return GetBlock()->GetGraph()->GetDoubleConstant(
4252 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4253 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004254
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004255 DECLARE_INSTRUCTION(Sub);
4256
4257 private:
4258 DISALLOW_COPY_AND_ASSIGN(HSub);
4259};
4260
Calin Juravle34bacdf2014-10-07 20:23:36 +01004261class HMul : public HBinaryOperation {
4262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004263 HMul(Primitive::Type result_type,
4264 HInstruction* left,
4265 HInstruction* right,
4266 uint32_t dex_pc = kNoDexPc)
4267 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004268
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004269 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004270
Roland Levillain9867bc72015-08-05 10:21:34 +01004271 template <typename T> T Compute(T x, T y) const { return x * y; }
4272
4273 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004274 return GetBlock()->GetGraph()->GetIntConstant(
4275 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004276 }
4277 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004278 return GetBlock()->GetGraph()->GetLongConstant(
4279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004280 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004281 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4282 return GetBlock()->GetGraph()->GetFloatConstant(
4283 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4284 }
4285 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4286 return GetBlock()->GetGraph()->GetDoubleConstant(
4287 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4288 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004289
4290 DECLARE_INSTRUCTION(Mul);
4291
4292 private:
4293 DISALLOW_COPY_AND_ASSIGN(HMul);
4294};
4295
Calin Juravle7c4954d2014-10-28 16:57:40 +00004296class HDiv : public HBinaryOperation {
4297 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004298 HDiv(Primitive::Type result_type,
4299 HInstruction* left,
4300 HInstruction* right,
4301 uint32_t dex_pc)
4302 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004303
Roland Levillain9867bc72015-08-05 10:21:34 +01004304 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004305 T ComputeIntegral(T x, T y) const {
4306 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004307 // Our graph structure ensures we never have 0 for `y` during
4308 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004309 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004310 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004311 return (y == -1) ? -x : x / y;
4312 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004313
Roland Levillain31dd3d62016-02-16 12:21:02 +00004314 template <typename T>
4315 T ComputeFP(T x, T y) const {
4316 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4317 return x / y;
4318 }
4319
Roland Levillain9867bc72015-08-05 10:21:34 +01004320 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004321 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004322 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004323 }
4324 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004325 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004326 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4327 }
4328 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4329 return GetBlock()->GetGraph()->GetFloatConstant(
4330 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4331 }
4332 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4333 return GetBlock()->GetGraph()->GetDoubleConstant(
4334 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004335 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004336
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004337 static SideEffects SideEffectsForArchRuntimeCalls() {
4338 // The generated code can use a runtime call.
4339 return SideEffects::CanTriggerGC();
4340 }
4341
Calin Juravle7c4954d2014-10-28 16:57:40 +00004342 DECLARE_INSTRUCTION(Div);
4343
4344 private:
4345 DISALLOW_COPY_AND_ASSIGN(HDiv);
4346};
4347
Calin Juravlebacfec32014-11-14 15:54:36 +00004348class HRem : public HBinaryOperation {
4349 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004350 HRem(Primitive::Type result_type,
4351 HInstruction* left,
4352 HInstruction* right,
4353 uint32_t dex_pc)
4354 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004355
Roland Levillain9867bc72015-08-05 10:21:34 +01004356 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004357 T ComputeIntegral(T x, T y) const {
4358 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004359 // Our graph structure ensures we never have 0 for `y` during
4360 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004361 DCHECK_NE(y, 0);
4362 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4363 return (y == -1) ? 0 : x % y;
4364 }
4365
Roland Levillain31dd3d62016-02-16 12:21:02 +00004366 template <typename T>
4367 T ComputeFP(T x, T y) const {
4368 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4369 return std::fmod(x, y);
4370 }
4371
Roland Levillain9867bc72015-08-05 10:21:34 +01004372 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004373 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004374 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004375 }
4376 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004377 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004378 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004379 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004380 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4381 return GetBlock()->GetGraph()->GetFloatConstant(
4382 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4383 }
4384 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4385 return GetBlock()->GetGraph()->GetDoubleConstant(
4386 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4387 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004388
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004389 static SideEffects SideEffectsForArchRuntimeCalls() {
4390 return SideEffects::CanTriggerGC();
4391 }
4392
Calin Juravlebacfec32014-11-14 15:54:36 +00004393 DECLARE_INSTRUCTION(Rem);
4394
4395 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004396 DISALLOW_COPY_AND_ASSIGN(HRem);
4397};
4398
Calin Juravled0d48522014-11-04 16:40:20 +00004399class HDivZeroCheck : public HExpression<1> {
4400 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004401 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4402 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004403 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004404 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004405 SetRawInputAt(0, value);
4406 }
4407
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004408 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4409
Calin Juravled0d48522014-11-04 16:40:20 +00004410 bool CanBeMoved() const OVERRIDE { return true; }
4411
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004412 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004413 return true;
4414 }
4415
4416 bool NeedsEnvironment() const OVERRIDE { return true; }
4417 bool CanThrow() const OVERRIDE { return true; }
4418
Calin Juravled0d48522014-11-04 16:40:20 +00004419 DECLARE_INSTRUCTION(DivZeroCheck);
4420
4421 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004422 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4423};
4424
Calin Juravle9aec02f2014-11-18 23:06:35 +00004425class HShl : public HBinaryOperation {
4426 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004427 HShl(Primitive::Type result_type,
4428 HInstruction* left,
4429 HInstruction* right,
4430 uint32_t dex_pc = kNoDexPc)
4431 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004432
Roland Levillain9867bc72015-08-05 10:21:34 +01004433 template <typename T, typename U, typename V>
4434 T Compute(T x, U y, V max_shift_value) const {
4435 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4436 "V is not the unsigned integer type corresponding to T");
4437 return x << (y & max_shift_value);
4438 }
4439
4440 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4441 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004442 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004443 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004444 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4445 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004446 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004447 }
4448 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4449 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004450 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004451 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004452 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4453 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4454 LOG(FATAL) << DebugName() << " is not defined for float values";
4455 UNREACHABLE();
4456 }
4457 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4458 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4459 LOG(FATAL) << DebugName() << " is not defined for double values";
4460 UNREACHABLE();
4461 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004462
4463 DECLARE_INSTRUCTION(Shl);
4464
4465 private:
4466 DISALLOW_COPY_AND_ASSIGN(HShl);
4467};
4468
4469class HShr : public HBinaryOperation {
4470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004471 HShr(Primitive::Type result_type,
4472 HInstruction* left,
4473 HInstruction* right,
4474 uint32_t dex_pc = kNoDexPc)
4475 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004476
Roland Levillain9867bc72015-08-05 10:21:34 +01004477 template <typename T, typename U, typename V>
4478 T Compute(T x, U y, V max_shift_value) const {
4479 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4480 "V is not the unsigned integer type corresponding to T");
4481 return x >> (y & max_shift_value);
4482 }
4483
4484 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4485 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004486 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004487 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004488 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4489 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004490 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004491 }
4492 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4493 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004495 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004496 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4497 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4498 LOG(FATAL) << DebugName() << " is not defined for float values";
4499 UNREACHABLE();
4500 }
4501 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4502 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4503 LOG(FATAL) << DebugName() << " is not defined for double values";
4504 UNREACHABLE();
4505 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004506
4507 DECLARE_INSTRUCTION(Shr);
4508
4509 private:
4510 DISALLOW_COPY_AND_ASSIGN(HShr);
4511};
4512
4513class HUShr : public HBinaryOperation {
4514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004515 HUShr(Primitive::Type result_type,
4516 HInstruction* left,
4517 HInstruction* right,
4518 uint32_t dex_pc = kNoDexPc)
4519 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004520
Roland Levillain9867bc72015-08-05 10:21:34 +01004521 template <typename T, typename U, typename V>
4522 T Compute(T x, U y, V max_shift_value) const {
4523 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4524 "V is not the unsigned integer type corresponding to T");
4525 V ux = static_cast<V>(x);
4526 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004527 }
4528
Roland Levillain9867bc72015-08-05 10:21:34 +01004529 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4530 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004531 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004532 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004533 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4534 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004535 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004536 }
4537 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4538 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004539 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004540 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004541 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4542 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4543 LOG(FATAL) << DebugName() << " is not defined for float values";
4544 UNREACHABLE();
4545 }
4546 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4547 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4548 LOG(FATAL) << DebugName() << " is not defined for double values";
4549 UNREACHABLE();
4550 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004551
4552 DECLARE_INSTRUCTION(UShr);
4553
4554 private:
4555 DISALLOW_COPY_AND_ASSIGN(HUShr);
4556};
4557
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004558class HAnd : public HBinaryOperation {
4559 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004560 HAnd(Primitive::Type result_type,
4561 HInstruction* left,
4562 HInstruction* right,
4563 uint32_t dex_pc = kNoDexPc)
4564 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004565
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004566 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004567
Roland Levillaine53bd812016-02-24 14:54:18 +00004568 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004569
4570 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004571 return GetBlock()->GetGraph()->GetIntConstant(
4572 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004573 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004574 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004575 return GetBlock()->GetGraph()->GetLongConstant(
4576 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004577 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004578 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4579 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4580 LOG(FATAL) << DebugName() << " is not defined for float values";
4581 UNREACHABLE();
4582 }
4583 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4584 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4585 LOG(FATAL) << DebugName() << " is not defined for double values";
4586 UNREACHABLE();
4587 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004588
4589 DECLARE_INSTRUCTION(And);
4590
4591 private:
4592 DISALLOW_COPY_AND_ASSIGN(HAnd);
4593};
4594
4595class HOr : public HBinaryOperation {
4596 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004597 HOr(Primitive::Type result_type,
4598 HInstruction* left,
4599 HInstruction* right,
4600 uint32_t dex_pc = kNoDexPc)
4601 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004602
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004603 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004604
Roland Levillaine53bd812016-02-24 14:54:18 +00004605 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004606
4607 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004608 return GetBlock()->GetGraph()->GetIntConstant(
4609 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004610 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004611 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004612 return GetBlock()->GetGraph()->GetLongConstant(
4613 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004614 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004615 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4616 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4617 LOG(FATAL) << DebugName() << " is not defined for float values";
4618 UNREACHABLE();
4619 }
4620 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4621 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4622 LOG(FATAL) << DebugName() << " is not defined for double values";
4623 UNREACHABLE();
4624 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004625
4626 DECLARE_INSTRUCTION(Or);
4627
4628 private:
4629 DISALLOW_COPY_AND_ASSIGN(HOr);
4630};
4631
4632class HXor : public HBinaryOperation {
4633 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004634 HXor(Primitive::Type result_type,
4635 HInstruction* left,
4636 HInstruction* right,
4637 uint32_t dex_pc = kNoDexPc)
4638 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004639
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004640 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004641
Roland Levillaine53bd812016-02-24 14:54:18 +00004642 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004643
4644 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004645 return GetBlock()->GetGraph()->GetIntConstant(
4646 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004647 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004648 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004649 return GetBlock()->GetGraph()->GetLongConstant(
4650 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004651 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004652 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4653 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4654 LOG(FATAL) << DebugName() << " is not defined for float values";
4655 UNREACHABLE();
4656 }
4657 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4658 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4659 LOG(FATAL) << DebugName() << " is not defined for double values";
4660 UNREACHABLE();
4661 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004662
4663 DECLARE_INSTRUCTION(Xor);
4664
4665 private:
4666 DISALLOW_COPY_AND_ASSIGN(HXor);
4667};
4668
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004669class HRor : public HBinaryOperation {
4670 public:
4671 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4672 : HBinaryOperation(result_type, value, distance) {}
4673
4674 template <typename T, typename U, typename V>
4675 T Compute(T x, U y, V max_shift_value) const {
4676 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4677 "V is not the unsigned integer type corresponding to T");
4678 V ux = static_cast<V>(x);
4679 if ((y & max_shift_value) == 0) {
4680 return static_cast<T>(ux);
4681 } else {
4682 const V reg_bits = sizeof(T) * 8;
4683 return static_cast<T>(ux >> (y & max_shift_value)) |
4684 (x << (reg_bits - (y & max_shift_value)));
4685 }
4686 }
4687
4688 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4689 return GetBlock()->GetGraph()->GetIntConstant(
4690 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4691 }
4692 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4693 return GetBlock()->GetGraph()->GetLongConstant(
4694 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4695 }
4696 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4697 return GetBlock()->GetGraph()->GetLongConstant(
4698 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4699 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004700 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4701 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4702 LOG(FATAL) << DebugName() << " is not defined for float values";
4703 UNREACHABLE();
4704 }
4705 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4706 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4707 LOG(FATAL) << DebugName() << " is not defined for double values";
4708 UNREACHABLE();
4709 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004710
4711 DECLARE_INSTRUCTION(Ror);
4712
4713 private:
4714 DISALLOW_COPY_AND_ASSIGN(HRor);
4715};
4716
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004717// The value of a parameter in this method. Its location depends on
4718// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004719class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004720 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004721 HParameterValue(const DexFile& dex_file,
4722 uint16_t type_index,
4723 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004724 Primitive::Type parameter_type,
4725 bool is_this = false)
4726 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004727 dex_file_(dex_file),
4728 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004729 index_(index) {
4730 SetPackedFlag<kFlagIsThis>(is_this);
4731 SetPackedFlag<kFlagCanBeNull>(!is_this);
4732 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004733
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004734 const DexFile& GetDexFile() const { return dex_file_; }
4735 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004736 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004737 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004738
Vladimir Markoa1de9182016-02-25 11:37:38 +00004739 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4740 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004741
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004742 DECLARE_INSTRUCTION(ParameterValue);
4743
4744 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004745 // Whether or not the parameter value corresponds to 'this' argument.
4746 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4747 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4748 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4749 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4750 "Too many packed fields.");
4751
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004752 const DexFile& dex_file_;
4753 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004754 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004755 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004756 const uint8_t index_;
4757
4758 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4759};
4760
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004761class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004762 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004763 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4764 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004765
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004766 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004767 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004768 return true;
4769 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004770
Roland Levillain9867bc72015-08-05 10:21:34 +01004771 template <typename T> T Compute(T x) const { return ~x; }
4772
4773 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004774 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004775 }
4776 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004777 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004778 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004779 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4780 LOG(FATAL) << DebugName() << " is not defined for float values";
4781 UNREACHABLE();
4782 }
4783 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4784 LOG(FATAL) << DebugName() << " is not defined for double values";
4785 UNREACHABLE();
4786 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004787
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004788 DECLARE_INSTRUCTION(Not);
4789
4790 private:
4791 DISALLOW_COPY_AND_ASSIGN(HNot);
4792};
4793
David Brazdil66d126e2015-04-03 16:02:44 +01004794class HBooleanNot : public HUnaryOperation {
4795 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004796 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4797 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004798
4799 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004800 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004801 return true;
4802 }
4803
Roland Levillain9867bc72015-08-05 10:21:34 +01004804 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004805 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004806 return !x;
4807 }
4808
Roland Levillain9867bc72015-08-05 10:21:34 +01004809 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004810 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004811 }
4812 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4813 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004814 UNREACHABLE();
4815 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004816 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4817 LOG(FATAL) << DebugName() << " is not defined for float values";
4818 UNREACHABLE();
4819 }
4820 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4821 LOG(FATAL) << DebugName() << " is not defined for double values";
4822 UNREACHABLE();
4823 }
David Brazdil66d126e2015-04-03 16:02:44 +01004824
4825 DECLARE_INSTRUCTION(BooleanNot);
4826
4827 private:
4828 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4829};
4830
Roland Levillaindff1f282014-11-05 14:15:05 +00004831class HTypeConversion : public HExpression<1> {
4832 public:
4833 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004834 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 : HExpression(result_type,
4836 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4837 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004838 SetRawInputAt(0, input);
4839 DCHECK_NE(input->GetType(), result_type);
4840 }
4841
4842 HInstruction* GetInput() const { return InputAt(0); }
4843 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4844 Primitive::Type GetResultType() const { return GetType(); }
4845
4846 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004847 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004848
Mark Mendelle82549b2015-05-06 10:55:34 -04004849 // Try to statically evaluate the conversion and return a HConstant
4850 // containing the result. If the input cannot be converted, return nullptr.
4851 HConstant* TryStaticEvaluation() const;
4852
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004853 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4854 Primitive::Type result_type) {
4855 // Some architectures may not require the 'GC' side effects, but at this point
4856 // in the compilation process we do not know what architecture we will
4857 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004858 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4859 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004860 return SideEffects::CanTriggerGC();
4861 }
4862 return SideEffects::None();
4863 }
4864
Roland Levillaindff1f282014-11-05 14:15:05 +00004865 DECLARE_INSTRUCTION(TypeConversion);
4866
4867 private:
4868 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4869};
4870
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004871static constexpr uint32_t kNoRegNumber = -1;
4872
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004873class HPhi : public HInstruction {
4874 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004875 HPhi(ArenaAllocator* arena,
4876 uint32_t reg_number,
4877 size_t number_of_inputs,
4878 Primitive::Type type,
4879 uint32_t dex_pc = kNoDexPc)
4880 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004881 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004882 reg_number_(reg_number) {
4883 SetPackedField<TypeField>(ToPhiType(type));
4884 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4885 // Phis are constructed live and marked dead if conflicting or unused.
4886 // Individual steps of SsaBuilder should assume that if a phi has been
4887 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4888 SetPackedFlag<kFlagIsLive>(true);
4889 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004890 }
4891
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004892 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4893 static Primitive::Type ToPhiType(Primitive::Type type) {
4894 switch (type) {
4895 case Primitive::kPrimBoolean:
4896 case Primitive::kPrimByte:
4897 case Primitive::kPrimShort:
4898 case Primitive::kPrimChar:
4899 return Primitive::kPrimInt;
4900 default:
4901 return type;
4902 }
4903 }
4904
David Brazdilffee3d32015-07-06 11:48:53 +01004905 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4906
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004907 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004908
4909 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004910 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004911
Vladimir Markoa1de9182016-02-25 11:37:38 +00004912 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004913 void SetType(Primitive::Type new_type) {
4914 // Make sure that only valid type changes occur. The following are allowed:
4915 // (1) int -> float/ref (primitive type propagation),
4916 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004917 DCHECK(GetType() == new_type ||
4918 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4919 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4920 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4921 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004922 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004923
Vladimir Markoa1de9182016-02-25 11:37:38 +00004924 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4925 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004926
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004927 uint32_t GetRegNumber() const { return reg_number_; }
4928
Vladimir Markoa1de9182016-02-25 11:37:38 +00004929 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4930 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4931 bool IsDead() const { return !IsLive(); }
4932 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004933
David Brazdil77a48ae2015-09-15 12:34:04 +00004934 bool IsVRegEquivalentOf(HInstruction* other) const {
4935 return other != nullptr
4936 && other->IsPhi()
4937 && other->AsPhi()->GetBlock() == GetBlock()
4938 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4939 }
4940
Calin Juravlea4f88312015-04-16 12:57:19 +01004941 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4942 // An equivalent phi is a phi having the same dex register and type.
4943 // It assumes that phis with the same dex register are adjacent.
4944 HPhi* GetNextEquivalentPhiWithSameType() {
4945 HInstruction* next = GetNext();
4946 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4947 if (next->GetType() == GetType()) {
4948 return next->AsPhi();
4949 }
4950 next = next->GetNext();
4951 }
4952 return nullptr;
4953 }
4954
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004955 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004956
David Brazdil1abb4192015-02-17 18:33:36 +00004957 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004958 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004959 return inputs_[index];
4960 }
David Brazdil1abb4192015-02-17 18:33:36 +00004961
4962 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004963 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004964 }
4965
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004966 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004967 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
4968 static constexpr size_t kFieldTypeSize =
4969 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4970 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
4971 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
4972 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
4973 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4974 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
4975
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004976 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004977 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004978
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004979 DISALLOW_COPY_AND_ASSIGN(HPhi);
4980};
4981
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004982class HNullCheck : public HExpression<1> {
4983 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004984 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4985 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004986 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004987 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988 SetRawInputAt(0, value);
4989 }
4990
Calin Juravle10e244f2015-01-26 18:54:32 +00004991 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004992 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004993 return true;
4994 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004995
Calin Juravle10e244f2015-01-26 18:54:32 +00004996 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004997
Calin Juravle10e244f2015-01-26 18:54:32 +00004998 bool CanThrow() const OVERRIDE { return true; }
4999
5000 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005001
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005002
5003 DECLARE_INSTRUCTION(NullCheck);
5004
5005 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005006 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5007};
5008
5009class FieldInfo : public ValueObject {
5010 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005011 FieldInfo(MemberOffset field_offset,
5012 Primitive::Type field_type,
5013 bool is_volatile,
5014 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005015 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005016 const DexFile& dex_file,
5017 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005018 : field_offset_(field_offset),
5019 field_type_(field_type),
5020 is_volatile_(is_volatile),
5021 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005022 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005023 dex_file_(dex_file),
5024 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005025
5026 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005027 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005028 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005029 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005030 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005031 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005032 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005033
5034 private:
5035 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005036 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005037 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005038 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005039 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005040 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005041 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005042};
5043
5044class HInstanceFieldGet : public HExpression<1> {
5045 public:
5046 HInstanceFieldGet(HInstruction* value,
5047 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005048 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005049 bool is_volatile,
5050 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005051 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005052 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005053 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005054 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005055 : HExpression(field_type,
5056 SideEffects::FieldReadOfType(field_type, is_volatile),
5057 dex_pc),
5058 field_info_(field_offset,
5059 field_type,
5060 is_volatile,
5061 field_idx,
5062 declaring_class_def_index,
5063 dex_file,
5064 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005065 SetRawInputAt(0, value);
5066 }
5067
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005068 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005069
5070 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5071 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5072 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005073 }
5074
Calin Juravle641547a2015-04-21 22:08:51 +01005075 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5076 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005077 }
5078
5079 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005080 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5081 }
5082
Calin Juravle52c48962014-12-16 17:02:57 +00005083 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005084 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005085 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005086 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005087
5088 DECLARE_INSTRUCTION(InstanceFieldGet);
5089
5090 private:
5091 const FieldInfo field_info_;
5092
5093 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5094};
5095
5096class HInstanceFieldSet : public HTemplateInstruction<2> {
5097 public:
5098 HInstanceFieldSet(HInstruction* object,
5099 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005100 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005101 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005102 bool is_volatile,
5103 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005104 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005105 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005106 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005107 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005108 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5109 dex_pc),
5110 field_info_(field_offset,
5111 field_type,
5112 is_volatile,
5113 field_idx,
5114 declaring_class_def_index,
5115 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005116 dex_cache) {
5117 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005118 SetRawInputAt(0, object);
5119 SetRawInputAt(1, value);
5120 }
5121
Calin Juravle641547a2015-04-21 22:08:51 +01005122 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5123 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005124 }
5125
Calin Juravle52c48962014-12-16 17:02:57 +00005126 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005127 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005128 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005129 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005130 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005131 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5132 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005133
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005134 DECLARE_INSTRUCTION(InstanceFieldSet);
5135
5136 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005137 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5138 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5139 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5140 "Too many packed fields.");
5141
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005142 const FieldInfo field_info_;
5143
5144 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5145};
5146
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147class HArrayGet : public HExpression<2> {
5148 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005149 HArrayGet(HInstruction* array,
5150 HInstruction* index,
5151 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005152 uint32_t dex_pc,
5153 SideEffects additional_side_effects = SideEffects::None())
5154 : HExpression(type,
5155 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005156 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005157 SetRawInputAt(0, array);
5158 SetRawInputAt(1, index);
5159 }
5160
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005161 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005162 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005163 return true;
5164 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005165 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005166 // TODO: We can be smarter here.
5167 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5168 // which generates the implicit null check. There are cases when these can be removed
5169 // to produce better code. If we ever add optimizations to do so we should allow an
5170 // implicit check here (as long as the address falls in the first page).
5171 return false;
5172 }
5173
David Brazdil4833f5a2015-12-16 10:37:39 +00005174 bool IsEquivalentOf(HArrayGet* other) const {
5175 bool result = (GetDexPc() == other->GetDexPc());
5176 if (kIsDebugBuild && result) {
5177 DCHECK_EQ(GetBlock(), other->GetBlock());
5178 DCHECK_EQ(GetArray(), other->GetArray());
5179 DCHECK_EQ(GetIndex(), other->GetIndex());
5180 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005181 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005182 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005183 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5184 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005185 }
5186 }
5187 return result;
5188 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005189
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005190 HInstruction* GetArray() const { return InputAt(0); }
5191 HInstruction* GetIndex() const { return InputAt(1); }
5192
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005193 DECLARE_INSTRUCTION(ArrayGet);
5194
5195 private:
5196 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5197};
5198
5199class HArraySet : public HTemplateInstruction<3> {
5200 public:
5201 HArraySet(HInstruction* array,
5202 HInstruction* index,
5203 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005204 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005205 uint32_t dex_pc,
5206 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005207 : HTemplateInstruction(
5208 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005209 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5210 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005211 dex_pc) {
5212 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5213 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5214 SetPackedFlag<kFlagValueCanBeNull>(true);
5215 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216 SetRawInputAt(0, array);
5217 SetRawInputAt(1, index);
5218 SetRawInputAt(2, value);
5219 }
5220
Calin Juravle77520bc2015-01-12 18:45:46 +00005221 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005222 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005223 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224 }
5225
Mingyao Yang81014cb2015-06-02 03:16:27 -07005226 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005227 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005228
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005229 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005230 // TODO: Same as for ArrayGet.
5231 return false;
5232 }
5233
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005234 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005235 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005236 }
5237
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005238 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005239 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005240 }
5241
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005242 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005243 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005244 }
5245
Vladimir Markoa1de9182016-02-25 11:37:38 +00005246 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5247 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5248 bool StaticTypeOfArrayIsObjectArray() const {
5249 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5250 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005251
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005252 HInstruction* GetArray() const { return InputAt(0); }
5253 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005254 HInstruction* GetValue() const { return InputAt(2); }
5255
5256 Primitive::Type GetComponentType() const {
5257 // The Dex format does not type floating point index operations. Since the
5258 // `expected_component_type_` is set during building and can therefore not
5259 // be correct, we also check what is the value type. If it is a floating
5260 // point type, we must use that type.
5261 Primitive::Type value_type = GetValue()->GetType();
5262 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5263 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005264 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005265 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005266
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005267 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005268 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005269 }
5270
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005271 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5272 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5273 }
5274
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275 DECLARE_INSTRUCTION(ArraySet);
5276
5277 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005278 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5279 static constexpr size_t kFieldExpectedComponentTypeSize =
5280 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5281 static constexpr size_t kFlagNeedsTypeCheck =
5282 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5283 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005284 // Cached information for the reference_type_info_ so that codegen
5285 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005286 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5287 static constexpr size_t kNumberOfArraySetPackedBits =
5288 kFlagStaticTypeOfArrayIsObjectArray + 1;
5289 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5290 using ExpectedComponentTypeField =
5291 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005292
5293 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5294};
5295
5296class HArrayLength : public HExpression<1> {
5297 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005298 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005299 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005300 // Note that arrays do not change length, so the instruction does not
5301 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 SetRawInputAt(0, array);
5303 }
5304
Calin Juravle77520bc2015-01-12 18:45:46 +00005305 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005306 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005307 return true;
5308 }
Calin Juravle641547a2015-04-21 22:08:51 +01005309 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5310 return obj == InputAt(0);
5311 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005312
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313 DECLARE_INSTRUCTION(ArrayLength);
5314
5315 private:
5316 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5317};
5318
5319class HBoundsCheck : public HExpression<2> {
5320 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005321 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5322 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005323 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005324 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325 DCHECK(index->GetType() == Primitive::kPrimInt);
5326 SetRawInputAt(0, index);
5327 SetRawInputAt(1, length);
5328 }
5329
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005330 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005331 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005332 return true;
5333 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005334
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005335 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005337 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005338
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005339 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340
5341 DECLARE_INSTRUCTION(BoundsCheck);
5342
5343 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005344 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5345};
5346
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005347class HSuspendCheck : public HTemplateInstruction<0> {
5348 public:
5349 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005350 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005351
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005352 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005353 return true;
5354 }
5355
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005356 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5357 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005358
5359 DECLARE_INSTRUCTION(SuspendCheck);
5360
5361 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005362 // Only used for code generation, in order to share the same slow path between back edges
5363 // of a same loop.
5364 SlowPathCode* slow_path_;
5365
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005366 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5367};
5368
David Srbecky0cf44932015-12-09 14:09:59 +00005369// Pseudo-instruction which provides the native debugger with mapping information.
5370// It ensures that we can generate line number and local variables at this point.
5371class HNativeDebugInfo : public HTemplateInstruction<0> {
5372 public:
5373 explicit HNativeDebugInfo(uint32_t dex_pc)
5374 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5375
5376 bool NeedsEnvironment() const OVERRIDE {
5377 return true;
5378 }
5379
5380 DECLARE_INSTRUCTION(NativeDebugInfo);
5381
5382 private:
5383 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5384};
5385
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005386/**
5387 * Instruction to load a Class object.
5388 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005389class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005390 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005391 HLoadClass(HCurrentMethod* current_method,
5392 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005393 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005394 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005395 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005396 bool needs_access_check,
5397 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005398 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005399 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005400 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005401 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005402 // Referrers class should not need access check. We never inline unverified
5403 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005404 DCHECK(!is_referrers_class || !needs_access_check);
5405
5406 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5407 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5408 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5409 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005410 SetRawInputAt(0, current_method);
5411 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005412
5413 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005414
5415 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005416 // Note that we don't need to test for generate_clinit_check_.
5417 // Whether or not we need to generate the clinit check is processed in
5418 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005419 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005420 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421 }
5422
5423 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5424
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005425 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005426 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005427
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005428 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005429 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005430 }
5431
Calin Juravle0ba218d2015-05-19 18:46:01 +01005432 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005433 // The entrypoint the code generator is going to call does not do
5434 // clinit of the class.
5435 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005436 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005437 }
5438
5439 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005440 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005441 (!IsReferrersClass() && !IsInDexCache()) ||
5442 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005443 }
5444
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005445
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005446 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005447 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005448 }
5449
Calin Juravleacf735c2015-02-12 15:25:22 +00005450 ReferenceTypeInfo GetLoadedClassRTI() {
5451 return loaded_class_rti_;
5452 }
5453
5454 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5455 // Make sure we only set exact types (the loaded class should never be merged).
5456 DCHECK(rti.IsExact());
5457 loaded_class_rti_ = rti;
5458 }
5459
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005460 const DexFile& GetDexFile() { return dex_file_; }
5461
Vladimir Markoa1de9182016-02-25 11:37:38 +00005462 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005463
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005464 static SideEffects SideEffectsForArchRuntimeCalls() {
5465 return SideEffects::CanTriggerGC();
5466 }
5467
Vladimir Markoa1de9182016-02-25 11:37:38 +00005468 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5469 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5470 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5471 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005472
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005473 DECLARE_INSTRUCTION(LoadClass);
5474
5475 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005476 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5477 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5478 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005479 // Whether this instruction must generate the initialization check.
5480 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005481 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5482 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5483 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5484
5485 const uint16_t type_index_;
5486 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005487
Calin Juravleacf735c2015-02-12 15:25:22 +00005488 ReferenceTypeInfo loaded_class_rti_;
5489
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005490 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5491};
5492
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005493class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005494 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005495 HLoadString(HCurrentMethod* current_method,
5496 uint32_t string_index,
5497 uint32_t dex_pc,
5498 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005499 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005500 string_index_(string_index) {
5501 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005502 SetRawInputAt(0, current_method);
5503 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005504
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005505 bool CanBeMoved() const OVERRIDE { return true; }
5506
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005507 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5508 return other->AsLoadString()->string_index_ == string_index_;
5509 }
5510
5511 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5512
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005513 uint32_t GetStringIndex() const { return string_index_; }
5514
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005515 // Will call the runtime if the string is not already in the dex cache.
5516 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5517
Vladimir Markodc151b22015-10-15 18:02:30 +01005518 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005519 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005520 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005521
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005522 static SideEffects SideEffectsForArchRuntimeCalls() {
5523 return SideEffects::CanTriggerGC();
5524 }
5525
Vladimir Markoa1de9182016-02-25 11:37:38 +00005526 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5527
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005528 DECLARE_INSTRUCTION(LoadString);
5529
5530 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005531 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
5532 static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1;
5533 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5534
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005535 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005536
5537 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5538};
5539
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005540/**
5541 * Performs an initialization check on its Class object input.
5542 */
5543class HClinitCheck : public HExpression<1> {
5544 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005545 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005546 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005547 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005548 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5549 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005550 SetRawInputAt(0, constant);
5551 }
5552
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005553 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005554 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005555 return true;
5556 }
5557
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005558 bool NeedsEnvironment() const OVERRIDE {
5559 // May call runtime to initialize the class.
5560 return true;
5561 }
5562
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005563 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005564
5565 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5566
5567 DECLARE_INSTRUCTION(ClinitCheck);
5568
5569 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005570 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5571};
5572
5573class HStaticFieldGet : public HExpression<1> {
5574 public:
5575 HStaticFieldGet(HInstruction* cls,
5576 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005577 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005578 bool is_volatile,
5579 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005580 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005581 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005582 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005583 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005584 : HExpression(field_type,
5585 SideEffects::FieldReadOfType(field_type, is_volatile),
5586 dex_pc),
5587 field_info_(field_offset,
5588 field_type,
5589 is_volatile,
5590 field_idx,
5591 declaring_class_def_index,
5592 dex_file,
5593 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005594 SetRawInputAt(0, cls);
5595 }
5596
Calin Juravle52c48962014-12-16 17:02:57 +00005597
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005598 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005599
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005600 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005601 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5602 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005603 }
5604
5605 size_t ComputeHashCode() const OVERRIDE {
5606 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5607 }
5608
Calin Juravle52c48962014-12-16 17:02:57 +00005609 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005610 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5611 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005612 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005613
5614 DECLARE_INSTRUCTION(StaticFieldGet);
5615
5616 private:
5617 const FieldInfo field_info_;
5618
5619 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5620};
5621
5622class HStaticFieldSet : public HTemplateInstruction<2> {
5623 public:
5624 HStaticFieldSet(HInstruction* cls,
5625 HInstruction* value,
5626 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005627 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005628 bool is_volatile,
5629 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005630 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005631 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005632 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005633 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005634 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5635 dex_pc),
5636 field_info_(field_offset,
5637 field_type,
5638 is_volatile,
5639 field_idx,
5640 declaring_class_def_index,
5641 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005642 dex_cache) {
5643 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005644 SetRawInputAt(0, cls);
5645 SetRawInputAt(1, value);
5646 }
5647
Calin Juravle52c48962014-12-16 17:02:57 +00005648 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005649 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5650 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005651 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005652
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005653 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005654 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5655 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005656
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005657 DECLARE_INSTRUCTION(StaticFieldSet);
5658
5659 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005660 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5661 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5662 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5663 "Too many packed fields.");
5664
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005665 const FieldInfo field_info_;
5666
5667 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5668};
5669
Calin Juravlee460d1d2015-09-29 04:52:17 +01005670class HUnresolvedInstanceFieldGet : public HExpression<1> {
5671 public:
5672 HUnresolvedInstanceFieldGet(HInstruction* obj,
5673 Primitive::Type field_type,
5674 uint32_t field_index,
5675 uint32_t dex_pc)
5676 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5677 field_index_(field_index) {
5678 SetRawInputAt(0, obj);
5679 }
5680
5681 bool NeedsEnvironment() const OVERRIDE { return true; }
5682 bool CanThrow() const OVERRIDE { return true; }
5683
5684 Primitive::Type GetFieldType() const { return GetType(); }
5685 uint32_t GetFieldIndex() const { return field_index_; }
5686
5687 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5688
5689 private:
5690 const uint32_t field_index_;
5691
5692 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5693};
5694
5695class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5696 public:
5697 HUnresolvedInstanceFieldSet(HInstruction* obj,
5698 HInstruction* value,
5699 Primitive::Type field_type,
5700 uint32_t field_index,
5701 uint32_t dex_pc)
5702 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005703 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005704 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005705 DCHECK_EQ(field_type, value->GetType());
5706 SetRawInputAt(0, obj);
5707 SetRawInputAt(1, value);
5708 }
5709
5710 bool NeedsEnvironment() const OVERRIDE { return true; }
5711 bool CanThrow() const OVERRIDE { return true; }
5712
Vladimir Markoa1de9182016-02-25 11:37:38 +00005713 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005714 uint32_t GetFieldIndex() const { return field_index_; }
5715
5716 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5717
5718 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005719 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5720 static constexpr size_t kFieldFieldTypeSize =
5721 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5722 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5723 kFieldFieldType + kFieldFieldTypeSize;
5724 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5725 "Too many packed fields.");
5726 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5727
Calin Juravlee460d1d2015-09-29 04:52:17 +01005728 const uint32_t field_index_;
5729
5730 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5731};
5732
5733class HUnresolvedStaticFieldGet : public HExpression<0> {
5734 public:
5735 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5736 uint32_t field_index,
5737 uint32_t dex_pc)
5738 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5739 field_index_(field_index) {
5740 }
5741
5742 bool NeedsEnvironment() const OVERRIDE { return true; }
5743 bool CanThrow() const OVERRIDE { return true; }
5744
5745 Primitive::Type GetFieldType() const { return GetType(); }
5746 uint32_t GetFieldIndex() const { return field_index_; }
5747
5748 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5749
5750 private:
5751 const uint32_t field_index_;
5752
5753 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5754};
5755
5756class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5757 public:
5758 HUnresolvedStaticFieldSet(HInstruction* value,
5759 Primitive::Type field_type,
5760 uint32_t field_index,
5761 uint32_t dex_pc)
5762 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005763 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005764 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005765 DCHECK_EQ(field_type, value->GetType());
5766 SetRawInputAt(0, value);
5767 }
5768
5769 bool NeedsEnvironment() const OVERRIDE { return true; }
5770 bool CanThrow() const OVERRIDE { return true; }
5771
Vladimir Markoa1de9182016-02-25 11:37:38 +00005772 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005773 uint32_t GetFieldIndex() const { return field_index_; }
5774
5775 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5776
5777 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005778 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5779 static constexpr size_t kFieldFieldTypeSize =
5780 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5781 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5782 kFieldFieldType + kFieldFieldTypeSize;
5783 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5784 "Too many packed fields.");
5785 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5786
Calin Juravlee460d1d2015-09-29 04:52:17 +01005787 const uint32_t field_index_;
5788
5789 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5790};
5791
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005792// Implement the move-exception DEX instruction.
5793class HLoadException : public HExpression<0> {
5794 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005795 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5796 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005797
David Brazdilbbd733e2015-08-18 17:48:17 +01005798 bool CanBeNull() const OVERRIDE { return false; }
5799
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005800 DECLARE_INSTRUCTION(LoadException);
5801
5802 private:
5803 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5804};
5805
David Brazdilcb1c0552015-08-04 16:22:25 +01005806// Implicit part of move-exception which clears thread-local exception storage.
5807// Must not be removed because the runtime expects the TLS to get cleared.
5808class HClearException : public HTemplateInstruction<0> {
5809 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005810 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5811 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005812
5813 DECLARE_INSTRUCTION(ClearException);
5814
5815 private:
5816 DISALLOW_COPY_AND_ASSIGN(HClearException);
5817};
5818
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005819class HThrow : public HTemplateInstruction<1> {
5820 public:
5821 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005822 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005823 SetRawInputAt(0, exception);
5824 }
5825
5826 bool IsControlFlow() const OVERRIDE { return true; }
5827
5828 bool NeedsEnvironment() const OVERRIDE { return true; }
5829
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005830 bool CanThrow() const OVERRIDE { return true; }
5831
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005832
5833 DECLARE_INSTRUCTION(Throw);
5834
5835 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005836 DISALLOW_COPY_AND_ASSIGN(HThrow);
5837};
5838
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005839/**
5840 * Implementation strategies for the code generator of a HInstanceOf
5841 * or `HCheckCast`.
5842 */
5843enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005844 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005845 kExactCheck, // Can do a single class compare.
5846 kClassHierarchyCheck, // Can just walk the super class chain.
5847 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5848 kInterfaceCheck, // No optimization yet when checking against an interface.
5849 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005850 kArrayCheck, // No optimization yet when checking against a generic array.
5851 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005852};
5853
Roland Levillain86503782016-02-11 19:07:30 +00005854std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5855
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005856class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005857 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005858 HInstanceOf(HInstruction* object,
5859 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005861 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005862 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005863 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005864 dex_pc) {
5865 SetPackedField<TypeCheckKindField>(check_kind);
5866 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005867 SetRawInputAt(0, object);
5868 SetRawInputAt(1, constant);
5869 }
5870
5871 bool CanBeMoved() const OVERRIDE { return true; }
5872
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005873 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005874 return true;
5875 }
5876
5877 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005878 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005879 }
5880
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005881 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005882 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5883 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5884 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5885 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005886
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005887 static bool CanCallRuntime(TypeCheckKind check_kind) {
5888 // Mips currently does runtime calls for any other checks.
5889 return check_kind != TypeCheckKind::kExactCheck;
5890 }
5891
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005892 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005893 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005894 }
5895
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005896 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005897
5898 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005899 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5900 static constexpr size_t kFieldTypeCheckKindSize =
5901 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5902 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5903 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5904 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5905 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005906
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005907 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5908};
5909
Calin Juravleb1498f62015-02-16 13:13:29 +00005910class HBoundType : public HExpression<1> {
5911 public:
David Brazdilf5552582015-12-27 13:36:12 +00005912 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005913 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005914 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5915 SetPackedFlag<kFlagUpperCanBeNull>(true);
5916 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005917 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005918 SetRawInputAt(0, input);
5919 }
5920
David Brazdilf5552582015-12-27 13:36:12 +00005921 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005922 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005923 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005924 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005925
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005926 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005927 DCHECK(GetUpperCanBeNull() || !can_be_null);
5928 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005929 }
5930
Vladimir Markoa1de9182016-02-25 11:37:38 +00005931 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005932
Calin Juravleb1498f62015-02-16 13:13:29 +00005933 DECLARE_INSTRUCTION(BoundType);
5934
5935 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005936 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5937 // is false then CanBeNull() cannot be true).
5938 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
5939 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
5940 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
5941 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5942
Calin Juravleb1498f62015-02-16 13:13:29 +00005943 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005944 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5945 // It is used to bound the type in cases like:
5946 // if (x instanceof ClassX) {
5947 // // uper_bound_ will be ClassX
5948 // }
David Brazdilf5552582015-12-27 13:36:12 +00005949 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005950
5951 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5952};
5953
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005954class HCheckCast : public HTemplateInstruction<2> {
5955 public:
5956 HCheckCast(HInstruction* object,
5957 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005958 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005959 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00005960 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
5961 SetPackedField<TypeCheckKindField>(check_kind);
5962 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005963 SetRawInputAt(0, object);
5964 SetRawInputAt(1, constant);
5965 }
5966
5967 bool CanBeMoved() const OVERRIDE { return true; }
5968
5969 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5970 return true;
5971 }
5972
5973 bool NeedsEnvironment() const OVERRIDE {
5974 // Instruction may throw a CheckCastError.
5975 return true;
5976 }
5977
5978 bool CanThrow() const OVERRIDE { return true; }
5979
Vladimir Markoa1de9182016-02-25 11:37:38 +00005980 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5981 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5982 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5983 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005984
5985 DECLARE_INSTRUCTION(CheckCast);
5986
5987 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005988 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
5989 static constexpr size_t kFieldTypeCheckKindSize =
5990 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5991 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5992 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
5993 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5994 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005995
5996 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005997};
5998
Calin Juravle27df7582015-04-17 19:12:31 +01005999class HMemoryBarrier : public HTemplateInstruction<0> {
6000 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006001 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006002 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6004 SetPackedField<BarrierKindField>(barrier_kind);
6005 }
Calin Juravle27df7582015-04-17 19:12:31 +01006006
Vladimir Markoa1de9182016-02-25 11:37:38 +00006007 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006008
6009 DECLARE_INSTRUCTION(MemoryBarrier);
6010
6011 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006012 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6013 static constexpr size_t kFieldBarrierKindSize =
6014 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6015 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6016 kFieldBarrierKind + kFieldBarrierKindSize;
6017 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6018 "Too many packed fields.");
6019 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006020
6021 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6022};
6023
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006024class HMonitorOperation : public HTemplateInstruction<1> {
6025 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006026 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006027 kEnter,
6028 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006029 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006030 };
6031
6032 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006033 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006034 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6035 dex_pc) {
6036 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006037 SetRawInputAt(0, object);
6038 }
6039
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006040 // Instruction may go into runtime, so we need an environment.
6041 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006042
6043 bool CanThrow() const OVERRIDE {
6044 // Verifier guarantees that monitor-exit cannot throw.
6045 // This is important because it allows the HGraphBuilder to remove
6046 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6047 return IsEnter();
6048 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006049
Vladimir Markoa1de9182016-02-25 11:37:38 +00006050 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6051 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006052
6053 DECLARE_INSTRUCTION(MonitorOperation);
6054
Calin Juravle52c48962014-12-16 17:02:57 +00006055 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006056 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6057 static constexpr size_t kFieldOperationKindSize =
6058 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6059 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6060 kFieldOperationKind + kFieldOperationKindSize;
6061 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6062 "Too many packed fields.");
6063 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006064
6065 private:
6066 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6067};
6068
David Brazdil74eb1b22015-12-14 11:44:01 +00006069class HSelect : public HExpression<3> {
6070 public:
6071 HSelect(HInstruction* condition,
6072 HInstruction* true_value,
6073 HInstruction* false_value,
6074 uint32_t dex_pc)
6075 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6076 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6077
6078 // First input must be `true_value` or `false_value` to allow codegens to
6079 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6080 // that architectures which implement HSelect as a conditional move also
6081 // will not need to invert the condition.
6082 SetRawInputAt(0, false_value);
6083 SetRawInputAt(1, true_value);
6084 SetRawInputAt(2, condition);
6085 }
6086
6087 HInstruction* GetFalseValue() const { return InputAt(0); }
6088 HInstruction* GetTrueValue() const { return InputAt(1); }
6089 HInstruction* GetCondition() const { return InputAt(2); }
6090
6091 bool CanBeMoved() const OVERRIDE { return true; }
6092 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6093
6094 bool CanBeNull() const OVERRIDE {
6095 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6096 }
6097
6098 DECLARE_INSTRUCTION(Select);
6099
6100 private:
6101 DISALLOW_COPY_AND_ASSIGN(HSelect);
6102};
6103
Vladimir Markof9f64412015-09-02 14:05:49 +01006104class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006105 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006106 MoveOperands(Location source,
6107 Location destination,
6108 Primitive::Type type,
6109 HInstruction* instruction)
6110 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006111
6112 Location GetSource() const { return source_; }
6113 Location GetDestination() const { return destination_; }
6114
6115 void SetSource(Location value) { source_ = value; }
6116 void SetDestination(Location value) { destination_ = value; }
6117
6118 // The parallel move resolver marks moves as "in-progress" by clearing the
6119 // destination (but not the source).
6120 Location MarkPending() {
6121 DCHECK(!IsPending());
6122 Location dest = destination_;
6123 destination_ = Location::NoLocation();
6124 return dest;
6125 }
6126
6127 void ClearPending(Location dest) {
6128 DCHECK(IsPending());
6129 destination_ = dest;
6130 }
6131
6132 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006133 DCHECK(source_.IsValid() || destination_.IsInvalid());
6134 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006135 }
6136
6137 // True if this blocks a move from the given location.
6138 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006139 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006140 }
6141
6142 // A move is redundant if it's been eliminated, if its source and
6143 // destination are the same, or if its destination is unneeded.
6144 bool IsRedundant() const {
6145 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6146 }
6147
6148 // We clear both operands to indicate move that's been eliminated.
6149 void Eliminate() {
6150 source_ = destination_ = Location::NoLocation();
6151 }
6152
6153 bool IsEliminated() const {
6154 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6155 return source_.IsInvalid();
6156 }
6157
Alexey Frunze4dda3372015-06-01 18:31:49 -07006158 Primitive::Type GetType() const { return type_; }
6159
Nicolas Geoffray90218252015-04-15 11:56:51 +01006160 bool Is64BitMove() const {
6161 return Primitive::Is64BitType(type_);
6162 }
6163
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006164 HInstruction* GetInstruction() const { return instruction_; }
6165
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006166 private:
6167 Location source_;
6168 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006169 // The type this move is for.
6170 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006171 // The instruction this move is assocatied with. Null when this move is
6172 // for moving an input in the expected locations of user (including a phi user).
6173 // This is only used in debug mode, to ensure we do not connect interval siblings
6174 // in the same parallel move.
6175 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006176};
6177
Roland Levillainc9285912015-12-18 10:38:42 +00006178std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6179
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006180static constexpr size_t kDefaultNumberOfMoves = 4;
6181
6182class HParallelMove : public HTemplateInstruction<0> {
6183 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006184 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006185 : HTemplateInstruction(SideEffects::None(), dex_pc),
6186 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6187 moves_.reserve(kDefaultNumberOfMoves);
6188 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006189
Nicolas Geoffray90218252015-04-15 11:56:51 +01006190 void AddMove(Location source,
6191 Location destination,
6192 Primitive::Type type,
6193 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006194 DCHECK(source.IsValid());
6195 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006196 if (kIsDebugBuild) {
6197 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006198 for (const MoveOperands& move : moves_) {
6199 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006200 // Special case the situation where the move is for the spill slot
6201 // of the instruction.
6202 if ((GetPrevious() == instruction)
6203 || ((GetPrevious() == nullptr)
6204 && instruction->IsPhi()
6205 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006206 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006207 << "Doing parallel moves for the same instruction.";
6208 } else {
6209 DCHECK(false) << "Doing parallel moves for the same instruction.";
6210 }
6211 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006212 }
6213 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006214 for (const MoveOperands& move : moves_) {
6215 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006216 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006217 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006218 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006219 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006220 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006221 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006222 }
6223
Vladimir Marko225b6462015-09-28 12:17:40 +01006224 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006225 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006226 }
6227
Vladimir Marko225b6462015-09-28 12:17:40 +01006228 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006229
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006230 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006231
6232 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006233 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006234
6235 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6236};
6237
Mark Mendell0616ae02015-04-17 12:49:27 -04006238} // namespace art
6239
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006240#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6241#include "nodes_shared.h"
6242#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006243#ifdef ART_ENABLE_CODEGEN_arm
6244#include "nodes_arm.h"
6245#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006246#ifdef ART_ENABLE_CODEGEN_arm64
6247#include "nodes_arm64.h"
6248#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006249#ifdef ART_ENABLE_CODEGEN_x86
6250#include "nodes_x86.h"
6251#endif
6252
6253namespace art {
6254
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006255class HGraphVisitor : public ValueObject {
6256 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006257 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6258 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006259
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006260 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006261 virtual void VisitBasicBlock(HBasicBlock* block);
6262
Roland Levillain633021e2014-10-01 14:12:25 +01006263 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006264 void VisitInsertionOrder();
6265
Roland Levillain633021e2014-10-01 14:12:25 +01006266 // Visit the graph following dominator tree reverse post-order.
6267 void VisitReversePostOrder();
6268
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006269 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006270
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006271 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006272#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006273 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6274
6275 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6276
6277#undef DECLARE_VISIT_INSTRUCTION
6278
6279 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006280 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006281
6282 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6283};
6284
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006285class HGraphDelegateVisitor : public HGraphVisitor {
6286 public:
6287 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6288 virtual ~HGraphDelegateVisitor() {}
6289
6290 // Visit functions that delegate to to super class.
6291#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006292 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006293
6294 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6295
6296#undef DECLARE_VISIT_INSTRUCTION
6297
6298 private:
6299 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6300};
6301
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006302class HInsertionOrderIterator : public ValueObject {
6303 public:
6304 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6305
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006306 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006307 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006308 void Advance() { ++index_; }
6309
6310 private:
6311 const HGraph& graph_;
6312 size_t index_;
6313
6314 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6315};
6316
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006317class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006318 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006319 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6320 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006321 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006322 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006323
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006324 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6325 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006326 void Advance() { ++index_; }
6327
6328 private:
6329 const HGraph& graph_;
6330 size_t index_;
6331
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006332 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006333};
6334
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006335class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006336 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006337 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006338 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006339 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006340 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006341 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006342
6343 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006344 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006345 void Advance() { --index_; }
6346
6347 private:
6348 const HGraph& graph_;
6349 size_t index_;
6350
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006351 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006352};
6353
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006354class HLinearPostOrderIterator : public ValueObject {
6355 public:
6356 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006357 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006358
6359 bool Done() const { return index_ == 0; }
6360
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006361 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006362
6363 void Advance() {
6364 --index_;
6365 DCHECK_GE(index_, 0U);
6366 }
6367
6368 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006369 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006370 size_t index_;
6371
6372 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6373};
6374
6375class HLinearOrderIterator : public ValueObject {
6376 public:
6377 explicit HLinearOrderIterator(const HGraph& graph)
6378 : order_(graph.GetLinearOrder()), index_(0) {}
6379
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006380 bool Done() const { return index_ == order_.size(); }
6381 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006382 void Advance() { ++index_; }
6383
6384 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006385 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006386 size_t index_;
6387
6388 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6389};
6390
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006391// Iterator over the blocks that art part of the loop. Includes blocks part
6392// of an inner loop. The order in which the blocks are iterated is on their
6393// block id.
6394class HBlocksInLoopIterator : public ValueObject {
6395 public:
6396 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6397 : blocks_in_loop_(info.GetBlocks()),
6398 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6399 index_(0) {
6400 if (!blocks_in_loop_.IsBitSet(index_)) {
6401 Advance();
6402 }
6403 }
6404
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006405 bool Done() const { return index_ == blocks_.size(); }
6406 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006407 void Advance() {
6408 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006409 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006410 if (blocks_in_loop_.IsBitSet(index_)) {
6411 break;
6412 }
6413 }
6414 }
6415
6416 private:
6417 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006418 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006419 size_t index_;
6420
6421 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6422};
6423
Mingyao Yang3584bce2015-05-19 16:01:59 -07006424// Iterator over the blocks that art part of the loop. Includes blocks part
6425// of an inner loop. The order in which the blocks are iterated is reverse
6426// post order.
6427class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6428 public:
6429 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6430 : blocks_in_loop_(info.GetBlocks()),
6431 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6432 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006433 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006434 Advance();
6435 }
6436 }
6437
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006438 bool Done() const { return index_ == blocks_.size(); }
6439 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006440 void Advance() {
6441 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006442 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6443 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006444 break;
6445 }
6446 }
6447 }
6448
6449 private:
6450 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006451 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006452 size_t index_;
6453
6454 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6455};
6456
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006457inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006458 if (constant->IsIntConstant()) {
6459 return constant->AsIntConstant()->GetValue();
6460 } else if (constant->IsLongConstant()) {
6461 return constant->AsLongConstant()->GetValue();
6462 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006463 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006464 return 0;
6465 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006466}
6467
Vladimir Marko58155012015-08-19 12:49:41 +00006468inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6469 // For the purposes of the compiler, the dex files must actually be the same object
6470 // if we want to safely treat them as the same. This is especially important for JIT
6471 // as custom class loaders can open the same underlying file (or memory) multiple
6472 // times and provide different class resolution but no two class loaders should ever
6473 // use the same DexFile object - doing so is an unsupported hack that can lead to
6474 // all sorts of weird failures.
6475 return &lhs == &rhs;
6476}
6477
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006478#define INSTRUCTION_TYPE_CHECK(type, super) \
6479 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6480 inline const H##type* HInstruction::As##type() const { \
6481 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6482 } \
6483 inline H##type* HInstruction::As##type() { \
6484 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6485 }
6486
6487 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6488#undef INSTRUCTION_TYPE_CHECK
6489
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006490class SwitchTable : public ValueObject {
6491 public:
6492 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6493 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6494 int32_t table_offset = instruction.VRegB_31t();
6495 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6496 if (sparse) {
6497 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6498 } else {
6499 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6500 }
6501 num_entries_ = table[1];
6502 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6503 }
6504
6505 uint16_t GetNumEntries() const {
6506 return num_entries_;
6507 }
6508
6509 void CheckIndex(size_t index) const {
6510 if (sparse_) {
6511 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6512 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6513 } else {
6514 // In a packed table, we have the starting key and num_entries_ values.
6515 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6516 }
6517 }
6518
6519 int32_t GetEntryAt(size_t index) const {
6520 CheckIndex(index);
6521 return values_[index];
6522 }
6523
6524 uint32_t GetDexPcForIndex(size_t index) const {
6525 CheckIndex(index);
6526 return dex_pc_ +
6527 (reinterpret_cast<const int16_t*>(values_ + index) -
6528 reinterpret_cast<const int16_t*>(&instruction_));
6529 }
6530
6531 // Index of the first value in the table.
6532 size_t GetFirstValueIndex() const {
6533 if (sparse_) {
6534 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6535 return num_entries_;
6536 } else {
6537 // In a packed table, we have the starting key and num_entries_ values.
6538 return 1;
6539 }
6540 }
6541
6542 private:
6543 const Instruction& instruction_;
6544 const uint32_t dex_pc_;
6545
6546 // Whether this is a sparse-switch table (or a packed-switch one).
6547 const bool sparse_;
6548
6549 // This can't be const as it needs to be computed off of the given instruction, and complicated
6550 // expressions in the initializer list seemed very ugly.
6551 uint16_t num_entries_;
6552
6553 const int32_t* values_;
6554
6555 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6556};
6557
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006558// Create space in `blocks` for adding `number_of_new_blocks` entries
6559// starting at location `at`. Blocks after `at` are moved accordingly.
6560inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6561 size_t number_of_new_blocks,
6562 size_t after) {
6563 DCHECK_LT(after, blocks->size());
6564 size_t old_size = blocks->size();
6565 size_t new_size = old_size + number_of_new_blocks;
6566 blocks->resize(new_size);
6567 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6568}
6569
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006570} // namespace art
6571
6572#endif // ART_COMPILER_OPTIMIZING_NODES_H_