blob: 9eddfc7e0ec1298de8f6d4f57ba65d9645bca8c8 [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
Mingyao Yang3584bce2015-05-19 16:01:59 -0700356 // Need to add a couple of blocks to test if the loop body is entered and
357 // put deoptimization instructions, etc.
358 void TransformLoopHeaderForBCE(HBasicBlock* header);
359
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000360 // Removes `block` from the graph. Assumes `block` has been disconnected from
361 // other blocks and has no instructions or phis.
362 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000363
David Brazdilfc6a86a2015-06-26 10:33:45 +0000364 // Splits the edge between `block` and `successor` while preserving the
365 // indices in the predecessor/successor lists. If there are multiple edges
366 // between the blocks, the lowest indices are used.
367 // Returns the new block which is empty and has the same dex pc as `successor`.
368 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
369
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100370 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
371 void SimplifyLoop(HBasicBlock* header);
372
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000373 int32_t GetNextInstructionId() {
374 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000375 return current_instruction_id_++;
376 }
377
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000378 int32_t GetCurrentInstructionId() const {
379 return current_instruction_id_;
380 }
381
382 void SetCurrentInstructionId(int32_t id) {
383 current_instruction_id_ = id;
384 }
385
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100386 uint16_t GetMaximumNumberOfOutVRegs() const {
387 return maximum_number_of_out_vregs_;
388 }
389
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000390 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
391 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100392 }
393
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100394 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
395 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
396 }
397
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000398 void UpdateTemporariesVRegSlots(size_t slots) {
399 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100400 }
401
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000402 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100403 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000404 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100405 }
406
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100407 void SetNumberOfVRegs(uint16_t number_of_vregs) {
408 number_of_vregs_ = number_of_vregs;
409 }
410
411 uint16_t GetNumberOfVRegs() const {
412 return number_of_vregs_;
413 }
414
415 void SetNumberOfInVRegs(uint16_t value) {
416 number_of_in_vregs_ = value;
417 }
418
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100419 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100420 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100421 return number_of_vregs_ - number_of_in_vregs_;
422 }
423
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100424 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100425 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100426 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100427
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100428 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100429 return linear_order_;
430 }
431
Mark Mendell1152c922015-04-24 17:06:35 -0400432 bool HasBoundsChecks() const {
433 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800434 }
435
Mark Mendell1152c922015-04-24 17:06:35 -0400436 void SetHasBoundsChecks(bool value) {
437 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800438 }
439
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100440 bool ShouldGenerateConstructorBarrier() const {
441 return should_generate_constructor_barrier_;
442 }
443
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000444 bool IsDebuggable() const { return debuggable_; }
445
David Brazdil8d5b8b22015-03-24 10:51:52 +0000446 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000447 // already, it is created and inserted into the graph. This method is only for
448 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600449 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000450
451 // TODO: This is problematic for the consistency of reference type propagation
452 // because it can be created anytime after the pass and thus it will be left
453 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600454 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000455
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600456 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
457 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000458 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600459 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
460 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000461 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600462 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
463 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000464 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600465 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
466 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000467 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000468
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100469 HCurrentMethod* GetCurrentMethod();
470
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100471 const DexFile& GetDexFile() const {
472 return dex_file_;
473 }
474
475 uint32_t GetMethodIdx() const {
476 return method_idx_;
477 }
478
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100479 InvokeType GetInvokeType() const {
480 return invoke_type_;
481 }
482
Mark Mendellc4701932015-04-10 13:18:51 -0400483 InstructionSet GetInstructionSet() const {
484 return instruction_set_;
485 }
486
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000487 bool IsCompilingOsr() const { return osr_; }
488
David Brazdil77a48ae2015-09-15 12:34:04 +0000489 bool HasTryCatch() const { return has_try_catch_; }
490 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100491
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000492 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
493 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
494
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100495 ArtMethod* GetArtMethod() const { return art_method_; }
496 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
497
Mark Mendellf6529172015-11-17 11:16:56 -0500498 // Returns an instruction with the opposite boolean value from 'cond'.
499 // The instruction has been inserted into the graph, either as a constant, or
500 // before cursor.
501 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
502
David Brazdil2d7352b2015-04-20 14:52:42 +0100503 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000504 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100505 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000507 template <class InstructionType, typename ValueType>
508 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600509 ArenaSafeMap<ValueType, InstructionType*>* cache,
510 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000511 // Try to find an existing constant of the given value.
512 InstructionType* constant = nullptr;
513 auto cached_constant = cache->find(value);
514 if (cached_constant != cache->end()) {
515 constant = cached_constant->second;
516 }
517
518 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100519 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000520 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600521 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 cache->Overwrite(value, constant);
523 InsertConstant(constant);
524 }
525 return constant;
526 }
527
David Brazdil8d5b8b22015-03-24 10:51:52 +0000528 void InsertConstant(HConstant* instruction);
529
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000530 // Cache a float constant into the graph. This method should only be
531 // called by the SsaBuilder when creating "equivalent" instructions.
532 void CacheFloatConstant(HFloatConstant* constant);
533
534 // See CacheFloatConstant comment.
535 void CacheDoubleConstant(HDoubleConstant* constant);
536
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000537 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000538
539 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100540 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000541
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100543 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000544
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100545 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100546 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100547
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000548 HBasicBlock* entry_block_;
549 HBasicBlock* exit_block_;
550
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100551 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100552 uint16_t maximum_number_of_out_vregs_;
553
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100554 // The number of virtual registers in this method. Contains the parameters.
555 uint16_t number_of_vregs_;
556
557 // The number of virtual registers used by parameters of this method.
558 uint16_t number_of_in_vregs_;
559
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000560 // Number of vreg size slots that the temporaries use (used in baseline compiler).
561 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100562
Mark Mendell1152c922015-04-24 17:06:35 -0400563 // Has bounds checks. We can totally skip BCE if it's false.
564 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800565
David Brazdil77a48ae2015-09-15 12:34:04 +0000566 // Flag whether there are any try/catch blocks in the graph. We will skip
567 // try/catch-related passes if false.
568 bool has_try_catch_;
569
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000570 // Flag whether there are any irreducible loops in the graph.
571 bool has_irreducible_loops_;
572
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000573 // Indicates whether the graph should be compiled in a way that
574 // ensures full debuggability. If false, we can apply more
575 // aggressive optimizations that may limit the level of debugging.
576 const bool debuggable_;
577
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000578 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000579 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000580
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100581 // The dex file from which the method is from.
582 const DexFile& dex_file_;
583
584 // The method index in the dex file.
585 const uint32_t method_idx_;
586
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100587 // If inlined, this encodes how the callee is being invoked.
588 const InvokeType invoke_type_;
589
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100590 // Whether the graph has been transformed to SSA form. Only used
591 // in debug mode to ensure we are not using properties only valid
592 // for non-SSA form (like the number of temporaries).
593 bool in_ssa_form_;
594
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100595 const bool should_generate_constructor_barrier_;
596
Mathieu Chartiere401d142015-04-22 13:56:20 -0700597 const InstructionSet instruction_set_;
598
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000599 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000600 HNullConstant* cached_null_constant_;
601 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000602 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000603 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000604 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000605
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100606 HCurrentMethod* cached_current_method_;
607
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100608 // The ArtMethod this graph is for. Note that for AOT, it may be null,
609 // for example for methods whose declaring class could not be resolved
610 // (such as when the superclass could not be found).
611 ArtMethod* art_method_;
612
David Brazdil4833f5a2015-12-16 10:37:39 +0000613 // Keep the RTI of inexact Object to avoid having to pass stack handle
614 // collection pointer to passes which may create NullConstant.
615 ReferenceTypeInfo inexact_object_rti_;
616
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000617 // Whether we are compiling this graph for on stack replacement: this will
618 // make all loops seen as irreducible and emit special stack maps to mark
619 // compiled code entries which the interpreter can directly jump to.
620 const bool osr_;
621
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100623 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000624 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000625 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626 DISALLOW_COPY_AND_ASSIGN(HGraph);
627};
628
Vladimir Markof9f64412015-09-02 14:05:49 +0100629class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000630 public:
631 HLoopInformation(HBasicBlock* header, HGraph* graph)
632 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100633 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000634 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100635 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100636 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100637 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
638 back_edges_.reserve(kDefaultNumberOfBackEdges);
639 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100640
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000641 bool IsIrreducible() const { return irreducible_; }
642
643 void Dump(std::ostream& os);
644
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100645 HBasicBlock* GetHeader() const {
646 return header_;
647 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000649 void SetHeader(HBasicBlock* block) {
650 header_ = block;
651 }
652
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100653 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
654 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
655 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
656
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100658 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000659 }
660
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100661 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100662 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100663 }
664
David Brazdil46e2a392015-03-16 17:31:52 +0000665 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100666 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100667 }
668
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000669 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100670 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000671 }
672
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100673 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100677 }
678
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100679 // Returns the lifetime position of the back edge that has the
680 // greatest lifetime position.
681 size_t GetLifetimeEnd() const;
682
683 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100684 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100685 }
686
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000687 // Finds blocks that are part of this loop.
688 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100689
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100690 // Returns whether this loop information contains `block`.
691 // Note that this loop information *must* be populated before entering this function.
692 bool Contains(const HBasicBlock& block) const;
693
694 // Returns whether this loop information is an inner loop of `other`.
695 // Note that `other` *must* be populated before entering this function.
696 bool IsIn(const HLoopInformation& other) const;
697
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800698 // Returns true if instruction is not defined within this loop.
699 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700700
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100701 const ArenaBitVector& GetBlocks() const { return blocks_; }
702
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000703 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000704 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000705
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000706 void ClearAllBlocks() {
707 blocks_.ClearAllBits();
708 }
709
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000710 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100711 // Internal recursive implementation of `Populate`.
712 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000713 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100714
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000715 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100716 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000717 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100718 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100719 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000720
721 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
722};
723
David Brazdilec16f792015-08-19 15:04:01 +0100724// Stores try/catch information for basic blocks.
725// Note that HGraph is constructed so that catch blocks cannot simultaneously
726// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100727class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100728 public:
729 // Try block information constructor.
730 explicit TryCatchInformation(const HTryBoundary& try_entry)
731 : try_entry_(&try_entry),
732 catch_dex_file_(nullptr),
733 catch_type_index_(DexFile::kDexNoIndex16) {
734 DCHECK(try_entry_ != nullptr);
735 }
736
737 // Catch block information constructor.
738 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
739 : try_entry_(nullptr),
740 catch_dex_file_(&dex_file),
741 catch_type_index_(catch_type_index) {}
742
743 bool IsTryBlock() const { return try_entry_ != nullptr; }
744
745 const HTryBoundary& GetTryEntry() const {
746 DCHECK(IsTryBlock());
747 return *try_entry_;
748 }
749
750 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
751
752 bool IsCatchAllTypeIndex() const {
753 DCHECK(IsCatchBlock());
754 return catch_type_index_ == DexFile::kDexNoIndex16;
755 }
756
757 uint16_t GetCatchTypeIndex() const {
758 DCHECK(IsCatchBlock());
759 return catch_type_index_;
760 }
761
762 const DexFile& GetCatchDexFile() const {
763 DCHECK(IsCatchBlock());
764 return *catch_dex_file_;
765 }
766
767 private:
768 // One of possibly several TryBoundary instructions entering the block's try.
769 // Only set for try blocks.
770 const HTryBoundary* try_entry_;
771
772 // Exception type information. Only set for catch blocks.
773 const DexFile* catch_dex_file_;
774 const uint16_t catch_type_index_;
775};
776
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100777static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100778static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100779
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780// A block in a method. Contains the list of instructions represented
781// as a double linked list. Each block knows its predecessors and
782// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100783
Vladimir Markof9f64412015-09-02 14:05:49 +0100784class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000785 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600786 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000788 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
789 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000790 loop_information_(nullptr),
791 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000792 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100793 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100794 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100795 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000796 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000797 try_catch_information_(nullptr) {
798 predecessors_.reserve(kDefaultNumberOfPredecessors);
799 successors_.reserve(kDefaultNumberOfSuccessors);
800 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
801 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000802
Vladimir Marko60584552015-09-03 13:35:12 +0000803 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805 }
806
Vladimir Marko60584552015-09-03 13:35:12 +0000807 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000809 }
810
David Brazdild26a4112015-11-10 11:07:31 +0000811 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
812 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
813
Vladimir Marko60584552015-09-03 13:35:12 +0000814 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
815 return ContainsElement(successors_, block, start_from);
816 }
817
818 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100819 return dominated_blocks_;
820 }
821
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100822 bool IsEntryBlock() const {
823 return graph_->GetEntryBlock() == this;
824 }
825
826 bool IsExitBlock() const {
827 return graph_->GetExitBlock() == this;
828 }
829
David Brazdil46e2a392015-03-16 17:31:52 +0000830 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000831 bool IsSingleTryBoundary() const;
832
833 // Returns true if this block emits nothing but a jump.
834 bool IsSingleJump() const {
835 HLoopInformation* loop_info = GetLoopInformation();
836 return (IsSingleGoto() || IsSingleTryBoundary())
837 // Back edges generate a suspend check.
838 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
839 }
David Brazdil46e2a392015-03-16 17:31:52 +0000840
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000841 void AddBackEdge(HBasicBlock* back_edge) {
842 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000843 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000844 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100845 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000846 loop_information_->AddBackEdge(back_edge);
847 }
848
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000849 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100852 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000853 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600854 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000855
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000856 HBasicBlock* GetDominator() const { return dominator_; }
857 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000858 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
859
860 void RemoveDominatedBlock(HBasicBlock* block) {
861 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100862 }
Vladimir Marko60584552015-09-03 13:35:12 +0000863
864 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
865 ReplaceElement(dominated_blocks_, existing, new_block);
866 }
867
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100868 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000869
870 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100871 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000872 }
873
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100874 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
875 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100876 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100877 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100878 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
879 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000880
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000881 HInstruction* GetFirstInstructionDisregardMoves() const;
882
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000883 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000884 successors_.push_back(block);
885 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000886 }
887
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100888 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
889 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100890 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000891 new_block->predecessors_.push_back(this);
892 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000893 }
894
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000895 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
896 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000897 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000898 new_block->successors_.push_back(this);
899 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000900 }
901
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100902 // Insert `this` between `predecessor` and `successor. This method
903 // preserves the indicies, and will update the first edge found between
904 // `predecessor` and `successor`.
905 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
906 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100907 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000908 successor->predecessors_[predecessor_index] = this;
909 predecessor->successors_[successor_index] = this;
910 successors_.push_back(successor);
911 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100912 }
913
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100914 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000915 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100916 }
917
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000918 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000919 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000920 }
921
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100922 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000923 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100924 }
925
926 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000927 predecessors_.push_back(block);
928 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100929 }
930
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100931 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 DCHECK_EQ(predecessors_.size(), 2u);
933 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100934 }
935
David Brazdil769c9e52015-04-27 13:54:09 +0100936 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000937 DCHECK_EQ(successors_.size(), 2u);
938 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100939 }
940
David Brazdilfc6a86a2015-06-26 10:33:45 +0000941 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000942 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100943 }
944
David Brazdilfc6a86a2015-06-26 10:33:45 +0000945 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000946 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100947 }
948
David Brazdilfc6a86a2015-06-26 10:33:45 +0000949 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100951 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000952 }
953
954 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000955 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100956 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000957 }
958
959 // Returns whether the first occurrence of `predecessor` in the list of
960 // predecessors is at index `idx`.
961 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100962 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000963 return GetPredecessorIndexOf(predecessor) == idx;
964 }
965
David Brazdild7558da2015-09-22 13:04:14 +0100966 // Create a new block between this block and its predecessors. The new block
967 // is added to the graph, all predecessor edges are relinked to it and an edge
968 // is created to `this`. Returns the new empty block. Reverse post order or
969 // loop and try/catch information are not updated.
970 HBasicBlock* CreateImmediateDominator();
971
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100973 // created, latter block. Note that this method will add the block to the
974 // graph, create a Goto at the end of the former block and will create an edge
975 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100976 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000977 HBasicBlock* SplitBefore(HInstruction* cursor);
978
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000979 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000980 // created block. Note that this method just updates raw block information,
981 // like predecessors, successors, dominators, and instruction list. It does not
982 // update the graph, reverse post order, loop information, nor make sure the
983 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000984 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
985
986 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
987 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000988
David Brazdil9bc43612015-11-05 21:25:24 +0000989 // Split catch block into two blocks after the original move-exception bytecode
990 // instruction, or at the beginning if not present. Returns the newly created,
991 // latter block, or nullptr if such block could not be created (must be dead
992 // in that case). Note that this method just updates raw block information,
993 // like predecessors, successors, dominators, and instruction list. It does not
994 // update the graph, reverse post order, loop information, nor make sure the
995 // blocks are consistent (for example ending with a control flow instruction).
996 HBasicBlock* SplitCatchBlockAfterMoveException();
997
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000998 // Merge `other` at the end of `this`. Successors and dominated blocks of
999 // `other` are changed to be successors and dominated blocks of `this`. Note
1000 // that this method does not update the graph, reverse post order, loop
1001 // information, nor make sure the blocks are consistent (for example ending
1002 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001003 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001004
1005 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1006 // of `this` are moved to `other`.
1007 // Note that this method does not update the graph, reverse post order, loop
1008 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001009 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001010 void ReplaceWith(HBasicBlock* other);
1011
David Brazdil2d7352b2015-04-20 14:52:42 +01001012 // Merge `other` at the end of `this`. This method updates loops, reverse post
1013 // order, links to predecessors, successors, dominators and deletes the block
1014 // from the graph. The two blocks must be successive, i.e. `this` the only
1015 // predecessor of `other` and vice versa.
1016 void MergeWith(HBasicBlock* other);
1017
1018 // Disconnects `this` from all its predecessors, successors and dominator,
1019 // removes it from all loops it is included in and eventually from the graph.
1020 // The block must not dominate any other block. Predecessors and successors
1021 // are safely updated.
1022 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001023
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001024 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001025 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001026 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001027 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001028 // Replace instruction `initial` with `replacement` within this block.
1029 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1030 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001031 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001032 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001033 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001034 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1035 // instruction list. With 'ensure_safety' set to true, it verifies that the
1036 // instruction is not in use and removes it from the use lists of its inputs.
1037 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1038 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001039 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001040
1041 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001042 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001043 }
1044
Roland Levillain6b879dd2014-09-22 17:13:44 +01001045 bool IsLoopPreHeaderFirstPredecessor() const {
1046 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001047 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001048 }
1049
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001050 bool IsFirstPredecessorBackEdge() const {
1051 DCHECK(IsLoopHeader());
1052 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1053 }
1054
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001055 HLoopInformation* GetLoopInformation() const {
1056 return loop_information_;
1057 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001058
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001059 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001060 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001061 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001062 void SetInLoop(HLoopInformation* info) {
1063 if (IsLoopHeader()) {
1064 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001065 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001066 loop_information_ = info;
1067 } else if (loop_information_->Contains(*info->GetHeader())) {
1068 // Block is currently part of an outer loop. Make it part of this inner loop.
1069 // Note that a non loop header having a loop information means this loop information
1070 // has already been populated
1071 loop_information_ = info;
1072 } else {
1073 // Block is part of an inner loop. Do not update the loop information.
1074 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1075 // at this point, because this method is being called while populating `info`.
1076 }
1077 }
1078
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001079 // Raw update of the loop information.
1080 void SetLoopInformation(HLoopInformation* info) {
1081 loop_information_ = info;
1082 }
1083
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001084 bool IsInLoop() const { return loop_information_ != nullptr; }
1085
David Brazdilec16f792015-08-19 15:04:01 +01001086 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1087
1088 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1089 try_catch_information_ = try_catch_information;
1090 }
1091
1092 bool IsTryBlock() const {
1093 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1094 }
1095
1096 bool IsCatchBlock() const {
1097 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1098 }
David Brazdilffee3d32015-07-06 11:48:53 +01001099
1100 // Returns the try entry that this block's successors should have. They will
1101 // be in the same try, unless the block ends in a try boundary. In that case,
1102 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001103 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001104
David Brazdild7558da2015-09-22 13:04:14 +01001105 bool HasThrowingInstructions() const;
1106
David Brazdila4b8c212015-05-07 09:59:30 +01001107 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001108 bool Dominates(HBasicBlock* block) const;
1109
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001110 size_t GetLifetimeStart() const { return lifetime_start_; }
1111 size_t GetLifetimeEnd() const { return lifetime_end_; }
1112
1113 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1114 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1115
David Brazdil8d5b8b22015-03-24 10:51:52 +00001116 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001117 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001118 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001119 bool HasSinglePhi() const;
1120
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001121 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001122 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001123 ArenaVector<HBasicBlock*> predecessors_;
1124 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001125 HInstructionList instructions_;
1126 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001127 HLoopInformation* loop_information_;
1128 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001129 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001130 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001131 // The dex program counter of the first instruction of this block.
1132 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001133 size_t lifetime_start_;
1134 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001135 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001136
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001137 friend class HGraph;
1138 friend class HInstruction;
1139
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001140 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1141};
1142
David Brazdilb2bd1c52015-03-25 11:17:37 +00001143// Iterates over the LoopInformation of all loops which contain 'block'
1144// from the innermost to the outermost.
1145class HLoopInformationOutwardIterator : public ValueObject {
1146 public:
1147 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1148 : current_(block.GetLoopInformation()) {}
1149
1150 bool Done() const { return current_ == nullptr; }
1151
1152 void Advance() {
1153 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001154 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001155 }
1156
1157 HLoopInformation* Current() const {
1158 DCHECK(!Done());
1159 return current_;
1160 }
1161
1162 private:
1163 HLoopInformation* current_;
1164
1165 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1166};
1167
Alexandre Ramesef20f712015-06-09 10:29:30 +01001168#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001169 M(Above, Condition) \
1170 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001171 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001172 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001173 M(ArrayGet, Instruction) \
1174 M(ArrayLength, Instruction) \
1175 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001176 M(Below, Condition) \
1177 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001178 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001179 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001180 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001181 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001182 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001183 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001184 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001185 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001186 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001187 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001188 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001189 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001190 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001191 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001192 M(Exit, Instruction) \
1193 M(FloatConstant, Constant) \
1194 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001195 M(GreaterThan, Condition) \
1196 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001197 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001198 M(InstanceFieldGet, Instruction) \
1199 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001200 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001201 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001202 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001203 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001204 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001205 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001206 M(LessThan, Condition) \
1207 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001208 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001209 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001211 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001212 M(Local, Instruction) \
1213 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001214 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001215 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001216 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001217 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001218 M(Neg, UnaryOperation) \
1219 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001220 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001221 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001223 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001225 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001226 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001227 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001228 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001230 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001231 M(Return, Instruction) \
1232 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001233 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001234 M(Shl, BinaryOperation) \
1235 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001236 M(StaticFieldGet, Instruction) \
1237 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001238 M(UnresolvedInstanceFieldGet, Instruction) \
1239 M(UnresolvedInstanceFieldSet, Instruction) \
1240 M(UnresolvedStaticFieldGet, Instruction) \
1241 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001242 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001243 M(StoreLocal, Instruction) \
1244 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001245 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001246 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001247 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001248 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001249 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001250 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001251
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001252/*
1253 * Instructions, shared across several (not all) architectures.
1254 */
1255#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1256#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1257#else
1258#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
1259 M(MultiplyAccumulate, Instruction)
1260#endif
1261
Vladimir Markob4536b72015-11-24 13:45:23 +00001262#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001263#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001264#else
1265#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1266 M(ArmDexCacheArraysBase, Instruction)
1267#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001268
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001269#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001270#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001271#else
1272#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001273 M(Arm64BitwiseNegatedRight, Instruction) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001274 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001275 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001276#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001277
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001278#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1279
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001280#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1281
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001282#ifndef ART_ENABLE_CODEGEN_x86
1283#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1284#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001285#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1286 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001287 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001288 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001289 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001290#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001291
1292#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1293
1294#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1295 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001296 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001297 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1298 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001299 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001300 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001301 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1302 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1303
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001304#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1305 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001306 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001307 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001308 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001309 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001310
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001311#define FOR_EACH_INSTRUCTION(M) \
1312 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1313 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1314
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001315#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001316FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1317#undef FORWARD_DECLARATION
1318
Roland Levillainccc07a92014-09-16 14:48:16 +01001319#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001320 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001321 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001322 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001323 return other->Is##type(); \
1324 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001325 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001326
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001327#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1328 bool Is##type() const { return As##type() != nullptr; } \
1329 const H##type* As##type() const { return this; } \
1330 H##type* As##type() { return this; }
1331
David Brazdiled596192015-01-23 10:39:45 +00001332template <typename T> class HUseList;
1333
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001334template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001335class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001336 public:
David Brazdiled596192015-01-23 10:39:45 +00001337 HUseListNode* GetPrevious() const { return prev_; }
1338 HUseListNode* GetNext() const { return next_; }
1339 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001340 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001341 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001342
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001343 private:
David Brazdiled596192015-01-23 10:39:45 +00001344 HUseListNode(T user, size_t index)
1345 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1346
1347 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001348 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001349 HUseListNode<T>* prev_;
1350 HUseListNode<T>* next_;
1351
1352 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001353
1354 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1355};
1356
David Brazdiled596192015-01-23 10:39:45 +00001357template <typename T>
1358class HUseList : public ValueObject {
1359 public:
1360 HUseList() : first_(nullptr) {}
1361
1362 void Clear() {
1363 first_ = nullptr;
1364 }
1365
1366 // Adds a new entry at the beginning of the use list and returns
1367 // the newly created node.
1368 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001369 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001370 if (IsEmpty()) {
1371 first_ = new_node;
1372 } else {
1373 first_->prev_ = new_node;
1374 new_node->next_ = first_;
1375 first_ = new_node;
1376 }
1377 return new_node;
1378 }
1379
1380 HUseListNode<T>* GetFirst() const {
1381 return first_;
1382 }
1383
1384 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001385 DCHECK(node != nullptr);
1386 DCHECK(Contains(node));
1387
David Brazdiled596192015-01-23 10:39:45 +00001388 if (node->prev_ != nullptr) {
1389 node->prev_->next_ = node->next_;
1390 }
1391 if (node->next_ != nullptr) {
1392 node->next_->prev_ = node->prev_;
1393 }
1394 if (node == first_) {
1395 first_ = node->next_;
1396 }
1397 }
1398
David Brazdil1abb4192015-02-17 18:33:36 +00001399 bool Contains(const HUseListNode<T>* node) const {
1400 if (node == nullptr) {
1401 return false;
1402 }
1403 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1404 if (current == node) {
1405 return true;
1406 }
1407 }
1408 return false;
1409 }
1410
David Brazdiled596192015-01-23 10:39:45 +00001411 bool IsEmpty() const {
1412 return first_ == nullptr;
1413 }
1414
1415 bool HasOnlyOneUse() const {
1416 return first_ != nullptr && first_->next_ == nullptr;
1417 }
1418
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001419 size_t SizeSlow() const {
1420 size_t count = 0;
1421 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1422 ++count;
1423 }
1424 return count;
1425 }
1426
David Brazdiled596192015-01-23 10:39:45 +00001427 private:
1428 HUseListNode<T>* first_;
1429};
1430
1431template<typename T>
1432class HUseIterator : public ValueObject {
1433 public:
1434 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1435
1436 bool Done() const { return current_ == nullptr; }
1437
1438 void Advance() {
1439 DCHECK(!Done());
1440 current_ = current_->GetNext();
1441 }
1442
1443 HUseListNode<T>* Current() const {
1444 DCHECK(!Done());
1445 return current_;
1446 }
1447
1448 private:
1449 HUseListNode<T>* current_;
1450
1451 friend class HValue;
1452};
1453
David Brazdil1abb4192015-02-17 18:33:36 +00001454// This class is used by HEnvironment and HInstruction classes to record the
1455// instructions they use and pointers to the corresponding HUseListNodes kept
1456// by the used instructions.
1457template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001458class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001459 public:
1460 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1461 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1462
1463 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1464 : instruction_(old_record.instruction_), use_node_(use_node) {
1465 DCHECK(instruction_ != nullptr);
1466 DCHECK(use_node_ != nullptr);
1467 DCHECK(old_record.use_node_ == nullptr);
1468 }
1469
1470 HInstruction* GetInstruction() const { return instruction_; }
1471 HUseListNode<T>* GetUseNode() const { return use_node_; }
1472
1473 private:
1474 // Instruction used by the user.
1475 HInstruction* instruction_;
1476
1477 // Corresponding entry in the use list kept by 'instruction_'.
1478 HUseListNode<T>* use_node_;
1479};
1480
Aart Bik854a02b2015-07-14 16:07:00 -07001481/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001482 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001483 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001484 * For write/read dependences on fields/arrays, the dependence analysis uses
1485 * type disambiguation (e.g. a float field write cannot modify the value of an
1486 * integer field read) and the access type (e.g. a reference array write cannot
1487 * modify the value of a reference field read [although it may modify the
1488 * reference fetch prior to reading the field, which is represented by its own
1489 * write/read dependence]). The analysis makes conservative points-to
1490 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1491 * the same, and any reference read depends on any reference read without
1492 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001493 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001494 * The internal representation uses 38-bit and is described in the table below.
1495 * The first line indicates the side effect, and for field/array accesses the
1496 * second line indicates the type of the access (in the order of the
1497 * Primitive::Type enum).
1498 * The two numbered lines below indicate the bit position in the bitfield (read
1499 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001500 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001501 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1502 * +-------------+---------+---------+--------------+---------+---------+
1503 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1504 * | 3 |333333322|222222221| 1 |111111110|000000000|
1505 * | 7 |654321098|765432109| 8 |765432109|876543210|
1506 *
1507 * Note that, to ease the implementation, 'changes' bits are least significant
1508 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001509 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001510class SideEffects : public ValueObject {
1511 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001512 SideEffects() : flags_(0) {}
1513
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001514 static SideEffects None() {
1515 return SideEffects(0);
1516 }
1517
1518 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001519 return SideEffects(kAllChangeBits | kAllDependOnBits);
1520 }
1521
1522 static SideEffects AllChanges() {
1523 return SideEffects(kAllChangeBits);
1524 }
1525
1526 static SideEffects AllDependencies() {
1527 return SideEffects(kAllDependOnBits);
1528 }
1529
1530 static SideEffects AllExceptGCDependency() {
1531 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1532 }
1533
1534 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001535 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536 }
1537
Aart Bik34c3ba92015-07-20 14:08:59 -07001538 static SideEffects AllWrites() {
1539 return SideEffects(kAllWrites);
1540 }
1541
1542 static SideEffects AllReads() {
1543 return SideEffects(kAllReads);
1544 }
1545
1546 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1547 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001548 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001549 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001550 }
1551
Aart Bik854a02b2015-07-14 16:07:00 -07001552 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1553 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001554 }
1555
Aart Bik34c3ba92015-07-20 14:08:59 -07001556 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1557 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001558 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001559 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001560 }
1561
1562 static SideEffects ArrayReadOfType(Primitive::Type type) {
1563 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1564 }
1565
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001566 static SideEffects CanTriggerGC() {
1567 return SideEffects(1ULL << kCanTriggerGCBit);
1568 }
1569
1570 static SideEffects DependsOnGC() {
1571 return SideEffects(1ULL << kDependsOnGCBit);
1572 }
1573
Aart Bik854a02b2015-07-14 16:07:00 -07001574 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001575 SideEffects Union(SideEffects other) const {
1576 return SideEffects(flags_ | other.flags_);
1577 }
1578
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 SideEffects Exclusion(SideEffects other) const {
1580 return SideEffects(flags_ & ~other.flags_);
1581 }
1582
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001583 void Add(SideEffects other) {
1584 flags_ |= other.flags_;
1585 }
1586
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001587 bool Includes(SideEffects other) const {
1588 return (other.flags_ & flags_) == other.flags_;
1589 }
1590
1591 bool HasSideEffects() const {
1592 return (flags_ & kAllChangeBits);
1593 }
1594
1595 bool HasDependencies() const {
1596 return (flags_ & kAllDependOnBits);
1597 }
1598
1599 // Returns true if there are no side effects or dependencies.
1600 bool DoesNothing() const {
1601 return flags_ == 0;
1602 }
1603
Aart Bik854a02b2015-07-14 16:07:00 -07001604 // Returns true if something is written.
1605 bool DoesAnyWrite() const {
1606 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001607 }
1608
Aart Bik854a02b2015-07-14 16:07:00 -07001609 // Returns true if something is read.
1610 bool DoesAnyRead() const {
1611 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001612 }
1613
Aart Bik854a02b2015-07-14 16:07:00 -07001614 // Returns true if potentially everything is written and read
1615 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 bool DoesAllReadWrite() const {
1617 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1618 }
1619
Aart Bik854a02b2015-07-14 16:07:00 -07001620 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001622 }
1623
Roland Levillain0d5a2812015-11-13 10:07:31 +00001624 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001625 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001626 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1627 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001628 }
1629
1630 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001632 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001633 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001634 for (int s = kLastBit; s >= 0; s--) {
1635 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1636 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1637 // This is a bit for the GC side effect.
1638 if (current_bit_is_set) {
1639 flags += "GC";
1640 }
Aart Bik854a02b2015-07-14 16:07:00 -07001641 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 } else {
1643 // This is a bit for the array/field analysis.
1644 // The underscore character stands for the 'can trigger GC' bit.
1645 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1646 if (current_bit_is_set) {
1647 flags += kDebug[s];
1648 }
1649 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1650 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1651 flags += "|";
1652 }
1653 }
Aart Bik854a02b2015-07-14 16:07:00 -07001654 }
1655 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001656 }
1657
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001658 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001659
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001660 private:
1661 static constexpr int kFieldArrayAnalysisBits = 9;
1662
1663 static constexpr int kFieldWriteOffset = 0;
1664 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1665 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1666 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1667
1668 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1669
1670 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1671 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1672 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1673 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1674
1675 static constexpr int kLastBit = kDependsOnGCBit;
1676 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1677
1678 // Aliases.
1679
1680 static_assert(kChangeBits == kDependOnBits,
1681 "the 'change' bits should match the 'depend on' bits.");
1682
1683 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1684 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1685 static constexpr uint64_t kAllWrites =
1686 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1687 static constexpr uint64_t kAllReads =
1688 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001689
Aart Bik854a02b2015-07-14 16:07:00 -07001690 // Work around the fact that HIR aliases I/F and J/D.
1691 // TODO: remove this interceptor once HIR types are clean
1692 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1693 switch (type) {
1694 case Primitive::kPrimInt:
1695 case Primitive::kPrimFloat:
1696 return TypeFlag(Primitive::kPrimInt, offset) |
1697 TypeFlag(Primitive::kPrimFloat, offset);
1698 case Primitive::kPrimLong:
1699 case Primitive::kPrimDouble:
1700 return TypeFlag(Primitive::kPrimLong, offset) |
1701 TypeFlag(Primitive::kPrimDouble, offset);
1702 default:
1703 return TypeFlag(type, offset);
1704 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001705 }
1706
Aart Bik854a02b2015-07-14 16:07:00 -07001707 // Translates type to bit flag.
1708 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1709 CHECK_NE(type, Primitive::kPrimVoid);
1710 const uint64_t one = 1;
1711 const int shift = type; // 0-based consecutive enum
1712 DCHECK_LE(kFieldWriteOffset, shift);
1713 DCHECK_LT(shift, kArrayWriteOffset);
1714 return one << (type + offset);
1715 }
1716
1717 // Private constructor on direct flags value.
1718 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1719
1720 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001721};
1722
David Brazdiled596192015-01-23 10:39:45 +00001723// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001724class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001725 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001726 HEnvironment(ArenaAllocator* arena,
1727 size_t number_of_vregs,
1728 const DexFile& dex_file,
1729 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001730 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001731 InvokeType invoke_type,
1732 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001733 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1734 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001735 parent_(nullptr),
1736 dex_file_(dex_file),
1737 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001738 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001739 invoke_type_(invoke_type),
1740 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001741 }
1742
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001743 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001744 : HEnvironment(arena,
1745 to_copy.Size(),
1746 to_copy.GetDexFile(),
1747 to_copy.GetMethodIdx(),
1748 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001749 to_copy.GetInvokeType(),
1750 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001751
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001752 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001753 if (parent_ != nullptr) {
1754 parent_->SetAndCopyParentChain(allocator, parent);
1755 } else {
1756 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1757 parent_->CopyFrom(parent);
1758 if (parent->GetParent() != nullptr) {
1759 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1760 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001761 }
David Brazdiled596192015-01-23 10:39:45 +00001762 }
1763
Vladimir Marko71bf8092015-09-15 15:33:14 +01001764 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001765 void CopyFrom(HEnvironment* environment);
1766
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001767 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1768 // input to the loop phi instead. This is for inserting instructions that
1769 // require an environment (like HDeoptimization) in the loop pre-header.
1770 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001771
1772 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001773 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001774 }
1775
1776 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001777 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001778 }
1779
David Brazdil1abb4192015-02-17 18:33:36 +00001780 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001781
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001782 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001783
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001784 HEnvironment* GetParent() const { return parent_; }
1785
1786 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001787 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001788 }
1789
1790 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001791 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001792 }
1793
1794 uint32_t GetDexPc() const {
1795 return dex_pc_;
1796 }
1797
1798 uint32_t GetMethodIdx() const {
1799 return method_idx_;
1800 }
1801
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001802 InvokeType GetInvokeType() const {
1803 return invoke_type_;
1804 }
1805
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001806 const DexFile& GetDexFile() const {
1807 return dex_file_;
1808 }
1809
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001810 HInstruction* GetHolder() const {
1811 return holder_;
1812 }
1813
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001814
1815 bool IsFromInlinedInvoke() const {
1816 return GetParent() != nullptr;
1817 }
1818
David Brazdiled596192015-01-23 10:39:45 +00001819 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001820 // Record instructions' use entries of this environment for constant-time removal.
1821 // It should only be called by HInstruction when a new environment use is added.
1822 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1823 DCHECK(env_use->GetUser() == this);
1824 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001825 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001826 }
David Brazdiled596192015-01-23 10:39:45 +00001827
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001828 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1829 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001830 HEnvironment* parent_;
1831 const DexFile& dex_file_;
1832 const uint32_t method_idx_;
1833 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001834 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001835
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001836 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001837 HInstruction* const holder_;
1838
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001839 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001840
1841 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1842};
1843
Vladimir Markof9f64412015-09-02 14:05:49 +01001844class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001845 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001846 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847 : previous_(nullptr),
1848 next_(nullptr),
1849 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001850 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001851 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001852 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001853 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001854 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001855 locations_(nullptr),
1856 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001857 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001858 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001859 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1860 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1861 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001862
Dave Allison20dfc792014-06-16 20:44:29 -07001863 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001864
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001865#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001866 enum InstructionKind {
1867 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1868 };
1869#undef DECLARE_KIND
1870
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001871 HInstruction* GetNext() const { return next_; }
1872 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001873
Calin Juravle77520bc2015-01-12 18:45:46 +00001874 HInstruction* GetNextDisregardingMoves() const;
1875 HInstruction* GetPreviousDisregardingMoves() const;
1876
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001877 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001878 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001879 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001880 bool IsInBlock() const { return block_ != nullptr; }
1881 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001882 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1883 bool IsIrreducibleLoopHeaderPhi() const {
1884 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1885 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001886
Roland Levillain6b879dd2014-09-22 17:13:44 +01001887 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001888 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001889
1890 virtual void Accept(HGraphVisitor* visitor) = 0;
1891 virtual const char* DebugName() const = 0;
1892
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001893 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001894 void SetRawInputAt(size_t index, HInstruction* input) {
1895 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1896 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001898 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001899
1900 uint32_t GetDexPc() const { return dex_pc_; }
1901
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001902 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001903
Roland Levillaine161a2a2014-10-03 12:45:18 +01001904 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001905 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001906
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001907 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001908 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001909
Calin Juravle10e244f2015-01-26 18:54:32 +00001910 // Does not apply for all instructions, but having this at top level greatly
1911 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001912 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001913 virtual bool CanBeNull() const {
1914 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1915 return true;
1916 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001917
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001918 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001919 return false;
1920 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001921
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001922 virtual bool IsActualObject() const {
1923 return GetType() == Primitive::kPrimNot;
1924 }
1925
Calin Juravle2e768302015-07-28 14:41:11 +00001926 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001927
Calin Juravle61d544b2015-02-23 16:46:57 +00001928 ReferenceTypeInfo GetReferenceTypeInfo() const {
1929 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001930 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
1931 GetPackedFlag<kFlagReferenceTypeIsExact>());;
Calin Juravle61d544b2015-02-23 16:46:57 +00001932 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001933
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001934 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001935 DCHECK(user != nullptr);
1936 HUseListNode<HInstruction*>* use =
1937 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1938 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001939 }
1940
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001941 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001942 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001943 HUseListNode<HEnvironment*>* env_use =
1944 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1945 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001946 }
1947
David Brazdil1abb4192015-02-17 18:33:36 +00001948 void RemoveAsUserOfInput(size_t input) {
1949 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1950 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1951 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001952
David Brazdil1abb4192015-02-17 18:33:36 +00001953 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1954 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955
David Brazdiled596192015-01-23 10:39:45 +00001956 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1957 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001958 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001959 bool HasOnlyOneNonEnvironmentUse() const {
1960 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1961 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001962
Roland Levillain6c82d402014-10-13 16:10:27 +01001963 // Does this instruction strictly dominate `other_instruction`?
1964 // Returns false if this instruction and `other_instruction` are the same.
1965 // Aborts if this instruction and `other_instruction` are both phis.
1966 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001967
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001968 int GetId() const { return id_; }
1969 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001970
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001971 int GetSsaIndex() const { return ssa_index_; }
1972 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1973 bool HasSsaIndex() const { return ssa_index_ != -1; }
1974
1975 bool HasEnvironment() const { return environment_ != nullptr; }
1976 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001977 // Set the `environment_` field. Raw because this method does not
1978 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001979 void SetRawEnvironment(HEnvironment* environment) {
1980 DCHECK(environment_ == nullptr);
1981 DCHECK_EQ(environment->GetHolder(), this);
1982 environment_ = environment;
1983 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001984
1985 // Set the environment of this instruction, copying it from `environment`. While
1986 // copying, the uses lists are being updated.
1987 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001988 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001989 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001990 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001991 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001992 if (environment->GetParent() != nullptr) {
1993 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1994 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001995 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001996
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001997 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1998 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001999 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002000 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002001 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002002 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002003 if (environment->GetParent() != nullptr) {
2004 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2005 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002006 }
2007
Nicolas Geoffray39468442014-09-02 15:17:15 +01002008 // Returns the number of entries in the environment. Typically, that is the
2009 // number of dex registers in a method. It could be more in case of inlining.
2010 size_t EnvironmentSize() const;
2011
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002012 LocationSummary* GetLocations() const { return locations_; }
2013 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002014
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002015 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002016 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017
Alexandre Rames188d4312015-04-09 18:30:21 +01002018 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2019 // uses of this instruction by `other` are *not* updated.
2020 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2021 ReplaceWith(other);
2022 other->ReplaceInput(this, use_index);
2023 }
2024
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002025 // Move `this` instruction before `cursor`.
2026 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002027
Vladimir Markofb337ea2015-11-25 15:25:10 +00002028 // Move `this` before its first user and out of any loops. If there is no
2029 // out-of-loop user that dominates all other users, move the instruction
2030 // to the end of the out-of-loop common dominator of the user's blocks.
2031 //
2032 // This can be used only on non-throwing instructions with no side effects that
2033 // have at least one use but no environment uses.
2034 void MoveBeforeFirstUserAndOutOfLoops();
2035
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002036#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002037 bool Is##type() const; \
2038 const H##type* As##type() const; \
2039 H##type* As##type();
2040
2041 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2042#undef INSTRUCTION_TYPE_CHECK
2043
2044#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002045 bool Is##type() const { return (As##type() != nullptr); } \
2046 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002047 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002048 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002049#undef INSTRUCTION_TYPE_CHECK
2050
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002051 // Returns whether the instruction can be moved within the graph.
2052 virtual bool CanBeMoved() const { return false; }
2053
2054 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002055 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002056 return false;
2057 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002058
2059 // Returns whether any data encoded in the two instructions is equal.
2060 // This method does not look at the inputs. Both instructions must be
2061 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002062 virtual bool InstructionDataEquals(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 two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002067 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002068 // 2) Their inputs are identical.
2069 bool Equals(HInstruction* other) const;
2070
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002071 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2072 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2073 // the virtual function because the __attribute__((__pure__)) doesn't really
2074 // apply the strong requirement for virtual functions, preventing optimizations.
2075 InstructionKind GetKind() const PURE;
2076 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002077
2078 virtual size_t ComputeHashCode() const {
2079 size_t result = GetKind();
2080 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2081 result = (result * 31) + InputAt(i)->GetId();
2082 }
2083 return result;
2084 }
2085
2086 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002087 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002088 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002089
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002090 size_t GetLifetimePosition() const { return lifetime_position_; }
2091 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2092 LiveInterval* GetLiveInterval() const { return live_interval_; }
2093 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2094 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2095
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002096 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2097
2098 // Returns whether the code generation of the instruction will require to have access
2099 // to the current method. Such instructions are:
2100 // (1): Instructions that require an environment, as calling the runtime requires
2101 // to walk the stack and have the current method stored at a specific stack address.
2102 // (2): Object literals like classes and strings, that are loaded from the dex cache
2103 // fields of the current method.
2104 bool NeedsCurrentMethod() const {
2105 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2106 }
2107
Vladimir Markodc151b22015-10-15 18:02:30 +01002108 // Returns whether the code generation of the instruction will require to have access
2109 // to the dex cache of the current method's declaring class via the current method.
2110 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002111
Mark Mendellc4701932015-04-10 13:18:51 -04002112 // Does this instruction have any use in an environment before
2113 // control flow hits 'other'?
2114 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2115
2116 // Remove all references to environment uses of this instruction.
2117 // The caller must ensure that this is safe to do.
2118 void RemoveEnvironmentUsers();
2119
Vladimir Markoa1de9182016-02-25 11:37:38 +00002120 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2121 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002122
David Brazdil1abb4192015-02-17 18:33:36 +00002123 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002124 // If set, the machine code for this instruction is assumed to be generated by
2125 // its users. Used by liveness analysis to compute use positions accordingly.
2126 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2127 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2128 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2129 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2130
David Brazdil1abb4192015-02-17 18:33:36 +00002131 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2132 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2133
Vladimir Markoa1de9182016-02-25 11:37:38 +00002134 uint32_t GetPackedFields() const {
2135 return packed_fields_;
2136 }
2137
2138 template <size_t flag>
2139 bool GetPackedFlag() const {
2140 return (packed_fields_ & (1u << flag)) != 0u;
2141 }
2142
2143 template <size_t flag>
2144 void SetPackedFlag(bool value = true) {
2145 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2146 }
2147
2148 template <typename BitFieldType>
2149 typename BitFieldType::value_type GetPackedField() const {
2150 return BitFieldType::Decode(packed_fields_);
2151 }
2152
2153 template <typename BitFieldType>
2154 void SetPackedField(typename BitFieldType::value_type value) {
2155 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2156 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2157 }
2158
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002160 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2161
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002162 HInstruction* previous_;
2163 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002164 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002165 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002166
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002167 // An instruction gets an id when it is added to the graph.
2168 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002169 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002170 int id_;
2171
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002172 // When doing liveness analysis, instructions that have uses get an SSA index.
2173 int ssa_index_;
2174
Vladimir Markoa1de9182016-02-25 11:37:38 +00002175 // Packed fields.
2176 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002177
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002178 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002179 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002180
2181 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002182 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002183
Nicolas Geoffray39468442014-09-02 15:17:15 +01002184 // The environment associated with this instruction. Not null if the instruction
2185 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002186 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002187
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002188 // Set by the code generator.
2189 LocationSummary* locations_;
2190
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002191 // Set by the liveness analysis.
2192 LiveInterval* live_interval_;
2193
2194 // Set by the liveness analysis, this is the position in a linear
2195 // order of blocks where this instruction's live interval start.
2196 size_t lifetime_position_;
2197
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002198 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002199
Vladimir Markoa1de9182016-02-25 11:37:38 +00002200 // The reference handle part of the reference type info.
2201 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002202 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002203 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002204
David Brazdil1abb4192015-02-17 18:33:36 +00002205 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002206 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002207 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002208 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002209 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002210
2211 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2212};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002213std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002214
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002215class HInputIterator : public ValueObject {
2216 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002217 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002218
2219 bool Done() const { return index_ == instruction_->InputCount(); }
2220 HInstruction* Current() const { return instruction_->InputAt(index_); }
2221 void Advance() { index_++; }
2222
2223 private:
2224 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002225 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226
2227 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2228};
2229
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230class HInstructionIterator : public ValueObject {
2231 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002232 explicit HInstructionIterator(const HInstructionList& instructions)
2233 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002234 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002235 }
2236
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002237 bool Done() const { return instruction_ == nullptr; }
2238 HInstruction* Current() const { return instruction_; }
2239 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002240 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002241 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242 }
2243
2244 private:
2245 HInstruction* instruction_;
2246 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002247
2248 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002249};
2250
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002251class HBackwardInstructionIterator : public ValueObject {
2252 public:
2253 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2254 : instruction_(instructions.last_instruction_) {
2255 next_ = Done() ? nullptr : instruction_->GetPrevious();
2256 }
2257
2258 bool Done() const { return instruction_ == nullptr; }
2259 HInstruction* Current() const { return instruction_; }
2260 void Advance() {
2261 instruction_ = next_;
2262 next_ = Done() ? nullptr : instruction_->GetPrevious();
2263 }
2264
2265 private:
2266 HInstruction* instruction_;
2267 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002268
2269 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002270};
2271
Vladimir Markof9f64412015-09-02 14:05:49 +01002272template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002273class HTemplateInstruction: public HInstruction {
2274 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002275 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002276 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002277 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002278
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002279 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002280
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002281 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002282 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2283 DCHECK_LT(i, N);
2284 return inputs_[i];
2285 }
David Brazdil1abb4192015-02-17 18:33:36 +00002286
2287 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002288 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002289 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002290 }
2291
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002292 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002293 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002294
2295 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002296};
2297
Vladimir Markof9f64412015-09-02 14:05:49 +01002298// HTemplateInstruction specialization for N=0.
2299template<>
2300class HTemplateInstruction<0>: public HInstruction {
2301 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002302 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 : HInstruction(side_effects, dex_pc) {}
2304
Vladimir Markof9f64412015-09-02 14:05:49 +01002305 virtual ~HTemplateInstruction() {}
2306
2307 size_t InputCount() const OVERRIDE { return 0; }
2308
2309 protected:
2310 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2311 LOG(FATAL) << "Unreachable";
2312 UNREACHABLE();
2313 }
2314
2315 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2316 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2317 LOG(FATAL) << "Unreachable";
2318 UNREACHABLE();
2319 }
2320
2321 private:
2322 friend class SsaBuilder;
2323};
2324
Dave Allison20dfc792014-06-16 20:44:29 -07002325template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002326class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002327 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002328 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002329 : HTemplateInstruction<N>(side_effects, dex_pc) {
2330 this->template SetPackedField<TypeField>(type);
2331 }
Dave Allison20dfc792014-06-16 20:44:29 -07002332 virtual ~HExpression() {}
2333
Vladimir Markoa1de9182016-02-25 11:37:38 +00002334 Primitive::Type GetType() const OVERRIDE {
2335 return TypeField::Decode(this->GetPackedFields());
2336 }
Dave Allison20dfc792014-06-16 20:44:29 -07002337
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002338 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002339 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2340 static constexpr size_t kFieldTypeSize =
2341 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2342 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2343 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2344 "Too many packed fields.");
2345 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002346};
2347
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002348// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2349// instruction that branches to the exit block.
2350class HReturnVoid : public HTemplateInstruction<0> {
2351 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002352 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2353 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002354
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002355 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002356
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002357 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002358
2359 private:
2360 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2361};
2362
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002363// Represents dex's RETURN opcodes. A HReturn is a control flow
2364// instruction that branches to the exit block.
2365class HReturn : public HTemplateInstruction<1> {
2366 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002367 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2368 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002369 SetRawInputAt(0, value);
2370 }
2371
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002372 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002373
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002374 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002375
2376 private:
2377 DISALLOW_COPY_AND_ASSIGN(HReturn);
2378};
2379
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002380// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002381// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002382// exit block.
2383class HExit : public HTemplateInstruction<0> {
2384 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002385 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002386
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002387 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002388
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002389 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002390
2391 private:
2392 DISALLOW_COPY_AND_ASSIGN(HExit);
2393};
2394
2395// Jumps from one block to another.
2396class HGoto : public HTemplateInstruction<0> {
2397 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002398 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002399
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002400 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002401
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002402 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002403 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002404 }
2405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002406 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002407
2408 private:
2409 DISALLOW_COPY_AND_ASSIGN(HGoto);
2410};
2411
Roland Levillain9867bc72015-08-05 10:21:34 +01002412class HConstant : public HExpression<0> {
2413 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002414 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2415 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002416
2417 bool CanBeMoved() const OVERRIDE { return true; }
2418
2419 virtual bool IsMinusOne() const { return false; }
2420 virtual bool IsZero() const { return false; }
2421 virtual bool IsOne() const { return false; }
2422
David Brazdil77a48ae2015-09-15 12:34:04 +00002423 virtual uint64_t GetValueAsUint64() const = 0;
2424
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002425 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002426
2427 private:
2428 DISALLOW_COPY_AND_ASSIGN(HConstant);
2429};
2430
2431class HNullConstant : public HConstant {
2432 public:
2433 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2434 return true;
2435 }
2436
David Brazdil77a48ae2015-09-15 12:34:04 +00002437 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2438
Roland Levillain9867bc72015-08-05 10:21:34 +01002439 size_t ComputeHashCode() const OVERRIDE { return 0; }
2440
2441 DECLARE_INSTRUCTION(NullConstant);
2442
2443 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002444 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002445
2446 friend class HGraph;
2447 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2448};
2449
2450// Constants of the type int. Those can be from Dex instructions, or
2451// synthesized (for example with the if-eqz instruction).
2452class HIntConstant : public HConstant {
2453 public:
2454 int32_t GetValue() const { return value_; }
2455
David Brazdil9f389d42015-10-01 14:32:56 +01002456 uint64_t GetValueAsUint64() const OVERRIDE {
2457 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2458 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002459
Roland Levillain9867bc72015-08-05 10:21:34 +01002460 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002461 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002462 return other->AsIntConstant()->value_ == value_;
2463 }
2464
2465 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2466
2467 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2468 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2469 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2470
2471 DECLARE_INSTRUCTION(IntConstant);
2472
2473 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002474 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2475 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2476 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2477 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002478
2479 const int32_t value_;
2480
2481 friend class HGraph;
2482 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2483 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2484 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2485};
2486
2487class HLongConstant : public HConstant {
2488 public:
2489 int64_t GetValue() const { return value_; }
2490
David Brazdil77a48ae2015-09-15 12:34:04 +00002491 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2492
Roland Levillain9867bc72015-08-05 10:21:34 +01002493 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002494 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002495 return other->AsLongConstant()->value_ == value_;
2496 }
2497
2498 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2499
2500 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2501 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2502 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2503
2504 DECLARE_INSTRUCTION(LongConstant);
2505
2506 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002507 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2508 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002509
2510 const int64_t value_;
2511
2512 friend class HGraph;
2513 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2514};
Dave Allison20dfc792014-06-16 20:44:29 -07002515
Roland Levillain31dd3d62016-02-16 12:21:02 +00002516class HFloatConstant : public HConstant {
2517 public:
2518 float GetValue() const { return value_; }
2519
2520 uint64_t GetValueAsUint64() const OVERRIDE {
2521 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2522 }
2523
2524 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2525 DCHECK(other->IsFloatConstant()) << other->DebugName();
2526 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2527 }
2528
2529 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2530
2531 bool IsMinusOne() const OVERRIDE {
2532 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2533 }
2534 bool IsZero() const OVERRIDE {
2535 return value_ == 0.0f;
2536 }
2537 bool IsOne() const OVERRIDE {
2538 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2539 }
2540 bool IsNaN() const {
2541 return std::isnan(value_);
2542 }
2543
2544 DECLARE_INSTRUCTION(FloatConstant);
2545
2546 private:
2547 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2548 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2549 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2550 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2551
2552 const float value_;
2553
2554 // Only the SsaBuilder and HGraph can create floating-point constants.
2555 friend class SsaBuilder;
2556 friend class HGraph;
2557 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2558};
2559
2560class HDoubleConstant : public HConstant {
2561 public:
2562 double GetValue() const { return value_; }
2563
2564 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2565
2566 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2567 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2568 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2569 }
2570
2571 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2572
2573 bool IsMinusOne() const OVERRIDE {
2574 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2575 }
2576 bool IsZero() const OVERRIDE {
2577 return value_ == 0.0;
2578 }
2579 bool IsOne() const OVERRIDE {
2580 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2581 }
2582 bool IsNaN() const {
2583 return std::isnan(value_);
2584 }
2585
2586 DECLARE_INSTRUCTION(DoubleConstant);
2587
2588 private:
2589 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2590 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2591 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2592 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2593
2594 const double value_;
2595
2596 // Only the SsaBuilder and HGraph can create floating-point constants.
2597 friend class SsaBuilder;
2598 friend class HGraph;
2599 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2600};
2601
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002602// Conditional branch. A block ending with an HIf instruction must have
2603// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002604class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002605 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002606 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2607 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002608 SetRawInputAt(0, input);
2609 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002610
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002611 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002612
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002613 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002614 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002615 }
2616
2617 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002618 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002619 }
2620
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002621 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002622
2623 private:
2624 DISALLOW_COPY_AND_ASSIGN(HIf);
2625};
2626
David Brazdilfc6a86a2015-06-26 10:33:45 +00002627
2628// Abstract instruction which marks the beginning and/or end of a try block and
2629// links it to the respective exception handlers. Behaves the same as a Goto in
2630// non-exceptional control flow.
2631// Normal-flow successor is stored at index zero, exception handlers under
2632// higher indices in no particular order.
2633class HTryBoundary : public HTemplateInstruction<0> {
2634 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002635 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002636 kEntry,
2637 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002638 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002639 };
2640
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002641 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002642 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2643 SetPackedField<BoundaryKindField>(kind);
2644 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002645
2646 bool IsControlFlow() const OVERRIDE { return true; }
2647
2648 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002649 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002650
David Brazdild26a4112015-11-10 11:07:31 +00002651 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2652 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2653 }
2654
David Brazdilfc6a86a2015-06-26 10:33:45 +00002655 // Returns whether `handler` is among its exception handlers (non-zero index
2656 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002657 bool HasExceptionHandler(const HBasicBlock& handler) const {
2658 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002659 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002660 }
2661
2662 // If not present already, adds `handler` to its block's list of exception
2663 // handlers.
2664 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002665 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002666 GetBlock()->AddSuccessor(handler);
2667 }
2668 }
2669
Vladimir Markoa1de9182016-02-25 11:37:38 +00002670 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2671 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002672
David Brazdilffee3d32015-07-06 11:48:53 +01002673 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2674
David Brazdilfc6a86a2015-06-26 10:33:45 +00002675 DECLARE_INSTRUCTION(TryBoundary);
2676
2677 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002678 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2679 static constexpr size_t kFieldBoundaryKindSize =
2680 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2681 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2682 kFieldBoundaryKind + kFieldBoundaryKindSize;
2683 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2684 "Too many packed fields.");
2685 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002686
2687 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2688};
2689
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002690// Deoptimize to interpreter, upon checking a condition.
2691class HDeoptimize : public HTemplateInstruction<1> {
2692 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002693 // We set CanTriggerGC to prevent any intermediate address to be live
2694 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002695 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002696 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002697 SetRawInputAt(0, cond);
2698 }
2699
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002700 bool CanBeMoved() const OVERRIDE { return true; }
2701 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2702 return true;
2703 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002704 bool NeedsEnvironment() const OVERRIDE { return true; }
2705 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002706
2707 DECLARE_INSTRUCTION(Deoptimize);
2708
2709 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002710 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2711};
2712
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002713// Represents the ArtMethod that was passed as a first argument to
2714// the method. It is used by instructions that depend on it, like
2715// instructions that work with the dex cache.
2716class HCurrentMethod : public HExpression<0> {
2717 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002718 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2719 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002720
2721 DECLARE_INSTRUCTION(CurrentMethod);
2722
2723 private:
2724 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2725};
2726
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002727// Fetches an ArtMethod from the virtual table or the interface method table
2728// of a class.
2729class HClassTableGet : public HExpression<1> {
2730 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002731 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002732 kVTable,
2733 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002734 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002735 };
2736 HClassTableGet(HInstruction* cls,
2737 Primitive::Type type,
2738 TableKind kind,
2739 size_t index,
2740 uint32_t dex_pc)
2741 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002742 index_(index) {
2743 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002744 SetRawInputAt(0, cls);
2745 }
2746
2747 bool CanBeMoved() const OVERRIDE { return true; }
2748 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2749 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002750 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002751 }
2752
Vladimir Markoa1de9182016-02-25 11:37:38 +00002753 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002754 size_t GetIndex() const { return index_; }
2755
2756 DECLARE_INSTRUCTION(ClassTableGet);
2757
2758 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002759 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2760 static constexpr size_t kFieldTableKindSize =
2761 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2762 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2763 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2764 "Too many packed fields.");
2765 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2766
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002767 // The index of the ArtMethod in the table.
2768 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002769
2770 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2771};
2772
Mark Mendellfe57faa2015-09-18 09:26:15 -04002773// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2774// have one successor for each entry in the switch table, and the final successor
2775// will be the block containing the next Dex opcode.
2776class HPackedSwitch : public HTemplateInstruction<1> {
2777 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002778 HPackedSwitch(int32_t start_value,
2779 uint32_t num_entries,
2780 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002781 uint32_t dex_pc = kNoDexPc)
2782 : HTemplateInstruction(SideEffects::None(), dex_pc),
2783 start_value_(start_value),
2784 num_entries_(num_entries) {
2785 SetRawInputAt(0, input);
2786 }
2787
2788 bool IsControlFlow() const OVERRIDE { return true; }
2789
2790 int32_t GetStartValue() const { return start_value_; }
2791
Vladimir Marko211c2112015-09-24 16:52:33 +01002792 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002793
2794 HBasicBlock* GetDefaultBlock() const {
2795 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002796 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002797 }
2798 DECLARE_INSTRUCTION(PackedSwitch);
2799
2800 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002801 const int32_t start_value_;
2802 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002803
2804 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2805};
2806
Roland Levillain88cb1752014-10-20 16:36:47 +01002807class HUnaryOperation : public HExpression<1> {
2808 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002809 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2810 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002811 SetRawInputAt(0, input);
2812 }
2813
2814 HInstruction* GetInput() const { return InputAt(0); }
2815 Primitive::Type GetResultType() const { return GetType(); }
2816
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002817 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002818 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002819 return true;
2820 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002821
Roland Levillain31dd3d62016-02-16 12:21:02 +00002822 // Try to statically evaluate `this` and return a HConstant
2823 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002824 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002825 HConstant* TryStaticEvaluation() const;
2826
2827 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002828 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2829 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002830 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2831 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002832
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002833 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002834
2835 private:
2836 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2837};
2838
Dave Allison20dfc792014-06-16 20:44:29 -07002839class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002840 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002841 HBinaryOperation(Primitive::Type result_type,
2842 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002843 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002844 SideEffects side_effects = SideEffects::None(),
2845 uint32_t dex_pc = kNoDexPc)
2846 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002847 SetRawInputAt(0, left);
2848 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002849 }
2850
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002851 HInstruction* GetLeft() const { return InputAt(0); }
2852 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002853 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002854
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002855 virtual bool IsCommutative() const { return false; }
2856
2857 // Put constant on the right.
2858 // Returns whether order is changed.
2859 bool OrderInputsWithConstantOnTheRight() {
2860 HInstruction* left = InputAt(0);
2861 HInstruction* right = InputAt(1);
2862 if (left->IsConstant() && !right->IsConstant()) {
2863 ReplaceInput(right, 0);
2864 ReplaceInput(left, 1);
2865 return true;
2866 }
2867 return false;
2868 }
2869
2870 // Order inputs by instruction id, but favor constant on the right side.
2871 // This helps GVN for commutative ops.
2872 void OrderInputs() {
2873 DCHECK(IsCommutative());
2874 HInstruction* left = InputAt(0);
2875 HInstruction* right = InputAt(1);
2876 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2877 return;
2878 }
2879 if (OrderInputsWithConstantOnTheRight()) {
2880 return;
2881 }
2882 // Order according to instruction id.
2883 if (left->GetId() > right->GetId()) {
2884 ReplaceInput(right, 0);
2885 ReplaceInput(left, 1);
2886 }
2887 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002888
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002889 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002890 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002891 return true;
2892 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002893
Roland Levillain31dd3d62016-02-16 12:21:02 +00002894 // Try to statically evaluate `this` and return a HConstant
2895 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002896 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002897 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002898
2899 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002900 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2901 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002902 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2903 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002904 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002905 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2906 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002907 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2908 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002909 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2910 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002911 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002912 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2913 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002914
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002915 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002916 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002917 HConstant* GetConstantRight() const;
2918
2919 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002920 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002921 HInstruction* GetLeastConstantLeft() const;
2922
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002923 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002924
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002925 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002926 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2927};
2928
Mark Mendellc4701932015-04-10 13:18:51 -04002929// The comparison bias applies for floating point operations and indicates how NaN
2930// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002931enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002932 kNoBias, // bias is not applicable (i.e. for long operation)
2933 kGtBias, // return 1 for NaN comparisons
2934 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00002935 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04002936};
2937
Roland Levillain31dd3d62016-02-16 12:21:02 +00002938std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2939
Dave Allison20dfc792014-06-16 20:44:29 -07002940class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002941 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002942 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002943 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
2944 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
2945 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002946
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002947 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002948 // `instruction`, and disregard moves in between.
2949 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002950
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002951 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002952
2953 virtual IfCondition GetCondition() const = 0;
2954
Mark Mendellc4701932015-04-10 13:18:51 -04002955 virtual IfCondition GetOppositeCondition() const = 0;
2956
Vladimir Markoa1de9182016-02-25 11:37:38 +00002957 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
2958 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
2959 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04002960
2961 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002962 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04002963 }
2964
Roland Levillain4fa13f62015-07-06 18:11:54 +01002965 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002966 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002967 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002968 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002969 }
2970
2971 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002972 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002973 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002974 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002975 }
2976
Roland Levillain31dd3d62016-02-16 12:21:02 +00002977 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002978 // Needed if we merge a HCompare into a HCondition.
2979 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
2980 static constexpr size_t kFieldComparisonBiasSize =
2981 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
2982 static constexpr size_t kNumberOfConditionPackedBits =
2983 kFieldComparisonBias + kFieldComparisonBiasSize;
2984 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2985 using ComparisonBiasField =
2986 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
2987
Roland Levillain31dd3d62016-02-16 12:21:02 +00002988 template <typename T>
2989 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
2990
2991 template <typename T>
2992 int32_t CompareFP(T x, T y) const {
2993 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
2994 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
2995 // Handle the bias.
2996 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
2997 }
2998
2999 // Return an integer constant containing the result of a condition evaluated at compile time.
3000 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3001 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3002 }
3003
Dave Allison20dfc792014-06-16 20:44:29 -07003004 private:
3005 DISALLOW_COPY_AND_ASSIGN(HCondition);
3006};
3007
3008// Instruction to check if two inputs are equal to each other.
3009class HEqual : public HCondition {
3010 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003011 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3012 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003013
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003014 bool IsCommutative() const OVERRIDE { return true; }
3015
Vladimir Marko9e23df52015-11-10 17:14:35 +00003016 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3017 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018 return MakeConstantCondition(true, GetDexPc());
3019 }
3020 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3021 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3022 }
3023 // In the following Evaluate methods, a HCompare instruction has
3024 // been merged into this HEqual instruction; evaluate it as
3025 // `Compare(x, y) == 0`.
3026 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3027 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3028 GetDexPc());
3029 }
3030 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3031 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3032 }
3033 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3034 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003035 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003036
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003037 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003038
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003039 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003040 return kCondEQ;
3041 }
3042
Mark Mendellc4701932015-04-10 13:18:51 -04003043 IfCondition GetOppositeCondition() const OVERRIDE {
3044 return kCondNE;
3045 }
3046
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003047 private:
Aart Bike9f37602015-10-09 11:15:55 -07003048 template <typename T> bool Compute(T x, T y) const { return x == y; }
3049
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003050 DISALLOW_COPY_AND_ASSIGN(HEqual);
3051};
3052
Dave Allison20dfc792014-06-16 20:44:29 -07003053class HNotEqual : public HCondition {
3054 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003055 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3056 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003057
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003058 bool IsCommutative() const OVERRIDE { return true; }
3059
Vladimir Marko9e23df52015-11-10 17:14:35 +00003060 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3061 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003062 return MakeConstantCondition(false, GetDexPc());
3063 }
3064 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3065 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3066 }
3067 // In the following Evaluate methods, a HCompare instruction has
3068 // been merged into this HNotEqual instruction; evaluate it as
3069 // `Compare(x, y) != 0`.
3070 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3071 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3072 }
3073 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3074 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3075 }
3076 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3077 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003078 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003079
Dave Allison20dfc792014-06-16 20:44:29 -07003080 DECLARE_INSTRUCTION(NotEqual);
3081
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003082 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003083 return kCondNE;
3084 }
3085
Mark Mendellc4701932015-04-10 13:18:51 -04003086 IfCondition GetOppositeCondition() const OVERRIDE {
3087 return kCondEQ;
3088 }
3089
Dave Allison20dfc792014-06-16 20:44:29 -07003090 private:
Aart Bike9f37602015-10-09 11:15:55 -07003091 template <typename T> bool Compute(T x, T y) const { return x != y; }
3092
Dave Allison20dfc792014-06-16 20:44:29 -07003093 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3094};
3095
3096class HLessThan : public HCondition {
3097 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003098 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3099 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003100
Roland Levillain9867bc72015-08-05 10:21:34 +01003101 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003102 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003103 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104 // In the following Evaluate methods, a HCompare instruction has
3105 // been merged into this HLessThan instruction; evaluate it as
3106 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003107 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003108 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3109 }
3110 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3111 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3112 }
3113 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3114 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003115 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003116
Dave Allison20dfc792014-06-16 20:44:29 -07003117 DECLARE_INSTRUCTION(LessThan);
3118
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003119 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003120 return kCondLT;
3121 }
3122
Mark Mendellc4701932015-04-10 13:18:51 -04003123 IfCondition GetOppositeCondition() const OVERRIDE {
3124 return kCondGE;
3125 }
3126
Dave Allison20dfc792014-06-16 20:44:29 -07003127 private:
Aart Bike9f37602015-10-09 11:15:55 -07003128 template <typename T> bool Compute(T x, T y) const { return x < y; }
3129
Dave Allison20dfc792014-06-16 20:44:29 -07003130 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3131};
3132
3133class HLessThanOrEqual : public HCondition {
3134 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003135 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3136 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003137
Roland Levillain9867bc72015-08-05 10:21:34 +01003138 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003139 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003140 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003141 // In the following Evaluate methods, a HCompare instruction has
3142 // been merged into this HLessThanOrEqual instruction; evaluate it as
3143 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003144 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003145 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3146 }
3147 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3148 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3149 }
3150 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3151 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003152 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003153
Dave Allison20dfc792014-06-16 20:44:29 -07003154 DECLARE_INSTRUCTION(LessThanOrEqual);
3155
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003156 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003157 return kCondLE;
3158 }
3159
Mark Mendellc4701932015-04-10 13:18:51 -04003160 IfCondition GetOppositeCondition() const OVERRIDE {
3161 return kCondGT;
3162 }
3163
Dave Allison20dfc792014-06-16 20:44:29 -07003164 private:
Aart Bike9f37602015-10-09 11:15:55 -07003165 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3166
Dave Allison20dfc792014-06-16 20:44:29 -07003167 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3168};
3169
3170class HGreaterThan : public HCondition {
3171 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003172 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3173 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003174
Roland Levillain9867bc72015-08-05 10:21:34 +01003175 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003176 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003177 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003178 // In the following Evaluate methods, a HCompare instruction has
3179 // been merged into this HGreaterThan instruction; evaluate it as
3180 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003181 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003182 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3183 }
3184 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3185 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3186 }
3187 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3188 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003189 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003190
Dave Allison20dfc792014-06-16 20:44:29 -07003191 DECLARE_INSTRUCTION(GreaterThan);
3192
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003193 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003194 return kCondGT;
3195 }
3196
Mark Mendellc4701932015-04-10 13:18:51 -04003197 IfCondition GetOppositeCondition() const OVERRIDE {
3198 return kCondLE;
3199 }
3200
Dave Allison20dfc792014-06-16 20:44:29 -07003201 private:
Aart Bike9f37602015-10-09 11:15:55 -07003202 template <typename T> bool Compute(T x, T y) const { return x > y; }
3203
Dave Allison20dfc792014-06-16 20:44:29 -07003204 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3205};
3206
3207class HGreaterThanOrEqual : public HCondition {
3208 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003209 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3210 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003211
Roland Levillain9867bc72015-08-05 10:21:34 +01003212 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003213 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003214 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003215 // In the following Evaluate methods, a HCompare instruction has
3216 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3217 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003218 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003219 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3220 }
3221 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3222 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3223 }
3224 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3225 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003226 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003227
Dave Allison20dfc792014-06-16 20:44:29 -07003228 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3229
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003230 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003231 return kCondGE;
3232 }
3233
Mark Mendellc4701932015-04-10 13:18:51 -04003234 IfCondition GetOppositeCondition() const OVERRIDE {
3235 return kCondLT;
3236 }
3237
Dave Allison20dfc792014-06-16 20:44:29 -07003238 private:
Aart Bike9f37602015-10-09 11:15:55 -07003239 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3240
Dave Allison20dfc792014-06-16 20:44:29 -07003241 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3242};
3243
Aart Bike9f37602015-10-09 11:15:55 -07003244class HBelow : public HCondition {
3245 public:
3246 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3247 : HCondition(first, second, dex_pc) {}
3248
3249 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003250 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003251 }
3252 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003253 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3254 }
3255 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3256 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3257 LOG(FATAL) << DebugName() << " is not defined for float values";
3258 UNREACHABLE();
3259 }
3260 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3261 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3262 LOG(FATAL) << DebugName() << " is not defined for double values";
3263 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003264 }
3265
3266 DECLARE_INSTRUCTION(Below);
3267
3268 IfCondition GetCondition() const OVERRIDE {
3269 return kCondB;
3270 }
3271
3272 IfCondition GetOppositeCondition() const OVERRIDE {
3273 return kCondAE;
3274 }
3275
3276 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003277 template <typename T> bool Compute(T x, T y) const {
3278 return MakeUnsigned(x) < MakeUnsigned(y);
3279 }
Aart Bike9f37602015-10-09 11:15:55 -07003280
3281 DISALLOW_COPY_AND_ASSIGN(HBelow);
3282};
3283
3284class HBelowOrEqual : public HCondition {
3285 public:
3286 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3287 : HCondition(first, second, dex_pc) {}
3288
3289 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003290 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003291 }
3292 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003293 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3294 }
3295 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3296 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3297 LOG(FATAL) << DebugName() << " is not defined for float values";
3298 UNREACHABLE();
3299 }
3300 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3301 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3302 LOG(FATAL) << DebugName() << " is not defined for double values";
3303 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003304 }
3305
3306 DECLARE_INSTRUCTION(BelowOrEqual);
3307
3308 IfCondition GetCondition() const OVERRIDE {
3309 return kCondBE;
3310 }
3311
3312 IfCondition GetOppositeCondition() const OVERRIDE {
3313 return kCondA;
3314 }
3315
3316 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003317 template <typename T> bool Compute(T x, T y) const {
3318 return MakeUnsigned(x) <= MakeUnsigned(y);
3319 }
Aart Bike9f37602015-10-09 11:15:55 -07003320
3321 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3322};
3323
3324class HAbove : public HCondition {
3325 public:
3326 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3327 : HCondition(first, second, dex_pc) {}
3328
3329 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003330 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003331 }
3332 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003333 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3334 }
3335 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3336 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3337 LOG(FATAL) << DebugName() << " is not defined for float values";
3338 UNREACHABLE();
3339 }
3340 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3341 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3342 LOG(FATAL) << DebugName() << " is not defined for double values";
3343 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003344 }
3345
3346 DECLARE_INSTRUCTION(Above);
3347
3348 IfCondition GetCondition() const OVERRIDE {
3349 return kCondA;
3350 }
3351
3352 IfCondition GetOppositeCondition() const OVERRIDE {
3353 return kCondBE;
3354 }
3355
3356 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003357 template <typename T> bool Compute(T x, T y) const {
3358 return MakeUnsigned(x) > MakeUnsigned(y);
3359 }
Aart Bike9f37602015-10-09 11:15:55 -07003360
3361 DISALLOW_COPY_AND_ASSIGN(HAbove);
3362};
3363
3364class HAboveOrEqual : public HCondition {
3365 public:
3366 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3367 : HCondition(first, second, dex_pc) {}
3368
3369 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003370 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003371 }
3372 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003373 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3374 }
3375 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3376 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3377 LOG(FATAL) << DebugName() << " is not defined for float values";
3378 UNREACHABLE();
3379 }
3380 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3381 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3382 LOG(FATAL) << DebugName() << " is not defined for double values";
3383 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003384 }
3385
3386 DECLARE_INSTRUCTION(AboveOrEqual);
3387
3388 IfCondition GetCondition() const OVERRIDE {
3389 return kCondAE;
3390 }
3391
3392 IfCondition GetOppositeCondition() const OVERRIDE {
3393 return kCondB;
3394 }
3395
3396 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003397 template <typename T> bool Compute(T x, T y) const {
3398 return MakeUnsigned(x) >= MakeUnsigned(y);
3399 }
Aart Bike9f37602015-10-09 11:15:55 -07003400
3401 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3402};
Dave Allison20dfc792014-06-16 20:44:29 -07003403
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003404// Instruction to check how two inputs compare to each other.
3405// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3406class HCompare : public HBinaryOperation {
3407 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003408 HCompare(Primitive::Type type,
3409 HInstruction* first,
3410 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003411 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003412 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003413 : HBinaryOperation(Primitive::kPrimInt,
3414 first,
3415 second,
3416 SideEffectsForArchRuntimeCalls(type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003417 dex_pc) {
3418 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003419 DCHECK_EQ(type, first->GetType());
3420 DCHECK_EQ(type, second->GetType());
3421 }
3422
Roland Levillain9867bc72015-08-05 10:21:34 +01003423 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3425
3426 template <typename T>
3427 int32_t ComputeFP(T x, T y) const {
3428 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3429 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3430 // Handle the bias.
3431 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3432 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003433
Roland Levillain9867bc72015-08-05 10:21:34 +01003434 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003435 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3436 // reach this code path when processing a freshly built HIR
3437 // graph. However HCompare integer instructions can be synthesized
3438 // by the instruction simplifier to implement IntegerCompare and
3439 // IntegerSignum intrinsics, so we have to handle this case.
3440 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003441 }
3442 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003443 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3444 }
3445 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3446 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3447 }
3448 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3449 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003450 }
3451
Calin Juravleddb7df22014-11-25 20:56:51 +00003452 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003453 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003454 }
3455
Vladimir Markoa1de9182016-02-25 11:37:38 +00003456 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003457
Roland Levillain31dd3d62016-02-16 12:21:02 +00003458 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003459 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003460 bool IsGtBias() const {
3461 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003462 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003463 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003464
3465 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3466 // MIPS64 uses a runtime call for FP comparisons.
3467 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3468 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003469
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003470 DECLARE_INSTRUCTION(Compare);
3471
Roland Levillain31dd3d62016-02-16 12:21:02 +00003472 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003473 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3474 static constexpr size_t kFieldComparisonBiasSize =
3475 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3476 static constexpr size_t kNumberOfComparePackedBits =
3477 kFieldComparisonBias + kFieldComparisonBiasSize;
3478 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3479 using ComparisonBiasField =
3480 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3481
Roland Levillain31dd3d62016-02-16 12:21:02 +00003482 // Return an integer constant containing the result of a comparison evaluated at compile time.
3483 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3484 DCHECK(value == -1 || value == 0 || value == 1) << value;
3485 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3486 }
3487
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003488 private:
3489 DISALLOW_COPY_AND_ASSIGN(HCompare);
3490};
3491
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003492// A local in the graph. Corresponds to a Dex register.
3493class HLocal : public HTemplateInstruction<0> {
3494 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003495 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003496 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003497
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003498 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003499
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003500 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003501
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003502 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003503 // The Dex register number.
3504 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003505
3506 DISALLOW_COPY_AND_ASSIGN(HLocal);
3507};
3508
3509// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003510class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003511 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003512 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3513 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003514 SetRawInputAt(0, local);
3515 }
3516
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003517 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3518
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003519 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003520
3521 private:
3522 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3523};
3524
3525// Store a value in a given local. This instruction has two inputs: the value
3526// and the local.
3527class HStoreLocal : public HTemplateInstruction<2> {
3528 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003529 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3530 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003531 SetRawInputAt(0, local);
3532 SetRawInputAt(1, value);
3533 }
3534
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003535 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3536
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003537 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003538
3539 private:
3540 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3541};
3542
David Brazdil6de19382016-01-08 17:37:10 +00003543class HNewInstance : public HExpression<2> {
3544 public:
3545 HNewInstance(HInstruction* cls,
3546 HCurrentMethod* current_method,
3547 uint32_t dex_pc,
3548 uint16_t type_index,
3549 const DexFile& dex_file,
3550 bool can_throw,
3551 bool finalizable,
3552 QuickEntrypointEnum entrypoint)
3553 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3554 type_index_(type_index),
3555 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003556 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003557 SetPackedFlag<kFlagCanThrow>(can_throw);
3558 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003559 SetRawInputAt(0, cls);
3560 SetRawInputAt(1, current_method);
3561 }
3562
3563 uint16_t GetTypeIndex() const { return type_index_; }
3564 const DexFile& GetDexFile() const { return dex_file_; }
3565
3566 // Calls runtime so needs an environment.
3567 bool NeedsEnvironment() const OVERRIDE { return true; }
3568
3569 // It may throw when called on type that's not instantiable/accessible.
3570 // It can throw OOME.
3571 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003572 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003573
Vladimir Markoa1de9182016-02-25 11:37:38 +00003574 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003575
3576 bool CanBeNull() const OVERRIDE { return false; }
3577
3578 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3579
3580 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3581 entrypoint_ = entrypoint;
3582 }
3583
3584 bool IsStringAlloc() const;
3585
3586 DECLARE_INSTRUCTION(NewInstance);
3587
3588 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003589 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3590 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3591 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3592 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3593 "Too many packed fields.");
3594
David Brazdil6de19382016-01-08 17:37:10 +00003595 const uint16_t type_index_;
3596 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003597 QuickEntrypointEnum entrypoint_;
3598
3599 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3600};
3601
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003602enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003603#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3604 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003605#include "intrinsics_list.h"
3606 kNone,
3607 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3608#undef INTRINSICS_LIST
3609#undef OPTIMIZING_INTRINSICS
3610};
3611std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3612
Agi Csaki05f20562015-08-19 14:58:14 -07003613enum IntrinsicNeedsEnvironmentOrCache {
3614 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3615 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003616};
3617
Aart Bik5d75afe2015-12-14 11:57:01 -08003618enum IntrinsicSideEffects {
3619 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3620 kReadSideEffects, // Intrinsic may read heap memory.
3621 kWriteSideEffects, // Intrinsic may write heap memory.
3622 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3623};
3624
3625enum IntrinsicExceptions {
3626 kNoThrow, // Intrinsic does not throw any exceptions.
3627 kCanThrow // Intrinsic may throw exceptions.
3628};
3629
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003630class HInvoke : public HInstruction {
3631 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003632 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003633
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003634 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003635
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003636 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003637 SetRawInputAt(index, argument);
3638 }
3639
Roland Levillain3e3d7332015-04-28 11:00:54 +01003640 // Return the number of arguments. This number can be lower than
3641 // the number of inputs returned by InputCount(), as some invoke
3642 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3643 // inputs at the end of their list of inputs.
3644 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3645
Vladimir Markoa1de9182016-02-25 11:37:38 +00003646 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003647
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003648 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003649 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003650
Vladimir Markoa1de9182016-02-25 11:37:38 +00003651 InvokeType GetOriginalInvokeType() const {
3652 return GetPackedField<OriginalInvokeTypeField>();
3653 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003654
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003655 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003656 return intrinsic_;
3657 }
3658
Aart Bik5d75afe2015-12-14 11:57:01 -08003659 void SetIntrinsic(Intrinsics intrinsic,
3660 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3661 IntrinsicSideEffects side_effects,
3662 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003663
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003664 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003665 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003666 }
3667
Vladimir Markoa1de9182016-02-25 11:37:38 +00003668 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003669
3670 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3671
3672 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3673 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3674 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003675
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003676 uint32_t* GetIntrinsicOptimizations() {
3677 return &intrinsic_optimizations_;
3678 }
3679
3680 const uint32_t* GetIntrinsicOptimizations() const {
3681 return &intrinsic_optimizations_;
3682 }
3683
3684 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3685
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003686 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003687
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003688 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003689 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3690 static constexpr size_t kFieldOriginalInvokeTypeSize =
3691 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3692 static constexpr size_t kFieldReturnType =
3693 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3694 static constexpr size_t kFieldReturnTypeSize =
3695 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3696 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3697 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3698 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3699 using OriginalInvokeTypeField =
3700 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3701 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3702
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003703 HInvoke(ArenaAllocator* arena,
3704 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003705 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003706 Primitive::Type return_type,
3707 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003708 uint32_t dex_method_index,
3709 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003710 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003711 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003712 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003713 inputs_(number_of_arguments + number_of_other_inputs,
3714 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003715 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003716 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003717 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003718 SetPackedField<ReturnTypeField>(return_type);
3719 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3720 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003721 }
3722
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003723 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003724 return inputs_[index];
3725 }
3726
David Brazdil1abb4192015-02-17 18:33:36 +00003727 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003728 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003729 }
3730
Vladimir Markoa1de9182016-02-25 11:37:38 +00003731 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003732
Roland Levillain3e3d7332015-04-28 11:00:54 +01003733 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003734 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003735 const uint32_t dex_method_index_;
3736 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003737
3738 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3739 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003740
3741 private:
3742 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3743};
3744
Calin Juravle175dc732015-08-25 15:42:32 +01003745class HInvokeUnresolved : public HInvoke {
3746 public:
3747 HInvokeUnresolved(ArenaAllocator* arena,
3748 uint32_t number_of_arguments,
3749 Primitive::Type return_type,
3750 uint32_t dex_pc,
3751 uint32_t dex_method_index,
3752 InvokeType invoke_type)
3753 : HInvoke(arena,
3754 number_of_arguments,
3755 0u /* number_of_other_inputs */,
3756 return_type,
3757 dex_pc,
3758 dex_method_index,
3759 invoke_type) {
3760 }
3761
3762 DECLARE_INSTRUCTION(InvokeUnresolved);
3763
3764 private:
3765 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3766};
3767
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003768class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003769 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003770 // Requirements of this method call regarding the class
3771 // initialization (clinit) check of its declaring class.
3772 enum class ClinitCheckRequirement {
3773 kNone, // Class already initialized.
3774 kExplicit, // Static call having explicit clinit check as last input.
3775 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003776 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003777 };
3778
Vladimir Marko58155012015-08-19 12:49:41 +00003779 // Determines how to load the target ArtMethod*.
3780 enum class MethodLoadKind {
3781 // Use a String init ArtMethod* loaded from Thread entrypoints.
3782 kStringInit,
3783
3784 // Use the method's own ArtMethod* loaded by the register allocator.
3785 kRecursive,
3786
3787 // Use ArtMethod* at a known address, embed the direct address in the code.
3788 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3789 kDirectAddress,
3790
3791 // Use ArtMethod* at an address that will be known at link time, embed the direct
3792 // address in the code. If the image is relocatable, emit .patch_oat entry.
3793 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3794 // the image relocatable or not.
3795 kDirectAddressWithFixup,
3796
Vladimir Markoa1de9182016-02-25 11:37:38 +00003797 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003798 // Used when we need to use the dex cache, for example for invoke-static that
3799 // may cause class initialization (the entry may point to a resolution method),
3800 // and we know that we can access the dex cache arrays using a PC-relative load.
3801 kDexCachePcRelative,
3802
3803 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3804 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3805 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3806 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3807 kDexCacheViaMethod,
3808 };
3809
3810 // Determines the location of the code pointer.
3811 enum class CodePtrLocation {
3812 // Recursive call, use local PC-relative call instruction.
3813 kCallSelf,
3814
3815 // Use PC-relative call instruction patched at link time.
3816 // Used for calls within an oat file, boot->boot or app->app.
3817 kCallPCRelative,
3818
3819 // Call to a known target address, embed the direct address in code.
3820 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3821 kCallDirect,
3822
3823 // Call to a target address that will be known at link time, embed the direct
3824 // address in code. If the image is relocatable, emit .patch_oat entry.
3825 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3826 // the image relocatable or not.
3827 kCallDirectWithFixup,
3828
3829 // Use code pointer from the ArtMethod*.
3830 // Used when we don't know the target code. This is also the last-resort-kind used when
3831 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3832 kCallArtMethod,
3833 };
3834
3835 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003836 MethodLoadKind method_load_kind;
3837 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003838 // The method load data holds
3839 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3840 // Note that there are multiple string init methods, each having its own offset.
3841 // - the method address for kDirectAddress
3842 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003843 uint64_t method_load_data;
3844 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003845 };
3846
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003847 HInvokeStaticOrDirect(ArenaAllocator* arena,
3848 uint32_t number_of_arguments,
3849 Primitive::Type return_type,
3850 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003851 uint32_t method_index,
3852 MethodReference target_method,
3853 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003854 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003855 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003856 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003857 : HInvoke(arena,
3858 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003859 // There is potentially one extra argument for the HCurrentMethod node, and
3860 // potentially one other if the clinit check is explicit, and potentially
3861 // one other if the method is a string factory.
3862 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3863 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3864 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003865 return_type,
3866 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003867 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003868 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003869 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003870 dispatch_info_(dispatch_info) {
3871 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3872 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3873 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003874
Vladimir Markodc151b22015-10-15 18:02:30 +01003875 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003876 bool had_current_method_input = HasCurrentMethodInput();
3877 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3878
3879 // Using the current method is the default and once we find a better
3880 // method load kind, we should not go back to using the current method.
3881 DCHECK(had_current_method_input || !needs_current_method_input);
3882
3883 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003884 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3885 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003886 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003887 dispatch_info_ = dispatch_info;
3888 }
3889
Vladimir Markoc53c0792015-11-19 15:48:33 +00003890 void AddSpecialInput(HInstruction* input) {
3891 // We allow only one special input.
3892 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3893 DCHECK(InputCount() == GetSpecialInputIndex() ||
3894 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3895 InsertInputAt(GetSpecialInputIndex(), input);
3896 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003897
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003898 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003899 // We access the method via the dex cache so we can't do an implicit null check.
3900 // TODO: for intrinsics we can generate implicit null checks.
3901 return false;
3902 }
3903
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003904 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003905 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003906 }
3907
Vladimir Markoc53c0792015-11-19 15:48:33 +00003908 // Get the index of the special input, if any.
3909 //
David Brazdil6de19382016-01-08 17:37:10 +00003910 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3911 // method pointer; otherwise there may be one platform-specific special input,
3912 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003913 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003914 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003915
Vladimir Markoa1de9182016-02-25 11:37:38 +00003916 InvokeType GetOptimizedInvokeType() const {
3917 return GetPackedField<OptimizedInvokeTypeField>();
3918 }
3919
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003920 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003921 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003922 }
3923
Vladimir Marko58155012015-08-19 12:49:41 +00003924 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3925 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3926 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003927 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003928 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003929 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003930 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003931 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3932 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003933 bool HasCurrentMethodInput() const {
3934 // This function can be called only after the invoke has been fully initialized by the builder.
3935 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003936 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003937 return true;
3938 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003939 DCHECK(InputCount() == GetSpecialInputIndex() ||
3940 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003941 return false;
3942 }
3943 }
Vladimir Marko58155012015-08-19 12:49:41 +00003944 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3945 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003946 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003947
3948 int32_t GetStringInitOffset() const {
3949 DCHECK(IsStringInit());
3950 return dispatch_info_.method_load_data;
3951 }
3952
3953 uint64_t GetMethodAddress() const {
3954 DCHECK(HasMethodAddress());
3955 return dispatch_info_.method_load_data;
3956 }
3957
3958 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003959 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003960 return dispatch_info_.method_load_data;
3961 }
3962
3963 uint64_t GetDirectCodePtr() const {
3964 DCHECK(HasDirectCodePtr());
3965 return dispatch_info_.direct_code_ptr;
3966 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003967
Vladimir Markoa1de9182016-02-25 11:37:38 +00003968 ClinitCheckRequirement GetClinitCheckRequirement() const {
3969 return GetPackedField<ClinitCheckRequirementField>();
3970 }
Calin Juravle68ad6492015-08-18 17:08:12 +01003971
Roland Levillain4c0eb422015-04-24 16:43:49 +01003972 // Is this instruction a call to a static method?
3973 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003974 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003975 }
3976
Vladimir Markofbb184a2015-11-13 14:47:00 +00003977 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3978 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3979 // instruction; only relevant for static calls with explicit clinit check.
3980 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003981 DCHECK(IsStaticWithExplicitClinitCheck());
3982 size_t last_input_index = InputCount() - 1;
3983 HInstruction* last_input = InputAt(last_input_index);
3984 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003985 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003986 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003987 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003988 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003989 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003990 }
3991
David Brazdilbc9ab162016-01-20 14:50:19 +00003992 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003993 DCHECK(IsStringInit());
3994 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003995 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003996 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003997 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003998 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003999 }
4000
Roland Levillain4c0eb422015-04-24 16:43:49 +01004001 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004002 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004003 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004004 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004005 }
4006
4007 // Is this a call to a static method whose declaring class has an
4008 // implicit intialization check requirement?
4009 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004010 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004011 }
4012
Vladimir Markob554b5a2015-11-06 12:57:55 +00004013 // Does this method load kind need the current method as an input?
4014 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4015 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4016 }
4017
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004018 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004019
Roland Levillain4c0eb422015-04-24 16:43:49 +01004020 protected:
4021 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4022 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4023 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4024 HInstruction* input = input_record.GetInstruction();
4025 // `input` is the last input of a static invoke marked as having
4026 // an explicit clinit check. It must either be:
4027 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4028 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4029 DCHECK(input != nullptr);
4030 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4031 }
4032 return input_record;
4033 }
4034
Vladimir Markoc53c0792015-11-19 15:48:33 +00004035 void InsertInputAt(size_t index, HInstruction* input);
4036 void RemoveInputAt(size_t index);
4037
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004038 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004039 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4040 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4041 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4042 static constexpr size_t kFieldClinitCheckRequirement =
4043 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4044 static constexpr size_t kFieldClinitCheckRequirementSize =
4045 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4046 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4047 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4048 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4049 "Too many packed fields.");
4050 using OptimizedInvokeTypeField =
4051 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4052 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4053 kFieldClinitCheckRequirement,
4054 kFieldClinitCheckRequirementSize>;
4055
Vladimir Marko58155012015-08-19 12:49:41 +00004056 // The target method may refer to different dex file or method index than the original
4057 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4058 // in derived class to increase visibility.
4059 MethodReference target_method_;
4060 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004061
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004062 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004063};
Vladimir Markof64242a2015-12-01 14:58:23 +00004064std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004065std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004066
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004067class HInvokeVirtual : public HInvoke {
4068 public:
4069 HInvokeVirtual(ArenaAllocator* arena,
4070 uint32_t number_of_arguments,
4071 Primitive::Type return_type,
4072 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004073 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004074 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004075 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004076 vtable_index_(vtable_index) {}
4077
Calin Juravle641547a2015-04-21 22:08:51 +01004078 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004079 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004080 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004081 }
4082
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004083 uint32_t GetVTableIndex() const { return vtable_index_; }
4084
4085 DECLARE_INSTRUCTION(InvokeVirtual);
4086
4087 private:
4088 const uint32_t vtable_index_;
4089
4090 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4091};
4092
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004093class HInvokeInterface : public HInvoke {
4094 public:
4095 HInvokeInterface(ArenaAllocator* arena,
4096 uint32_t number_of_arguments,
4097 Primitive::Type return_type,
4098 uint32_t dex_pc,
4099 uint32_t dex_method_index,
4100 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004101 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004102 imt_index_(imt_index) {}
4103
Calin Juravle641547a2015-04-21 22:08:51 +01004104 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004105 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004106 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004107 }
4108
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004109 uint32_t GetImtIndex() const { return imt_index_; }
4110 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4111
4112 DECLARE_INSTRUCTION(InvokeInterface);
4113
4114 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004115 const uint32_t imt_index_;
4116
4117 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4118};
4119
Roland Levillain88cb1752014-10-20 16:36:47 +01004120class HNeg : public HUnaryOperation {
4121 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004122 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4123 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004124
Roland Levillain9867bc72015-08-05 10:21:34 +01004125 template <typename T> T Compute(T x) const { return -x; }
4126
4127 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004128 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004129 }
4130 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004131 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004132 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004133 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4134 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4135 }
4136 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4137 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4138 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004139
Roland Levillain88cb1752014-10-20 16:36:47 +01004140 DECLARE_INSTRUCTION(Neg);
4141
4142 private:
4143 DISALLOW_COPY_AND_ASSIGN(HNeg);
4144};
4145
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004146class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004147 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004148 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004149 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004150 uint32_t dex_pc,
4151 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004152 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004153 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004154 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004155 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004156 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004157 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004158 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004159 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004160 }
4161
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004162 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004163 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004164
4165 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004166 bool NeedsEnvironment() const OVERRIDE { return true; }
4167
Mingyao Yang0c365e62015-03-31 15:09:29 -07004168 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4169 bool CanThrow() const OVERRIDE { return true; }
4170
Calin Juravle10e244f2015-01-26 18:54:32 +00004171 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004172
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004173 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4174
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004175 DECLARE_INSTRUCTION(NewArray);
4176
4177 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004178 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004179 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004180 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004181
4182 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4183};
4184
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004185class HAdd : public HBinaryOperation {
4186 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004187 HAdd(Primitive::Type result_type,
4188 HInstruction* left,
4189 HInstruction* right,
4190 uint32_t dex_pc = kNoDexPc)
4191 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004192
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004193 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004194
Roland Levillain9867bc72015-08-05 10:21:34 +01004195 template <typename T> T Compute(T x, T y) const { return x + y; }
4196
4197 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004198 return GetBlock()->GetGraph()->GetIntConstant(
4199 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004200 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004201 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004202 return GetBlock()->GetGraph()->GetLongConstant(
4203 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004204 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004205 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4206 return GetBlock()->GetGraph()->GetFloatConstant(
4207 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4208 }
4209 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4210 return GetBlock()->GetGraph()->GetDoubleConstant(
4211 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4212 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004213
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004214 DECLARE_INSTRUCTION(Add);
4215
4216 private:
4217 DISALLOW_COPY_AND_ASSIGN(HAdd);
4218};
4219
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004220class HSub : public HBinaryOperation {
4221 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004222 HSub(Primitive::Type result_type,
4223 HInstruction* left,
4224 HInstruction* right,
4225 uint32_t dex_pc = kNoDexPc)
4226 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004227
Roland Levillain9867bc72015-08-05 10:21:34 +01004228 template <typename T> T Compute(T x, T y) const { return x - y; }
4229
4230 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004231 return GetBlock()->GetGraph()->GetIntConstant(
4232 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004233 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004234 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004235 return GetBlock()->GetGraph()->GetLongConstant(
4236 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004237 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004238 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4239 return GetBlock()->GetGraph()->GetFloatConstant(
4240 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4241 }
4242 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4243 return GetBlock()->GetGraph()->GetDoubleConstant(
4244 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4245 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004246
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004247 DECLARE_INSTRUCTION(Sub);
4248
4249 private:
4250 DISALLOW_COPY_AND_ASSIGN(HSub);
4251};
4252
Calin Juravle34bacdf2014-10-07 20:23:36 +01004253class HMul : public HBinaryOperation {
4254 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004255 HMul(Primitive::Type result_type,
4256 HInstruction* left,
4257 HInstruction* right,
4258 uint32_t dex_pc = kNoDexPc)
4259 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004260
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004261 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004262
Roland Levillain9867bc72015-08-05 10:21:34 +01004263 template <typename T> T Compute(T x, T y) const { return x * y; }
4264
4265 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004266 return GetBlock()->GetGraph()->GetIntConstant(
4267 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004268 }
4269 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004270 return GetBlock()->GetGraph()->GetLongConstant(
4271 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004272 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004273 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4274 return GetBlock()->GetGraph()->GetFloatConstant(
4275 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4276 }
4277 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4278 return GetBlock()->GetGraph()->GetDoubleConstant(
4279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4280 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004281
4282 DECLARE_INSTRUCTION(Mul);
4283
4284 private:
4285 DISALLOW_COPY_AND_ASSIGN(HMul);
4286};
4287
Calin Juravle7c4954d2014-10-28 16:57:40 +00004288class HDiv : public HBinaryOperation {
4289 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004290 HDiv(Primitive::Type result_type,
4291 HInstruction* left,
4292 HInstruction* right,
4293 uint32_t dex_pc)
4294 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004295
Roland Levillain9867bc72015-08-05 10:21:34 +01004296 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004297 T ComputeIntegral(T x, T y) const {
4298 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004299 // Our graph structure ensures we never have 0 for `y` during
4300 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004301 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004302 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004303 return (y == -1) ? -x : x / y;
4304 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004305
Roland Levillain31dd3d62016-02-16 12:21:02 +00004306 template <typename T>
4307 T ComputeFP(T x, T y) const {
4308 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4309 return x / y;
4310 }
4311
Roland Levillain9867bc72015-08-05 10:21:34 +01004312 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004313 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004314 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004315 }
4316 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004317 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004318 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4319 }
4320 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4321 return GetBlock()->GetGraph()->GetFloatConstant(
4322 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4323 }
4324 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4325 return GetBlock()->GetGraph()->GetDoubleConstant(
4326 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004327 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004328
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004329 static SideEffects SideEffectsForArchRuntimeCalls() {
4330 // The generated code can use a runtime call.
4331 return SideEffects::CanTriggerGC();
4332 }
4333
Calin Juravle7c4954d2014-10-28 16:57:40 +00004334 DECLARE_INSTRUCTION(Div);
4335
4336 private:
4337 DISALLOW_COPY_AND_ASSIGN(HDiv);
4338};
4339
Calin Juravlebacfec32014-11-14 15:54:36 +00004340class HRem : public HBinaryOperation {
4341 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004342 HRem(Primitive::Type result_type,
4343 HInstruction* left,
4344 HInstruction* right,
4345 uint32_t dex_pc)
4346 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004347
Roland Levillain9867bc72015-08-05 10:21:34 +01004348 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004349 T ComputeIntegral(T x, T y) const {
4350 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004351 // Our graph structure ensures we never have 0 for `y` during
4352 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004353 DCHECK_NE(y, 0);
4354 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4355 return (y == -1) ? 0 : x % y;
4356 }
4357
Roland Levillain31dd3d62016-02-16 12:21:02 +00004358 template <typename T>
4359 T ComputeFP(T x, T y) const {
4360 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4361 return std::fmod(x, y);
4362 }
4363
Roland Levillain9867bc72015-08-05 10:21:34 +01004364 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004365 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004366 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004367 }
4368 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004369 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004370 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004371 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004372 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4373 return GetBlock()->GetGraph()->GetFloatConstant(
4374 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4375 }
4376 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4377 return GetBlock()->GetGraph()->GetDoubleConstant(
4378 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4379 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004380
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004381 static SideEffects SideEffectsForArchRuntimeCalls() {
4382 return SideEffects::CanTriggerGC();
4383 }
4384
Calin Juravlebacfec32014-11-14 15:54:36 +00004385 DECLARE_INSTRUCTION(Rem);
4386
4387 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004388 DISALLOW_COPY_AND_ASSIGN(HRem);
4389};
4390
Calin Juravled0d48522014-11-04 16:40:20 +00004391class HDivZeroCheck : public HExpression<1> {
4392 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004393 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4394 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004395 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004396 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004397 SetRawInputAt(0, value);
4398 }
4399
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004400 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4401
Calin Juravled0d48522014-11-04 16:40:20 +00004402 bool CanBeMoved() const OVERRIDE { return true; }
4403
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004404 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004405 return true;
4406 }
4407
4408 bool NeedsEnvironment() const OVERRIDE { return true; }
4409 bool CanThrow() const OVERRIDE { return true; }
4410
Calin Juravled0d48522014-11-04 16:40:20 +00004411 DECLARE_INSTRUCTION(DivZeroCheck);
4412
4413 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004414 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4415};
4416
Calin Juravle9aec02f2014-11-18 23:06:35 +00004417class HShl : public HBinaryOperation {
4418 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004419 HShl(Primitive::Type result_type,
4420 HInstruction* left,
4421 HInstruction* right,
4422 uint32_t dex_pc = kNoDexPc)
4423 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004424
Roland Levillain9867bc72015-08-05 10:21:34 +01004425 template <typename T, typename U, typename V>
4426 T Compute(T x, U y, V max_shift_value) const {
4427 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4428 "V is not the unsigned integer type corresponding to T");
4429 return x << (y & max_shift_value);
4430 }
4431
4432 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4433 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004434 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004435 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004436 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4437 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004438 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004439 }
4440 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4441 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004442 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004443 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004444 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4445 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4446 LOG(FATAL) << DebugName() << " is not defined for float values";
4447 UNREACHABLE();
4448 }
4449 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4450 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4451 LOG(FATAL) << DebugName() << " is not defined for double values";
4452 UNREACHABLE();
4453 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004454
4455 DECLARE_INSTRUCTION(Shl);
4456
4457 private:
4458 DISALLOW_COPY_AND_ASSIGN(HShl);
4459};
4460
4461class HShr : public HBinaryOperation {
4462 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004463 HShr(Primitive::Type result_type,
4464 HInstruction* left,
4465 HInstruction* right,
4466 uint32_t dex_pc = kNoDexPc)
4467 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004468
Roland Levillain9867bc72015-08-05 10:21:34 +01004469 template <typename T, typename U, typename V>
4470 T Compute(T x, U y, V max_shift_value) const {
4471 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4472 "V is not the unsigned integer type corresponding to T");
4473 return x >> (y & max_shift_value);
4474 }
4475
4476 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4477 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004478 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004479 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004480 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4481 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004483 }
4484 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4485 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004486 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004487 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004488 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4489 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4490 LOG(FATAL) << DebugName() << " is not defined for float values";
4491 UNREACHABLE();
4492 }
4493 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4494 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4495 LOG(FATAL) << DebugName() << " is not defined for double values";
4496 UNREACHABLE();
4497 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004498
4499 DECLARE_INSTRUCTION(Shr);
4500
4501 private:
4502 DISALLOW_COPY_AND_ASSIGN(HShr);
4503};
4504
4505class HUShr : public HBinaryOperation {
4506 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004507 HUShr(Primitive::Type result_type,
4508 HInstruction* left,
4509 HInstruction* right,
4510 uint32_t dex_pc = kNoDexPc)
4511 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004512
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 template <typename T, typename U, typename V>
4514 T Compute(T x, U y, V max_shift_value) const {
4515 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4516 "V is not the unsigned integer type corresponding to T");
4517 V ux = static_cast<V>(x);
4518 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004519 }
4520
Roland Levillain9867bc72015-08-05 10:21:34 +01004521 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4522 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004523 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004524 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004525 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4526 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004527 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004528 }
4529 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4530 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004531 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004532 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004533 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4534 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4535 LOG(FATAL) << DebugName() << " is not defined for float values";
4536 UNREACHABLE();
4537 }
4538 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4539 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4540 LOG(FATAL) << DebugName() << " is not defined for double values";
4541 UNREACHABLE();
4542 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004543
4544 DECLARE_INSTRUCTION(UShr);
4545
4546 private:
4547 DISALLOW_COPY_AND_ASSIGN(HUShr);
4548};
4549
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004550class HAnd : public HBinaryOperation {
4551 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004552 HAnd(Primitive::Type result_type,
4553 HInstruction* left,
4554 HInstruction* right,
4555 uint32_t dex_pc = kNoDexPc)
4556 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004557
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004558 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004559
Roland Levillaine53bd812016-02-24 14:54:18 +00004560 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004561
4562 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004563 return GetBlock()->GetGraph()->GetIntConstant(
4564 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004565 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004566 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004567 return GetBlock()->GetGraph()->GetLongConstant(
4568 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004569 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004570 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4571 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4572 LOG(FATAL) << DebugName() << " is not defined for float values";
4573 UNREACHABLE();
4574 }
4575 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4576 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4577 LOG(FATAL) << DebugName() << " is not defined for double values";
4578 UNREACHABLE();
4579 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004580
4581 DECLARE_INSTRUCTION(And);
4582
4583 private:
4584 DISALLOW_COPY_AND_ASSIGN(HAnd);
4585};
4586
4587class HOr : public HBinaryOperation {
4588 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004589 HOr(Primitive::Type result_type,
4590 HInstruction* left,
4591 HInstruction* right,
4592 uint32_t dex_pc = kNoDexPc)
4593 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004594
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004595 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004596
Roland Levillaine53bd812016-02-24 14:54:18 +00004597 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004598
4599 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004600 return GetBlock()->GetGraph()->GetIntConstant(
4601 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004602 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004603 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004604 return GetBlock()->GetGraph()->GetLongConstant(
4605 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004606 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004607 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4608 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4609 LOG(FATAL) << DebugName() << " is not defined for float values";
4610 UNREACHABLE();
4611 }
4612 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4613 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4614 LOG(FATAL) << DebugName() << " is not defined for double values";
4615 UNREACHABLE();
4616 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004617
4618 DECLARE_INSTRUCTION(Or);
4619
4620 private:
4621 DISALLOW_COPY_AND_ASSIGN(HOr);
4622};
4623
4624class HXor : public HBinaryOperation {
4625 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004626 HXor(Primitive::Type result_type,
4627 HInstruction* left,
4628 HInstruction* right,
4629 uint32_t dex_pc = kNoDexPc)
4630 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004631
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004632 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004633
Roland Levillaine53bd812016-02-24 14:54:18 +00004634 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004635
4636 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004637 return GetBlock()->GetGraph()->GetIntConstant(
4638 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004639 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004640 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004641 return GetBlock()->GetGraph()->GetLongConstant(
4642 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004643 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004644 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4645 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4646 LOG(FATAL) << DebugName() << " is not defined for float values";
4647 UNREACHABLE();
4648 }
4649 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4650 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4651 LOG(FATAL) << DebugName() << " is not defined for double values";
4652 UNREACHABLE();
4653 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004654
4655 DECLARE_INSTRUCTION(Xor);
4656
4657 private:
4658 DISALLOW_COPY_AND_ASSIGN(HXor);
4659};
4660
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004661class HRor : public HBinaryOperation {
4662 public:
4663 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4664 : HBinaryOperation(result_type, value, distance) {}
4665
4666 template <typename T, typename U, typename V>
4667 T Compute(T x, U y, V max_shift_value) const {
4668 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4669 "V is not the unsigned integer type corresponding to T");
4670 V ux = static_cast<V>(x);
4671 if ((y & max_shift_value) == 0) {
4672 return static_cast<T>(ux);
4673 } else {
4674 const V reg_bits = sizeof(T) * 8;
4675 return static_cast<T>(ux >> (y & max_shift_value)) |
4676 (x << (reg_bits - (y & max_shift_value)));
4677 }
4678 }
4679
4680 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4681 return GetBlock()->GetGraph()->GetIntConstant(
4682 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4683 }
4684 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4685 return GetBlock()->GetGraph()->GetLongConstant(
4686 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4687 }
4688 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4689 return GetBlock()->GetGraph()->GetLongConstant(
4690 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4691 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004692 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4693 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4694 LOG(FATAL) << DebugName() << " is not defined for float values";
4695 UNREACHABLE();
4696 }
4697 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4698 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4699 LOG(FATAL) << DebugName() << " is not defined for double values";
4700 UNREACHABLE();
4701 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004702
4703 DECLARE_INSTRUCTION(Ror);
4704
4705 private:
4706 DISALLOW_COPY_AND_ASSIGN(HRor);
4707};
4708
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004709// The value of a parameter in this method. Its location depends on
4710// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004711class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004712 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004713 HParameterValue(const DexFile& dex_file,
4714 uint16_t type_index,
4715 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004716 Primitive::Type parameter_type,
4717 bool is_this = false)
4718 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004719 dex_file_(dex_file),
4720 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004721 index_(index) {
4722 SetPackedFlag<kFlagIsThis>(is_this);
4723 SetPackedFlag<kFlagCanBeNull>(!is_this);
4724 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004725
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004726 const DexFile& GetDexFile() const { return dex_file_; }
4727 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004728 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004729 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004730
Vladimir Markoa1de9182016-02-25 11:37:38 +00004731 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4732 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004733
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004734 DECLARE_INSTRUCTION(ParameterValue);
4735
4736 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004737 // Whether or not the parameter value corresponds to 'this' argument.
4738 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4739 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4740 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4741 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4742 "Too many packed fields.");
4743
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004744 const DexFile& dex_file_;
4745 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004746 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004747 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004748 const uint8_t index_;
4749
4750 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4751};
4752
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004753class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004754 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004755 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4756 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004757
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004758 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004759 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004760 return true;
4761 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004762
Roland Levillain9867bc72015-08-05 10:21:34 +01004763 template <typename T> T Compute(T x) const { return ~x; }
4764
4765 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004766 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004767 }
4768 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004769 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004770 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004771 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4772 LOG(FATAL) << DebugName() << " is not defined for float values";
4773 UNREACHABLE();
4774 }
4775 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4776 LOG(FATAL) << DebugName() << " is not defined for double values";
4777 UNREACHABLE();
4778 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004779
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004780 DECLARE_INSTRUCTION(Not);
4781
4782 private:
4783 DISALLOW_COPY_AND_ASSIGN(HNot);
4784};
4785
David Brazdil66d126e2015-04-03 16:02:44 +01004786class HBooleanNot : public HUnaryOperation {
4787 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004788 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4789 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004790
4791 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004792 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004793 return true;
4794 }
4795
Roland Levillain9867bc72015-08-05 10:21:34 +01004796 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004797 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004798 return !x;
4799 }
4800
Roland Levillain9867bc72015-08-05 10:21:34 +01004801 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004803 }
4804 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4805 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004806 UNREACHABLE();
4807 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004808 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4809 LOG(FATAL) << DebugName() << " is not defined for float values";
4810 UNREACHABLE();
4811 }
4812 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4813 LOG(FATAL) << DebugName() << " is not defined for double values";
4814 UNREACHABLE();
4815 }
David Brazdil66d126e2015-04-03 16:02:44 +01004816
4817 DECLARE_INSTRUCTION(BooleanNot);
4818
4819 private:
4820 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4821};
4822
Roland Levillaindff1f282014-11-05 14:15:05 +00004823class HTypeConversion : public HExpression<1> {
4824 public:
4825 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004826 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004827 : HExpression(result_type,
4828 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4829 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004830 SetRawInputAt(0, input);
4831 DCHECK_NE(input->GetType(), result_type);
4832 }
4833
4834 HInstruction* GetInput() const { return InputAt(0); }
4835 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4836 Primitive::Type GetResultType() const { return GetType(); }
4837
4838 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004839 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004840
Mark Mendelle82549b2015-05-06 10:55:34 -04004841 // Try to statically evaluate the conversion and return a HConstant
4842 // containing the result. If the input cannot be converted, return nullptr.
4843 HConstant* TryStaticEvaluation() const;
4844
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004845 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4846 Primitive::Type result_type) {
4847 // Some architectures may not require the 'GC' side effects, but at this point
4848 // in the compilation process we do not know what architecture we will
4849 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004850 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4851 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004852 return SideEffects::CanTriggerGC();
4853 }
4854 return SideEffects::None();
4855 }
4856
Roland Levillaindff1f282014-11-05 14:15:05 +00004857 DECLARE_INSTRUCTION(TypeConversion);
4858
4859 private:
4860 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4861};
4862
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004863static constexpr uint32_t kNoRegNumber = -1;
4864
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004865class HPhi : public HInstruction {
4866 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004867 HPhi(ArenaAllocator* arena,
4868 uint32_t reg_number,
4869 size_t number_of_inputs,
4870 Primitive::Type type,
4871 uint32_t dex_pc = kNoDexPc)
4872 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004873 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004874 reg_number_(reg_number) {
4875 SetPackedField<TypeField>(ToPhiType(type));
4876 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4877 // Phis are constructed live and marked dead if conflicting or unused.
4878 // Individual steps of SsaBuilder should assume that if a phi has been
4879 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4880 SetPackedFlag<kFlagIsLive>(true);
4881 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004882 }
4883
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004884 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4885 static Primitive::Type ToPhiType(Primitive::Type type) {
4886 switch (type) {
4887 case Primitive::kPrimBoolean:
4888 case Primitive::kPrimByte:
4889 case Primitive::kPrimShort:
4890 case Primitive::kPrimChar:
4891 return Primitive::kPrimInt;
4892 default:
4893 return type;
4894 }
4895 }
4896
David Brazdilffee3d32015-07-06 11:48:53 +01004897 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4898
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004899 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004900
4901 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004902 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004903
Vladimir Markoa1de9182016-02-25 11:37:38 +00004904 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004905 void SetType(Primitive::Type new_type) {
4906 // Make sure that only valid type changes occur. The following are allowed:
4907 // (1) int -> float/ref (primitive type propagation),
4908 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004909 DCHECK(GetType() == new_type ||
4910 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4911 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4912 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4913 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004914 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004915
Vladimir Markoa1de9182016-02-25 11:37:38 +00004916 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4917 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004918
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004919 uint32_t GetRegNumber() const { return reg_number_; }
4920
Vladimir Markoa1de9182016-02-25 11:37:38 +00004921 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4922 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4923 bool IsDead() const { return !IsLive(); }
4924 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004925
David Brazdil77a48ae2015-09-15 12:34:04 +00004926 bool IsVRegEquivalentOf(HInstruction* other) const {
4927 return other != nullptr
4928 && other->IsPhi()
4929 && other->AsPhi()->GetBlock() == GetBlock()
4930 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4931 }
4932
Calin Juravlea4f88312015-04-16 12:57:19 +01004933 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4934 // An equivalent phi is a phi having the same dex register and type.
4935 // It assumes that phis with the same dex register are adjacent.
4936 HPhi* GetNextEquivalentPhiWithSameType() {
4937 HInstruction* next = GetNext();
4938 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4939 if (next->GetType() == GetType()) {
4940 return next->AsPhi();
4941 }
4942 next = next->GetNext();
4943 }
4944 return nullptr;
4945 }
4946
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004947 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004948
David Brazdil1abb4192015-02-17 18:33:36 +00004949 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004950 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004951 return inputs_[index];
4952 }
David Brazdil1abb4192015-02-17 18:33:36 +00004953
4954 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004955 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004956 }
4957
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004958 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004959 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
4960 static constexpr size_t kFieldTypeSize =
4961 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4962 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
4963 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
4964 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
4965 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4966 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
4967
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004968 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004969 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004970
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004971 DISALLOW_COPY_AND_ASSIGN(HPhi);
4972};
4973
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004974class HNullCheck : public HExpression<1> {
4975 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004976 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4977 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004979 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004980 SetRawInputAt(0, value);
4981 }
4982
Calin Juravle10e244f2015-01-26 18:54:32 +00004983 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004984 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004985 return true;
4986 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004987
Calin Juravle10e244f2015-01-26 18:54:32 +00004988 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004989
Calin Juravle10e244f2015-01-26 18:54:32 +00004990 bool CanThrow() const OVERRIDE { return true; }
4991
4992 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004993
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004994
4995 DECLARE_INSTRUCTION(NullCheck);
4996
4997 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004998 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4999};
5000
5001class FieldInfo : public ValueObject {
5002 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005003 FieldInfo(MemberOffset field_offset,
5004 Primitive::Type field_type,
5005 bool is_volatile,
5006 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005007 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005008 const DexFile& dex_file,
5009 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005010 : field_offset_(field_offset),
5011 field_type_(field_type),
5012 is_volatile_(is_volatile),
5013 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005014 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005015 dex_file_(dex_file),
5016 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005017
5018 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005019 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005020 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005021 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005022 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005023 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005024 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005025
5026 private:
5027 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005028 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005029 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005030 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005031 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005032 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005033 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005034};
5035
5036class HInstanceFieldGet : public HExpression<1> {
5037 public:
5038 HInstanceFieldGet(HInstruction* value,
5039 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005040 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005041 bool is_volatile,
5042 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005043 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005044 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005045 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005046 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005047 : HExpression(field_type,
5048 SideEffects::FieldReadOfType(field_type, is_volatile),
5049 dex_pc),
5050 field_info_(field_offset,
5051 field_type,
5052 is_volatile,
5053 field_idx,
5054 declaring_class_def_index,
5055 dex_file,
5056 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057 SetRawInputAt(0, value);
5058 }
5059
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005060 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005061
5062 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5063 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5064 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005065 }
5066
Calin Juravle641547a2015-04-21 22:08:51 +01005067 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5068 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005069 }
5070
5071 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005072 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5073 }
5074
Calin Juravle52c48962014-12-16 17:02:57 +00005075 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005076 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005077 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005078 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005079
5080 DECLARE_INSTRUCTION(InstanceFieldGet);
5081
5082 private:
5083 const FieldInfo field_info_;
5084
5085 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5086};
5087
5088class HInstanceFieldSet : public HTemplateInstruction<2> {
5089 public:
5090 HInstanceFieldSet(HInstruction* object,
5091 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005092 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005093 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005094 bool is_volatile,
5095 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005096 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005097 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005098 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005099 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005100 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5101 dex_pc),
5102 field_info_(field_offset,
5103 field_type,
5104 is_volatile,
5105 field_idx,
5106 declaring_class_def_index,
5107 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005108 dex_cache) {
5109 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005110 SetRawInputAt(0, object);
5111 SetRawInputAt(1, value);
5112 }
5113
Calin Juravle641547a2015-04-21 22:08:51 +01005114 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5115 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005116 }
5117
Calin Juravle52c48962014-12-16 17:02:57 +00005118 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005119 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005120 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005121 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005122 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005123 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5124 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005125
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005126 DECLARE_INSTRUCTION(InstanceFieldSet);
5127
5128 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005129 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5130 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5131 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5132 "Too many packed fields.");
5133
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005134 const FieldInfo field_info_;
5135
5136 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5137};
5138
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005139class HArrayGet : public HExpression<2> {
5140 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005141 HArrayGet(HInstruction* array,
5142 HInstruction* index,
5143 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005144 uint32_t dex_pc,
5145 SideEffects additional_side_effects = SideEffects::None())
5146 : HExpression(type,
5147 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005148 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 SetRawInputAt(0, array);
5150 SetRawInputAt(1, index);
5151 }
5152
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005153 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005154 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005155 return true;
5156 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005157 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005158 // TODO: We can be smarter here.
5159 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5160 // which generates the implicit null check. There are cases when these can be removed
5161 // to produce better code. If we ever add optimizations to do so we should allow an
5162 // implicit check here (as long as the address falls in the first page).
5163 return false;
5164 }
5165
David Brazdil4833f5a2015-12-16 10:37:39 +00005166 bool IsEquivalentOf(HArrayGet* other) const {
5167 bool result = (GetDexPc() == other->GetDexPc());
5168 if (kIsDebugBuild && result) {
5169 DCHECK_EQ(GetBlock(), other->GetBlock());
5170 DCHECK_EQ(GetArray(), other->GetArray());
5171 DCHECK_EQ(GetIndex(), other->GetIndex());
5172 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005173 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005174 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005175 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5176 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005177 }
5178 }
5179 return result;
5180 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005181
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005182 HInstruction* GetArray() const { return InputAt(0); }
5183 HInstruction* GetIndex() const { return InputAt(1); }
5184
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185 DECLARE_INSTRUCTION(ArrayGet);
5186
5187 private:
5188 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5189};
5190
5191class HArraySet : public HTemplateInstruction<3> {
5192 public:
5193 HArraySet(HInstruction* array,
5194 HInstruction* index,
5195 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005196 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005197 uint32_t dex_pc,
5198 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005199 : HTemplateInstruction(
5200 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005201 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5202 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005203 dex_pc) {
5204 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5205 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5206 SetPackedFlag<kFlagValueCanBeNull>(true);
5207 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 SetRawInputAt(0, array);
5209 SetRawInputAt(1, index);
5210 SetRawInputAt(2, value);
5211 }
5212
Calin Juravle77520bc2015-01-12 18:45:46 +00005213 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005214 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005215 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216 }
5217
Mingyao Yang81014cb2015-06-02 03:16:27 -07005218 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005219 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005220
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005221 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005222 // TODO: Same as for ArrayGet.
5223 return false;
5224 }
5225
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005226 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005227 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005228 }
5229
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005230 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005231 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005232 }
5233
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005234 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005235 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005236 }
5237
Vladimir Markoa1de9182016-02-25 11:37:38 +00005238 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5239 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5240 bool StaticTypeOfArrayIsObjectArray() const {
5241 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5242 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005243
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005244 HInstruction* GetArray() const { return InputAt(0); }
5245 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005246 HInstruction* GetValue() const { return InputAt(2); }
5247
5248 Primitive::Type GetComponentType() const {
5249 // The Dex format does not type floating point index operations. Since the
5250 // `expected_component_type_` is set during building and can therefore not
5251 // be correct, we also check what is the value type. If it is a floating
5252 // point type, we must use that type.
5253 Primitive::Type value_type = GetValue()->GetType();
5254 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5255 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005256 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005257 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005258
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005259 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005260 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005261 }
5262
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005263 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5264 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5265 }
5266
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 DECLARE_INSTRUCTION(ArraySet);
5268
5269 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005270 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5271 static constexpr size_t kFieldExpectedComponentTypeSize =
5272 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5273 static constexpr size_t kFlagNeedsTypeCheck =
5274 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5275 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005276 // Cached information for the reference_type_info_ so that codegen
5277 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005278 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5279 static constexpr size_t kNumberOfArraySetPackedBits =
5280 kFlagStaticTypeOfArrayIsObjectArray + 1;
5281 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5282 using ExpectedComponentTypeField =
5283 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284
5285 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5286};
5287
5288class HArrayLength : public HExpression<1> {
5289 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005290 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005291 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005292 // Note that arrays do not change length, so the instruction does not
5293 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294 SetRawInputAt(0, array);
5295 }
5296
Calin Juravle77520bc2015-01-12 18:45:46 +00005297 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005298 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005299 return true;
5300 }
Calin Juravle641547a2015-04-21 22:08:51 +01005301 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5302 return obj == InputAt(0);
5303 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005304
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 DECLARE_INSTRUCTION(ArrayLength);
5306
5307 private:
5308 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5309};
5310
5311class HBoundsCheck : public HExpression<2> {
5312 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005313 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5314 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005316 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 DCHECK(index->GetType() == Primitive::kPrimInt);
5318 SetRawInputAt(0, index);
5319 SetRawInputAt(1, length);
5320 }
5321
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005322 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005323 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005324 return true;
5325 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005326
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005327 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005328
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005329 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005330
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005331 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332
5333 DECLARE_INSTRUCTION(BoundsCheck);
5334
5335 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5337};
5338
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005339class HSuspendCheck : public HTemplateInstruction<0> {
5340 public:
5341 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005342 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005343
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005344 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005345 return true;
5346 }
5347
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005348 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5349 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005350
5351 DECLARE_INSTRUCTION(SuspendCheck);
5352
5353 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005354 // Only used for code generation, in order to share the same slow path between back edges
5355 // of a same loop.
5356 SlowPathCode* slow_path_;
5357
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005358 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5359};
5360
David Srbecky0cf44932015-12-09 14:09:59 +00005361// Pseudo-instruction which provides the native debugger with mapping information.
5362// It ensures that we can generate line number and local variables at this point.
5363class HNativeDebugInfo : public HTemplateInstruction<0> {
5364 public:
5365 explicit HNativeDebugInfo(uint32_t dex_pc)
5366 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5367
5368 bool NeedsEnvironment() const OVERRIDE {
5369 return true;
5370 }
5371
5372 DECLARE_INSTRUCTION(NativeDebugInfo);
5373
5374 private:
5375 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5376};
5377
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005378/**
5379 * Instruction to load a Class object.
5380 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005381class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005382 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005383 HLoadClass(HCurrentMethod* current_method,
5384 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005385 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005386 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005387 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005388 bool needs_access_check,
5389 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005390 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005391 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005392 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005393 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005394 // Referrers class should not need access check. We never inline unverified
5395 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005396 DCHECK(!is_referrers_class || !needs_access_check);
5397
5398 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5399 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5400 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5401 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005402 SetRawInputAt(0, current_method);
5403 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005404
5405 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005406
5407 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005408 // Note that we don't need to test for generate_clinit_check_.
5409 // Whether or not we need to generate the clinit check is processed in
5410 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005411 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005412 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005413 }
5414
5415 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5416
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005417 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005418 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005419
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005420 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005421 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005422 }
5423
Calin Juravle0ba218d2015-05-19 18:46:01 +01005424 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005425 // The entrypoint the code generator is going to call does not do
5426 // clinit of the class.
5427 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005428 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005429 }
5430
5431 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005432 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005433 (!IsReferrersClass() && !IsInDexCache()) ||
5434 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005435 }
5436
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005437
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005438 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005439 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005440 }
5441
Calin Juravleacf735c2015-02-12 15:25:22 +00005442 ReferenceTypeInfo GetLoadedClassRTI() {
5443 return loaded_class_rti_;
5444 }
5445
5446 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5447 // Make sure we only set exact types (the loaded class should never be merged).
5448 DCHECK(rti.IsExact());
5449 loaded_class_rti_ = rti;
5450 }
5451
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005452 const DexFile& GetDexFile() { return dex_file_; }
5453
Vladimir Markoa1de9182016-02-25 11:37:38 +00005454 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005455
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005456 static SideEffects SideEffectsForArchRuntimeCalls() {
5457 return SideEffects::CanTriggerGC();
5458 }
5459
Vladimir Markoa1de9182016-02-25 11:37:38 +00005460 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5461 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5462 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5463 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005464
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005465 DECLARE_INSTRUCTION(LoadClass);
5466
5467 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005468 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5469 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5470 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005471 // Whether this instruction must generate the initialization check.
5472 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005473 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5474 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5475 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5476
5477 const uint16_t type_index_;
5478 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005479
Calin Juravleacf735c2015-02-12 15:25:22 +00005480 ReferenceTypeInfo loaded_class_rti_;
5481
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005482 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5483};
5484
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005485class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005486 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005487 HLoadString(HCurrentMethod* current_method,
5488 uint32_t string_index,
5489 uint32_t dex_pc,
5490 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005491 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005492 string_index_(string_index) {
5493 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005494 SetRawInputAt(0, current_method);
5495 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005496
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005497 bool CanBeMoved() const OVERRIDE { return true; }
5498
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005499 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5500 return other->AsLoadString()->string_index_ == string_index_;
5501 }
5502
5503 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5504
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005505 uint32_t GetStringIndex() const { return string_index_; }
5506
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005507 // Will call the runtime if the string is not already in the dex cache.
5508 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5509
Vladimir Markodc151b22015-10-15 18:02:30 +01005510 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005511 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005512 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005513
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005514 static SideEffects SideEffectsForArchRuntimeCalls() {
5515 return SideEffects::CanTriggerGC();
5516 }
5517
Vladimir Markoa1de9182016-02-25 11:37:38 +00005518 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5519
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005520 DECLARE_INSTRUCTION(LoadString);
5521
5522 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005523 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
5524 static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1;
5525 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5526
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005527 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005528
5529 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5530};
5531
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005532/**
5533 * Performs an initialization check on its Class object input.
5534 */
5535class HClinitCheck : public HExpression<1> {
5536 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005537 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005538 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005539 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005540 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5541 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005542 SetRawInputAt(0, constant);
5543 }
5544
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005545 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005546 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005547 return true;
5548 }
5549
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005550 bool NeedsEnvironment() const OVERRIDE {
5551 // May call runtime to initialize the class.
5552 return true;
5553 }
5554
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005555 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005556
5557 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5558
5559 DECLARE_INSTRUCTION(ClinitCheck);
5560
5561 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005562 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5563};
5564
5565class HStaticFieldGet : public HExpression<1> {
5566 public:
5567 HStaticFieldGet(HInstruction* cls,
5568 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005569 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005570 bool is_volatile,
5571 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005572 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005573 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005574 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005575 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005576 : HExpression(field_type,
5577 SideEffects::FieldReadOfType(field_type, is_volatile),
5578 dex_pc),
5579 field_info_(field_offset,
5580 field_type,
5581 is_volatile,
5582 field_idx,
5583 declaring_class_def_index,
5584 dex_file,
5585 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005586 SetRawInputAt(0, cls);
5587 }
5588
Calin Juravle52c48962014-12-16 17:02:57 +00005589
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005590 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005591
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005592 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005593 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5594 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005595 }
5596
5597 size_t ComputeHashCode() const OVERRIDE {
5598 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5599 }
5600
Calin Juravle52c48962014-12-16 17:02:57 +00005601 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005602 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5603 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005604 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005605
5606 DECLARE_INSTRUCTION(StaticFieldGet);
5607
5608 private:
5609 const FieldInfo field_info_;
5610
5611 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5612};
5613
5614class HStaticFieldSet : public HTemplateInstruction<2> {
5615 public:
5616 HStaticFieldSet(HInstruction* cls,
5617 HInstruction* value,
5618 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005619 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005620 bool is_volatile,
5621 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005622 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005623 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005624 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005625 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005626 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5627 dex_pc),
5628 field_info_(field_offset,
5629 field_type,
5630 is_volatile,
5631 field_idx,
5632 declaring_class_def_index,
5633 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005634 dex_cache) {
5635 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005636 SetRawInputAt(0, cls);
5637 SetRawInputAt(1, value);
5638 }
5639
Calin Juravle52c48962014-12-16 17:02:57 +00005640 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005641 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5642 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005643 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005644
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005645 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005646 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5647 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005648
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005649 DECLARE_INSTRUCTION(StaticFieldSet);
5650
5651 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005652 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5653 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5654 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5655 "Too many packed fields.");
5656
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005657 const FieldInfo field_info_;
5658
5659 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5660};
5661
Calin Juravlee460d1d2015-09-29 04:52:17 +01005662class HUnresolvedInstanceFieldGet : public HExpression<1> {
5663 public:
5664 HUnresolvedInstanceFieldGet(HInstruction* obj,
5665 Primitive::Type field_type,
5666 uint32_t field_index,
5667 uint32_t dex_pc)
5668 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5669 field_index_(field_index) {
5670 SetRawInputAt(0, obj);
5671 }
5672
5673 bool NeedsEnvironment() const OVERRIDE { return true; }
5674 bool CanThrow() const OVERRIDE { return true; }
5675
5676 Primitive::Type GetFieldType() const { return GetType(); }
5677 uint32_t GetFieldIndex() const { return field_index_; }
5678
5679 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5680
5681 private:
5682 const uint32_t field_index_;
5683
5684 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5685};
5686
5687class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5688 public:
5689 HUnresolvedInstanceFieldSet(HInstruction* obj,
5690 HInstruction* value,
5691 Primitive::Type field_type,
5692 uint32_t field_index,
5693 uint32_t dex_pc)
5694 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005695 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005696 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005697 DCHECK_EQ(field_type, value->GetType());
5698 SetRawInputAt(0, obj);
5699 SetRawInputAt(1, value);
5700 }
5701
5702 bool NeedsEnvironment() const OVERRIDE { return true; }
5703 bool CanThrow() const OVERRIDE { return true; }
5704
Vladimir Markoa1de9182016-02-25 11:37:38 +00005705 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005706 uint32_t GetFieldIndex() const { return field_index_; }
5707
5708 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5709
5710 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005711 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5712 static constexpr size_t kFieldFieldTypeSize =
5713 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5714 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5715 kFieldFieldType + kFieldFieldTypeSize;
5716 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5717 "Too many packed fields.");
5718 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5719
Calin Juravlee460d1d2015-09-29 04:52:17 +01005720 const uint32_t field_index_;
5721
5722 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5723};
5724
5725class HUnresolvedStaticFieldGet : public HExpression<0> {
5726 public:
5727 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5728 uint32_t field_index,
5729 uint32_t dex_pc)
5730 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5731 field_index_(field_index) {
5732 }
5733
5734 bool NeedsEnvironment() const OVERRIDE { return true; }
5735 bool CanThrow() const OVERRIDE { return true; }
5736
5737 Primitive::Type GetFieldType() const { return GetType(); }
5738 uint32_t GetFieldIndex() const { return field_index_; }
5739
5740 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5741
5742 private:
5743 const uint32_t field_index_;
5744
5745 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5746};
5747
5748class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5749 public:
5750 HUnresolvedStaticFieldSet(HInstruction* value,
5751 Primitive::Type field_type,
5752 uint32_t field_index,
5753 uint32_t dex_pc)
5754 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005755 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005756 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005757 DCHECK_EQ(field_type, value->GetType());
5758 SetRawInputAt(0, value);
5759 }
5760
5761 bool NeedsEnvironment() const OVERRIDE { return true; }
5762 bool CanThrow() const OVERRIDE { return true; }
5763
Vladimir Markoa1de9182016-02-25 11:37:38 +00005764 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005765 uint32_t GetFieldIndex() const { return field_index_; }
5766
5767 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5768
5769 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005770 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5771 static constexpr size_t kFieldFieldTypeSize =
5772 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5773 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5774 kFieldFieldType + kFieldFieldTypeSize;
5775 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5776 "Too many packed fields.");
5777 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5778
Calin Juravlee460d1d2015-09-29 04:52:17 +01005779 const uint32_t field_index_;
5780
5781 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5782};
5783
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005784// Implement the move-exception DEX instruction.
5785class HLoadException : public HExpression<0> {
5786 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005787 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5788 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005789
David Brazdilbbd733e2015-08-18 17:48:17 +01005790 bool CanBeNull() const OVERRIDE { return false; }
5791
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005792 DECLARE_INSTRUCTION(LoadException);
5793
5794 private:
5795 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5796};
5797
David Brazdilcb1c0552015-08-04 16:22:25 +01005798// Implicit part of move-exception which clears thread-local exception storage.
5799// Must not be removed because the runtime expects the TLS to get cleared.
5800class HClearException : public HTemplateInstruction<0> {
5801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005802 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5803 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005804
5805 DECLARE_INSTRUCTION(ClearException);
5806
5807 private:
5808 DISALLOW_COPY_AND_ASSIGN(HClearException);
5809};
5810
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005811class HThrow : public HTemplateInstruction<1> {
5812 public:
5813 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005814 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005815 SetRawInputAt(0, exception);
5816 }
5817
5818 bool IsControlFlow() const OVERRIDE { return true; }
5819
5820 bool NeedsEnvironment() const OVERRIDE { return true; }
5821
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005822 bool CanThrow() const OVERRIDE { return true; }
5823
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005824
5825 DECLARE_INSTRUCTION(Throw);
5826
5827 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005828 DISALLOW_COPY_AND_ASSIGN(HThrow);
5829};
5830
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831/**
5832 * Implementation strategies for the code generator of a HInstanceOf
5833 * or `HCheckCast`.
5834 */
5835enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005836 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005837 kExactCheck, // Can do a single class compare.
5838 kClassHierarchyCheck, // Can just walk the super class chain.
5839 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5840 kInterfaceCheck, // No optimization yet when checking against an interface.
5841 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005842 kArrayCheck, // No optimization yet when checking against a generic array.
5843 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844};
5845
Roland Levillain86503782016-02-11 19:07:30 +00005846std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5847
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005848class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005849 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005850 HInstanceOf(HInstruction* object,
5851 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005852 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005853 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005854 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005855 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005856 dex_pc) {
5857 SetPackedField<TypeCheckKindField>(check_kind);
5858 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005859 SetRawInputAt(0, object);
5860 SetRawInputAt(1, constant);
5861 }
5862
5863 bool CanBeMoved() const OVERRIDE { return true; }
5864
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005865 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005866 return true;
5867 }
5868
5869 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005870 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005871 }
5872
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005873 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005874 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5875 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5876 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5877 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005878
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005879 static bool CanCallRuntime(TypeCheckKind check_kind) {
5880 // Mips currently does runtime calls for any other checks.
5881 return check_kind != TypeCheckKind::kExactCheck;
5882 }
5883
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005884 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005885 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005886 }
5887
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005888 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005889
5890 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005891 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5892 static constexpr size_t kFieldTypeCheckKindSize =
5893 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5894 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5895 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5896 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5897 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005898
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005899 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5900};
5901
Calin Juravleb1498f62015-02-16 13:13:29 +00005902class HBoundType : public HExpression<1> {
5903 public:
David Brazdilf5552582015-12-27 13:36:12 +00005904 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005905 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005906 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5907 SetPackedFlag<kFlagUpperCanBeNull>(true);
5908 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005909 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005910 SetRawInputAt(0, input);
5911 }
5912
David Brazdilf5552582015-12-27 13:36:12 +00005913 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005914 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005915 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005916 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005917
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005918 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005919 DCHECK(GetUpperCanBeNull() || !can_be_null);
5920 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005921 }
5922
Vladimir Markoa1de9182016-02-25 11:37:38 +00005923 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005924
Calin Juravleb1498f62015-02-16 13:13:29 +00005925 DECLARE_INSTRUCTION(BoundType);
5926
5927 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005928 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5929 // is false then CanBeNull() cannot be true).
5930 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
5931 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
5932 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
5933 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5934
Calin Juravleb1498f62015-02-16 13:13:29 +00005935 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005936 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5937 // It is used to bound the type in cases like:
5938 // if (x instanceof ClassX) {
5939 // // uper_bound_ will be ClassX
5940 // }
David Brazdilf5552582015-12-27 13:36:12 +00005941 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005942
5943 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5944};
5945
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005946class HCheckCast : public HTemplateInstruction<2> {
5947 public:
5948 HCheckCast(HInstruction* object,
5949 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005950 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005951 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00005952 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
5953 SetPackedField<TypeCheckKindField>(check_kind);
5954 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005955 SetRawInputAt(0, object);
5956 SetRawInputAt(1, constant);
5957 }
5958
5959 bool CanBeMoved() const OVERRIDE { return true; }
5960
5961 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5962 return true;
5963 }
5964
5965 bool NeedsEnvironment() const OVERRIDE {
5966 // Instruction may throw a CheckCastError.
5967 return true;
5968 }
5969
5970 bool CanThrow() const OVERRIDE { return true; }
5971
Vladimir Markoa1de9182016-02-25 11:37:38 +00005972 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5973 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5974 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5975 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005976
5977 DECLARE_INSTRUCTION(CheckCast);
5978
5979 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005980 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
5981 static constexpr size_t kFieldTypeCheckKindSize =
5982 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5983 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5984 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
5985 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5986 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005987
5988 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005989};
5990
Calin Juravle27df7582015-04-17 19:12:31 +01005991class HMemoryBarrier : public HTemplateInstruction<0> {
5992 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005993 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005994 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00005995 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
5996 SetPackedField<BarrierKindField>(barrier_kind);
5997 }
Calin Juravle27df7582015-04-17 19:12:31 +01005998
Vladimir Markoa1de9182016-02-25 11:37:38 +00005999 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006000
6001 DECLARE_INSTRUCTION(MemoryBarrier);
6002
6003 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6005 static constexpr size_t kFieldBarrierKindSize =
6006 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6007 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6008 kFieldBarrierKind + kFieldBarrierKindSize;
6009 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6010 "Too many packed fields.");
6011 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006012
6013 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6014};
6015
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006016class HMonitorOperation : public HTemplateInstruction<1> {
6017 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006018 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006019 kEnter,
6020 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006021 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006022 };
6023
6024 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006025 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006026 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6027 dex_pc) {
6028 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006029 SetRawInputAt(0, object);
6030 }
6031
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006032 // Instruction may go into runtime, so we need an environment.
6033 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006034
6035 bool CanThrow() const OVERRIDE {
6036 // Verifier guarantees that monitor-exit cannot throw.
6037 // This is important because it allows the HGraphBuilder to remove
6038 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6039 return IsEnter();
6040 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006041
Vladimir Markoa1de9182016-02-25 11:37:38 +00006042 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6043 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006044
6045 DECLARE_INSTRUCTION(MonitorOperation);
6046
Calin Juravle52c48962014-12-16 17:02:57 +00006047 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006048 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6049 static constexpr size_t kFieldOperationKindSize =
6050 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6051 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6052 kFieldOperationKind + kFieldOperationKindSize;
6053 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6054 "Too many packed fields.");
6055 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006056
6057 private:
6058 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6059};
6060
David Brazdil74eb1b22015-12-14 11:44:01 +00006061class HSelect : public HExpression<3> {
6062 public:
6063 HSelect(HInstruction* condition,
6064 HInstruction* true_value,
6065 HInstruction* false_value,
6066 uint32_t dex_pc)
6067 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6068 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6069
6070 // First input must be `true_value` or `false_value` to allow codegens to
6071 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6072 // that architectures which implement HSelect as a conditional move also
6073 // will not need to invert the condition.
6074 SetRawInputAt(0, false_value);
6075 SetRawInputAt(1, true_value);
6076 SetRawInputAt(2, condition);
6077 }
6078
6079 HInstruction* GetFalseValue() const { return InputAt(0); }
6080 HInstruction* GetTrueValue() const { return InputAt(1); }
6081 HInstruction* GetCondition() const { return InputAt(2); }
6082
6083 bool CanBeMoved() const OVERRIDE { return true; }
6084 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6085
6086 bool CanBeNull() const OVERRIDE {
6087 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6088 }
6089
6090 DECLARE_INSTRUCTION(Select);
6091
6092 private:
6093 DISALLOW_COPY_AND_ASSIGN(HSelect);
6094};
6095
Vladimir Markof9f64412015-09-02 14:05:49 +01006096class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006097 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006098 MoveOperands(Location source,
6099 Location destination,
6100 Primitive::Type type,
6101 HInstruction* instruction)
6102 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006103
6104 Location GetSource() const { return source_; }
6105 Location GetDestination() const { return destination_; }
6106
6107 void SetSource(Location value) { source_ = value; }
6108 void SetDestination(Location value) { destination_ = value; }
6109
6110 // The parallel move resolver marks moves as "in-progress" by clearing the
6111 // destination (but not the source).
6112 Location MarkPending() {
6113 DCHECK(!IsPending());
6114 Location dest = destination_;
6115 destination_ = Location::NoLocation();
6116 return dest;
6117 }
6118
6119 void ClearPending(Location dest) {
6120 DCHECK(IsPending());
6121 destination_ = dest;
6122 }
6123
6124 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006125 DCHECK(source_.IsValid() || destination_.IsInvalid());
6126 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006127 }
6128
6129 // True if this blocks a move from the given location.
6130 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006131 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006132 }
6133
6134 // A move is redundant if it's been eliminated, if its source and
6135 // destination are the same, or if its destination is unneeded.
6136 bool IsRedundant() const {
6137 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6138 }
6139
6140 // We clear both operands to indicate move that's been eliminated.
6141 void Eliminate() {
6142 source_ = destination_ = Location::NoLocation();
6143 }
6144
6145 bool IsEliminated() const {
6146 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6147 return source_.IsInvalid();
6148 }
6149
Alexey Frunze4dda3372015-06-01 18:31:49 -07006150 Primitive::Type GetType() const { return type_; }
6151
Nicolas Geoffray90218252015-04-15 11:56:51 +01006152 bool Is64BitMove() const {
6153 return Primitive::Is64BitType(type_);
6154 }
6155
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006156 HInstruction* GetInstruction() const { return instruction_; }
6157
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006158 private:
6159 Location source_;
6160 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006161 // The type this move is for.
6162 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006163 // The instruction this move is assocatied with. Null when this move is
6164 // for moving an input in the expected locations of user (including a phi user).
6165 // This is only used in debug mode, to ensure we do not connect interval siblings
6166 // in the same parallel move.
6167 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006168};
6169
Roland Levillainc9285912015-12-18 10:38:42 +00006170std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6171
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006172static constexpr size_t kDefaultNumberOfMoves = 4;
6173
6174class HParallelMove : public HTemplateInstruction<0> {
6175 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006176 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006177 : HTemplateInstruction(SideEffects::None(), dex_pc),
6178 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6179 moves_.reserve(kDefaultNumberOfMoves);
6180 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006181
Nicolas Geoffray90218252015-04-15 11:56:51 +01006182 void AddMove(Location source,
6183 Location destination,
6184 Primitive::Type type,
6185 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006186 DCHECK(source.IsValid());
6187 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006188 if (kIsDebugBuild) {
6189 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006190 for (const MoveOperands& move : moves_) {
6191 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006192 // Special case the situation where the move is for the spill slot
6193 // of the instruction.
6194 if ((GetPrevious() == instruction)
6195 || ((GetPrevious() == nullptr)
6196 && instruction->IsPhi()
6197 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006198 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006199 << "Doing parallel moves for the same instruction.";
6200 } else {
6201 DCHECK(false) << "Doing parallel moves for the same instruction.";
6202 }
6203 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006204 }
6205 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006206 for (const MoveOperands& move : moves_) {
6207 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006208 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006209 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006210 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006211 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006212 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006213 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006214 }
6215
Vladimir Marko225b6462015-09-28 12:17:40 +01006216 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006217 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006218 }
6219
Vladimir Marko225b6462015-09-28 12:17:40 +01006220 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006221
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006222 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006223
6224 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006225 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006226
6227 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6228};
6229
Mark Mendell0616ae02015-04-17 12:49:27 -04006230} // namespace art
6231
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006232#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6233#include "nodes_shared.h"
6234#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006235#ifdef ART_ENABLE_CODEGEN_arm
6236#include "nodes_arm.h"
6237#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006238#ifdef ART_ENABLE_CODEGEN_arm64
6239#include "nodes_arm64.h"
6240#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006241#ifdef ART_ENABLE_CODEGEN_x86
6242#include "nodes_x86.h"
6243#endif
6244
6245namespace art {
6246
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006247class HGraphVisitor : public ValueObject {
6248 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006249 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6250 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006251
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006252 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006253 virtual void VisitBasicBlock(HBasicBlock* block);
6254
Roland Levillain633021e2014-10-01 14:12:25 +01006255 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006256 void VisitInsertionOrder();
6257
Roland Levillain633021e2014-10-01 14:12:25 +01006258 // Visit the graph following dominator tree reverse post-order.
6259 void VisitReversePostOrder();
6260
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006261 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006262
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006263 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006264#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006265 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6266
6267 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6268
6269#undef DECLARE_VISIT_INSTRUCTION
6270
6271 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006272 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006273
6274 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6275};
6276
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006277class HGraphDelegateVisitor : public HGraphVisitor {
6278 public:
6279 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6280 virtual ~HGraphDelegateVisitor() {}
6281
6282 // Visit functions that delegate to to super class.
6283#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006284 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006285
6286 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6287
6288#undef DECLARE_VISIT_INSTRUCTION
6289
6290 private:
6291 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6292};
6293
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006294class HInsertionOrderIterator : public ValueObject {
6295 public:
6296 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6297
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006298 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006299 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006300 void Advance() { ++index_; }
6301
6302 private:
6303 const HGraph& graph_;
6304 size_t index_;
6305
6306 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6307};
6308
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006309class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006310 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006311 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6312 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006313 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006314 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006315
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006316 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6317 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006318 void Advance() { ++index_; }
6319
6320 private:
6321 const HGraph& graph_;
6322 size_t index_;
6323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006324 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006325};
6326
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006327class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006328 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006329 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006330 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006331 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006332 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006333 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006334
6335 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006336 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006337 void Advance() { --index_; }
6338
6339 private:
6340 const HGraph& graph_;
6341 size_t index_;
6342
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006343 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006344};
6345
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006346class HLinearPostOrderIterator : public ValueObject {
6347 public:
6348 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006349 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006350
6351 bool Done() const { return index_ == 0; }
6352
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006353 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006354
6355 void Advance() {
6356 --index_;
6357 DCHECK_GE(index_, 0U);
6358 }
6359
6360 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006361 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006362 size_t index_;
6363
6364 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6365};
6366
6367class HLinearOrderIterator : public ValueObject {
6368 public:
6369 explicit HLinearOrderIterator(const HGraph& graph)
6370 : order_(graph.GetLinearOrder()), index_(0) {}
6371
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006372 bool Done() const { return index_ == order_.size(); }
6373 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006374 void Advance() { ++index_; }
6375
6376 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006377 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006378 size_t index_;
6379
6380 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6381};
6382
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006383// Iterator over the blocks that art part of the loop. Includes blocks part
6384// of an inner loop. The order in which the blocks are iterated is on their
6385// block id.
6386class HBlocksInLoopIterator : public ValueObject {
6387 public:
6388 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6389 : blocks_in_loop_(info.GetBlocks()),
6390 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6391 index_(0) {
6392 if (!blocks_in_loop_.IsBitSet(index_)) {
6393 Advance();
6394 }
6395 }
6396
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006397 bool Done() const { return index_ == blocks_.size(); }
6398 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006399 void Advance() {
6400 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006401 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006402 if (blocks_in_loop_.IsBitSet(index_)) {
6403 break;
6404 }
6405 }
6406 }
6407
6408 private:
6409 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006410 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006411 size_t index_;
6412
6413 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6414};
6415
Mingyao Yang3584bce2015-05-19 16:01:59 -07006416// Iterator over the blocks that art part of the loop. Includes blocks part
6417// of an inner loop. The order in which the blocks are iterated is reverse
6418// post order.
6419class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6420 public:
6421 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6422 : blocks_in_loop_(info.GetBlocks()),
6423 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6424 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006425 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006426 Advance();
6427 }
6428 }
6429
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006430 bool Done() const { return index_ == blocks_.size(); }
6431 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006432 void Advance() {
6433 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006434 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6435 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006436 break;
6437 }
6438 }
6439 }
6440
6441 private:
6442 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006443 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006444 size_t index_;
6445
6446 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6447};
6448
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006449inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006450 if (constant->IsIntConstant()) {
6451 return constant->AsIntConstant()->GetValue();
6452 } else if (constant->IsLongConstant()) {
6453 return constant->AsLongConstant()->GetValue();
6454 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006455 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006456 return 0;
6457 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006458}
6459
Vladimir Marko58155012015-08-19 12:49:41 +00006460inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6461 // For the purposes of the compiler, the dex files must actually be the same object
6462 // if we want to safely treat them as the same. This is especially important for JIT
6463 // as custom class loaders can open the same underlying file (or memory) multiple
6464 // times and provide different class resolution but no two class loaders should ever
6465 // use the same DexFile object - doing so is an unsupported hack that can lead to
6466 // all sorts of weird failures.
6467 return &lhs == &rhs;
6468}
6469
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006470#define INSTRUCTION_TYPE_CHECK(type, super) \
6471 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6472 inline const H##type* HInstruction::As##type() const { \
6473 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6474 } \
6475 inline H##type* HInstruction::As##type() { \
6476 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6477 }
6478
6479 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6480#undef INSTRUCTION_TYPE_CHECK
6481
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006482class SwitchTable : public ValueObject {
6483 public:
6484 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6485 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6486 int32_t table_offset = instruction.VRegB_31t();
6487 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6488 if (sparse) {
6489 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6490 } else {
6491 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6492 }
6493 num_entries_ = table[1];
6494 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6495 }
6496
6497 uint16_t GetNumEntries() const {
6498 return num_entries_;
6499 }
6500
6501 void CheckIndex(size_t index) const {
6502 if (sparse_) {
6503 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6504 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6505 } else {
6506 // In a packed table, we have the starting key and num_entries_ values.
6507 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6508 }
6509 }
6510
6511 int32_t GetEntryAt(size_t index) const {
6512 CheckIndex(index);
6513 return values_[index];
6514 }
6515
6516 uint32_t GetDexPcForIndex(size_t index) const {
6517 CheckIndex(index);
6518 return dex_pc_ +
6519 (reinterpret_cast<const int16_t*>(values_ + index) -
6520 reinterpret_cast<const int16_t*>(&instruction_));
6521 }
6522
6523 // Index of the first value in the table.
6524 size_t GetFirstValueIndex() const {
6525 if (sparse_) {
6526 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6527 return num_entries_;
6528 } else {
6529 // In a packed table, we have the starting key and num_entries_ values.
6530 return 1;
6531 }
6532 }
6533
6534 private:
6535 const Instruction& instruction_;
6536 const uint32_t dex_pc_;
6537
6538 // Whether this is a sparse-switch table (or a packed-switch one).
6539 const bool sparse_;
6540
6541 // This can't be const as it needs to be computed off of the given instruction, and complicated
6542 // expressions in the initializer list seemed very ugly.
6543 uint16_t num_entries_;
6544
6545 const int32_t* values_;
6546
6547 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6548};
6549
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006550// Create space in `blocks` for adding `number_of_new_blocks` entries
6551// starting at location `at`. Blocks after `at` are moved accordingly.
6552inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6553 size_t number_of_new_blocks,
6554 size_t after) {
6555 DCHECK_LT(after, blocks->size());
6556 size_t old_size = blocks->size();
6557 size_t new_size = old_size + number_of_new_blocks;
6558 blocks->resize(new_size);
6559 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6560}
6561
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006562} // namespace art
6563
6564#endif // ART_COMPILER_OPTIMIZING_NODES_H_