blob: e3dbe16547a088728d39bde906e59ee1d506a481 [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) {
390 current_instruction_id_ = id;
391 }
392
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100393 uint16_t GetMaximumNumberOfOutVRegs() const {
394 return maximum_number_of_out_vregs_;
395 }
396
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000397 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
398 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100399 }
400
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100401 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
402 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
403 }
404
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000405 void UpdateTemporariesVRegSlots(size_t slots) {
406 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 }
408
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000409 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100410 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000411 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100414 void SetNumberOfVRegs(uint16_t number_of_vregs) {
415 number_of_vregs_ = number_of_vregs;
416 }
417
418 uint16_t GetNumberOfVRegs() const {
419 return number_of_vregs_;
420 }
421
422 void SetNumberOfInVRegs(uint16_t value) {
423 number_of_in_vregs_ = value;
424 }
425
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100426 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100427 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100428 return number_of_vregs_ - number_of_in_vregs_;
429 }
430
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100431 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100432 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100433 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100434
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100435 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100436 return linear_order_;
437 }
438
Mark Mendell1152c922015-04-24 17:06:35 -0400439 bool HasBoundsChecks() const {
440 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800441 }
442
Mark Mendell1152c922015-04-24 17:06:35 -0400443 void SetHasBoundsChecks(bool value) {
444 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800445 }
446
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100447 bool ShouldGenerateConstructorBarrier() const {
448 return should_generate_constructor_barrier_;
449 }
450
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000451 bool IsDebuggable() const { return debuggable_; }
452
David Brazdil8d5b8b22015-03-24 10:51:52 +0000453 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000454 // already, it is created and inserted into the graph. This method is only for
455 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600456 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000457
458 // TODO: This is problematic for the consistency of reference type propagation
459 // because it can be created anytime after the pass and thus it will be left
460 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600461 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000462
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600463 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
464 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000465 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600466 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
467 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000468 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600469 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
470 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600472 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
473 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000474 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000475
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100476 HCurrentMethod* GetCurrentMethod();
477
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100478 const DexFile& GetDexFile() const {
479 return dex_file_;
480 }
481
482 uint32_t GetMethodIdx() const {
483 return method_idx_;
484 }
485
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100486 InvokeType GetInvokeType() const {
487 return invoke_type_;
488 }
489
Mark Mendellc4701932015-04-10 13:18:51 -0400490 InstructionSet GetInstructionSet() const {
491 return instruction_set_;
492 }
493
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000494 bool IsCompilingOsr() const { return osr_; }
495
David Brazdil77a48ae2015-09-15 12:34:04 +0000496 bool HasTryCatch() const { return has_try_catch_; }
497 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100498
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000499 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
500 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
501
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100502 ArtMethod* GetArtMethod() const { return art_method_; }
503 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
504
Mark Mendellf6529172015-11-17 11:16:56 -0500505 // Returns an instruction with the opposite boolean value from 'cond'.
506 // The instruction has been inserted into the graph, either as a constant, or
507 // before cursor.
508 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
509
David Brazdil2d7352b2015-04-20 14:52:42 +0100510 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000511 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100512 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000513
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000514 template <class InstructionType, typename ValueType>
515 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600516 ArenaSafeMap<ValueType, InstructionType*>* cache,
517 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000518 // Try to find an existing constant of the given value.
519 InstructionType* constant = nullptr;
520 auto cached_constant = cache->find(value);
521 if (cached_constant != cache->end()) {
522 constant = cached_constant->second;
523 }
524
525 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100526 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000527 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600528 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 cache->Overwrite(value, constant);
530 InsertConstant(constant);
531 }
532 return constant;
533 }
534
David Brazdil8d5b8b22015-03-24 10:51:52 +0000535 void InsertConstant(HConstant* instruction);
536
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000537 // Cache a float constant into the graph. This method should only be
538 // called by the SsaBuilder when creating "equivalent" instructions.
539 void CacheFloatConstant(HFloatConstant* constant);
540
541 // See CacheFloatConstant comment.
542 void CacheDoubleConstant(HDoubleConstant* constant);
543
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000544 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000545
546 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100547 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000548
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100549 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100550 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100552 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100553 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100554
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000555 HBasicBlock* entry_block_;
556 HBasicBlock* exit_block_;
557
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100558 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100559 uint16_t maximum_number_of_out_vregs_;
560
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100561 // The number of virtual registers in this method. Contains the parameters.
562 uint16_t number_of_vregs_;
563
564 // The number of virtual registers used by parameters of this method.
565 uint16_t number_of_in_vregs_;
566
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000567 // Number of vreg size slots that the temporaries use (used in baseline compiler).
568 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100569
Mark Mendell1152c922015-04-24 17:06:35 -0400570 // Has bounds checks. We can totally skip BCE if it's false.
571 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800572
David Brazdil77a48ae2015-09-15 12:34:04 +0000573 // Flag whether there are any try/catch blocks in the graph. We will skip
574 // try/catch-related passes if false.
575 bool has_try_catch_;
576
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000577 // Flag whether there are any irreducible loops in the graph.
578 bool has_irreducible_loops_;
579
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000580 // Indicates whether the graph should be compiled in a way that
581 // ensures full debuggability. If false, we can apply more
582 // aggressive optimizations that may limit the level of debugging.
583 const bool debuggable_;
584
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000585 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000586 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000587
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100588 // The dex file from which the method is from.
589 const DexFile& dex_file_;
590
591 // The method index in the dex file.
592 const uint32_t method_idx_;
593
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100594 // If inlined, this encodes how the callee is being invoked.
595 const InvokeType invoke_type_;
596
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100597 // Whether the graph has been transformed to SSA form. Only used
598 // in debug mode to ensure we are not using properties only valid
599 // for non-SSA form (like the number of temporaries).
600 bool in_ssa_form_;
601
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100602 const bool should_generate_constructor_barrier_;
603
Mathieu Chartiere401d142015-04-22 13:56:20 -0700604 const InstructionSet instruction_set_;
605
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000607 HNullConstant* cached_null_constant_;
608 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000609 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000610 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000611 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000612
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100613 HCurrentMethod* cached_current_method_;
614
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100615 // The ArtMethod this graph is for. Note that for AOT, it may be null,
616 // for example for methods whose declaring class could not be resolved
617 // (such as when the superclass could not be found).
618 ArtMethod* art_method_;
619
David Brazdil4833f5a2015-12-16 10:37:39 +0000620 // Keep the RTI of inexact Object to avoid having to pass stack handle
621 // collection pointer to passes which may create NullConstant.
622 ReferenceTypeInfo inexact_object_rti_;
623
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000624 // Whether we are compiling this graph for on stack replacement: this will
625 // make all loops seen as irreducible and emit special stack maps to mark
626 // compiled code entries which the interpreter can directly jump to.
627 const bool osr_;
628
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000629 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100630 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000631 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000632 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000633 DISALLOW_COPY_AND_ASSIGN(HGraph);
634};
635
Vladimir Markof9f64412015-09-02 14:05:49 +0100636class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000637 public:
638 HLoopInformation(HBasicBlock* header, HGraph* graph)
639 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100640 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000641 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100642 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100643 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100644 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
645 back_edges_.reserve(kDefaultNumberOfBackEdges);
646 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100647
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000648 bool IsIrreducible() const { return irreducible_; }
649
650 void Dump(std::ostream& os);
651
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100652 HBasicBlock* GetHeader() const {
653 return header_;
654 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000655
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000656 void SetHeader(HBasicBlock* block) {
657 header_ = block;
658 }
659
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100660 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
661 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
662 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
663
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000664 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100665 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000666 }
667
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100668 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100669 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670 }
671
David Brazdil46e2a392015-03-16 17:31:52 +0000672 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100673 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674 }
675
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000676 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100677 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000678 }
679
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100680 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100682 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100683 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100684 }
685
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100686 // Returns the lifetime position of the back edge that has the
687 // greatest lifetime position.
688 size_t GetLifetimeEnd() const;
689
690 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100691 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100692 }
693
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000694 // Finds blocks that are part of this loop.
695 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100696
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 // Returns whether this loop information contains `block`.
698 // Note that this loop information *must* be populated before entering this function.
699 bool Contains(const HBasicBlock& block) const;
700
701 // Returns whether this loop information is an inner loop of `other`.
702 // Note that `other` *must* be populated before entering this function.
703 bool IsIn(const HLoopInformation& other) const;
704
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800705 // Returns true if instruction is not defined within this loop.
706 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700707
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 const ArenaBitVector& GetBlocks() const { return blocks_; }
709
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000710 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000711 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000712
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000713 void ClearAllBlocks() {
714 blocks_.ClearAllBits();
715 }
716
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000717 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100718 // Internal recursive implementation of `Populate`.
719 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000720 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100721
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000722 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100723 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000724 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100725 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100726 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000727
728 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
729};
730
David Brazdilec16f792015-08-19 15:04:01 +0100731// Stores try/catch information for basic blocks.
732// Note that HGraph is constructed so that catch blocks cannot simultaneously
733// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100734class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100735 public:
736 // Try block information constructor.
737 explicit TryCatchInformation(const HTryBoundary& try_entry)
738 : try_entry_(&try_entry),
739 catch_dex_file_(nullptr),
740 catch_type_index_(DexFile::kDexNoIndex16) {
741 DCHECK(try_entry_ != nullptr);
742 }
743
744 // Catch block information constructor.
745 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
746 : try_entry_(nullptr),
747 catch_dex_file_(&dex_file),
748 catch_type_index_(catch_type_index) {}
749
750 bool IsTryBlock() const { return try_entry_ != nullptr; }
751
752 const HTryBoundary& GetTryEntry() const {
753 DCHECK(IsTryBlock());
754 return *try_entry_;
755 }
756
757 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
758
759 bool IsCatchAllTypeIndex() const {
760 DCHECK(IsCatchBlock());
761 return catch_type_index_ == DexFile::kDexNoIndex16;
762 }
763
764 uint16_t GetCatchTypeIndex() const {
765 DCHECK(IsCatchBlock());
766 return catch_type_index_;
767 }
768
769 const DexFile& GetCatchDexFile() const {
770 DCHECK(IsCatchBlock());
771 return *catch_dex_file_;
772 }
773
774 private:
775 // One of possibly several TryBoundary instructions entering the block's try.
776 // Only set for try blocks.
777 const HTryBoundary* try_entry_;
778
779 // Exception type information. Only set for catch blocks.
780 const DexFile* catch_dex_file_;
781 const uint16_t catch_type_index_;
782};
783
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100784static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100785static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100786
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787// A block in a method. Contains the list of instructions represented
788// as a double linked list. Each block knows its predecessors and
789// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100790
Vladimir Markof9f64412015-09-02 14:05:49 +0100791class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000792 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600793 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000794 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000795 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
796 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000797 loop_information_(nullptr),
798 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000799 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100800 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100801 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100802 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000803 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000804 try_catch_information_(nullptr) {
805 predecessors_.reserve(kDefaultNumberOfPredecessors);
806 successors_.reserve(kDefaultNumberOfSuccessors);
807 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
808 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000809
Vladimir Marko60584552015-09-03 13:35:12 +0000810 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100811 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000812 }
813
Vladimir Marko60584552015-09-03 13:35:12 +0000814 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100815 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000816 }
817
David Brazdild26a4112015-11-10 11:07:31 +0000818 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
819 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
820
Vladimir Marko60584552015-09-03 13:35:12 +0000821 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
822 return ContainsElement(successors_, block, start_from);
823 }
824
825 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100826 return dominated_blocks_;
827 }
828
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100829 bool IsEntryBlock() const {
830 return graph_->GetEntryBlock() == this;
831 }
832
833 bool IsExitBlock() const {
834 return graph_->GetExitBlock() == this;
835 }
836
David Brazdil46e2a392015-03-16 17:31:52 +0000837 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000838 bool IsSingleTryBoundary() const;
839
840 // Returns true if this block emits nothing but a jump.
841 bool IsSingleJump() const {
842 HLoopInformation* loop_info = GetLoopInformation();
843 return (IsSingleGoto() || IsSingleTryBoundary())
844 // Back edges generate a suspend check.
845 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
846 }
David Brazdil46e2a392015-03-16 17:31:52 +0000847
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000848 void AddBackEdge(HBasicBlock* back_edge) {
849 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000850 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000851 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100852 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000853 loop_information_->AddBackEdge(back_edge);
854 }
855
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000856 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000857 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000858
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100859 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000860 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600861 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000863 HBasicBlock* GetDominator() const { return dominator_; }
864 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000865 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
866
867 void RemoveDominatedBlock(HBasicBlock* block) {
868 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100869 }
Vladimir Marko60584552015-09-03 13:35:12 +0000870
871 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
872 ReplaceElement(dominated_blocks_, existing, new_block);
873 }
874
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100875 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000876
877 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100878 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000879 }
880
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
882 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100883 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100884 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100885 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
886 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000887
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000888 HInstruction* GetFirstInstructionDisregardMoves() const;
889
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000890 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000891 successors_.push_back(block);
892 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000893 }
894
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100895 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
896 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100897 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000898 new_block->predecessors_.push_back(this);
899 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000900 }
901
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000902 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
903 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000904 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000905 new_block->successors_.push_back(this);
906 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000907 }
908
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100909 // Insert `this` between `predecessor` and `successor. This method
910 // preserves the indicies, and will update the first edge found between
911 // `predecessor` and `successor`.
912 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
913 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100914 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000915 successor->predecessors_[predecessor_index] = this;
916 predecessor->successors_[successor_index] = this;
917 successors_.push_back(successor);
918 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100919 }
920
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100921 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000922 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100923 }
924
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000925 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000926 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000927 }
928
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100929 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000930 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100931 }
932
933 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000934 predecessors_.push_back(block);
935 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100936 }
937
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100938 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000939 DCHECK_EQ(predecessors_.size(), 2u);
940 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100941 }
942
David Brazdil769c9e52015-04-27 13:54:09 +0100943 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000944 DCHECK_EQ(successors_.size(), 2u);
945 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100946 }
947
David Brazdilfc6a86a2015-06-26 10:33:45 +0000948 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000949 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100950 }
951
David Brazdilfc6a86a2015-06-26 10:33:45 +0000952 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000953 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100954 }
955
David Brazdilfc6a86a2015-06-26 10:33:45 +0000956 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000957 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100958 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000959 }
960
961 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000962 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100963 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000964 }
965
966 // Returns whether the first occurrence of `predecessor` in the list of
967 // predecessors is at index `idx`.
968 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100969 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000970 return GetPredecessorIndexOf(predecessor) == idx;
971 }
972
David Brazdild7558da2015-09-22 13:04:14 +0100973 // Create a new block between this block and its predecessors. The new block
974 // is added to the graph, all predecessor edges are relinked to it and an edge
975 // is created to `this`. Returns the new empty block. Reverse post order or
976 // loop and try/catch information are not updated.
977 HBasicBlock* CreateImmediateDominator();
978
David Brazdilfc6a86a2015-06-26 10:33:45 +0000979 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100980 // created, latter block. Note that this method will add the block to the
981 // graph, create a Goto at the end of the former block and will create an edge
982 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100983 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000984 HBasicBlock* SplitBefore(HInstruction* cursor);
985
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000986 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000987 // created block. Note that this method just updates raw block information,
988 // like predecessors, successors, dominators, and instruction list. It does not
989 // update the graph, reverse post order, loop information, nor make sure the
990 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000991 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
992
993 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
994 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000995
David Brazdil9bc43612015-11-05 21:25:24 +0000996 // Split catch block into two blocks after the original move-exception bytecode
997 // instruction, or at the beginning if not present. Returns the newly created,
998 // latter block, or nullptr if such block could not be created (must be dead
999 // in that case). Note that this method just updates raw block information,
1000 // like predecessors, successors, dominators, and instruction list. It does not
1001 // update the graph, reverse post order, loop information, nor make sure the
1002 // blocks are consistent (for example ending with a control flow instruction).
1003 HBasicBlock* SplitCatchBlockAfterMoveException();
1004
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001005 // Merge `other` at the end of `this`. Successors and dominated blocks of
1006 // `other` are changed to be successors and dominated blocks of `this`. Note
1007 // that this method does not update the graph, reverse post order, loop
1008 // information, nor make sure the blocks are consistent (for example ending
1009 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001010 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001011
1012 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1013 // of `this` are moved to `other`.
1014 // Note that this method does not update the graph, reverse post order, loop
1015 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001016 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001017 void ReplaceWith(HBasicBlock* other);
1018
David Brazdil2d7352b2015-04-20 14:52:42 +01001019 // Merge `other` at the end of `this`. This method updates loops, reverse post
1020 // order, links to predecessors, successors, dominators and deletes the block
1021 // from the graph. The two blocks must be successive, i.e. `this` the only
1022 // predecessor of `other` and vice versa.
1023 void MergeWith(HBasicBlock* other);
1024
1025 // Disconnects `this` from all its predecessors, successors and dominator,
1026 // removes it from all loops it is included in and eventually from the graph.
1027 // The block must not dominate any other block. Predecessors and successors
1028 // are safely updated.
1029 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001030
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001032 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001033 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001034 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001035 // Replace instruction `initial` with `replacement` within this block.
1036 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1037 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001038 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001039 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001040 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001041 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1042 // instruction list. With 'ensure_safety' set to true, it verifies that the
1043 // instruction is not in use and removes it from the use lists of its inputs.
1044 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1045 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001046 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001047
1048 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001049 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001050 }
1051
Roland Levillain6b879dd2014-09-22 17:13:44 +01001052 bool IsLoopPreHeaderFirstPredecessor() const {
1053 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001054 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001055 }
1056
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001057 bool IsFirstPredecessorBackEdge() const {
1058 DCHECK(IsLoopHeader());
1059 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1060 }
1061
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001062 HLoopInformation* GetLoopInformation() const {
1063 return loop_information_;
1064 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001066 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001067 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001068 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001069 void SetInLoop(HLoopInformation* info) {
1070 if (IsLoopHeader()) {
1071 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001072 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001073 loop_information_ = info;
1074 } else if (loop_information_->Contains(*info->GetHeader())) {
1075 // Block is currently part of an outer loop. Make it part of this inner loop.
1076 // Note that a non loop header having a loop information means this loop information
1077 // has already been populated
1078 loop_information_ = info;
1079 } else {
1080 // Block is part of an inner loop. Do not update the loop information.
1081 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1082 // at this point, because this method is being called while populating `info`.
1083 }
1084 }
1085
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001086 // Raw update of the loop information.
1087 void SetLoopInformation(HLoopInformation* info) {
1088 loop_information_ = info;
1089 }
1090
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001091 bool IsInLoop() const { return loop_information_ != nullptr; }
1092
David Brazdilec16f792015-08-19 15:04:01 +01001093 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1094
1095 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1096 try_catch_information_ = try_catch_information;
1097 }
1098
1099 bool IsTryBlock() const {
1100 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1101 }
1102
1103 bool IsCatchBlock() const {
1104 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1105 }
David Brazdilffee3d32015-07-06 11:48:53 +01001106
1107 // Returns the try entry that this block's successors should have. They will
1108 // be in the same try, unless the block ends in a try boundary. In that case,
1109 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001110 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001111
David Brazdild7558da2015-09-22 13:04:14 +01001112 bool HasThrowingInstructions() const;
1113
David Brazdila4b8c212015-05-07 09:59:30 +01001114 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001115 bool Dominates(HBasicBlock* block) const;
1116
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001117 size_t GetLifetimeStart() const { return lifetime_start_; }
1118 size_t GetLifetimeEnd() const { return lifetime_end_; }
1119
1120 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1121 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1122
David Brazdil8d5b8b22015-03-24 10:51:52 +00001123 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001124 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001125 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001126 bool HasSinglePhi() const;
1127
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001128 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001129 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001130 ArenaVector<HBasicBlock*> predecessors_;
1131 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001132 HInstructionList instructions_;
1133 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001134 HLoopInformation* loop_information_;
1135 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001136 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001137 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001138 // The dex program counter of the first instruction of this block.
1139 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001140 size_t lifetime_start_;
1141 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001142 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001143
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001144 friend class HGraph;
1145 friend class HInstruction;
1146
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001147 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1148};
1149
David Brazdilb2bd1c52015-03-25 11:17:37 +00001150// Iterates over the LoopInformation of all loops which contain 'block'
1151// from the innermost to the outermost.
1152class HLoopInformationOutwardIterator : public ValueObject {
1153 public:
1154 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1155 : current_(block.GetLoopInformation()) {}
1156
1157 bool Done() const { return current_ == nullptr; }
1158
1159 void Advance() {
1160 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001161 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001162 }
1163
1164 HLoopInformation* Current() const {
1165 DCHECK(!Done());
1166 return current_;
1167 }
1168
1169 private:
1170 HLoopInformation* current_;
1171
1172 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1173};
1174
Alexandre Ramesef20f712015-06-09 10:29:30 +01001175#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001176 M(Above, Condition) \
1177 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001179 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001180 M(ArrayGet, Instruction) \
1181 M(ArrayLength, Instruction) \
1182 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001183 M(Below, Condition) \
1184 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001185 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001186 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001187 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001188 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001189 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001190 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001191 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001192 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001193 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001194 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001195 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001196 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001197 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001198 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(Exit, Instruction) \
1200 M(FloatConstant, Constant) \
1201 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001202 M(GreaterThan, Condition) \
1203 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001205 M(InstanceFieldGet, Instruction) \
1206 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001207 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001209 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001210 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001211 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001212 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001213 M(LessThan, Condition) \
1214 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001215 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001216 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001217 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001218 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(Local, Instruction) \
1220 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001221 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001222 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001223 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001224 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(Neg, UnaryOperation) \
1226 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001227 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001228 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001230 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001232 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001233 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001234 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001235 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001237 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(Return, Instruction) \
1239 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001240 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001241 M(Shl, BinaryOperation) \
1242 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001243 M(StaticFieldGet, Instruction) \
1244 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 M(UnresolvedInstanceFieldGet, Instruction) \
1246 M(UnresolvedInstanceFieldSet, Instruction) \
1247 M(UnresolvedStaticFieldGet, Instruction) \
1248 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001249 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(StoreLocal, Instruction) \
1251 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001252 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001253 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001254 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001255 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001256 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001257 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001258
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001259/*
1260 * Instructions, shared across several (not all) architectures.
1261 */
1262#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1263#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1264#else
1265#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
1266 M(MultiplyAccumulate, Instruction)
1267#endif
1268
Vladimir Markob4536b72015-11-24 13:45:23 +00001269#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001270#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001271#else
1272#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1273 M(ArmDexCacheArraysBase, Instruction)
1274#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001275
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001276#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001277#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001278#else
1279#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001280 M(Arm64BitwiseNegatedRight, Instruction) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001281 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001282 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001283#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001284
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001285#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1286
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001287#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1288
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001289#ifndef ART_ENABLE_CODEGEN_x86
1290#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1291#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001292#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1293 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001294 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001295 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001296 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001297#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001298
1299#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1300
1301#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1302 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001303 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001304 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1305 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001306 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001307 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001308 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1309 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1310
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001311#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1312 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001313 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001314 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001315 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001316 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001317
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001318#define FOR_EACH_INSTRUCTION(M) \
1319 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1320 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1321
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001323FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1324#undef FORWARD_DECLARATION
1325
Roland Levillainccc07a92014-09-16 14:48:16 +01001326#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001327 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001328 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001329 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001330 return other->Is##type(); \
1331 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001332 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001333
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001334#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1335 bool Is##type() const { return As##type() != nullptr; } \
1336 const H##type* As##type() const { return this; } \
1337 H##type* As##type() { return this; }
1338
David Brazdiled596192015-01-23 10:39:45 +00001339template <typename T> class HUseList;
1340
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001341template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001342class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001343 public:
David Brazdiled596192015-01-23 10:39:45 +00001344 HUseListNode* GetPrevious() const { return prev_; }
1345 HUseListNode* GetNext() const { return next_; }
1346 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001347 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001348 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001349
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001350 private:
David Brazdiled596192015-01-23 10:39:45 +00001351 HUseListNode(T user, size_t index)
1352 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1353
1354 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001355 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001356 HUseListNode<T>* prev_;
1357 HUseListNode<T>* next_;
1358
1359 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001360
1361 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1362};
1363
David Brazdiled596192015-01-23 10:39:45 +00001364template <typename T>
1365class HUseList : public ValueObject {
1366 public:
1367 HUseList() : first_(nullptr) {}
1368
1369 void Clear() {
1370 first_ = nullptr;
1371 }
1372
1373 // Adds a new entry at the beginning of the use list and returns
1374 // the newly created node.
1375 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001376 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001377 if (IsEmpty()) {
1378 first_ = new_node;
1379 } else {
1380 first_->prev_ = new_node;
1381 new_node->next_ = first_;
1382 first_ = new_node;
1383 }
1384 return new_node;
1385 }
1386
1387 HUseListNode<T>* GetFirst() const {
1388 return first_;
1389 }
1390
1391 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001392 DCHECK(node != nullptr);
1393 DCHECK(Contains(node));
1394
David Brazdiled596192015-01-23 10:39:45 +00001395 if (node->prev_ != nullptr) {
1396 node->prev_->next_ = node->next_;
1397 }
1398 if (node->next_ != nullptr) {
1399 node->next_->prev_ = node->prev_;
1400 }
1401 if (node == first_) {
1402 first_ = node->next_;
1403 }
1404 }
1405
David Brazdil1abb4192015-02-17 18:33:36 +00001406 bool Contains(const HUseListNode<T>* node) const {
1407 if (node == nullptr) {
1408 return false;
1409 }
1410 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1411 if (current == node) {
1412 return true;
1413 }
1414 }
1415 return false;
1416 }
1417
David Brazdiled596192015-01-23 10:39:45 +00001418 bool IsEmpty() const {
1419 return first_ == nullptr;
1420 }
1421
1422 bool HasOnlyOneUse() const {
1423 return first_ != nullptr && first_->next_ == nullptr;
1424 }
1425
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001426 size_t SizeSlow() const {
1427 size_t count = 0;
1428 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1429 ++count;
1430 }
1431 return count;
1432 }
1433
David Brazdiled596192015-01-23 10:39:45 +00001434 private:
1435 HUseListNode<T>* first_;
1436};
1437
1438template<typename T>
1439class HUseIterator : public ValueObject {
1440 public:
1441 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1442
1443 bool Done() const { return current_ == nullptr; }
1444
1445 void Advance() {
1446 DCHECK(!Done());
1447 current_ = current_->GetNext();
1448 }
1449
1450 HUseListNode<T>* Current() const {
1451 DCHECK(!Done());
1452 return current_;
1453 }
1454
1455 private:
1456 HUseListNode<T>* current_;
1457
1458 friend class HValue;
1459};
1460
David Brazdil1abb4192015-02-17 18:33:36 +00001461// This class is used by HEnvironment and HInstruction classes to record the
1462// instructions they use and pointers to the corresponding HUseListNodes kept
1463// by the used instructions.
1464template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001465class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001466 public:
1467 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1468 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1469
1470 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1471 : instruction_(old_record.instruction_), use_node_(use_node) {
1472 DCHECK(instruction_ != nullptr);
1473 DCHECK(use_node_ != nullptr);
1474 DCHECK(old_record.use_node_ == nullptr);
1475 }
1476
1477 HInstruction* GetInstruction() const { return instruction_; }
1478 HUseListNode<T>* GetUseNode() const { return use_node_; }
1479
1480 private:
1481 // Instruction used by the user.
1482 HInstruction* instruction_;
1483
1484 // Corresponding entry in the use list kept by 'instruction_'.
1485 HUseListNode<T>* use_node_;
1486};
1487
Aart Bik854a02b2015-07-14 16:07:00 -07001488/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001489 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001490 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001491 * For write/read dependences on fields/arrays, the dependence analysis uses
1492 * type disambiguation (e.g. a float field write cannot modify the value of an
1493 * integer field read) and the access type (e.g. a reference array write cannot
1494 * modify the value of a reference field read [although it may modify the
1495 * reference fetch prior to reading the field, which is represented by its own
1496 * write/read dependence]). The analysis makes conservative points-to
1497 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1498 * the same, and any reference read depends on any reference read without
1499 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001500 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001501 * The internal representation uses 38-bit and is described in the table below.
1502 * The first line indicates the side effect, and for field/array accesses the
1503 * second line indicates the type of the access (in the order of the
1504 * Primitive::Type enum).
1505 * The two numbered lines below indicate the bit position in the bitfield (read
1506 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001507 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001508 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1509 * +-------------+---------+---------+--------------+---------+---------+
1510 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1511 * | 3 |333333322|222222221| 1 |111111110|000000000|
1512 * | 7 |654321098|765432109| 8 |765432109|876543210|
1513 *
1514 * Note that, to ease the implementation, 'changes' bits are least significant
1515 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001516 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001517class SideEffects : public ValueObject {
1518 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001519 SideEffects() : flags_(0) {}
1520
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001521 static SideEffects None() {
1522 return SideEffects(0);
1523 }
1524
1525 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001526 return SideEffects(kAllChangeBits | kAllDependOnBits);
1527 }
1528
1529 static SideEffects AllChanges() {
1530 return SideEffects(kAllChangeBits);
1531 }
1532
1533 static SideEffects AllDependencies() {
1534 return SideEffects(kAllDependOnBits);
1535 }
1536
1537 static SideEffects AllExceptGCDependency() {
1538 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1539 }
1540
1541 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001542 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001543 }
1544
Aart Bik34c3ba92015-07-20 14:08:59 -07001545 static SideEffects AllWrites() {
1546 return SideEffects(kAllWrites);
1547 }
1548
1549 static SideEffects AllReads() {
1550 return SideEffects(kAllReads);
1551 }
1552
1553 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1554 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001555 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001556 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001557 }
1558
Aart Bik854a02b2015-07-14 16:07:00 -07001559 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1560 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001561 }
1562
Aart Bik34c3ba92015-07-20 14:08:59 -07001563 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1564 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001565 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001566 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001567 }
1568
1569 static SideEffects ArrayReadOfType(Primitive::Type type) {
1570 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1571 }
1572
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001573 static SideEffects CanTriggerGC() {
1574 return SideEffects(1ULL << kCanTriggerGCBit);
1575 }
1576
1577 static SideEffects DependsOnGC() {
1578 return SideEffects(1ULL << kDependsOnGCBit);
1579 }
1580
Aart Bik854a02b2015-07-14 16:07:00 -07001581 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001582 SideEffects Union(SideEffects other) const {
1583 return SideEffects(flags_ | other.flags_);
1584 }
1585
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 SideEffects Exclusion(SideEffects other) const {
1587 return SideEffects(flags_ & ~other.flags_);
1588 }
1589
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001590 void Add(SideEffects other) {
1591 flags_ |= other.flags_;
1592 }
1593
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001594 bool Includes(SideEffects other) const {
1595 return (other.flags_ & flags_) == other.flags_;
1596 }
1597
1598 bool HasSideEffects() const {
1599 return (flags_ & kAllChangeBits);
1600 }
1601
1602 bool HasDependencies() const {
1603 return (flags_ & kAllDependOnBits);
1604 }
1605
1606 // Returns true if there are no side effects or dependencies.
1607 bool DoesNothing() const {
1608 return flags_ == 0;
1609 }
1610
Aart Bik854a02b2015-07-14 16:07:00 -07001611 // Returns true if something is written.
1612 bool DoesAnyWrite() const {
1613 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001614 }
1615
Aart Bik854a02b2015-07-14 16:07:00 -07001616 // Returns true if something is read.
1617 bool DoesAnyRead() const {
1618 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001619 }
1620
Aart Bik854a02b2015-07-14 16:07:00 -07001621 // Returns true if potentially everything is written and read
1622 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001623 bool DoesAllReadWrite() const {
1624 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1625 }
1626
Aart Bik854a02b2015-07-14 16:07:00 -07001627 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001629 }
1630
Roland Levillain0d5a2812015-11-13 10:07:31 +00001631 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001632 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001633 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1634 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001635 }
1636
1637 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001638 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001639 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001640 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001641 for (int s = kLastBit; s >= 0; s--) {
1642 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1643 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1644 // This is a bit for the GC side effect.
1645 if (current_bit_is_set) {
1646 flags += "GC";
1647 }
Aart Bik854a02b2015-07-14 16:07:00 -07001648 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001649 } else {
1650 // This is a bit for the array/field analysis.
1651 // The underscore character stands for the 'can trigger GC' bit.
1652 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1653 if (current_bit_is_set) {
1654 flags += kDebug[s];
1655 }
1656 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1657 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1658 flags += "|";
1659 }
1660 }
Aart Bik854a02b2015-07-14 16:07:00 -07001661 }
1662 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001663 }
1664
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001665 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001666
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001667 private:
1668 static constexpr int kFieldArrayAnalysisBits = 9;
1669
1670 static constexpr int kFieldWriteOffset = 0;
1671 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1672 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1673 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1674
1675 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1676
1677 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1678 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1679 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1680 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1681
1682 static constexpr int kLastBit = kDependsOnGCBit;
1683 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1684
1685 // Aliases.
1686
1687 static_assert(kChangeBits == kDependOnBits,
1688 "the 'change' bits should match the 'depend on' bits.");
1689
1690 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1691 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1692 static constexpr uint64_t kAllWrites =
1693 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1694 static constexpr uint64_t kAllReads =
1695 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001696
Aart Bik854a02b2015-07-14 16:07:00 -07001697 // Work around the fact that HIR aliases I/F and J/D.
1698 // TODO: remove this interceptor once HIR types are clean
1699 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1700 switch (type) {
1701 case Primitive::kPrimInt:
1702 case Primitive::kPrimFloat:
1703 return TypeFlag(Primitive::kPrimInt, offset) |
1704 TypeFlag(Primitive::kPrimFloat, offset);
1705 case Primitive::kPrimLong:
1706 case Primitive::kPrimDouble:
1707 return TypeFlag(Primitive::kPrimLong, offset) |
1708 TypeFlag(Primitive::kPrimDouble, offset);
1709 default:
1710 return TypeFlag(type, offset);
1711 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001712 }
1713
Aart Bik854a02b2015-07-14 16:07:00 -07001714 // Translates type to bit flag.
1715 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1716 CHECK_NE(type, Primitive::kPrimVoid);
1717 const uint64_t one = 1;
1718 const int shift = type; // 0-based consecutive enum
1719 DCHECK_LE(kFieldWriteOffset, shift);
1720 DCHECK_LT(shift, kArrayWriteOffset);
1721 return one << (type + offset);
1722 }
1723
1724 // Private constructor on direct flags value.
1725 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1726
1727 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001728};
1729
David Brazdiled596192015-01-23 10:39:45 +00001730// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001731class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001732 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001733 HEnvironment(ArenaAllocator* arena,
1734 size_t number_of_vregs,
1735 const DexFile& dex_file,
1736 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001737 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001738 InvokeType invoke_type,
1739 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001740 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1741 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001742 parent_(nullptr),
1743 dex_file_(dex_file),
1744 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001745 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001746 invoke_type_(invoke_type),
1747 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001748 }
1749
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001750 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001751 : HEnvironment(arena,
1752 to_copy.Size(),
1753 to_copy.GetDexFile(),
1754 to_copy.GetMethodIdx(),
1755 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001756 to_copy.GetInvokeType(),
1757 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001758
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001759 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001760 if (parent_ != nullptr) {
1761 parent_->SetAndCopyParentChain(allocator, parent);
1762 } else {
1763 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1764 parent_->CopyFrom(parent);
1765 if (parent->GetParent() != nullptr) {
1766 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1767 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001768 }
David Brazdiled596192015-01-23 10:39:45 +00001769 }
1770
Vladimir Marko71bf8092015-09-15 15:33:14 +01001771 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001772 void CopyFrom(HEnvironment* environment);
1773
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001774 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1775 // input to the loop phi instead. This is for inserting instructions that
1776 // require an environment (like HDeoptimization) in the loop pre-header.
1777 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001778
1779 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001780 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001781 }
1782
1783 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001784 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001785 }
1786
David Brazdil1abb4192015-02-17 18:33:36 +00001787 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001788
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001789 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001790
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001791 HEnvironment* GetParent() const { return parent_; }
1792
1793 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001794 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001795 }
1796
1797 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001798 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001799 }
1800
1801 uint32_t GetDexPc() const {
1802 return dex_pc_;
1803 }
1804
1805 uint32_t GetMethodIdx() const {
1806 return method_idx_;
1807 }
1808
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001809 InvokeType GetInvokeType() const {
1810 return invoke_type_;
1811 }
1812
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001813 const DexFile& GetDexFile() const {
1814 return dex_file_;
1815 }
1816
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001817 HInstruction* GetHolder() const {
1818 return holder_;
1819 }
1820
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001821
1822 bool IsFromInlinedInvoke() const {
1823 return GetParent() != nullptr;
1824 }
1825
David Brazdiled596192015-01-23 10:39:45 +00001826 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001827 // Record instructions' use entries of this environment for constant-time removal.
1828 // It should only be called by HInstruction when a new environment use is added.
1829 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1830 DCHECK(env_use->GetUser() == this);
1831 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001832 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001833 }
David Brazdiled596192015-01-23 10:39:45 +00001834
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001835 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1836 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001837 HEnvironment* parent_;
1838 const DexFile& dex_file_;
1839 const uint32_t method_idx_;
1840 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001841 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001842
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001843 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001844 HInstruction* const holder_;
1845
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001846 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001847
1848 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1849};
1850
Vladimir Markof9f64412015-09-02 14:05:49 +01001851class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001852 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001853 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001854 : previous_(nullptr),
1855 next_(nullptr),
1856 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001857 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001858 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001859 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001860 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001861 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001862 locations_(nullptr),
1863 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001864 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001865 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001866 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1867 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1868 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001869
Dave Allison20dfc792014-06-16 20:44:29 -07001870 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001871
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001872#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001873 enum InstructionKind {
1874 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1875 };
1876#undef DECLARE_KIND
1877
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001878 HInstruction* GetNext() const { return next_; }
1879 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001880
Calin Juravle77520bc2015-01-12 18:45:46 +00001881 HInstruction* GetNextDisregardingMoves() const;
1882 HInstruction* GetPreviousDisregardingMoves() const;
1883
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001884 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001885 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001886 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001887 bool IsInBlock() const { return block_ != nullptr; }
1888 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001889 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1890 bool IsIrreducibleLoopHeaderPhi() const {
1891 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1892 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001893
Roland Levillain6b879dd2014-09-22 17:13:44 +01001894 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001895 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001896
1897 virtual void Accept(HGraphVisitor* visitor) = 0;
1898 virtual const char* DebugName() const = 0;
1899
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001900 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001901 void SetRawInputAt(size_t index, HInstruction* input) {
1902 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1903 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001904
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001905 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001906
1907 uint32_t GetDexPc() const { return dex_pc_; }
1908
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001909 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001910
Roland Levillaine161a2a2014-10-03 12:45:18 +01001911 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001912 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001913
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001914 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001915 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001916
Calin Juravle10e244f2015-01-26 18:54:32 +00001917 // Does not apply for all instructions, but having this at top level greatly
1918 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001919 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001920 virtual bool CanBeNull() const {
1921 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1922 return true;
1923 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001924
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001925 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001926 return false;
1927 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001928
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001929 virtual bool IsActualObject() const {
1930 return GetType() == Primitive::kPrimNot;
1931 }
1932
Calin Juravle2e768302015-07-28 14:41:11 +00001933 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001934
Calin Juravle61d544b2015-02-23 16:46:57 +00001935 ReferenceTypeInfo GetReferenceTypeInfo() const {
1936 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001937 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
1938 GetPackedFlag<kFlagReferenceTypeIsExact>());;
Calin Juravle61d544b2015-02-23 16:46:57 +00001939 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001940
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001941 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001942 DCHECK(user != nullptr);
1943 HUseListNode<HInstruction*>* use =
1944 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1945 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001946 }
1947
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001948 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001949 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001950 HUseListNode<HEnvironment*>* env_use =
1951 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1952 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001953 }
1954
David Brazdil1abb4192015-02-17 18:33:36 +00001955 void RemoveAsUserOfInput(size_t input) {
1956 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1957 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1958 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959
David Brazdil1abb4192015-02-17 18:33:36 +00001960 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1961 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001962
David Brazdiled596192015-01-23 10:39:45 +00001963 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1964 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001965 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001966 bool HasOnlyOneNonEnvironmentUse() const {
1967 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1968 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001969
Roland Levillain6c82d402014-10-13 16:10:27 +01001970 // Does this instruction strictly dominate `other_instruction`?
1971 // Returns false if this instruction and `other_instruction` are the same.
1972 // Aborts if this instruction and `other_instruction` are both phis.
1973 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001974
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001975 int GetId() const { return id_; }
1976 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001977
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001978 int GetSsaIndex() const { return ssa_index_; }
1979 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1980 bool HasSsaIndex() const { return ssa_index_ != -1; }
1981
1982 bool HasEnvironment() const { return environment_ != nullptr; }
1983 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001984 // Set the `environment_` field. Raw because this method does not
1985 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001986 void SetRawEnvironment(HEnvironment* environment) {
1987 DCHECK(environment_ == nullptr);
1988 DCHECK_EQ(environment->GetHolder(), this);
1989 environment_ = environment;
1990 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001991
1992 // Set the environment of this instruction, copying it from `environment`. While
1993 // copying, the uses lists are being updated.
1994 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001995 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001996 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001997 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001998 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001999 if (environment->GetParent() != nullptr) {
2000 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2001 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002002 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002004 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2005 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002006 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002007 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002008 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002009 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002010 if (environment->GetParent() != nullptr) {
2011 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2012 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002013 }
2014
Nicolas Geoffray39468442014-09-02 15:17:15 +01002015 // Returns the number of entries in the environment. Typically, that is the
2016 // number of dex registers in a method. It could be more in case of inlining.
2017 size_t EnvironmentSize() const;
2018
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002019 LocationSummary* GetLocations() const { return locations_; }
2020 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002021
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002022 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002023 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002024
Alexandre Rames188d4312015-04-09 18:30:21 +01002025 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2026 // uses of this instruction by `other` are *not* updated.
2027 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2028 ReplaceWith(other);
2029 other->ReplaceInput(this, use_index);
2030 }
2031
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002032 // Move `this` instruction before `cursor`.
2033 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002034
Vladimir Markofb337ea2015-11-25 15:25:10 +00002035 // Move `this` before its first user and out of any loops. If there is no
2036 // out-of-loop user that dominates all other users, move the instruction
2037 // to the end of the out-of-loop common dominator of the user's blocks.
2038 //
2039 // This can be used only on non-throwing instructions with no side effects that
2040 // have at least one use but no environment uses.
2041 void MoveBeforeFirstUserAndOutOfLoops();
2042
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002043#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002044 bool Is##type() const; \
2045 const H##type* As##type() const; \
2046 H##type* As##type();
2047
2048 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2049#undef INSTRUCTION_TYPE_CHECK
2050
2051#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002052 bool Is##type() const { return (As##type() != nullptr); } \
2053 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002054 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002055 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002056#undef INSTRUCTION_TYPE_CHECK
2057
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002058 // Returns whether the instruction can be moved within the graph.
2059 virtual bool CanBeMoved() const { return false; }
2060
2061 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002062 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002063 return false;
2064 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002065
2066 // Returns whether any data encoded in the two instructions is equal.
2067 // This method does not look at the inputs. Both instructions must be
2068 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002069 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002070 return false;
2071 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002072
2073 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002074 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002075 // 2) Their inputs are identical.
2076 bool Equals(HInstruction* other) const;
2077
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002078 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2079 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2080 // the virtual function because the __attribute__((__pure__)) doesn't really
2081 // apply the strong requirement for virtual functions, preventing optimizations.
2082 InstructionKind GetKind() const PURE;
2083 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002084
2085 virtual size_t ComputeHashCode() const {
2086 size_t result = GetKind();
2087 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2088 result = (result * 31) + InputAt(i)->GetId();
2089 }
2090 return result;
2091 }
2092
2093 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002094 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002095 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002096
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002097 size_t GetLifetimePosition() const { return lifetime_position_; }
2098 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2099 LiveInterval* GetLiveInterval() const { return live_interval_; }
2100 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2101 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2102
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002103 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2104
2105 // Returns whether the code generation of the instruction will require to have access
2106 // to the current method. Such instructions are:
2107 // (1): Instructions that require an environment, as calling the runtime requires
2108 // to walk the stack and have the current method stored at a specific stack address.
2109 // (2): Object literals like classes and strings, that are loaded from the dex cache
2110 // fields of the current method.
2111 bool NeedsCurrentMethod() const {
2112 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2113 }
2114
Vladimir Markodc151b22015-10-15 18:02:30 +01002115 // Returns whether the code generation of the instruction will require to have access
2116 // to the dex cache of the current method's declaring class via the current method.
2117 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002118
Mark Mendellc4701932015-04-10 13:18:51 -04002119 // Does this instruction have any use in an environment before
2120 // control flow hits 'other'?
2121 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2122
2123 // Remove all references to environment uses of this instruction.
2124 // The caller must ensure that this is safe to do.
2125 void RemoveEnvironmentUsers();
2126
Vladimir Markoa1de9182016-02-25 11:37:38 +00002127 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2128 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002129
David Brazdil1abb4192015-02-17 18:33:36 +00002130 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002131 // If set, the machine code for this instruction is assumed to be generated by
2132 // its users. Used by liveness analysis to compute use positions accordingly.
2133 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2134 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2135 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2136 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2137
David Brazdil1abb4192015-02-17 18:33:36 +00002138 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2139 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2140
Vladimir Markoa1de9182016-02-25 11:37:38 +00002141 uint32_t GetPackedFields() const {
2142 return packed_fields_;
2143 }
2144
2145 template <size_t flag>
2146 bool GetPackedFlag() const {
2147 return (packed_fields_ & (1u << flag)) != 0u;
2148 }
2149
2150 template <size_t flag>
2151 void SetPackedFlag(bool value = true) {
2152 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2153 }
2154
2155 template <typename BitFieldType>
2156 typename BitFieldType::value_type GetPackedField() const {
2157 return BitFieldType::Decode(packed_fields_);
2158 }
2159
2160 template <typename BitFieldType>
2161 void SetPackedField(typename BitFieldType::value_type value) {
2162 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2163 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2164 }
2165
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002166 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002167 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2168
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002169 HInstruction* previous_;
2170 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002171 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002172 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002174 // An instruction gets an id when it is added to the graph.
2175 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002176 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002177 int id_;
2178
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002179 // When doing liveness analysis, instructions that have uses get an SSA index.
2180 int ssa_index_;
2181
Vladimir Markoa1de9182016-02-25 11:37:38 +00002182 // Packed fields.
2183 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002184
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002186 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187
2188 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002189 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002190
Nicolas Geoffray39468442014-09-02 15:17:15 +01002191 // The environment associated with this instruction. Not null if the instruction
2192 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002194
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002195 // Set by the code generator.
2196 LocationSummary* locations_;
2197
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002198 // Set by the liveness analysis.
2199 LiveInterval* live_interval_;
2200
2201 // Set by the liveness analysis, this is the position in a linear
2202 // order of blocks where this instruction's live interval start.
2203 size_t lifetime_position_;
2204
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002205 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002206
Vladimir Markoa1de9182016-02-25 11:37:38 +00002207 // The reference handle part of the reference type info.
2208 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002209 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002210 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002211
David Brazdil1abb4192015-02-17 18:33:36 +00002212 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002214 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002215 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002216 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217
2218 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2219};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002220std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002222class HInputIterator : public ValueObject {
2223 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002224 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002225
2226 bool Done() const { return index_ == instruction_->InputCount(); }
2227 HInstruction* Current() const { return instruction_->InputAt(index_); }
2228 void Advance() { index_++; }
2229
2230 private:
2231 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002232 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002233
2234 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2235};
2236
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002237class HInstructionIterator : public ValueObject {
2238 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002239 explicit HInstructionIterator(const HInstructionList& instructions)
2240 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002241 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242 }
2243
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002244 bool Done() const { return instruction_ == nullptr; }
2245 HInstruction* Current() const { return instruction_; }
2246 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002247 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002248 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002249 }
2250
2251 private:
2252 HInstruction* instruction_;
2253 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002254
2255 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256};
2257
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002258class HBackwardInstructionIterator : public ValueObject {
2259 public:
2260 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2261 : instruction_(instructions.last_instruction_) {
2262 next_ = Done() ? nullptr : instruction_->GetPrevious();
2263 }
2264
2265 bool Done() const { return instruction_ == nullptr; }
2266 HInstruction* Current() const { return instruction_; }
2267 void Advance() {
2268 instruction_ = next_;
2269 next_ = Done() ? nullptr : instruction_->GetPrevious();
2270 }
2271
2272 private:
2273 HInstruction* instruction_;
2274 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002275
2276 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002277};
2278
Vladimir Markof9f64412015-09-02 14:05:49 +01002279template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002280class HTemplateInstruction: public HInstruction {
2281 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002282 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002283 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002284 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002285
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002286 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002287
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002288 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002289 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2290 DCHECK_LT(i, N);
2291 return inputs_[i];
2292 }
David Brazdil1abb4192015-02-17 18:33:36 +00002293
2294 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002295 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002296 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002297 }
2298
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002300 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002301
2302 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303};
2304
Vladimir Markof9f64412015-09-02 14:05:49 +01002305// HTemplateInstruction specialization for N=0.
2306template<>
2307class HTemplateInstruction<0>: public HInstruction {
2308 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002309 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002310 : HInstruction(side_effects, dex_pc) {}
2311
Vladimir Markof9f64412015-09-02 14:05:49 +01002312 virtual ~HTemplateInstruction() {}
2313
2314 size_t InputCount() const OVERRIDE { return 0; }
2315
2316 protected:
2317 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2318 LOG(FATAL) << "Unreachable";
2319 UNREACHABLE();
2320 }
2321
2322 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2323 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2324 LOG(FATAL) << "Unreachable";
2325 UNREACHABLE();
2326 }
2327
2328 private:
2329 friend class SsaBuilder;
2330};
2331
Dave Allison20dfc792014-06-16 20:44:29 -07002332template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002333class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002334 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002335 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002336 : HTemplateInstruction<N>(side_effects, dex_pc) {
2337 this->template SetPackedField<TypeField>(type);
2338 }
Dave Allison20dfc792014-06-16 20:44:29 -07002339 virtual ~HExpression() {}
2340
Vladimir Markoa1de9182016-02-25 11:37:38 +00002341 Primitive::Type GetType() const OVERRIDE {
2342 return TypeField::Decode(this->GetPackedFields());
2343 }
Dave Allison20dfc792014-06-16 20:44:29 -07002344
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002345 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002346 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2347 static constexpr size_t kFieldTypeSize =
2348 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2349 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2350 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2351 "Too many packed fields.");
2352 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002353};
2354
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002355// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2356// instruction that branches to the exit block.
2357class HReturnVoid : public HTemplateInstruction<0> {
2358 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002359 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2360 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002362 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002363
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002364 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002365
2366 private:
2367 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2368};
2369
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002370// Represents dex's RETURN opcodes. A HReturn is a control flow
2371// instruction that branches to the exit block.
2372class HReturn : public HTemplateInstruction<1> {
2373 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002374 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2375 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002376 SetRawInputAt(0, value);
2377 }
2378
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002379 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002380
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002381 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002382
2383 private:
2384 DISALLOW_COPY_AND_ASSIGN(HReturn);
2385};
2386
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002387// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002388// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002389// exit block.
2390class HExit : public HTemplateInstruction<0> {
2391 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002392 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002393
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002394 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002395
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002396 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002397
2398 private:
2399 DISALLOW_COPY_AND_ASSIGN(HExit);
2400};
2401
2402// Jumps from one block to another.
2403class HGoto : public HTemplateInstruction<0> {
2404 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002405 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002406
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002407 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002408
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002409 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002410 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002411 }
2412
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002413 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002414
2415 private:
2416 DISALLOW_COPY_AND_ASSIGN(HGoto);
2417};
2418
Roland Levillain9867bc72015-08-05 10:21:34 +01002419class HConstant : public HExpression<0> {
2420 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002421 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2422 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002423
2424 bool CanBeMoved() const OVERRIDE { return true; }
2425
2426 virtual bool IsMinusOne() const { return false; }
2427 virtual bool IsZero() const { return false; }
2428 virtual bool IsOne() const { return false; }
2429
David Brazdil77a48ae2015-09-15 12:34:04 +00002430 virtual uint64_t GetValueAsUint64() const = 0;
2431
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002432 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002433
2434 private:
2435 DISALLOW_COPY_AND_ASSIGN(HConstant);
2436};
2437
2438class HNullConstant : public HConstant {
2439 public:
2440 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2441 return true;
2442 }
2443
David Brazdil77a48ae2015-09-15 12:34:04 +00002444 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2445
Roland Levillain9867bc72015-08-05 10:21:34 +01002446 size_t ComputeHashCode() const OVERRIDE { return 0; }
2447
2448 DECLARE_INSTRUCTION(NullConstant);
2449
2450 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002451 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002452
2453 friend class HGraph;
2454 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2455};
2456
2457// Constants of the type int. Those can be from Dex instructions, or
2458// synthesized (for example with the if-eqz instruction).
2459class HIntConstant : public HConstant {
2460 public:
2461 int32_t GetValue() const { return value_; }
2462
David Brazdil9f389d42015-10-01 14:32:56 +01002463 uint64_t GetValueAsUint64() const OVERRIDE {
2464 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2465 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002466
Roland Levillain9867bc72015-08-05 10:21:34 +01002467 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002468 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002469 return other->AsIntConstant()->value_ == value_;
2470 }
2471
2472 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2473
2474 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2475 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2476 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2477
2478 DECLARE_INSTRUCTION(IntConstant);
2479
2480 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002481 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2482 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2483 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2484 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002485
2486 const int32_t value_;
2487
2488 friend class HGraph;
2489 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2490 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2491 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2492};
2493
2494class HLongConstant : public HConstant {
2495 public:
2496 int64_t GetValue() const { return value_; }
2497
David Brazdil77a48ae2015-09-15 12:34:04 +00002498 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2499
Roland Levillain9867bc72015-08-05 10:21:34 +01002500 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002501 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002502 return other->AsLongConstant()->value_ == value_;
2503 }
2504
2505 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2506
2507 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2508 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2509 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2510
2511 DECLARE_INSTRUCTION(LongConstant);
2512
2513 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002514 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2515 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002516
2517 const int64_t value_;
2518
2519 friend class HGraph;
2520 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2521};
Dave Allison20dfc792014-06-16 20:44:29 -07002522
Roland Levillain31dd3d62016-02-16 12:21:02 +00002523class HFloatConstant : public HConstant {
2524 public:
2525 float GetValue() const { return value_; }
2526
2527 uint64_t GetValueAsUint64() const OVERRIDE {
2528 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2529 }
2530
2531 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2532 DCHECK(other->IsFloatConstant()) << other->DebugName();
2533 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2534 }
2535
2536 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2537
2538 bool IsMinusOne() const OVERRIDE {
2539 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2540 }
2541 bool IsZero() const OVERRIDE {
2542 return value_ == 0.0f;
2543 }
2544 bool IsOne() const OVERRIDE {
2545 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2546 }
2547 bool IsNaN() const {
2548 return std::isnan(value_);
2549 }
2550
2551 DECLARE_INSTRUCTION(FloatConstant);
2552
2553 private:
2554 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2555 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2556 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2557 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2558
2559 const float value_;
2560
2561 // Only the SsaBuilder and HGraph can create floating-point constants.
2562 friend class SsaBuilder;
2563 friend class HGraph;
2564 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2565};
2566
2567class HDoubleConstant : public HConstant {
2568 public:
2569 double GetValue() const { return value_; }
2570
2571 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2572
2573 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2574 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2575 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2576 }
2577
2578 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2579
2580 bool IsMinusOne() const OVERRIDE {
2581 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2582 }
2583 bool IsZero() const OVERRIDE {
2584 return value_ == 0.0;
2585 }
2586 bool IsOne() const OVERRIDE {
2587 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2588 }
2589 bool IsNaN() const {
2590 return std::isnan(value_);
2591 }
2592
2593 DECLARE_INSTRUCTION(DoubleConstant);
2594
2595 private:
2596 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2597 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2598 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2599 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2600
2601 const double value_;
2602
2603 // Only the SsaBuilder and HGraph can create floating-point constants.
2604 friend class SsaBuilder;
2605 friend class HGraph;
2606 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2607};
2608
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002609// Conditional branch. A block ending with an HIf instruction must have
2610// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002611class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002612 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002613 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2614 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002615 SetRawInputAt(0, input);
2616 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002617
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002618 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002619
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002620 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002621 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002622 }
2623
2624 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002625 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002626 }
2627
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002628 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002629
2630 private:
2631 DISALLOW_COPY_AND_ASSIGN(HIf);
2632};
2633
David Brazdilfc6a86a2015-06-26 10:33:45 +00002634
2635// Abstract instruction which marks the beginning and/or end of a try block and
2636// links it to the respective exception handlers. Behaves the same as a Goto in
2637// non-exceptional control flow.
2638// Normal-flow successor is stored at index zero, exception handlers under
2639// higher indices in no particular order.
2640class HTryBoundary : public HTemplateInstruction<0> {
2641 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002642 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002643 kEntry,
2644 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002645 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002646 };
2647
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002648 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002649 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2650 SetPackedField<BoundaryKindField>(kind);
2651 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002652
2653 bool IsControlFlow() const OVERRIDE { return true; }
2654
2655 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002656 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002657
David Brazdild26a4112015-11-10 11:07:31 +00002658 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2659 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2660 }
2661
David Brazdilfc6a86a2015-06-26 10:33:45 +00002662 // Returns whether `handler` is among its exception handlers (non-zero index
2663 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002664 bool HasExceptionHandler(const HBasicBlock& handler) const {
2665 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002666 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002667 }
2668
2669 // If not present already, adds `handler` to its block's list of exception
2670 // handlers.
2671 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002672 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002673 GetBlock()->AddSuccessor(handler);
2674 }
2675 }
2676
Vladimir Markoa1de9182016-02-25 11:37:38 +00002677 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2678 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002679
David Brazdilffee3d32015-07-06 11:48:53 +01002680 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2681
David Brazdilfc6a86a2015-06-26 10:33:45 +00002682 DECLARE_INSTRUCTION(TryBoundary);
2683
2684 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002685 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2686 static constexpr size_t kFieldBoundaryKindSize =
2687 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2688 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2689 kFieldBoundaryKind + kFieldBoundaryKindSize;
2690 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2691 "Too many packed fields.");
2692 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002693
2694 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2695};
2696
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002697// Deoptimize to interpreter, upon checking a condition.
2698class HDeoptimize : public HTemplateInstruction<1> {
2699 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002700 // We set CanTriggerGC to prevent any intermediate address to be live
2701 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002702 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002703 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002704 SetRawInputAt(0, cond);
2705 }
2706
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002707 bool CanBeMoved() const OVERRIDE { return true; }
2708 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2709 return true;
2710 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002711 bool NeedsEnvironment() const OVERRIDE { return true; }
2712 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002713
2714 DECLARE_INSTRUCTION(Deoptimize);
2715
2716 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002717 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2718};
2719
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002720// Represents the ArtMethod that was passed as a first argument to
2721// the method. It is used by instructions that depend on it, like
2722// instructions that work with the dex cache.
2723class HCurrentMethod : public HExpression<0> {
2724 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002725 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2726 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002727
2728 DECLARE_INSTRUCTION(CurrentMethod);
2729
2730 private:
2731 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2732};
2733
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002734// Fetches an ArtMethod from the virtual table or the interface method table
2735// of a class.
2736class HClassTableGet : public HExpression<1> {
2737 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002738 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002739 kVTable,
2740 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002741 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002742 };
2743 HClassTableGet(HInstruction* cls,
2744 Primitive::Type type,
2745 TableKind kind,
2746 size_t index,
2747 uint32_t dex_pc)
2748 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002749 index_(index) {
2750 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002751 SetRawInputAt(0, cls);
2752 }
2753
2754 bool CanBeMoved() const OVERRIDE { return true; }
2755 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2756 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002757 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002758 }
2759
Vladimir Markoa1de9182016-02-25 11:37:38 +00002760 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002761 size_t GetIndex() const { return index_; }
2762
2763 DECLARE_INSTRUCTION(ClassTableGet);
2764
2765 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002766 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2767 static constexpr size_t kFieldTableKindSize =
2768 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2769 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2770 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2771 "Too many packed fields.");
2772 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2773
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002774 // The index of the ArtMethod in the table.
2775 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002776
2777 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2778};
2779
Mark Mendellfe57faa2015-09-18 09:26:15 -04002780// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2781// have one successor for each entry in the switch table, and the final successor
2782// will be the block containing the next Dex opcode.
2783class HPackedSwitch : public HTemplateInstruction<1> {
2784 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002785 HPackedSwitch(int32_t start_value,
2786 uint32_t num_entries,
2787 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002788 uint32_t dex_pc = kNoDexPc)
2789 : HTemplateInstruction(SideEffects::None(), dex_pc),
2790 start_value_(start_value),
2791 num_entries_(num_entries) {
2792 SetRawInputAt(0, input);
2793 }
2794
2795 bool IsControlFlow() const OVERRIDE { return true; }
2796
2797 int32_t GetStartValue() const { return start_value_; }
2798
Vladimir Marko211c2112015-09-24 16:52:33 +01002799 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002800
2801 HBasicBlock* GetDefaultBlock() const {
2802 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002803 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002804 }
2805 DECLARE_INSTRUCTION(PackedSwitch);
2806
2807 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002808 const int32_t start_value_;
2809 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002810
2811 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2812};
2813
Roland Levillain88cb1752014-10-20 16:36:47 +01002814class HUnaryOperation : public HExpression<1> {
2815 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2817 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002818 SetRawInputAt(0, input);
2819 }
2820
2821 HInstruction* GetInput() const { return InputAt(0); }
2822 Primitive::Type GetResultType() const { return GetType(); }
2823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002824 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002825 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002826 return true;
2827 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002828
Roland Levillain31dd3d62016-02-16 12:21:02 +00002829 // Try to statically evaluate `this` and return a HConstant
2830 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002831 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002832 HConstant* TryStaticEvaluation() const;
2833
2834 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002835 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2836 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002837 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2838 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002839
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002840 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002841
2842 private:
2843 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2844};
2845
Dave Allison20dfc792014-06-16 20:44:29 -07002846class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002847 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002848 HBinaryOperation(Primitive::Type result_type,
2849 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002850 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002851 SideEffects side_effects = SideEffects::None(),
2852 uint32_t dex_pc = kNoDexPc)
2853 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002854 SetRawInputAt(0, left);
2855 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002856 }
2857
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002858 HInstruction* GetLeft() const { return InputAt(0); }
2859 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002860 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002861
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002862 virtual bool IsCommutative() const { return false; }
2863
2864 // Put constant on the right.
2865 // Returns whether order is changed.
2866 bool OrderInputsWithConstantOnTheRight() {
2867 HInstruction* left = InputAt(0);
2868 HInstruction* right = InputAt(1);
2869 if (left->IsConstant() && !right->IsConstant()) {
2870 ReplaceInput(right, 0);
2871 ReplaceInput(left, 1);
2872 return true;
2873 }
2874 return false;
2875 }
2876
2877 // Order inputs by instruction id, but favor constant on the right side.
2878 // This helps GVN for commutative ops.
2879 void OrderInputs() {
2880 DCHECK(IsCommutative());
2881 HInstruction* left = InputAt(0);
2882 HInstruction* right = InputAt(1);
2883 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2884 return;
2885 }
2886 if (OrderInputsWithConstantOnTheRight()) {
2887 return;
2888 }
2889 // Order according to instruction id.
2890 if (left->GetId() > right->GetId()) {
2891 ReplaceInput(right, 0);
2892 ReplaceInput(left, 1);
2893 }
2894 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002895
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002896 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002897 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002898 return true;
2899 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002900
Roland Levillain31dd3d62016-02-16 12:21:02 +00002901 // Try to statically evaluate `this` and return a HConstant
2902 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002903 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002904 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002905
2906 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002907 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2908 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002909 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2910 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002911 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002912 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2913 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002914 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2915 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002916 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2917 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002918 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002919 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2920 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002921
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002922 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002923 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002924 HConstant* GetConstantRight() const;
2925
2926 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002927 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002928 HInstruction* GetLeastConstantLeft() const;
2929
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002930 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002931
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002932 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002933 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2934};
2935
Mark Mendellc4701932015-04-10 13:18:51 -04002936// The comparison bias applies for floating point operations and indicates how NaN
2937// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002938enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002939 kNoBias, // bias is not applicable (i.e. for long operation)
2940 kGtBias, // return 1 for NaN comparisons
2941 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00002942 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04002943};
2944
Roland Levillain31dd3d62016-02-16 12:21:02 +00002945std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2946
Dave Allison20dfc792014-06-16 20:44:29 -07002947class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002948 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002949 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002950 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
2951 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
2952 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002953
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002954 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002955 // `instruction`, and disregard moves in between.
2956 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002957
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002958 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002959
2960 virtual IfCondition GetCondition() const = 0;
2961
Mark Mendellc4701932015-04-10 13:18:51 -04002962 virtual IfCondition GetOppositeCondition() const = 0;
2963
Vladimir Markoa1de9182016-02-25 11:37:38 +00002964 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
2965 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
2966 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04002967
2968 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002969 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04002970 }
2971
Roland Levillain4fa13f62015-07-06 18:11:54 +01002972 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002973 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002974 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002975 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002976 }
2977
2978 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002979 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002980 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002981 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002982 }
2983
Roland Levillain31dd3d62016-02-16 12:21:02 +00002984 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002985 // Needed if we merge a HCompare into a HCondition.
2986 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
2987 static constexpr size_t kFieldComparisonBiasSize =
2988 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
2989 static constexpr size_t kNumberOfConditionPackedBits =
2990 kFieldComparisonBias + kFieldComparisonBiasSize;
2991 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2992 using ComparisonBiasField =
2993 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
2994
Roland Levillain31dd3d62016-02-16 12:21:02 +00002995 template <typename T>
2996 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
2997
2998 template <typename T>
2999 int32_t CompareFP(T x, T y) const {
3000 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3001 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3002 // Handle the bias.
3003 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3004 }
3005
3006 // Return an integer constant containing the result of a condition evaluated at compile time.
3007 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3008 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3009 }
3010
Dave Allison20dfc792014-06-16 20:44:29 -07003011 private:
3012 DISALLOW_COPY_AND_ASSIGN(HCondition);
3013};
3014
3015// Instruction to check if two inputs are equal to each other.
3016class HEqual : public HCondition {
3017 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003018 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3019 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003020
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003021 bool IsCommutative() const OVERRIDE { return true; }
3022
Vladimir Marko9e23df52015-11-10 17:14:35 +00003023 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3024 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003025 return MakeConstantCondition(true, GetDexPc());
3026 }
3027 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3028 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3029 }
3030 // In the following Evaluate methods, a HCompare instruction has
3031 // been merged into this HEqual instruction; evaluate it as
3032 // `Compare(x, y) == 0`.
3033 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3034 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3035 GetDexPc());
3036 }
3037 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3038 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3039 }
3040 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3041 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003042 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003043
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003044 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003045
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003046 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003047 return kCondEQ;
3048 }
3049
Mark Mendellc4701932015-04-10 13:18:51 -04003050 IfCondition GetOppositeCondition() const OVERRIDE {
3051 return kCondNE;
3052 }
3053
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003054 private:
Aart Bike9f37602015-10-09 11:15:55 -07003055 template <typename T> bool Compute(T x, T y) const { return x == y; }
3056
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003057 DISALLOW_COPY_AND_ASSIGN(HEqual);
3058};
3059
Dave Allison20dfc792014-06-16 20:44:29 -07003060class HNotEqual : public HCondition {
3061 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003062 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3063 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003064
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003065 bool IsCommutative() const OVERRIDE { return true; }
3066
Vladimir Marko9e23df52015-11-10 17:14:35 +00003067 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3068 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 return MakeConstantCondition(false, GetDexPc());
3070 }
3071 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3072 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3073 }
3074 // In the following Evaluate methods, a HCompare instruction has
3075 // been merged into this HNotEqual instruction; evaluate it as
3076 // `Compare(x, y) != 0`.
3077 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3078 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3079 }
3080 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3081 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3082 }
3083 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3084 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003085 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003086
Dave Allison20dfc792014-06-16 20:44:29 -07003087 DECLARE_INSTRUCTION(NotEqual);
3088
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003089 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003090 return kCondNE;
3091 }
3092
Mark Mendellc4701932015-04-10 13:18:51 -04003093 IfCondition GetOppositeCondition() const OVERRIDE {
3094 return kCondEQ;
3095 }
3096
Dave Allison20dfc792014-06-16 20:44:29 -07003097 private:
Aart Bike9f37602015-10-09 11:15:55 -07003098 template <typename T> bool Compute(T x, T y) const { return x != y; }
3099
Dave Allison20dfc792014-06-16 20:44:29 -07003100 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3101};
3102
3103class HLessThan : public HCondition {
3104 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003105 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3106 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003107
Roland Levillain9867bc72015-08-05 10:21:34 +01003108 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003109 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003110 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003111 // In the following Evaluate methods, a HCompare instruction has
3112 // been merged into this HLessThan instruction; evaluate it as
3113 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003114 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3116 }
3117 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3118 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3119 }
3120 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3121 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003122 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003123
Dave Allison20dfc792014-06-16 20:44:29 -07003124 DECLARE_INSTRUCTION(LessThan);
3125
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003126 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003127 return kCondLT;
3128 }
3129
Mark Mendellc4701932015-04-10 13:18:51 -04003130 IfCondition GetOppositeCondition() const OVERRIDE {
3131 return kCondGE;
3132 }
3133
Dave Allison20dfc792014-06-16 20:44:29 -07003134 private:
Aart Bike9f37602015-10-09 11:15:55 -07003135 template <typename T> bool Compute(T x, T y) const { return x < y; }
3136
Dave Allison20dfc792014-06-16 20:44:29 -07003137 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3138};
3139
3140class HLessThanOrEqual : public HCondition {
3141 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003142 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3143 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003144
Roland Levillain9867bc72015-08-05 10:21:34 +01003145 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003146 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003147 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003148 // In the following Evaluate methods, a HCompare instruction has
3149 // been merged into this HLessThanOrEqual instruction; evaluate it as
3150 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003151 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003152 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3153 }
3154 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3155 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3156 }
3157 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3158 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003159 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003160
Dave Allison20dfc792014-06-16 20:44:29 -07003161 DECLARE_INSTRUCTION(LessThanOrEqual);
3162
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003163 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003164 return kCondLE;
3165 }
3166
Mark Mendellc4701932015-04-10 13:18:51 -04003167 IfCondition GetOppositeCondition() const OVERRIDE {
3168 return kCondGT;
3169 }
3170
Dave Allison20dfc792014-06-16 20:44:29 -07003171 private:
Aart Bike9f37602015-10-09 11:15:55 -07003172 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3173
Dave Allison20dfc792014-06-16 20:44:29 -07003174 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3175};
3176
3177class HGreaterThan : public HCondition {
3178 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003179 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3180 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003181
Roland Levillain9867bc72015-08-05 10:21:34 +01003182 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003183 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003184 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003185 // In the following Evaluate methods, a HCompare instruction has
3186 // been merged into this HGreaterThan instruction; evaluate it as
3187 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003188 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003189 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3190 }
3191 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3192 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3193 }
3194 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3195 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003196 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003197
Dave Allison20dfc792014-06-16 20:44:29 -07003198 DECLARE_INSTRUCTION(GreaterThan);
3199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003200 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003201 return kCondGT;
3202 }
3203
Mark Mendellc4701932015-04-10 13:18:51 -04003204 IfCondition GetOppositeCondition() const OVERRIDE {
3205 return kCondLE;
3206 }
3207
Dave Allison20dfc792014-06-16 20:44:29 -07003208 private:
Aart Bike9f37602015-10-09 11:15:55 -07003209 template <typename T> bool Compute(T x, T y) const { return x > y; }
3210
Dave Allison20dfc792014-06-16 20:44:29 -07003211 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3212};
3213
3214class HGreaterThanOrEqual : public HCondition {
3215 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003216 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3217 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003218
Roland Levillain9867bc72015-08-05 10:21:34 +01003219 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003220 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003221 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003222 // In the following Evaluate methods, a HCompare instruction has
3223 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3224 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003225 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003226 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3227 }
3228 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3229 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3230 }
3231 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3232 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003233 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003234
Dave Allison20dfc792014-06-16 20:44:29 -07003235 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3236
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003237 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003238 return kCondGE;
3239 }
3240
Mark Mendellc4701932015-04-10 13:18:51 -04003241 IfCondition GetOppositeCondition() const OVERRIDE {
3242 return kCondLT;
3243 }
3244
Dave Allison20dfc792014-06-16 20:44:29 -07003245 private:
Aart Bike9f37602015-10-09 11:15:55 -07003246 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3247
Dave Allison20dfc792014-06-16 20:44:29 -07003248 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3249};
3250
Aart Bike9f37602015-10-09 11:15:55 -07003251class HBelow : public HCondition {
3252 public:
3253 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3254 : HCondition(first, second, dex_pc) {}
3255
3256 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003257 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003258 }
3259 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003260 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3261 }
3262 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3263 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3264 LOG(FATAL) << DebugName() << " is not defined for float values";
3265 UNREACHABLE();
3266 }
3267 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3268 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3269 LOG(FATAL) << DebugName() << " is not defined for double values";
3270 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003271 }
3272
3273 DECLARE_INSTRUCTION(Below);
3274
3275 IfCondition GetCondition() const OVERRIDE {
3276 return kCondB;
3277 }
3278
3279 IfCondition GetOppositeCondition() const OVERRIDE {
3280 return kCondAE;
3281 }
3282
3283 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003284 template <typename T> bool Compute(T x, T y) const {
3285 return MakeUnsigned(x) < MakeUnsigned(y);
3286 }
Aart Bike9f37602015-10-09 11:15:55 -07003287
3288 DISALLOW_COPY_AND_ASSIGN(HBelow);
3289};
3290
3291class HBelowOrEqual : public HCondition {
3292 public:
3293 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3294 : HCondition(first, second, dex_pc) {}
3295
3296 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003297 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003298 }
3299 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003300 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3301 }
3302 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3303 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3304 LOG(FATAL) << DebugName() << " is not defined for float values";
3305 UNREACHABLE();
3306 }
3307 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3308 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3309 LOG(FATAL) << DebugName() << " is not defined for double values";
3310 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003311 }
3312
3313 DECLARE_INSTRUCTION(BelowOrEqual);
3314
3315 IfCondition GetCondition() const OVERRIDE {
3316 return kCondBE;
3317 }
3318
3319 IfCondition GetOppositeCondition() const OVERRIDE {
3320 return kCondA;
3321 }
3322
3323 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003324 template <typename T> bool Compute(T x, T y) const {
3325 return MakeUnsigned(x) <= MakeUnsigned(y);
3326 }
Aart Bike9f37602015-10-09 11:15:55 -07003327
3328 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3329};
3330
3331class HAbove : public HCondition {
3332 public:
3333 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3334 : HCondition(first, second, dex_pc) {}
3335
3336 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003337 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003338 }
3339 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003340 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3341 }
3342 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3343 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3344 LOG(FATAL) << DebugName() << " is not defined for float values";
3345 UNREACHABLE();
3346 }
3347 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3348 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3349 LOG(FATAL) << DebugName() << " is not defined for double values";
3350 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003351 }
3352
3353 DECLARE_INSTRUCTION(Above);
3354
3355 IfCondition GetCondition() const OVERRIDE {
3356 return kCondA;
3357 }
3358
3359 IfCondition GetOppositeCondition() const OVERRIDE {
3360 return kCondBE;
3361 }
3362
3363 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003364 template <typename T> bool Compute(T x, T y) const {
3365 return MakeUnsigned(x) > MakeUnsigned(y);
3366 }
Aart Bike9f37602015-10-09 11:15:55 -07003367
3368 DISALLOW_COPY_AND_ASSIGN(HAbove);
3369};
3370
3371class HAboveOrEqual : public HCondition {
3372 public:
3373 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3374 : HCondition(first, second, dex_pc) {}
3375
3376 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003377 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003378 }
3379 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003380 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3381 }
3382 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3383 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3384 LOG(FATAL) << DebugName() << " is not defined for float values";
3385 UNREACHABLE();
3386 }
3387 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3388 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3389 LOG(FATAL) << DebugName() << " is not defined for double values";
3390 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003391 }
3392
3393 DECLARE_INSTRUCTION(AboveOrEqual);
3394
3395 IfCondition GetCondition() const OVERRIDE {
3396 return kCondAE;
3397 }
3398
3399 IfCondition GetOppositeCondition() const OVERRIDE {
3400 return kCondB;
3401 }
3402
3403 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003404 template <typename T> bool Compute(T x, T y) const {
3405 return MakeUnsigned(x) >= MakeUnsigned(y);
3406 }
Aart Bike9f37602015-10-09 11:15:55 -07003407
3408 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3409};
Dave Allison20dfc792014-06-16 20:44:29 -07003410
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003411// Instruction to check how two inputs compare to each other.
3412// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3413class HCompare : public HBinaryOperation {
3414 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003415 HCompare(Primitive::Type type,
3416 HInstruction* first,
3417 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003418 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003419 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003420 : HBinaryOperation(Primitive::kPrimInt,
3421 first,
3422 second,
3423 SideEffectsForArchRuntimeCalls(type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003424 dex_pc) {
3425 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003426 DCHECK_EQ(type, first->GetType());
3427 DCHECK_EQ(type, second->GetType());
3428 }
3429
Roland Levillain9867bc72015-08-05 10:21:34 +01003430 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003431 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3432
3433 template <typename T>
3434 int32_t ComputeFP(T x, T y) const {
3435 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3436 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3437 // Handle the bias.
3438 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3439 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003440
Roland Levillain9867bc72015-08-05 10:21:34 +01003441 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003442 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3443 // reach this code path when processing a freshly built HIR
3444 // graph. However HCompare integer instructions can be synthesized
3445 // by the instruction simplifier to implement IntegerCompare and
3446 // IntegerSignum intrinsics, so we have to handle this case.
3447 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003448 }
3449 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003450 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3451 }
3452 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3453 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3454 }
3455 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3456 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003457 }
3458
Calin Juravleddb7df22014-11-25 20:56:51 +00003459 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003460 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003461 }
3462
Vladimir Markoa1de9182016-02-25 11:37:38 +00003463 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003464
Roland Levillain31dd3d62016-02-16 12:21:02 +00003465 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003466 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003467 bool IsGtBias() const {
3468 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003469 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003470 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003471
3472 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3473 // MIPS64 uses a runtime call for FP comparisons.
3474 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3475 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003476
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003477 DECLARE_INSTRUCTION(Compare);
3478
Roland Levillain31dd3d62016-02-16 12:21:02 +00003479 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003480 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3481 static constexpr size_t kFieldComparisonBiasSize =
3482 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3483 static constexpr size_t kNumberOfComparePackedBits =
3484 kFieldComparisonBias + kFieldComparisonBiasSize;
3485 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3486 using ComparisonBiasField =
3487 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3488
Roland Levillain31dd3d62016-02-16 12:21:02 +00003489 // Return an integer constant containing the result of a comparison evaluated at compile time.
3490 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3491 DCHECK(value == -1 || value == 0 || value == 1) << value;
3492 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3493 }
3494
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003495 private:
3496 DISALLOW_COPY_AND_ASSIGN(HCompare);
3497};
3498
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003499// A local in the graph. Corresponds to a Dex register.
3500class HLocal : public HTemplateInstruction<0> {
3501 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003502 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003503 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003504
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003505 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003506
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003507 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003508
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003509 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003510 // The Dex register number.
3511 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003512
3513 DISALLOW_COPY_AND_ASSIGN(HLocal);
3514};
3515
3516// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003517class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003518 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003519 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3520 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003521 SetRawInputAt(0, local);
3522 }
3523
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003524 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3525
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003526 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003527
3528 private:
3529 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3530};
3531
3532// Store a value in a given local. This instruction has two inputs: the value
3533// and the local.
3534class HStoreLocal : public HTemplateInstruction<2> {
3535 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003536 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3537 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003538 SetRawInputAt(0, local);
3539 SetRawInputAt(1, value);
3540 }
3541
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003542 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3543
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003544 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003545
3546 private:
3547 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3548};
3549
David Brazdil6de19382016-01-08 17:37:10 +00003550class HNewInstance : public HExpression<2> {
3551 public:
3552 HNewInstance(HInstruction* cls,
3553 HCurrentMethod* current_method,
3554 uint32_t dex_pc,
3555 uint16_t type_index,
3556 const DexFile& dex_file,
3557 bool can_throw,
3558 bool finalizable,
3559 QuickEntrypointEnum entrypoint)
3560 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3561 type_index_(type_index),
3562 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003563 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003564 SetPackedFlag<kFlagCanThrow>(can_throw);
3565 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003566 SetRawInputAt(0, cls);
3567 SetRawInputAt(1, current_method);
3568 }
3569
3570 uint16_t GetTypeIndex() const { return type_index_; }
3571 const DexFile& GetDexFile() const { return dex_file_; }
3572
3573 // Calls runtime so needs an environment.
3574 bool NeedsEnvironment() const OVERRIDE { return true; }
3575
3576 // It may throw when called on type that's not instantiable/accessible.
3577 // It can throw OOME.
3578 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003579 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003580
Vladimir Markoa1de9182016-02-25 11:37:38 +00003581 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003582
3583 bool CanBeNull() const OVERRIDE { return false; }
3584
3585 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3586
3587 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3588 entrypoint_ = entrypoint;
3589 }
3590
3591 bool IsStringAlloc() const;
3592
3593 DECLARE_INSTRUCTION(NewInstance);
3594
3595 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003596 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3597 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3598 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3599 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3600 "Too many packed fields.");
3601
David Brazdil6de19382016-01-08 17:37:10 +00003602 const uint16_t type_index_;
3603 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003604 QuickEntrypointEnum entrypoint_;
3605
3606 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3607};
3608
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003609enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003610#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3611 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003612#include "intrinsics_list.h"
3613 kNone,
3614 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3615#undef INTRINSICS_LIST
3616#undef OPTIMIZING_INTRINSICS
3617};
3618std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3619
Agi Csaki05f20562015-08-19 14:58:14 -07003620enum IntrinsicNeedsEnvironmentOrCache {
3621 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3622 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003623};
3624
Aart Bik5d75afe2015-12-14 11:57:01 -08003625enum IntrinsicSideEffects {
3626 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3627 kReadSideEffects, // Intrinsic may read heap memory.
3628 kWriteSideEffects, // Intrinsic may write heap memory.
3629 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3630};
3631
3632enum IntrinsicExceptions {
3633 kNoThrow, // Intrinsic does not throw any exceptions.
3634 kCanThrow // Intrinsic may throw exceptions.
3635};
3636
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003637class HInvoke : public HInstruction {
3638 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003639 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003640
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003641 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003642
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003643 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003644 SetRawInputAt(index, argument);
3645 }
3646
Roland Levillain3e3d7332015-04-28 11:00:54 +01003647 // Return the number of arguments. This number can be lower than
3648 // the number of inputs returned by InputCount(), as some invoke
3649 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3650 // inputs at the end of their list of inputs.
3651 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3652
Vladimir Markoa1de9182016-02-25 11:37:38 +00003653 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003654
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003655 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003656 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003657
Vladimir Markoa1de9182016-02-25 11:37:38 +00003658 InvokeType GetOriginalInvokeType() const {
3659 return GetPackedField<OriginalInvokeTypeField>();
3660 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003661
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003662 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003663 return intrinsic_;
3664 }
3665
Aart Bik5d75afe2015-12-14 11:57:01 -08003666 void SetIntrinsic(Intrinsics intrinsic,
3667 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3668 IntrinsicSideEffects side_effects,
3669 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003670
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003671 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003672 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003673 }
3674
Vladimir Markoa1de9182016-02-25 11:37:38 +00003675 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003676
3677 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3678
3679 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3680 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3681 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003682
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003683 uint32_t* GetIntrinsicOptimizations() {
3684 return &intrinsic_optimizations_;
3685 }
3686
3687 const uint32_t* GetIntrinsicOptimizations() const {
3688 return &intrinsic_optimizations_;
3689 }
3690
3691 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3692
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003693 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003694
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003695 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003696 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3697 static constexpr size_t kFieldOriginalInvokeTypeSize =
3698 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3699 static constexpr size_t kFieldReturnType =
3700 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3701 static constexpr size_t kFieldReturnTypeSize =
3702 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3703 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3704 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3705 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3706 using OriginalInvokeTypeField =
3707 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3708 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3709
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003710 HInvoke(ArenaAllocator* arena,
3711 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003712 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003713 Primitive::Type return_type,
3714 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003715 uint32_t dex_method_index,
3716 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003717 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003718 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003719 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003720 inputs_(number_of_arguments + number_of_other_inputs,
3721 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003722 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003723 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003724 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003725 SetPackedField<ReturnTypeField>(return_type);
3726 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3727 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003728 }
3729
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003730 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003731 return inputs_[index];
3732 }
3733
David Brazdil1abb4192015-02-17 18:33:36 +00003734 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003735 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003736 }
3737
Vladimir Markoa1de9182016-02-25 11:37:38 +00003738 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003739
Roland Levillain3e3d7332015-04-28 11:00:54 +01003740 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003741 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003742 const uint32_t dex_method_index_;
3743 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003744
3745 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3746 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003747
3748 private:
3749 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3750};
3751
Calin Juravle175dc732015-08-25 15:42:32 +01003752class HInvokeUnresolved : public HInvoke {
3753 public:
3754 HInvokeUnresolved(ArenaAllocator* arena,
3755 uint32_t number_of_arguments,
3756 Primitive::Type return_type,
3757 uint32_t dex_pc,
3758 uint32_t dex_method_index,
3759 InvokeType invoke_type)
3760 : HInvoke(arena,
3761 number_of_arguments,
3762 0u /* number_of_other_inputs */,
3763 return_type,
3764 dex_pc,
3765 dex_method_index,
3766 invoke_type) {
3767 }
3768
3769 DECLARE_INSTRUCTION(InvokeUnresolved);
3770
3771 private:
3772 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3773};
3774
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003775class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003776 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003777 // Requirements of this method call regarding the class
3778 // initialization (clinit) check of its declaring class.
3779 enum class ClinitCheckRequirement {
3780 kNone, // Class already initialized.
3781 kExplicit, // Static call having explicit clinit check as last input.
3782 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003783 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003784 };
3785
Vladimir Marko58155012015-08-19 12:49:41 +00003786 // Determines how to load the target ArtMethod*.
3787 enum class MethodLoadKind {
3788 // Use a String init ArtMethod* loaded from Thread entrypoints.
3789 kStringInit,
3790
3791 // Use the method's own ArtMethod* loaded by the register allocator.
3792 kRecursive,
3793
3794 // Use ArtMethod* at a known address, embed the direct address in the code.
3795 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3796 kDirectAddress,
3797
3798 // Use ArtMethod* at an address that will be known at link time, embed the direct
3799 // address in the code. If the image is relocatable, emit .patch_oat entry.
3800 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3801 // the image relocatable or not.
3802 kDirectAddressWithFixup,
3803
Vladimir Markoa1de9182016-02-25 11:37:38 +00003804 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003805 // Used when we need to use the dex cache, for example for invoke-static that
3806 // may cause class initialization (the entry may point to a resolution method),
3807 // and we know that we can access the dex cache arrays using a PC-relative load.
3808 kDexCachePcRelative,
3809
3810 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3811 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3812 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3813 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3814 kDexCacheViaMethod,
3815 };
3816
3817 // Determines the location of the code pointer.
3818 enum class CodePtrLocation {
3819 // Recursive call, use local PC-relative call instruction.
3820 kCallSelf,
3821
3822 // Use PC-relative call instruction patched at link time.
3823 // Used for calls within an oat file, boot->boot or app->app.
3824 kCallPCRelative,
3825
3826 // Call to a known target address, embed the direct address in code.
3827 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3828 kCallDirect,
3829
3830 // Call to a target address that will be known at link time, embed the direct
3831 // address in code. If the image is relocatable, emit .patch_oat entry.
3832 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3833 // the image relocatable or not.
3834 kCallDirectWithFixup,
3835
3836 // Use code pointer from the ArtMethod*.
3837 // Used when we don't know the target code. This is also the last-resort-kind used when
3838 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3839 kCallArtMethod,
3840 };
3841
3842 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003843 MethodLoadKind method_load_kind;
3844 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003845 // The method load data holds
3846 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3847 // Note that there are multiple string init methods, each having its own offset.
3848 // - the method address for kDirectAddress
3849 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003850 uint64_t method_load_data;
3851 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003852 };
3853
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003854 HInvokeStaticOrDirect(ArenaAllocator* arena,
3855 uint32_t number_of_arguments,
3856 Primitive::Type return_type,
3857 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003858 uint32_t method_index,
3859 MethodReference target_method,
3860 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003861 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003862 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003863 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003864 : HInvoke(arena,
3865 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003866 // There is potentially one extra argument for the HCurrentMethod node, and
3867 // potentially one other if the clinit check is explicit, and potentially
3868 // one other if the method is a string factory.
3869 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3870 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3871 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003872 return_type,
3873 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003874 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003875 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003876 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003877 dispatch_info_(dispatch_info) {
3878 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3879 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3880 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003881
Vladimir Markodc151b22015-10-15 18:02:30 +01003882 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003883 bool had_current_method_input = HasCurrentMethodInput();
3884 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3885
3886 // Using the current method is the default and once we find a better
3887 // method load kind, we should not go back to using the current method.
3888 DCHECK(had_current_method_input || !needs_current_method_input);
3889
3890 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003891 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3892 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003893 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003894 dispatch_info_ = dispatch_info;
3895 }
3896
Vladimir Markoc53c0792015-11-19 15:48:33 +00003897 void AddSpecialInput(HInstruction* input) {
3898 // We allow only one special input.
3899 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3900 DCHECK(InputCount() == GetSpecialInputIndex() ||
3901 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3902 InsertInputAt(GetSpecialInputIndex(), input);
3903 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003904
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003905 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003906 // We access the method via the dex cache so we can't do an implicit null check.
3907 // TODO: for intrinsics we can generate implicit null checks.
3908 return false;
3909 }
3910
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003911 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003912 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003913 }
3914
Vladimir Markoc53c0792015-11-19 15:48:33 +00003915 // Get the index of the special input, if any.
3916 //
David Brazdil6de19382016-01-08 17:37:10 +00003917 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3918 // method pointer; otherwise there may be one platform-specific special input,
3919 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003920 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003921 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003922
Vladimir Markoa1de9182016-02-25 11:37:38 +00003923 InvokeType GetOptimizedInvokeType() const {
3924 return GetPackedField<OptimizedInvokeTypeField>();
3925 }
3926
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003927 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003928 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003929 }
3930
Vladimir Marko58155012015-08-19 12:49:41 +00003931 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3932 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3933 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003934 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003935 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003936 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003937 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003938 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3939 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003940 bool HasCurrentMethodInput() const {
3941 // This function can be called only after the invoke has been fully initialized by the builder.
3942 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003943 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003944 return true;
3945 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003946 DCHECK(InputCount() == GetSpecialInputIndex() ||
3947 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003948 return false;
3949 }
3950 }
Vladimir Marko58155012015-08-19 12:49:41 +00003951 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3952 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003953 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003954
3955 int32_t GetStringInitOffset() const {
3956 DCHECK(IsStringInit());
3957 return dispatch_info_.method_load_data;
3958 }
3959
3960 uint64_t GetMethodAddress() const {
3961 DCHECK(HasMethodAddress());
3962 return dispatch_info_.method_load_data;
3963 }
3964
3965 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003966 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003967 return dispatch_info_.method_load_data;
3968 }
3969
3970 uint64_t GetDirectCodePtr() const {
3971 DCHECK(HasDirectCodePtr());
3972 return dispatch_info_.direct_code_ptr;
3973 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003974
Vladimir Markoa1de9182016-02-25 11:37:38 +00003975 ClinitCheckRequirement GetClinitCheckRequirement() const {
3976 return GetPackedField<ClinitCheckRequirementField>();
3977 }
Calin Juravle68ad6492015-08-18 17:08:12 +01003978
Roland Levillain4c0eb422015-04-24 16:43:49 +01003979 // Is this instruction a call to a static method?
3980 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003981 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003982 }
3983
Vladimir Markofbb184a2015-11-13 14:47:00 +00003984 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3985 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3986 // instruction; only relevant for static calls with explicit clinit check.
3987 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003988 DCHECK(IsStaticWithExplicitClinitCheck());
3989 size_t last_input_index = InputCount() - 1;
3990 HInstruction* last_input = InputAt(last_input_index);
3991 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003992 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003993 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003994 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003995 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003996 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003997 }
3998
David Brazdilbc9ab162016-01-20 14:50:19 +00003999 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00004000 DCHECK(IsStringInit());
4001 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00004002 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00004003 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004004 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00004005 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004006 }
4007
Roland Levillain4c0eb422015-04-24 16:43:49 +01004008 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004009 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004010 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004011 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004012 }
4013
4014 // Is this a call to a static method whose declaring class has an
4015 // implicit intialization check requirement?
4016 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004017 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004018 }
4019
Vladimir Markob554b5a2015-11-06 12:57:55 +00004020 // Does this method load kind need the current method as an input?
4021 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4022 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4023 }
4024
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004025 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004026
Roland Levillain4c0eb422015-04-24 16:43:49 +01004027 protected:
4028 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4029 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4030 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4031 HInstruction* input = input_record.GetInstruction();
4032 // `input` is the last input of a static invoke marked as having
4033 // an explicit clinit check. It must either be:
4034 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4035 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4036 DCHECK(input != nullptr);
4037 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4038 }
4039 return input_record;
4040 }
4041
Vladimir Markoc53c0792015-11-19 15:48:33 +00004042 void InsertInputAt(size_t index, HInstruction* input);
4043 void RemoveInputAt(size_t index);
4044
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004045 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004046 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4047 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4048 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4049 static constexpr size_t kFieldClinitCheckRequirement =
4050 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4051 static constexpr size_t kFieldClinitCheckRequirementSize =
4052 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4053 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4054 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4055 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4056 "Too many packed fields.");
4057 using OptimizedInvokeTypeField =
4058 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4059 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4060 kFieldClinitCheckRequirement,
4061 kFieldClinitCheckRequirementSize>;
4062
Vladimir Marko58155012015-08-19 12:49:41 +00004063 // The target method may refer to different dex file or method index than the original
4064 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4065 // in derived class to increase visibility.
4066 MethodReference target_method_;
4067 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004068
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004069 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004070};
Vladimir Markof64242a2015-12-01 14:58:23 +00004071std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004072std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004073
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004074class HInvokeVirtual : public HInvoke {
4075 public:
4076 HInvokeVirtual(ArenaAllocator* arena,
4077 uint32_t number_of_arguments,
4078 Primitive::Type return_type,
4079 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004080 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004081 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004082 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004083 vtable_index_(vtable_index) {}
4084
Calin Juravle641547a2015-04-21 22:08:51 +01004085 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004086 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004087 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004088 }
4089
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004090 uint32_t GetVTableIndex() const { return vtable_index_; }
4091
4092 DECLARE_INSTRUCTION(InvokeVirtual);
4093
4094 private:
4095 const uint32_t vtable_index_;
4096
4097 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4098};
4099
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004100class HInvokeInterface : public HInvoke {
4101 public:
4102 HInvokeInterface(ArenaAllocator* arena,
4103 uint32_t number_of_arguments,
4104 Primitive::Type return_type,
4105 uint32_t dex_pc,
4106 uint32_t dex_method_index,
4107 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004108 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004109 imt_index_(imt_index) {}
4110
Calin Juravle641547a2015-04-21 22:08:51 +01004111 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004112 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004113 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004114 }
4115
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004116 uint32_t GetImtIndex() const { return imt_index_; }
4117 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4118
4119 DECLARE_INSTRUCTION(InvokeInterface);
4120
4121 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004122 const uint32_t imt_index_;
4123
4124 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4125};
4126
Roland Levillain88cb1752014-10-20 16:36:47 +01004127class HNeg : public HUnaryOperation {
4128 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004129 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4130 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004131
Roland Levillain9867bc72015-08-05 10:21:34 +01004132 template <typename T> T Compute(T x) const { return -x; }
4133
4134 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004135 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004136 }
4137 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004138 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004139 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004140 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4141 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4142 }
4143 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4144 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4145 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004146
Roland Levillain88cb1752014-10-20 16:36:47 +01004147 DECLARE_INSTRUCTION(Neg);
4148
4149 private:
4150 DISALLOW_COPY_AND_ASSIGN(HNeg);
4151};
4152
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004153class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004154 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004155 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004156 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004157 uint32_t dex_pc,
4158 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004159 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004160 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004161 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004162 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004163 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004164 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004165 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004166 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004167 }
4168
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004169 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004170 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004171
4172 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004173 bool NeedsEnvironment() const OVERRIDE { return true; }
4174
Mingyao Yang0c365e62015-03-31 15:09:29 -07004175 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4176 bool CanThrow() const OVERRIDE { return true; }
4177
Calin Juravle10e244f2015-01-26 18:54:32 +00004178 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004179
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004180 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4181
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004182 DECLARE_INSTRUCTION(NewArray);
4183
4184 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004185 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004186 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004187 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004188
4189 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4190};
4191
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004192class HAdd : public HBinaryOperation {
4193 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004194 HAdd(Primitive::Type result_type,
4195 HInstruction* left,
4196 HInstruction* right,
4197 uint32_t dex_pc = kNoDexPc)
4198 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004200 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004201
Roland Levillain9867bc72015-08-05 10:21:34 +01004202 template <typename T> T Compute(T x, T y) const { return x + y; }
4203
4204 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004205 return GetBlock()->GetGraph()->GetIntConstant(
4206 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004207 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004208 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004209 return GetBlock()->GetGraph()->GetLongConstant(
4210 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004211 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004212 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4213 return GetBlock()->GetGraph()->GetFloatConstant(
4214 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4215 }
4216 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4217 return GetBlock()->GetGraph()->GetDoubleConstant(
4218 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4219 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004220
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004221 DECLARE_INSTRUCTION(Add);
4222
4223 private:
4224 DISALLOW_COPY_AND_ASSIGN(HAdd);
4225};
4226
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004227class HSub : public HBinaryOperation {
4228 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004229 HSub(Primitive::Type result_type,
4230 HInstruction* left,
4231 HInstruction* right,
4232 uint32_t dex_pc = kNoDexPc)
4233 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004234
Roland Levillain9867bc72015-08-05 10:21:34 +01004235 template <typename T> T Compute(T x, T y) const { return x - y; }
4236
4237 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004238 return GetBlock()->GetGraph()->GetIntConstant(
4239 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004240 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004241 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004242 return GetBlock()->GetGraph()->GetLongConstant(
4243 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004244 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004245 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4246 return GetBlock()->GetGraph()->GetFloatConstant(
4247 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4248 }
4249 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4250 return GetBlock()->GetGraph()->GetDoubleConstant(
4251 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4252 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004253
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004254 DECLARE_INSTRUCTION(Sub);
4255
4256 private:
4257 DISALLOW_COPY_AND_ASSIGN(HSub);
4258};
4259
Calin Juravle34bacdf2014-10-07 20:23:36 +01004260class HMul : public HBinaryOperation {
4261 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004262 HMul(Primitive::Type result_type,
4263 HInstruction* left,
4264 HInstruction* right,
4265 uint32_t dex_pc = kNoDexPc)
4266 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004268 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004269
Roland Levillain9867bc72015-08-05 10:21:34 +01004270 template <typename T> T Compute(T x, T y) const { return x * y; }
4271
4272 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004273 return GetBlock()->GetGraph()->GetIntConstant(
4274 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004275 }
4276 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004277 return GetBlock()->GetGraph()->GetLongConstant(
4278 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004279 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004280 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4281 return GetBlock()->GetGraph()->GetFloatConstant(
4282 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4283 }
4284 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4285 return GetBlock()->GetGraph()->GetDoubleConstant(
4286 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4287 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004288
4289 DECLARE_INSTRUCTION(Mul);
4290
4291 private:
4292 DISALLOW_COPY_AND_ASSIGN(HMul);
4293};
4294
Calin Juravle7c4954d2014-10-28 16:57:40 +00004295class HDiv : public HBinaryOperation {
4296 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004297 HDiv(Primitive::Type result_type,
4298 HInstruction* left,
4299 HInstruction* right,
4300 uint32_t dex_pc)
4301 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004302
Roland Levillain9867bc72015-08-05 10:21:34 +01004303 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004304 T ComputeIntegral(T x, T y) const {
4305 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004306 // Our graph structure ensures we never have 0 for `y` during
4307 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004308 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004309 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004310 return (y == -1) ? -x : x / y;
4311 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004312
Roland Levillain31dd3d62016-02-16 12:21:02 +00004313 template <typename T>
4314 T ComputeFP(T x, T y) const {
4315 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4316 return x / y;
4317 }
4318
Roland Levillain9867bc72015-08-05 10:21:34 +01004319 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004320 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004321 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004322 }
4323 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004324 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004325 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4326 }
4327 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4328 return GetBlock()->GetGraph()->GetFloatConstant(
4329 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4330 }
4331 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4332 return GetBlock()->GetGraph()->GetDoubleConstant(
4333 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004334 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004335
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004336 static SideEffects SideEffectsForArchRuntimeCalls() {
4337 // The generated code can use a runtime call.
4338 return SideEffects::CanTriggerGC();
4339 }
4340
Calin Juravle7c4954d2014-10-28 16:57:40 +00004341 DECLARE_INSTRUCTION(Div);
4342
4343 private:
4344 DISALLOW_COPY_AND_ASSIGN(HDiv);
4345};
4346
Calin Juravlebacfec32014-11-14 15:54:36 +00004347class HRem : public HBinaryOperation {
4348 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004349 HRem(Primitive::Type result_type,
4350 HInstruction* left,
4351 HInstruction* right,
4352 uint32_t dex_pc)
4353 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004354
Roland Levillain9867bc72015-08-05 10:21:34 +01004355 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004356 T ComputeIntegral(T x, T y) const {
4357 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004358 // Our graph structure ensures we never have 0 for `y` during
4359 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004360 DCHECK_NE(y, 0);
4361 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4362 return (y == -1) ? 0 : x % y;
4363 }
4364
Roland Levillain31dd3d62016-02-16 12:21:02 +00004365 template <typename T>
4366 T ComputeFP(T x, T y) const {
4367 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4368 return std::fmod(x, y);
4369 }
4370
Roland Levillain9867bc72015-08-05 10:21:34 +01004371 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004372 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004373 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004374 }
4375 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004376 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004377 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004378 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004379 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4380 return GetBlock()->GetGraph()->GetFloatConstant(
4381 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4382 }
4383 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4384 return GetBlock()->GetGraph()->GetDoubleConstant(
4385 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4386 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004387
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004388 static SideEffects SideEffectsForArchRuntimeCalls() {
4389 return SideEffects::CanTriggerGC();
4390 }
4391
Calin Juravlebacfec32014-11-14 15:54:36 +00004392 DECLARE_INSTRUCTION(Rem);
4393
4394 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004395 DISALLOW_COPY_AND_ASSIGN(HRem);
4396};
4397
Calin Juravled0d48522014-11-04 16:40:20 +00004398class HDivZeroCheck : public HExpression<1> {
4399 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004400 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4401 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004402 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004403 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004404 SetRawInputAt(0, value);
4405 }
4406
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004407 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4408
Calin Juravled0d48522014-11-04 16:40:20 +00004409 bool CanBeMoved() const OVERRIDE { return true; }
4410
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004411 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004412 return true;
4413 }
4414
4415 bool NeedsEnvironment() const OVERRIDE { return true; }
4416 bool CanThrow() const OVERRIDE { return true; }
4417
Calin Juravled0d48522014-11-04 16:40:20 +00004418 DECLARE_INSTRUCTION(DivZeroCheck);
4419
4420 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004421 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4422};
4423
Calin Juravle9aec02f2014-11-18 23:06:35 +00004424class HShl : public HBinaryOperation {
4425 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004426 HShl(Primitive::Type result_type,
4427 HInstruction* left,
4428 HInstruction* right,
4429 uint32_t dex_pc = kNoDexPc)
4430 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004431
Roland Levillain9867bc72015-08-05 10:21:34 +01004432 template <typename T, typename U, typename V>
4433 T Compute(T x, U y, V max_shift_value) const {
4434 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4435 "V is not the unsigned integer type corresponding to T");
4436 return x << (y & max_shift_value);
4437 }
4438
4439 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4440 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004441 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004442 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004443 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4444 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004445 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004446 }
4447 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4448 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004449 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004450 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004451 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4452 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4453 LOG(FATAL) << DebugName() << " is not defined for float values";
4454 UNREACHABLE();
4455 }
4456 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4457 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4458 LOG(FATAL) << DebugName() << " is not defined for double values";
4459 UNREACHABLE();
4460 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004461
4462 DECLARE_INSTRUCTION(Shl);
4463
4464 private:
4465 DISALLOW_COPY_AND_ASSIGN(HShl);
4466};
4467
4468class HShr : public HBinaryOperation {
4469 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004470 HShr(Primitive::Type result_type,
4471 HInstruction* left,
4472 HInstruction* right,
4473 uint32_t dex_pc = kNoDexPc)
4474 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004475
Roland Levillain9867bc72015-08-05 10:21:34 +01004476 template <typename T, typename U, typename V>
4477 T Compute(T x, U y, V max_shift_value) const {
4478 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4479 "V is not the unsigned integer type corresponding to T");
4480 return x >> (y & max_shift_value);
4481 }
4482
4483 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4484 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004485 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004486 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004487 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4488 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004489 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004490 }
4491 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4492 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004494 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004495 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4496 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4497 LOG(FATAL) << DebugName() << " is not defined for float values";
4498 UNREACHABLE();
4499 }
4500 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4501 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4502 LOG(FATAL) << DebugName() << " is not defined for double values";
4503 UNREACHABLE();
4504 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004505
4506 DECLARE_INSTRUCTION(Shr);
4507
4508 private:
4509 DISALLOW_COPY_AND_ASSIGN(HShr);
4510};
4511
4512class HUShr : public HBinaryOperation {
4513 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004514 HUShr(Primitive::Type result_type,
4515 HInstruction* left,
4516 HInstruction* right,
4517 uint32_t dex_pc = kNoDexPc)
4518 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004519
Roland Levillain9867bc72015-08-05 10:21:34 +01004520 template <typename T, typename U, typename V>
4521 T Compute(T x, U y, V max_shift_value) const {
4522 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4523 "V is not the unsigned integer type corresponding to T");
4524 V ux = static_cast<V>(x);
4525 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004526 }
4527
Roland Levillain9867bc72015-08-05 10:21:34 +01004528 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4529 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004530 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004531 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004532 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4533 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004534 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004535 }
4536 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4537 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004538 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004539 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004540 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4541 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4542 LOG(FATAL) << DebugName() << " is not defined for float values";
4543 UNREACHABLE();
4544 }
4545 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4546 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4547 LOG(FATAL) << DebugName() << " is not defined for double values";
4548 UNREACHABLE();
4549 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004550
4551 DECLARE_INSTRUCTION(UShr);
4552
4553 private:
4554 DISALLOW_COPY_AND_ASSIGN(HUShr);
4555};
4556
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004557class HAnd : public HBinaryOperation {
4558 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004559 HAnd(Primitive::Type result_type,
4560 HInstruction* left,
4561 HInstruction* right,
4562 uint32_t dex_pc = kNoDexPc)
4563 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004564
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004565 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004566
Roland Levillaine53bd812016-02-24 14:54:18 +00004567 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004568
4569 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004570 return GetBlock()->GetGraph()->GetIntConstant(
4571 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004572 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004573 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004574 return GetBlock()->GetGraph()->GetLongConstant(
4575 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004576 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004577 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4578 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4579 LOG(FATAL) << DebugName() << " is not defined for float values";
4580 UNREACHABLE();
4581 }
4582 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4583 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4584 LOG(FATAL) << DebugName() << " is not defined for double values";
4585 UNREACHABLE();
4586 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004587
4588 DECLARE_INSTRUCTION(And);
4589
4590 private:
4591 DISALLOW_COPY_AND_ASSIGN(HAnd);
4592};
4593
4594class HOr : public HBinaryOperation {
4595 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004596 HOr(Primitive::Type result_type,
4597 HInstruction* left,
4598 HInstruction* right,
4599 uint32_t dex_pc = kNoDexPc)
4600 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004601
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004602 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004603
Roland Levillaine53bd812016-02-24 14:54:18 +00004604 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004605
4606 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004607 return GetBlock()->GetGraph()->GetIntConstant(
4608 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004609 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004610 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 return GetBlock()->GetGraph()->GetLongConstant(
4612 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004613 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004614 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4615 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4616 LOG(FATAL) << DebugName() << " is not defined for float values";
4617 UNREACHABLE();
4618 }
4619 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4620 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4621 LOG(FATAL) << DebugName() << " is not defined for double values";
4622 UNREACHABLE();
4623 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004624
4625 DECLARE_INSTRUCTION(Or);
4626
4627 private:
4628 DISALLOW_COPY_AND_ASSIGN(HOr);
4629};
4630
4631class HXor : public HBinaryOperation {
4632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004633 HXor(Primitive::Type result_type,
4634 HInstruction* left,
4635 HInstruction* right,
4636 uint32_t dex_pc = kNoDexPc)
4637 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004638
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004639 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004640
Roland Levillaine53bd812016-02-24 14:54:18 +00004641 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004642
4643 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004644 return GetBlock()->GetGraph()->GetIntConstant(
4645 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004646 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004647 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004648 return GetBlock()->GetGraph()->GetLongConstant(
4649 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004651 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4652 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4653 LOG(FATAL) << DebugName() << " is not defined for float values";
4654 UNREACHABLE();
4655 }
4656 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4657 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4658 LOG(FATAL) << DebugName() << " is not defined for double values";
4659 UNREACHABLE();
4660 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004661
4662 DECLARE_INSTRUCTION(Xor);
4663
4664 private:
4665 DISALLOW_COPY_AND_ASSIGN(HXor);
4666};
4667
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004668class HRor : public HBinaryOperation {
4669 public:
4670 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4671 : HBinaryOperation(result_type, value, distance) {}
4672
4673 template <typename T, typename U, typename V>
4674 T Compute(T x, U y, V max_shift_value) const {
4675 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4676 "V is not the unsigned integer type corresponding to T");
4677 V ux = static_cast<V>(x);
4678 if ((y & max_shift_value) == 0) {
4679 return static_cast<T>(ux);
4680 } else {
4681 const V reg_bits = sizeof(T) * 8;
4682 return static_cast<T>(ux >> (y & max_shift_value)) |
4683 (x << (reg_bits - (y & max_shift_value)));
4684 }
4685 }
4686
4687 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4688 return GetBlock()->GetGraph()->GetIntConstant(
4689 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4690 }
4691 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4692 return GetBlock()->GetGraph()->GetLongConstant(
4693 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4694 }
4695 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4696 return GetBlock()->GetGraph()->GetLongConstant(
4697 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4698 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004699 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4700 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4701 LOG(FATAL) << DebugName() << " is not defined for float values";
4702 UNREACHABLE();
4703 }
4704 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4705 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4706 LOG(FATAL) << DebugName() << " is not defined for double values";
4707 UNREACHABLE();
4708 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004709
4710 DECLARE_INSTRUCTION(Ror);
4711
4712 private:
4713 DISALLOW_COPY_AND_ASSIGN(HRor);
4714};
4715
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004716// The value of a parameter in this method. Its location depends on
4717// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004718class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004719 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004720 HParameterValue(const DexFile& dex_file,
4721 uint16_t type_index,
4722 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004723 Primitive::Type parameter_type,
4724 bool is_this = false)
4725 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004726 dex_file_(dex_file),
4727 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004728 index_(index) {
4729 SetPackedFlag<kFlagIsThis>(is_this);
4730 SetPackedFlag<kFlagCanBeNull>(!is_this);
4731 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004732
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004733 const DexFile& GetDexFile() const { return dex_file_; }
4734 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004735 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004736 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004737
Vladimir Markoa1de9182016-02-25 11:37:38 +00004738 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4739 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004740
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004741 DECLARE_INSTRUCTION(ParameterValue);
4742
4743 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004744 // Whether or not the parameter value corresponds to 'this' argument.
4745 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4746 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4747 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4748 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4749 "Too many packed fields.");
4750
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004751 const DexFile& dex_file_;
4752 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004753 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004754 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004755 const uint8_t index_;
4756
4757 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4758};
4759
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004760class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004761 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004762 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4763 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004764
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004765 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004766 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004767 return true;
4768 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004769
Roland Levillain9867bc72015-08-05 10:21:34 +01004770 template <typename T> T Compute(T x) const { return ~x; }
4771
4772 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004773 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004774 }
4775 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004776 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004777 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004778 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4779 LOG(FATAL) << DebugName() << " is not defined for float values";
4780 UNREACHABLE();
4781 }
4782 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4783 LOG(FATAL) << DebugName() << " is not defined for double values";
4784 UNREACHABLE();
4785 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004786
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004787 DECLARE_INSTRUCTION(Not);
4788
4789 private:
4790 DISALLOW_COPY_AND_ASSIGN(HNot);
4791};
4792
David Brazdil66d126e2015-04-03 16:02:44 +01004793class HBooleanNot : public HUnaryOperation {
4794 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004795 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4796 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004797
4798 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004799 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004800 return true;
4801 }
4802
Roland Levillain9867bc72015-08-05 10:21:34 +01004803 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004804 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004805 return !x;
4806 }
4807
Roland Levillain9867bc72015-08-05 10:21:34 +01004808 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004809 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004810 }
4811 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4812 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004813 UNREACHABLE();
4814 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004815 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4816 LOG(FATAL) << DebugName() << " is not defined for float values";
4817 UNREACHABLE();
4818 }
4819 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4820 LOG(FATAL) << DebugName() << " is not defined for double values";
4821 UNREACHABLE();
4822 }
David Brazdil66d126e2015-04-03 16:02:44 +01004823
4824 DECLARE_INSTRUCTION(BooleanNot);
4825
4826 private:
4827 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4828};
4829
Roland Levillaindff1f282014-11-05 14:15:05 +00004830class HTypeConversion : public HExpression<1> {
4831 public:
4832 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004833 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004834 : HExpression(result_type,
4835 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4836 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004837 SetRawInputAt(0, input);
4838 DCHECK_NE(input->GetType(), result_type);
4839 }
4840
4841 HInstruction* GetInput() const { return InputAt(0); }
4842 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4843 Primitive::Type GetResultType() const { return GetType(); }
4844
4845 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004846 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004847
Mark Mendelle82549b2015-05-06 10:55:34 -04004848 // Try to statically evaluate the conversion and return a HConstant
4849 // containing the result. If the input cannot be converted, return nullptr.
4850 HConstant* TryStaticEvaluation() const;
4851
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004852 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4853 Primitive::Type result_type) {
4854 // Some architectures may not require the 'GC' side effects, but at this point
4855 // in the compilation process we do not know what architecture we will
4856 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004857 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4858 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004859 return SideEffects::CanTriggerGC();
4860 }
4861 return SideEffects::None();
4862 }
4863
Roland Levillaindff1f282014-11-05 14:15:05 +00004864 DECLARE_INSTRUCTION(TypeConversion);
4865
4866 private:
4867 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4868};
4869
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004870static constexpr uint32_t kNoRegNumber = -1;
4871
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004872class HPhi : public HInstruction {
4873 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004874 HPhi(ArenaAllocator* arena,
4875 uint32_t reg_number,
4876 size_t number_of_inputs,
4877 Primitive::Type type,
4878 uint32_t dex_pc = kNoDexPc)
4879 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004880 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004881 reg_number_(reg_number) {
4882 SetPackedField<TypeField>(ToPhiType(type));
4883 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4884 // Phis are constructed live and marked dead if conflicting or unused.
4885 // Individual steps of SsaBuilder should assume that if a phi has been
4886 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4887 SetPackedFlag<kFlagIsLive>(true);
4888 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004889 }
4890
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004891 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4892 static Primitive::Type ToPhiType(Primitive::Type type) {
4893 switch (type) {
4894 case Primitive::kPrimBoolean:
4895 case Primitive::kPrimByte:
4896 case Primitive::kPrimShort:
4897 case Primitive::kPrimChar:
4898 return Primitive::kPrimInt;
4899 default:
4900 return type;
4901 }
4902 }
4903
David Brazdilffee3d32015-07-06 11:48:53 +01004904 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4905
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004906 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004907
4908 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004909 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004910
Vladimir Markoa1de9182016-02-25 11:37:38 +00004911 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004912 void SetType(Primitive::Type new_type) {
4913 // Make sure that only valid type changes occur. The following are allowed:
4914 // (1) int -> float/ref (primitive type propagation),
4915 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004916 DCHECK(GetType() == new_type ||
4917 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4918 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4919 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4920 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004921 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004922
Vladimir Markoa1de9182016-02-25 11:37:38 +00004923 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4924 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004925
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004926 uint32_t GetRegNumber() const { return reg_number_; }
4927
Vladimir Markoa1de9182016-02-25 11:37:38 +00004928 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4929 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4930 bool IsDead() const { return !IsLive(); }
4931 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004932
David Brazdil77a48ae2015-09-15 12:34:04 +00004933 bool IsVRegEquivalentOf(HInstruction* other) const {
4934 return other != nullptr
4935 && other->IsPhi()
4936 && other->AsPhi()->GetBlock() == GetBlock()
4937 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4938 }
4939
Calin Juravlea4f88312015-04-16 12:57:19 +01004940 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4941 // An equivalent phi is a phi having the same dex register and type.
4942 // It assumes that phis with the same dex register are adjacent.
4943 HPhi* GetNextEquivalentPhiWithSameType() {
4944 HInstruction* next = GetNext();
4945 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4946 if (next->GetType() == GetType()) {
4947 return next->AsPhi();
4948 }
4949 next = next->GetNext();
4950 }
4951 return nullptr;
4952 }
4953
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004954 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004955
David Brazdil1abb4192015-02-17 18:33:36 +00004956 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004957 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004958 return inputs_[index];
4959 }
David Brazdil1abb4192015-02-17 18:33:36 +00004960
4961 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004962 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004963 }
4964
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004965 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004966 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
4967 static constexpr size_t kFieldTypeSize =
4968 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4969 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
4970 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
4971 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
4972 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4973 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
4974
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004975 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004976 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004977
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004978 DISALLOW_COPY_AND_ASSIGN(HPhi);
4979};
4980
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004981class HNullCheck : public HExpression<1> {
4982 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004983 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4984 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004986 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004987 SetRawInputAt(0, value);
4988 }
4989
Calin Juravle10e244f2015-01-26 18:54:32 +00004990 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004991 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004992 return true;
4993 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004994
Calin Juravle10e244f2015-01-26 18:54:32 +00004995 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004996
Calin Juravle10e244f2015-01-26 18:54:32 +00004997 bool CanThrow() const OVERRIDE { return true; }
4998
4999 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005000
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005001
5002 DECLARE_INSTRUCTION(NullCheck);
5003
5004 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005005 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5006};
5007
5008class FieldInfo : public ValueObject {
5009 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005010 FieldInfo(MemberOffset field_offset,
5011 Primitive::Type field_type,
5012 bool is_volatile,
5013 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005014 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005015 const DexFile& dex_file,
5016 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005017 : field_offset_(field_offset),
5018 field_type_(field_type),
5019 is_volatile_(is_volatile),
5020 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005021 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005022 dex_file_(dex_file),
5023 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005024
5025 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005026 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005027 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005028 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005029 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005030 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005031 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005032
5033 private:
5034 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005035 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005036 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005037 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005038 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005039 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005040 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005041};
5042
5043class HInstanceFieldGet : public HExpression<1> {
5044 public:
5045 HInstanceFieldGet(HInstruction* value,
5046 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005047 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005048 bool is_volatile,
5049 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005050 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005051 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005052 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005053 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005054 : HExpression(field_type,
5055 SideEffects::FieldReadOfType(field_type, is_volatile),
5056 dex_pc),
5057 field_info_(field_offset,
5058 field_type,
5059 is_volatile,
5060 field_idx,
5061 declaring_class_def_index,
5062 dex_file,
5063 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005064 SetRawInputAt(0, value);
5065 }
5066
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005067 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005068
5069 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5070 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5071 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005072 }
5073
Calin Juravle641547a2015-04-21 22:08:51 +01005074 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5075 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005076 }
5077
5078 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005079 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5080 }
5081
Calin Juravle52c48962014-12-16 17:02:57 +00005082 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005083 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005084 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005085 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086
5087 DECLARE_INSTRUCTION(InstanceFieldGet);
5088
5089 private:
5090 const FieldInfo field_info_;
5091
5092 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5093};
5094
5095class HInstanceFieldSet : public HTemplateInstruction<2> {
5096 public:
5097 HInstanceFieldSet(HInstruction* object,
5098 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005099 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005100 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005101 bool is_volatile,
5102 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005103 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005104 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005105 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005106 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005107 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5108 dex_pc),
5109 field_info_(field_offset,
5110 field_type,
5111 is_volatile,
5112 field_idx,
5113 declaring_class_def_index,
5114 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005115 dex_cache) {
5116 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005117 SetRawInputAt(0, object);
5118 SetRawInputAt(1, value);
5119 }
5120
Calin Juravle641547a2015-04-21 22:08:51 +01005121 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5122 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005123 }
5124
Calin Juravle52c48962014-12-16 17:02:57 +00005125 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005126 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005127 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005128 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005129 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005130 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5131 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005132
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005133 DECLARE_INSTRUCTION(InstanceFieldSet);
5134
5135 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005136 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5137 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5138 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5139 "Too many packed fields.");
5140
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005141 const FieldInfo field_info_;
5142
5143 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5144};
5145
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146class HArrayGet : public HExpression<2> {
5147 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005148 HArrayGet(HInstruction* array,
5149 HInstruction* index,
5150 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005151 uint32_t dex_pc,
5152 SideEffects additional_side_effects = SideEffects::None())
5153 : HExpression(type,
5154 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005155 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005156 SetRawInputAt(0, array);
5157 SetRawInputAt(1, index);
5158 }
5159
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005160 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005161 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005162 return true;
5163 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005164 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005165 // TODO: We can be smarter here.
5166 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5167 // which generates the implicit null check. There are cases when these can be removed
5168 // to produce better code. If we ever add optimizations to do so we should allow an
5169 // implicit check here (as long as the address falls in the first page).
5170 return false;
5171 }
5172
David Brazdil4833f5a2015-12-16 10:37:39 +00005173 bool IsEquivalentOf(HArrayGet* other) const {
5174 bool result = (GetDexPc() == other->GetDexPc());
5175 if (kIsDebugBuild && result) {
5176 DCHECK_EQ(GetBlock(), other->GetBlock());
5177 DCHECK_EQ(GetArray(), other->GetArray());
5178 DCHECK_EQ(GetIndex(), other->GetIndex());
5179 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005180 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005181 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005182 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5183 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005184 }
5185 }
5186 return result;
5187 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005188
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005189 HInstruction* GetArray() const { return InputAt(0); }
5190 HInstruction* GetIndex() const { return InputAt(1); }
5191
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005192 DECLARE_INSTRUCTION(ArrayGet);
5193
5194 private:
5195 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5196};
5197
5198class HArraySet : public HTemplateInstruction<3> {
5199 public:
5200 HArraySet(HInstruction* array,
5201 HInstruction* index,
5202 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005203 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005204 uint32_t dex_pc,
5205 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005206 : HTemplateInstruction(
5207 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005208 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5209 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005210 dex_pc) {
5211 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5212 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5213 SetPackedFlag<kFlagValueCanBeNull>(true);
5214 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 SetRawInputAt(0, array);
5216 SetRawInputAt(1, index);
5217 SetRawInputAt(2, value);
5218 }
5219
Calin Juravle77520bc2015-01-12 18:45:46 +00005220 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005221 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005222 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005223 }
5224
Mingyao Yang81014cb2015-06-02 03:16:27 -07005225 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005226 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005227
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005228 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005229 // TODO: Same as for ArrayGet.
5230 return false;
5231 }
5232
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005233 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005234 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005235 }
5236
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005237 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005238 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005239 }
5240
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005241 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005242 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005243 }
5244
Vladimir Markoa1de9182016-02-25 11:37:38 +00005245 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5246 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5247 bool StaticTypeOfArrayIsObjectArray() const {
5248 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5249 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005250
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005251 HInstruction* GetArray() const { return InputAt(0); }
5252 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005253 HInstruction* GetValue() const { return InputAt(2); }
5254
5255 Primitive::Type GetComponentType() const {
5256 // The Dex format does not type floating point index operations. Since the
5257 // `expected_component_type_` is set during building and can therefore not
5258 // be correct, we also check what is the value type. If it is a floating
5259 // point type, we must use that type.
5260 Primitive::Type value_type = GetValue()->GetType();
5261 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5262 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005263 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005264 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005265
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005266 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005267 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005268 }
5269
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005270 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5271 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5272 }
5273
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274 DECLARE_INSTRUCTION(ArraySet);
5275
5276 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005277 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5278 static constexpr size_t kFieldExpectedComponentTypeSize =
5279 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5280 static constexpr size_t kFlagNeedsTypeCheck =
5281 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5282 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 // Cached information for the reference_type_info_ so that codegen
5284 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005285 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5286 static constexpr size_t kNumberOfArraySetPackedBits =
5287 kFlagStaticTypeOfArrayIsObjectArray + 1;
5288 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5289 using ExpectedComponentTypeField =
5290 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005291
5292 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5293};
5294
5295class HArrayLength : public HExpression<1> {
5296 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005297 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005298 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005299 // Note that arrays do not change length, so the instruction does not
5300 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005301 SetRawInputAt(0, array);
5302 }
5303
Calin Juravle77520bc2015-01-12 18:45:46 +00005304 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005305 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005306 return true;
5307 }
Calin Juravle641547a2015-04-21 22:08:51 +01005308 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5309 return obj == InputAt(0);
5310 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005311
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005312 DECLARE_INSTRUCTION(ArrayLength);
5313
5314 private:
5315 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5316};
5317
5318class HBoundsCheck : public HExpression<2> {
5319 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005320 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5321 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005323 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005324 DCHECK(index->GetType() == Primitive::kPrimInt);
5325 SetRawInputAt(0, index);
5326 SetRawInputAt(1, length);
5327 }
5328
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005329 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005330 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005331 return true;
5332 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005334 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005336 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005337
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005338 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339
5340 DECLARE_INSTRUCTION(BoundsCheck);
5341
5342 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005343 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5344};
5345
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005346class HSuspendCheck : public HTemplateInstruction<0> {
5347 public:
5348 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005349 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005350
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005351 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005352 return true;
5353 }
5354
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005355 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5356 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005357
5358 DECLARE_INSTRUCTION(SuspendCheck);
5359
5360 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005361 // Only used for code generation, in order to share the same slow path between back edges
5362 // of a same loop.
5363 SlowPathCode* slow_path_;
5364
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005365 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5366};
5367
David Srbecky0cf44932015-12-09 14:09:59 +00005368// Pseudo-instruction which provides the native debugger with mapping information.
5369// It ensures that we can generate line number and local variables at this point.
5370class HNativeDebugInfo : public HTemplateInstruction<0> {
5371 public:
5372 explicit HNativeDebugInfo(uint32_t dex_pc)
5373 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5374
5375 bool NeedsEnvironment() const OVERRIDE {
5376 return true;
5377 }
5378
5379 DECLARE_INSTRUCTION(NativeDebugInfo);
5380
5381 private:
5382 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5383};
5384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005385/**
5386 * Instruction to load a Class object.
5387 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005388class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005389 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005390 HLoadClass(HCurrentMethod* current_method,
5391 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005392 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005393 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005394 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005395 bool needs_access_check,
5396 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005397 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005398 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005399 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005400 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005401 // Referrers class should not need access check. We never inline unverified
5402 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005403 DCHECK(!is_referrers_class || !needs_access_check);
5404
5405 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5406 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5407 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5408 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005409 SetRawInputAt(0, current_method);
5410 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005411
5412 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005413
5414 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005415 // Note that we don't need to test for generate_clinit_check_.
5416 // Whether or not we need to generate the clinit check is processed in
5417 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005418 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005419 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005420 }
5421
5422 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5423
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005424 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005425 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005426
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005427 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005428 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005429 }
5430
Calin Juravle0ba218d2015-05-19 18:46:01 +01005431 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005432 // The entrypoint the code generator is going to call does not do
5433 // clinit of the class.
5434 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005435 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005436 }
5437
5438 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005439 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005440 (!IsReferrersClass() && !IsInDexCache()) ||
5441 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005442 }
5443
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005444
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005445 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005446 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005447 }
5448
Calin Juravleacf735c2015-02-12 15:25:22 +00005449 ReferenceTypeInfo GetLoadedClassRTI() {
5450 return loaded_class_rti_;
5451 }
5452
5453 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5454 // Make sure we only set exact types (the loaded class should never be merged).
5455 DCHECK(rti.IsExact());
5456 loaded_class_rti_ = rti;
5457 }
5458
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005459 const DexFile& GetDexFile() { return dex_file_; }
5460
Vladimir Markoa1de9182016-02-25 11:37:38 +00005461 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005462
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005463 static SideEffects SideEffectsForArchRuntimeCalls() {
5464 return SideEffects::CanTriggerGC();
5465 }
5466
Vladimir Markoa1de9182016-02-25 11:37:38 +00005467 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5468 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5469 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5470 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005471
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005472 DECLARE_INSTRUCTION(LoadClass);
5473
5474 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005475 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5476 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5477 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005478 // Whether this instruction must generate the initialization check.
5479 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005480 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5481 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5482 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5483
5484 const uint16_t type_index_;
5485 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005486
Calin Juravleacf735c2015-02-12 15:25:22 +00005487 ReferenceTypeInfo loaded_class_rti_;
5488
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005489 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5490};
5491
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005492class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005493 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005494 HLoadString(HCurrentMethod* current_method,
5495 uint32_t string_index,
5496 uint32_t dex_pc,
5497 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005498 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005499 string_index_(string_index) {
5500 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005501 SetRawInputAt(0, current_method);
5502 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005503
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005504 bool CanBeMoved() const OVERRIDE { return true; }
5505
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005506 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5507 return other->AsLoadString()->string_index_ == string_index_;
5508 }
5509
5510 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5511
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005512 uint32_t GetStringIndex() const { return string_index_; }
5513
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005514 // Will call the runtime if the string is not already in the dex cache.
5515 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5516
Vladimir Markodc151b22015-10-15 18:02:30 +01005517 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005518 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005519 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005520
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005521 static SideEffects SideEffectsForArchRuntimeCalls() {
5522 return SideEffects::CanTriggerGC();
5523 }
5524
Vladimir Markoa1de9182016-02-25 11:37:38 +00005525 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5526
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005527 DECLARE_INSTRUCTION(LoadString);
5528
5529 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005530 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
5531 static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1;
5532 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5533
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005534 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005535
5536 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5537};
5538
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005539/**
5540 * Performs an initialization check on its Class object input.
5541 */
5542class HClinitCheck : public HExpression<1> {
5543 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005544 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005545 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005546 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005547 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5548 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005549 SetRawInputAt(0, constant);
5550 }
5551
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005552 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005553 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005554 return true;
5555 }
5556
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005557 bool NeedsEnvironment() const OVERRIDE {
5558 // May call runtime to initialize the class.
5559 return true;
5560 }
5561
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005562 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005563
5564 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5565
5566 DECLARE_INSTRUCTION(ClinitCheck);
5567
5568 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005569 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5570};
5571
5572class HStaticFieldGet : public HExpression<1> {
5573 public:
5574 HStaticFieldGet(HInstruction* cls,
5575 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005576 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005577 bool is_volatile,
5578 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005579 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005580 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005581 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005582 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005583 : HExpression(field_type,
5584 SideEffects::FieldReadOfType(field_type, is_volatile),
5585 dex_pc),
5586 field_info_(field_offset,
5587 field_type,
5588 is_volatile,
5589 field_idx,
5590 declaring_class_def_index,
5591 dex_file,
5592 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005593 SetRawInputAt(0, cls);
5594 }
5595
Calin Juravle52c48962014-12-16 17:02:57 +00005596
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005597 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005598
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005599 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005600 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5601 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005602 }
5603
5604 size_t ComputeHashCode() const OVERRIDE {
5605 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5606 }
5607
Calin Juravle52c48962014-12-16 17:02:57 +00005608 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005609 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5610 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005611 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005612
5613 DECLARE_INSTRUCTION(StaticFieldGet);
5614
5615 private:
5616 const FieldInfo field_info_;
5617
5618 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5619};
5620
5621class HStaticFieldSet : public HTemplateInstruction<2> {
5622 public:
5623 HStaticFieldSet(HInstruction* cls,
5624 HInstruction* value,
5625 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005626 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005627 bool is_volatile,
5628 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005629 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005630 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005631 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005632 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005633 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5634 dex_pc),
5635 field_info_(field_offset,
5636 field_type,
5637 is_volatile,
5638 field_idx,
5639 declaring_class_def_index,
5640 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005641 dex_cache) {
5642 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005643 SetRawInputAt(0, cls);
5644 SetRawInputAt(1, value);
5645 }
5646
Calin Juravle52c48962014-12-16 17:02:57 +00005647 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005648 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5649 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005650 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005651
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005652 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005653 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5654 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005655
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005656 DECLARE_INSTRUCTION(StaticFieldSet);
5657
5658 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005659 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5660 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5661 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5662 "Too many packed fields.");
5663
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005664 const FieldInfo field_info_;
5665
5666 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5667};
5668
Calin Juravlee460d1d2015-09-29 04:52:17 +01005669class HUnresolvedInstanceFieldGet : public HExpression<1> {
5670 public:
5671 HUnresolvedInstanceFieldGet(HInstruction* obj,
5672 Primitive::Type field_type,
5673 uint32_t field_index,
5674 uint32_t dex_pc)
5675 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5676 field_index_(field_index) {
5677 SetRawInputAt(0, obj);
5678 }
5679
5680 bool NeedsEnvironment() const OVERRIDE { return true; }
5681 bool CanThrow() const OVERRIDE { return true; }
5682
5683 Primitive::Type GetFieldType() const { return GetType(); }
5684 uint32_t GetFieldIndex() const { return field_index_; }
5685
5686 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5687
5688 private:
5689 const uint32_t field_index_;
5690
5691 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5692};
5693
5694class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5695 public:
5696 HUnresolvedInstanceFieldSet(HInstruction* obj,
5697 HInstruction* value,
5698 Primitive::Type field_type,
5699 uint32_t field_index,
5700 uint32_t dex_pc)
5701 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005702 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005703 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005704 DCHECK_EQ(field_type, value->GetType());
5705 SetRawInputAt(0, obj);
5706 SetRawInputAt(1, value);
5707 }
5708
5709 bool NeedsEnvironment() const OVERRIDE { return true; }
5710 bool CanThrow() const OVERRIDE { return true; }
5711
Vladimir Markoa1de9182016-02-25 11:37:38 +00005712 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005713 uint32_t GetFieldIndex() const { return field_index_; }
5714
5715 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5716
5717 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005718 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5719 static constexpr size_t kFieldFieldTypeSize =
5720 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5721 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5722 kFieldFieldType + kFieldFieldTypeSize;
5723 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5724 "Too many packed fields.");
5725 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5726
Calin Juravlee460d1d2015-09-29 04:52:17 +01005727 const uint32_t field_index_;
5728
5729 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5730};
5731
5732class HUnresolvedStaticFieldGet : public HExpression<0> {
5733 public:
5734 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5735 uint32_t field_index,
5736 uint32_t dex_pc)
5737 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5738 field_index_(field_index) {
5739 }
5740
5741 bool NeedsEnvironment() const OVERRIDE { return true; }
5742 bool CanThrow() const OVERRIDE { return true; }
5743
5744 Primitive::Type GetFieldType() const { return GetType(); }
5745 uint32_t GetFieldIndex() const { return field_index_; }
5746
5747 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5748
5749 private:
5750 const uint32_t field_index_;
5751
5752 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5753};
5754
5755class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5756 public:
5757 HUnresolvedStaticFieldSet(HInstruction* value,
5758 Primitive::Type field_type,
5759 uint32_t field_index,
5760 uint32_t dex_pc)
5761 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005762 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005763 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005764 DCHECK_EQ(field_type, value->GetType());
5765 SetRawInputAt(0, value);
5766 }
5767
5768 bool NeedsEnvironment() const OVERRIDE { return true; }
5769 bool CanThrow() const OVERRIDE { return true; }
5770
Vladimir Markoa1de9182016-02-25 11:37:38 +00005771 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005772 uint32_t GetFieldIndex() const { return field_index_; }
5773
5774 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5775
5776 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005777 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5778 static constexpr size_t kFieldFieldTypeSize =
5779 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5780 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5781 kFieldFieldType + kFieldFieldTypeSize;
5782 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5783 "Too many packed fields.");
5784 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5785
Calin Juravlee460d1d2015-09-29 04:52:17 +01005786 const uint32_t field_index_;
5787
5788 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5789};
5790
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005791// Implement the move-exception DEX instruction.
5792class HLoadException : public HExpression<0> {
5793 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005794 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5795 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005796
David Brazdilbbd733e2015-08-18 17:48:17 +01005797 bool CanBeNull() const OVERRIDE { return false; }
5798
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005799 DECLARE_INSTRUCTION(LoadException);
5800
5801 private:
5802 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5803};
5804
David Brazdilcb1c0552015-08-04 16:22:25 +01005805// Implicit part of move-exception which clears thread-local exception storage.
5806// Must not be removed because the runtime expects the TLS to get cleared.
5807class HClearException : public HTemplateInstruction<0> {
5808 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005809 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5810 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005811
5812 DECLARE_INSTRUCTION(ClearException);
5813
5814 private:
5815 DISALLOW_COPY_AND_ASSIGN(HClearException);
5816};
5817
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005818class HThrow : public HTemplateInstruction<1> {
5819 public:
5820 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005821 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005822 SetRawInputAt(0, exception);
5823 }
5824
5825 bool IsControlFlow() const OVERRIDE { return true; }
5826
5827 bool NeedsEnvironment() const OVERRIDE { return true; }
5828
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005829 bool CanThrow() const OVERRIDE { return true; }
5830
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005831
5832 DECLARE_INSTRUCTION(Throw);
5833
5834 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005835 DISALLOW_COPY_AND_ASSIGN(HThrow);
5836};
5837
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838/**
5839 * Implementation strategies for the code generator of a HInstanceOf
5840 * or `HCheckCast`.
5841 */
5842enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005843 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 kExactCheck, // Can do a single class compare.
5845 kClassHierarchyCheck, // Can just walk the super class chain.
5846 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5847 kInterfaceCheck, // No optimization yet when checking against an interface.
5848 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005849 kArrayCheck, // No optimization yet when checking against a generic array.
5850 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851};
5852
Roland Levillain86503782016-02-11 19:07:30 +00005853std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5854
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005855class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005856 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005857 HInstanceOf(HInstruction* object,
5858 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005859 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005860 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005861 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005862 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005863 dex_pc) {
5864 SetPackedField<TypeCheckKindField>(check_kind);
5865 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005866 SetRawInputAt(0, object);
5867 SetRawInputAt(1, constant);
5868 }
5869
5870 bool CanBeMoved() const OVERRIDE { return true; }
5871
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005872 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005873 return true;
5874 }
5875
5876 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005877 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005878 }
5879
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005880 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005881 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5882 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5883 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5884 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005885
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005886 static bool CanCallRuntime(TypeCheckKind check_kind) {
5887 // Mips currently does runtime calls for any other checks.
5888 return check_kind != TypeCheckKind::kExactCheck;
5889 }
5890
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005891 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005892 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005893 }
5894
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005895 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005896
5897 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005898 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5899 static constexpr size_t kFieldTypeCheckKindSize =
5900 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5901 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5902 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5903 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5904 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005905
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005906 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5907};
5908
Calin Juravleb1498f62015-02-16 13:13:29 +00005909class HBoundType : public HExpression<1> {
5910 public:
David Brazdilf5552582015-12-27 13:36:12 +00005911 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005912 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005913 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5914 SetPackedFlag<kFlagUpperCanBeNull>(true);
5915 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005916 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005917 SetRawInputAt(0, input);
5918 }
5919
David Brazdilf5552582015-12-27 13:36:12 +00005920 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005921 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005922 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005923 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005924
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005925 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005926 DCHECK(GetUpperCanBeNull() || !can_be_null);
5927 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005928 }
5929
Vladimir Markoa1de9182016-02-25 11:37:38 +00005930 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005931
Calin Juravleb1498f62015-02-16 13:13:29 +00005932 DECLARE_INSTRUCTION(BoundType);
5933
5934 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005935 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5936 // is false then CanBeNull() cannot be true).
5937 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
5938 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
5939 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
5940 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5941
Calin Juravleb1498f62015-02-16 13:13:29 +00005942 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005943 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5944 // It is used to bound the type in cases like:
5945 // if (x instanceof ClassX) {
5946 // // uper_bound_ will be ClassX
5947 // }
David Brazdilf5552582015-12-27 13:36:12 +00005948 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005949
5950 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5951};
5952
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005953class HCheckCast : public HTemplateInstruction<2> {
5954 public:
5955 HCheckCast(HInstruction* object,
5956 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005958 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00005959 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
5960 SetPackedField<TypeCheckKindField>(check_kind);
5961 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005962 SetRawInputAt(0, object);
5963 SetRawInputAt(1, constant);
5964 }
5965
5966 bool CanBeMoved() const OVERRIDE { return true; }
5967
5968 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5969 return true;
5970 }
5971
5972 bool NeedsEnvironment() const OVERRIDE {
5973 // Instruction may throw a CheckCastError.
5974 return true;
5975 }
5976
5977 bool CanThrow() const OVERRIDE { return true; }
5978
Vladimir Markoa1de9182016-02-25 11:37:38 +00005979 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5980 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5981 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5982 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005983
5984 DECLARE_INSTRUCTION(CheckCast);
5985
5986 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005987 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
5988 static constexpr size_t kFieldTypeCheckKindSize =
5989 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5990 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5991 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
5992 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5993 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005994
5995 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005996};
5997
Calin Juravle27df7582015-04-17 19:12:31 +01005998class HMemoryBarrier : public HTemplateInstruction<0> {
5999 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006000 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006001 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006002 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6003 SetPackedField<BarrierKindField>(barrier_kind);
6004 }
Calin Juravle27df7582015-04-17 19:12:31 +01006005
Vladimir Markoa1de9182016-02-25 11:37:38 +00006006 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006007
6008 DECLARE_INSTRUCTION(MemoryBarrier);
6009
6010 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006011 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6012 static constexpr size_t kFieldBarrierKindSize =
6013 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6014 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6015 kFieldBarrierKind + kFieldBarrierKindSize;
6016 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6017 "Too many packed fields.");
6018 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006019
6020 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6021};
6022
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006023class HMonitorOperation : public HTemplateInstruction<1> {
6024 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006025 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006026 kEnter,
6027 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006028 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006029 };
6030
6031 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006032 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006033 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6034 dex_pc) {
6035 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006036 SetRawInputAt(0, object);
6037 }
6038
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006039 // Instruction may go into runtime, so we need an environment.
6040 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006041
6042 bool CanThrow() const OVERRIDE {
6043 // Verifier guarantees that monitor-exit cannot throw.
6044 // This is important because it allows the HGraphBuilder to remove
6045 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6046 return IsEnter();
6047 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006048
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6050 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006051
6052 DECLARE_INSTRUCTION(MonitorOperation);
6053
Calin Juravle52c48962014-12-16 17:02:57 +00006054 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006055 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6056 static constexpr size_t kFieldOperationKindSize =
6057 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6058 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6059 kFieldOperationKind + kFieldOperationKindSize;
6060 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6061 "Too many packed fields.");
6062 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006063
6064 private:
6065 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6066};
6067
David Brazdil74eb1b22015-12-14 11:44:01 +00006068class HSelect : public HExpression<3> {
6069 public:
6070 HSelect(HInstruction* condition,
6071 HInstruction* true_value,
6072 HInstruction* false_value,
6073 uint32_t dex_pc)
6074 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6075 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6076
6077 // First input must be `true_value` or `false_value` to allow codegens to
6078 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6079 // that architectures which implement HSelect as a conditional move also
6080 // will not need to invert the condition.
6081 SetRawInputAt(0, false_value);
6082 SetRawInputAt(1, true_value);
6083 SetRawInputAt(2, condition);
6084 }
6085
6086 HInstruction* GetFalseValue() const { return InputAt(0); }
6087 HInstruction* GetTrueValue() const { return InputAt(1); }
6088 HInstruction* GetCondition() const { return InputAt(2); }
6089
6090 bool CanBeMoved() const OVERRIDE { return true; }
6091 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6092
6093 bool CanBeNull() const OVERRIDE {
6094 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6095 }
6096
6097 DECLARE_INSTRUCTION(Select);
6098
6099 private:
6100 DISALLOW_COPY_AND_ASSIGN(HSelect);
6101};
6102
Vladimir Markof9f64412015-09-02 14:05:49 +01006103class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006104 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006105 MoveOperands(Location source,
6106 Location destination,
6107 Primitive::Type type,
6108 HInstruction* instruction)
6109 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006110
6111 Location GetSource() const { return source_; }
6112 Location GetDestination() const { return destination_; }
6113
6114 void SetSource(Location value) { source_ = value; }
6115 void SetDestination(Location value) { destination_ = value; }
6116
6117 // The parallel move resolver marks moves as "in-progress" by clearing the
6118 // destination (but not the source).
6119 Location MarkPending() {
6120 DCHECK(!IsPending());
6121 Location dest = destination_;
6122 destination_ = Location::NoLocation();
6123 return dest;
6124 }
6125
6126 void ClearPending(Location dest) {
6127 DCHECK(IsPending());
6128 destination_ = dest;
6129 }
6130
6131 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006132 DCHECK(source_.IsValid() || destination_.IsInvalid());
6133 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006134 }
6135
6136 // True if this blocks a move from the given location.
6137 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006138 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006139 }
6140
6141 // A move is redundant if it's been eliminated, if its source and
6142 // destination are the same, or if its destination is unneeded.
6143 bool IsRedundant() const {
6144 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6145 }
6146
6147 // We clear both operands to indicate move that's been eliminated.
6148 void Eliminate() {
6149 source_ = destination_ = Location::NoLocation();
6150 }
6151
6152 bool IsEliminated() const {
6153 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6154 return source_.IsInvalid();
6155 }
6156
Alexey Frunze4dda3372015-06-01 18:31:49 -07006157 Primitive::Type GetType() const { return type_; }
6158
Nicolas Geoffray90218252015-04-15 11:56:51 +01006159 bool Is64BitMove() const {
6160 return Primitive::Is64BitType(type_);
6161 }
6162
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006163 HInstruction* GetInstruction() const { return instruction_; }
6164
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006165 private:
6166 Location source_;
6167 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006168 // The type this move is for.
6169 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006170 // The instruction this move is assocatied with. Null when this move is
6171 // for moving an input in the expected locations of user (including a phi user).
6172 // This is only used in debug mode, to ensure we do not connect interval siblings
6173 // in the same parallel move.
6174 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006175};
6176
Roland Levillainc9285912015-12-18 10:38:42 +00006177std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6178
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006179static constexpr size_t kDefaultNumberOfMoves = 4;
6180
6181class HParallelMove : public HTemplateInstruction<0> {
6182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006183 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006184 : HTemplateInstruction(SideEffects::None(), dex_pc),
6185 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6186 moves_.reserve(kDefaultNumberOfMoves);
6187 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006188
Nicolas Geoffray90218252015-04-15 11:56:51 +01006189 void AddMove(Location source,
6190 Location destination,
6191 Primitive::Type type,
6192 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006193 DCHECK(source.IsValid());
6194 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006195 if (kIsDebugBuild) {
6196 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006197 for (const MoveOperands& move : moves_) {
6198 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006199 // Special case the situation where the move is for the spill slot
6200 // of the instruction.
6201 if ((GetPrevious() == instruction)
6202 || ((GetPrevious() == nullptr)
6203 && instruction->IsPhi()
6204 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006205 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006206 << "Doing parallel moves for the same instruction.";
6207 } else {
6208 DCHECK(false) << "Doing parallel moves for the same instruction.";
6209 }
6210 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006211 }
6212 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006213 for (const MoveOperands& move : moves_) {
6214 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006215 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006216 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006217 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006218 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006219 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006220 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006221 }
6222
Vladimir Marko225b6462015-09-28 12:17:40 +01006223 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006224 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006225 }
6226
Vladimir Marko225b6462015-09-28 12:17:40 +01006227 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006228
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006229 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006230
6231 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006232 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006233
6234 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6235};
6236
Mark Mendell0616ae02015-04-17 12:49:27 -04006237} // namespace art
6238
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006239#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6240#include "nodes_shared.h"
6241#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006242#ifdef ART_ENABLE_CODEGEN_arm
6243#include "nodes_arm.h"
6244#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006245#ifdef ART_ENABLE_CODEGEN_arm64
6246#include "nodes_arm64.h"
6247#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006248#ifdef ART_ENABLE_CODEGEN_x86
6249#include "nodes_x86.h"
6250#endif
6251
6252namespace art {
6253
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006254class HGraphVisitor : public ValueObject {
6255 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006256 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6257 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006258
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006259 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006260 virtual void VisitBasicBlock(HBasicBlock* block);
6261
Roland Levillain633021e2014-10-01 14:12:25 +01006262 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006263 void VisitInsertionOrder();
6264
Roland Levillain633021e2014-10-01 14:12:25 +01006265 // Visit the graph following dominator tree reverse post-order.
6266 void VisitReversePostOrder();
6267
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006268 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006269
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006270 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006271#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006272 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6273
6274 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6275
6276#undef DECLARE_VISIT_INSTRUCTION
6277
6278 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006279 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006280
6281 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6282};
6283
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006284class HGraphDelegateVisitor : public HGraphVisitor {
6285 public:
6286 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6287 virtual ~HGraphDelegateVisitor() {}
6288
6289 // Visit functions that delegate to to super class.
6290#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006291 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006292
6293 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6294
6295#undef DECLARE_VISIT_INSTRUCTION
6296
6297 private:
6298 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6299};
6300
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006301class HInsertionOrderIterator : public ValueObject {
6302 public:
6303 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6304
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006305 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006306 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006307 void Advance() { ++index_; }
6308
6309 private:
6310 const HGraph& graph_;
6311 size_t index_;
6312
6313 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6314};
6315
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006316class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006317 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006318 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6319 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006320 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006321 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006322
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006323 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6324 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006325 void Advance() { ++index_; }
6326
6327 private:
6328 const HGraph& graph_;
6329 size_t index_;
6330
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006331 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006332};
6333
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006334class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006335 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006336 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006337 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006338 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006339 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006340 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006341
6342 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006343 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006344 void Advance() { --index_; }
6345
6346 private:
6347 const HGraph& graph_;
6348 size_t index_;
6349
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006350 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006351};
6352
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006353class HLinearPostOrderIterator : public ValueObject {
6354 public:
6355 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006356 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006357
6358 bool Done() const { return index_ == 0; }
6359
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006360 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006361
6362 void Advance() {
6363 --index_;
6364 DCHECK_GE(index_, 0U);
6365 }
6366
6367 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006368 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006369 size_t index_;
6370
6371 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6372};
6373
6374class HLinearOrderIterator : public ValueObject {
6375 public:
6376 explicit HLinearOrderIterator(const HGraph& graph)
6377 : order_(graph.GetLinearOrder()), index_(0) {}
6378
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006379 bool Done() const { return index_ == order_.size(); }
6380 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006381 void Advance() { ++index_; }
6382
6383 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006384 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006385 size_t index_;
6386
6387 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6388};
6389
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006390// Iterator over the blocks that art part of the loop. Includes blocks part
6391// of an inner loop. The order in which the blocks are iterated is on their
6392// block id.
6393class HBlocksInLoopIterator : public ValueObject {
6394 public:
6395 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6396 : blocks_in_loop_(info.GetBlocks()),
6397 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6398 index_(0) {
6399 if (!blocks_in_loop_.IsBitSet(index_)) {
6400 Advance();
6401 }
6402 }
6403
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006404 bool Done() const { return index_ == blocks_.size(); }
6405 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006406 void Advance() {
6407 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006408 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006409 if (blocks_in_loop_.IsBitSet(index_)) {
6410 break;
6411 }
6412 }
6413 }
6414
6415 private:
6416 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006417 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006418 size_t index_;
6419
6420 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6421};
6422
Mingyao Yang3584bce2015-05-19 16:01:59 -07006423// Iterator over the blocks that art part of the loop. Includes blocks part
6424// of an inner loop. The order in which the blocks are iterated is reverse
6425// post order.
6426class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6427 public:
6428 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6429 : blocks_in_loop_(info.GetBlocks()),
6430 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6431 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006432 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006433 Advance();
6434 }
6435 }
6436
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006437 bool Done() const { return index_ == blocks_.size(); }
6438 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006439 void Advance() {
6440 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006441 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6442 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006443 break;
6444 }
6445 }
6446 }
6447
6448 private:
6449 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006450 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006451 size_t index_;
6452
6453 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6454};
6455
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006456inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006457 if (constant->IsIntConstant()) {
6458 return constant->AsIntConstant()->GetValue();
6459 } else if (constant->IsLongConstant()) {
6460 return constant->AsLongConstant()->GetValue();
6461 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006462 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006463 return 0;
6464 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006465}
6466
Vladimir Marko58155012015-08-19 12:49:41 +00006467inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6468 // For the purposes of the compiler, the dex files must actually be the same object
6469 // if we want to safely treat them as the same. This is especially important for JIT
6470 // as custom class loaders can open the same underlying file (or memory) multiple
6471 // times and provide different class resolution but no two class loaders should ever
6472 // use the same DexFile object - doing so is an unsupported hack that can lead to
6473 // all sorts of weird failures.
6474 return &lhs == &rhs;
6475}
6476
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006477#define INSTRUCTION_TYPE_CHECK(type, super) \
6478 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6479 inline const H##type* HInstruction::As##type() const { \
6480 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6481 } \
6482 inline H##type* HInstruction::As##type() { \
6483 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6484 }
6485
6486 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6487#undef INSTRUCTION_TYPE_CHECK
6488
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006489class SwitchTable : public ValueObject {
6490 public:
6491 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6492 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6493 int32_t table_offset = instruction.VRegB_31t();
6494 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6495 if (sparse) {
6496 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6497 } else {
6498 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6499 }
6500 num_entries_ = table[1];
6501 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6502 }
6503
6504 uint16_t GetNumEntries() const {
6505 return num_entries_;
6506 }
6507
6508 void CheckIndex(size_t index) const {
6509 if (sparse_) {
6510 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6511 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6512 } else {
6513 // In a packed table, we have the starting key and num_entries_ values.
6514 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6515 }
6516 }
6517
6518 int32_t GetEntryAt(size_t index) const {
6519 CheckIndex(index);
6520 return values_[index];
6521 }
6522
6523 uint32_t GetDexPcForIndex(size_t index) const {
6524 CheckIndex(index);
6525 return dex_pc_ +
6526 (reinterpret_cast<const int16_t*>(values_ + index) -
6527 reinterpret_cast<const int16_t*>(&instruction_));
6528 }
6529
6530 // Index of the first value in the table.
6531 size_t GetFirstValueIndex() const {
6532 if (sparse_) {
6533 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6534 return num_entries_;
6535 } else {
6536 // In a packed table, we have the starting key and num_entries_ values.
6537 return 1;
6538 }
6539 }
6540
6541 private:
6542 const Instruction& instruction_;
6543 const uint32_t dex_pc_;
6544
6545 // Whether this is a sparse-switch table (or a packed-switch one).
6546 const bool sparse_;
6547
6548 // This can't be const as it needs to be computed off of the given instruction, and complicated
6549 // expressions in the initializer list seemed very ugly.
6550 uint16_t num_entries_;
6551
6552 const int32_t* values_;
6553
6554 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6555};
6556
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006557// Create space in `blocks` for adding `number_of_new_blocks` entries
6558// starting at location `at`. Blocks after `at` are moved accordingly.
6559inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6560 size_t number_of_new_blocks,
6561 size_t after) {
6562 DCHECK_LT(after, blocks->size());
6563 size_t old_size = blocks->size();
6564 size_t new_size = old_size + number_of_new_blocks;
6565 blocks->resize(new_size);
6566 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6567}
6568
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006569} // namespace art
6570
6571#endif // ART_COMPILER_OPTIMIZING_NODES_H_