blob: 854854f23837ff9a4891da0ab35f18a8922af756 [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);
134 void Add(const HInstructionList& instruction_list);
135
David Brazdil2d7352b2015-04-20 14:52:42 +0100136 // Return the number of instructions in the list. This is an expensive operation.
137 size_t CountSize() const;
138
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 private:
140 HInstruction* first_instruction_;
141 HInstruction* last_instruction_;
142
143 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000144 friend class HGraph;
145 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100146 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100147 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148
149 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
150};
151
David Brazdil4833f5a2015-12-16 10:37:39 +0000152class ReferenceTypeInfo : ValueObject {
153 public:
154 typedef Handle<mirror::Class> TypeHandle;
155
156 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
157 // The constructor will check that the type_handle is valid.
158 return ReferenceTypeInfo(type_handle, is_exact);
159 }
160
161 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
162
Vladimir Markof39745e2016-01-26 12:16:55 +0000163 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000164 return handle.GetReference() != nullptr;
165 }
166
167 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
168 return IsValidHandle(type_handle_);
169 }
170
171 bool IsExact() const { return is_exact_; }
172
173 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
174 DCHECK(IsValid());
175 return GetTypeHandle()->IsObjectClass();
176 }
177
178 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
179 DCHECK(IsValid());
180 return GetTypeHandle()->IsStringClass();
181 }
182
183 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
184 DCHECK(IsValid());
185 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
186 }
187
188 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
189 DCHECK(IsValid());
190 return GetTypeHandle()->IsInterface();
191 }
192
193 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
194 DCHECK(IsValid());
195 return GetTypeHandle()->IsArrayClass();
196 }
197
198 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
199 DCHECK(IsValid());
200 return GetTypeHandle()->IsPrimitiveArray();
201 }
202
203 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
204 DCHECK(IsValid());
205 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
206 }
207
208 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
209 DCHECK(IsValid());
210 if (!IsExact()) return false;
211 if (!IsArrayClass()) return false;
212 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
213 }
214
215 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
216 DCHECK(IsValid());
217 if (!IsExact()) return false;
218 if (!IsArrayClass()) return false;
219 if (!rti.IsArrayClass()) return false;
220 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
221 rti.GetTypeHandle()->GetComponentType());
222 }
223
224 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
225
226 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
227 DCHECK(IsValid());
228 DCHECK(rti.IsValid());
229 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
230 }
231
232 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
233 DCHECK(IsValid());
234 DCHECK(rti.IsValid());
235 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
236 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
237 }
238
239 // Returns true if the type information provide the same amount of details.
240 // Note that it does not mean that the instructions have the same actual type
241 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000242 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 if (!IsValid() && !rti.IsValid()) {
244 // Invalid types are equal.
245 return true;
246 }
247 if (!IsValid() || !rti.IsValid()) {
248 // One is valid, the other not.
249 return false;
250 }
251 return IsExact() == rti.IsExact()
252 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
253 }
254
255 private:
256 ReferenceTypeInfo();
257 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
258
259 // The class of the object.
260 TypeHandle type_handle_;
261 // Whether or not the type is exact or a superclass of the actual type.
262 // Whether or not we have any information about this type.
263 bool is_exact_;
264};
265
266std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
267
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000268// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100269class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000270 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100271 HGraph(ArenaAllocator* arena,
272 const DexFile& dex_file,
273 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100274 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100276 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100277 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000278 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100279 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100281 blocks_(arena->Adapter(kArenaAllocBlockList)),
282 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
283 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700284 entry_block_(nullptr),
285 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100286 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100287 number_of_vregs_(0),
288 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000289 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400290 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000291 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000292 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000293 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000294 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100295 dex_file_(dex_file),
296 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100297 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100298 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100299 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000301 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100302 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
303 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
304 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
305 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000306 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000307 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
308 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100309 blocks_.reserve(kDefaultNumberOfBlocks);
310 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000311
David Brazdilbadd8262016-02-02 16:28:56 +0000312 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
313 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
314
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000315 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100316 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
317
David Brazdil69ba7b72015-06-23 18:27:30 +0100318 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000319 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100320
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000321 HBasicBlock* GetEntryBlock() const { return entry_block_; }
322 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100323 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000324
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
326 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000327
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000328 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100329
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100330 void ComputeDominanceInformation();
331 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000332 void ClearLoopInformation();
333 void FindBackEdges(ArenaBitVector* visited);
334 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100335 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100336 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337
David Brazdil4833f5a2015-12-16 10:37:39 +0000338 // Analyze all natural loops in this graph. Returns a code specifying that it
339 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000340 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000341 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100342
David Brazdilffee3d32015-07-06 11:48:53 +0100343 // Iterate over blocks to compute try block membership. Needs reverse post
344 // order and loop information.
345 void ComputeTryBlockInformation();
346
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000347 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000348 // Returns the instruction to replace the invoke expression or null if the
349 // invoke is for a void method. Note that the caller is responsible for replacing
350 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000351 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000352
Mingyao Yang3584bce2015-05-19 16:01:59 -0700353 // Need to add a couple of blocks to test if the loop body is entered and
354 // put deoptimization instructions, etc.
355 void TransformLoopHeaderForBCE(HBasicBlock* header);
356
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000357 // Removes `block` from the graph. Assumes `block` has been disconnected from
358 // other blocks and has no instructions or phis.
359 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000360
David Brazdilfc6a86a2015-06-26 10:33:45 +0000361 // Splits the edge between `block` and `successor` while preserving the
362 // indices in the predecessor/successor lists. If there are multiple edges
363 // between the blocks, the lowest indices are used.
364 // Returns the new block which is empty and has the same dex pc as `successor`.
365 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
366
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100367 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
368 void SimplifyLoop(HBasicBlock* header);
369
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000370 int32_t GetNextInstructionId() {
371 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000372 return current_instruction_id_++;
373 }
374
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000375 int32_t GetCurrentInstructionId() const {
376 return current_instruction_id_;
377 }
378
379 void SetCurrentInstructionId(int32_t id) {
380 current_instruction_id_ = id;
381 }
382
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100383 uint16_t GetMaximumNumberOfOutVRegs() const {
384 return maximum_number_of_out_vregs_;
385 }
386
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000387 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
388 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100389 }
390
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100391 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
392 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
393 }
394
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000395 void UpdateTemporariesVRegSlots(size_t slots) {
396 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100397 }
398
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000399 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100400 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000401 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100402 }
403
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100404 void SetNumberOfVRegs(uint16_t number_of_vregs) {
405 number_of_vregs_ = number_of_vregs;
406 }
407
408 uint16_t GetNumberOfVRegs() const {
409 return number_of_vregs_;
410 }
411
412 void SetNumberOfInVRegs(uint16_t value) {
413 number_of_in_vregs_ = value;
414 }
415
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100416 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100417 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100418 return number_of_vregs_ - number_of_in_vregs_;
419 }
420
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100421 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100423 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100424
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100425 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100426 return linear_order_;
427 }
428
Mark Mendell1152c922015-04-24 17:06:35 -0400429 bool HasBoundsChecks() const {
430 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800431 }
432
Mark Mendell1152c922015-04-24 17:06:35 -0400433 void SetHasBoundsChecks(bool value) {
434 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800435 }
436
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100437 bool ShouldGenerateConstructorBarrier() const {
438 return should_generate_constructor_barrier_;
439 }
440
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000441 bool IsDebuggable() const { return debuggable_; }
442
David Brazdil8d5b8b22015-03-24 10:51:52 +0000443 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000444 // already, it is created and inserted into the graph. This method is only for
445 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600446 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000447
448 // TODO: This is problematic for the consistency of reference type propagation
449 // because it can be created anytime after the pass and thus it will be left
450 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600451 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000452
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600453 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
454 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600456 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
457 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000458 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600459 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
460 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000461 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600462 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
463 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000464 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000465
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100466 HCurrentMethod* GetCurrentMethod();
467
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100468 const DexFile& GetDexFile() const {
469 return dex_file_;
470 }
471
472 uint32_t GetMethodIdx() const {
473 return method_idx_;
474 }
475
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100476 InvokeType GetInvokeType() const {
477 return invoke_type_;
478 }
479
Mark Mendellc4701932015-04-10 13:18:51 -0400480 InstructionSet GetInstructionSet() const {
481 return instruction_set_;
482 }
483
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000484 bool IsCompilingOsr() const { return osr_; }
485
David Brazdil77a48ae2015-09-15 12:34:04 +0000486 bool HasTryCatch() const { return has_try_catch_; }
487 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100488
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000489 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
490 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
491
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100492 ArtMethod* GetArtMethod() const { return art_method_; }
493 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
494
Mark Mendellf6529172015-11-17 11:16:56 -0500495 // Returns an instruction with the opposite boolean value from 'cond'.
496 // The instruction has been inserted into the graph, either as a constant, or
497 // before cursor.
498 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
499
David Brazdil2d7352b2015-04-20 14:52:42 +0100500 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000501 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100502 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000503
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000504 template <class InstructionType, typename ValueType>
505 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600506 ArenaSafeMap<ValueType, InstructionType*>* cache,
507 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000508 // Try to find an existing constant of the given value.
509 InstructionType* constant = nullptr;
510 auto cached_constant = cache->find(value);
511 if (cached_constant != cache->end()) {
512 constant = cached_constant->second;
513 }
514
515 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100516 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000517 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600518 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000519 cache->Overwrite(value, constant);
520 InsertConstant(constant);
521 }
522 return constant;
523 }
524
David Brazdil8d5b8b22015-03-24 10:51:52 +0000525 void InsertConstant(HConstant* instruction);
526
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000527 // Cache a float constant into the graph. This method should only be
528 // called by the SsaBuilder when creating "equivalent" instructions.
529 void CacheFloatConstant(HFloatConstant* constant);
530
531 // See CacheFloatConstant comment.
532 void CacheDoubleConstant(HDoubleConstant* constant);
533
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000534 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000535
536 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100537 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000538
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100539 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100540 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000541
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100542 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100543 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100544
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000545 HBasicBlock* entry_block_;
546 HBasicBlock* exit_block_;
547
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100548 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100549 uint16_t maximum_number_of_out_vregs_;
550
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100551 // The number of virtual registers in this method. Contains the parameters.
552 uint16_t number_of_vregs_;
553
554 // The number of virtual registers used by parameters of this method.
555 uint16_t number_of_in_vregs_;
556
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000557 // Number of vreg size slots that the temporaries use (used in baseline compiler).
558 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100559
Mark Mendell1152c922015-04-24 17:06:35 -0400560 // Has bounds checks. We can totally skip BCE if it's false.
561 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800562
David Brazdil77a48ae2015-09-15 12:34:04 +0000563 // Flag whether there are any try/catch blocks in the graph. We will skip
564 // try/catch-related passes if false.
565 bool has_try_catch_;
566
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000567 // Flag whether there are any irreducible loops in the graph.
568 bool has_irreducible_loops_;
569
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000570 // Indicates whether the graph should be compiled in a way that
571 // ensures full debuggability. If false, we can apply more
572 // aggressive optimizations that may limit the level of debugging.
573 const bool debuggable_;
574
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000575 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000576 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000577
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100578 // The dex file from which the method is from.
579 const DexFile& dex_file_;
580
581 // The method index in the dex file.
582 const uint32_t method_idx_;
583
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100584 // If inlined, this encodes how the callee is being invoked.
585 const InvokeType invoke_type_;
586
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100587 // Whether the graph has been transformed to SSA form. Only used
588 // in debug mode to ensure we are not using properties only valid
589 // for non-SSA form (like the number of temporaries).
590 bool in_ssa_form_;
591
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100592 const bool should_generate_constructor_barrier_;
593
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594 const InstructionSet instruction_set_;
595
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000597 HNullConstant* cached_null_constant_;
598 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000599 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000600 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000601 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000602
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100603 HCurrentMethod* cached_current_method_;
604
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100605 // The ArtMethod this graph is for. Note that for AOT, it may be null,
606 // for example for methods whose declaring class could not be resolved
607 // (such as when the superclass could not be found).
608 ArtMethod* art_method_;
609
David Brazdil4833f5a2015-12-16 10:37:39 +0000610 // Keep the RTI of inexact Object to avoid having to pass stack handle
611 // collection pointer to passes which may create NullConstant.
612 ReferenceTypeInfo inexact_object_rti_;
613
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000614 // Whether we are compiling this graph for on stack replacement: this will
615 // make all loops seen as irreducible and emit special stack maps to mark
616 // compiled code entries which the interpreter can directly jump to.
617 const bool osr_;
618
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100620 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000621 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000622 DISALLOW_COPY_AND_ASSIGN(HGraph);
623};
624
Vladimir Markof9f64412015-09-02 14:05:49 +0100625class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000626 public:
627 HLoopInformation(HBasicBlock* header, HGraph* graph)
628 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100629 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000630 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100631 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100632 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100633 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
634 back_edges_.reserve(kDefaultNumberOfBackEdges);
635 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100636
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000637 bool IsIrreducible() const { return irreducible_; }
638
639 void Dump(std::ostream& os);
640
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100641 HBasicBlock* GetHeader() const {
642 return header_;
643 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000645 void SetHeader(HBasicBlock* block) {
646 header_ = block;
647 }
648
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100649 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
650 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
651 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
652
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000653 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100654 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000655 }
656
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100658 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659 }
660
David Brazdil46e2a392015-03-16 17:31:52 +0000661 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100662 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100663 }
664
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000665 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100666 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000667 }
668
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100669 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100670
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100671 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100672 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100673 }
674
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100675 // Returns the lifetime position of the back edge that has the
676 // greatest lifetime position.
677 size_t GetLifetimeEnd() const;
678
679 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100680 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100681 }
682
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000683 // Finds blocks that are part of this loop.
684 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100685
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100686 // Returns whether this loop information contains `block`.
687 // Note that this loop information *must* be populated before entering this function.
688 bool Contains(const HBasicBlock& block) const;
689
690 // Returns whether this loop information is an inner loop of `other`.
691 // Note that `other` *must* be populated before entering this function.
692 bool IsIn(const HLoopInformation& other) const;
693
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800694 // Returns true if instruction is not defined within this loop.
695 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700696
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 const ArenaBitVector& GetBlocks() const { return blocks_; }
698
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000699 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000700 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000701
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000702 void ClearAllBlocks() {
703 blocks_.ClearAllBits();
704 }
705
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000706 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100707 // Internal recursive implementation of `Populate`.
708 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000709 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100710
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000711 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100712 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000713 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100714 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100715 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000716
717 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
718};
719
David Brazdilec16f792015-08-19 15:04:01 +0100720// Stores try/catch information for basic blocks.
721// Note that HGraph is constructed so that catch blocks cannot simultaneously
722// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100723class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100724 public:
725 // Try block information constructor.
726 explicit TryCatchInformation(const HTryBoundary& try_entry)
727 : try_entry_(&try_entry),
728 catch_dex_file_(nullptr),
729 catch_type_index_(DexFile::kDexNoIndex16) {
730 DCHECK(try_entry_ != nullptr);
731 }
732
733 // Catch block information constructor.
734 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
735 : try_entry_(nullptr),
736 catch_dex_file_(&dex_file),
737 catch_type_index_(catch_type_index) {}
738
739 bool IsTryBlock() const { return try_entry_ != nullptr; }
740
741 const HTryBoundary& GetTryEntry() const {
742 DCHECK(IsTryBlock());
743 return *try_entry_;
744 }
745
746 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
747
748 bool IsCatchAllTypeIndex() const {
749 DCHECK(IsCatchBlock());
750 return catch_type_index_ == DexFile::kDexNoIndex16;
751 }
752
753 uint16_t GetCatchTypeIndex() const {
754 DCHECK(IsCatchBlock());
755 return catch_type_index_;
756 }
757
758 const DexFile& GetCatchDexFile() const {
759 DCHECK(IsCatchBlock());
760 return *catch_dex_file_;
761 }
762
763 private:
764 // One of possibly several TryBoundary instructions entering the block's try.
765 // Only set for try blocks.
766 const HTryBoundary* try_entry_;
767
768 // Exception type information. Only set for catch blocks.
769 const DexFile* catch_dex_file_;
770 const uint16_t catch_type_index_;
771};
772
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100773static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100774static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100775
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000776// A block in a method. Contains the list of instructions represented
777// as a double linked list. Each block knows its predecessors and
778// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100779
Vladimir Markof9f64412015-09-02 14:05:49 +0100780class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000781 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600782 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000783 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000784 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
785 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000786 loop_information_(nullptr),
787 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000788 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100789 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100790 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100791 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000792 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000793 try_catch_information_(nullptr) {
794 predecessors_.reserve(kDefaultNumberOfPredecessors);
795 successors_.reserve(kDefaultNumberOfSuccessors);
796 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
797 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000798
Vladimir Marko60584552015-09-03 13:35:12 +0000799 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100800 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000801 }
802
Vladimir Marko60584552015-09-03 13:35:12 +0000803 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805 }
806
David Brazdild26a4112015-11-10 11:07:31 +0000807 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
808 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
809
Vladimir Marko60584552015-09-03 13:35:12 +0000810 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
811 return ContainsElement(successors_, block, start_from);
812 }
813
814 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100815 return dominated_blocks_;
816 }
817
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100818 bool IsEntryBlock() const {
819 return graph_->GetEntryBlock() == this;
820 }
821
822 bool IsExitBlock() const {
823 return graph_->GetExitBlock() == this;
824 }
825
David Brazdil46e2a392015-03-16 17:31:52 +0000826 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000827 bool IsSingleTryBoundary() const;
828
829 // Returns true if this block emits nothing but a jump.
830 bool IsSingleJump() const {
831 HLoopInformation* loop_info = GetLoopInformation();
832 return (IsSingleGoto() || IsSingleTryBoundary())
833 // Back edges generate a suspend check.
834 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
835 }
David Brazdil46e2a392015-03-16 17:31:52 +0000836
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000837 void AddBackEdge(HBasicBlock* back_edge) {
838 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000839 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000840 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100841 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000842 loop_information_->AddBackEdge(back_edge);
843 }
844
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000845 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000847
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100848 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000849 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600850 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000852 HBasicBlock* GetDominator() const { return dominator_; }
853 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000854 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
855
856 void RemoveDominatedBlock(HBasicBlock* block) {
857 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100858 }
Vladimir Marko60584552015-09-03 13:35:12 +0000859
860 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
861 ReplaceElement(dominated_blocks_, existing, new_block);
862 }
863
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100864 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000865
866 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100867 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000868 }
869
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100870 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
871 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100872 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100873 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100874 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
875 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000876
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000877 HInstruction* GetFirstInstructionDisregardMoves() const;
878
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000879 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000880 successors_.push_back(block);
881 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882 }
883
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100884 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
885 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100886 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000887 new_block->predecessors_.push_back(this);
888 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000889 }
890
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000891 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
892 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000893 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000894 new_block->successors_.push_back(this);
895 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000896 }
897
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100898 // Insert `this` between `predecessor` and `successor. This method
899 // preserves the indicies, and will update the first edge found between
900 // `predecessor` and `successor`.
901 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
902 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100903 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000904 successor->predecessors_[predecessor_index] = this;
905 predecessor->successors_[successor_index] = this;
906 successors_.push_back(successor);
907 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100908 }
909
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100910 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000911 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100912 }
913
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000914 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000915 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000916 }
917
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100918 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000919 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100920 }
921
922 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000923 predecessors_.push_back(block);
924 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100925 }
926
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100927 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 DCHECK_EQ(predecessors_.size(), 2u);
929 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100930 }
931
David Brazdil769c9e52015-04-27 13:54:09 +0100932 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000933 DCHECK_EQ(successors_.size(), 2u);
934 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100935 }
936
David Brazdilfc6a86a2015-06-26 10:33:45 +0000937 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000938 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939 }
940
David Brazdilfc6a86a2015-06-26 10:33:45 +0000941 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000942 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100943 }
944
David Brazdilfc6a86a2015-06-26 10:33:45 +0000945 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000946 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100947 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000948 }
949
950 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000951 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100952 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000953 }
954
955 // Returns whether the first occurrence of `predecessor` in the list of
956 // predecessors is at index `idx`.
957 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100958 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000959 return GetPredecessorIndexOf(predecessor) == idx;
960 }
961
David Brazdild7558da2015-09-22 13:04:14 +0100962 // Create a new block between this block and its predecessors. The new block
963 // is added to the graph, all predecessor edges are relinked to it and an edge
964 // is created to `this`. Returns the new empty block. Reverse post order or
965 // loop and try/catch information are not updated.
966 HBasicBlock* CreateImmediateDominator();
967
David Brazdilfc6a86a2015-06-26 10:33:45 +0000968 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100969 // created, latter block. Note that this method will add the block to the
970 // graph, create a Goto at the end of the former block and will create an edge
971 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100972 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000973 HBasicBlock* SplitBefore(HInstruction* cursor);
974
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000975 // Split the block into two blocks just after `cursor`. Returns the newly
976 // created block. Note that this method just updates raw block information,
977 // like predecessors, successors, dominators, and instruction list. It does not
978 // update the graph, reverse post order, loop information, nor make sure the
979 // blocks are consistent (for example ending with a control flow instruction).
980 HBasicBlock* SplitAfter(HInstruction* cursor);
981
David Brazdil9bc43612015-11-05 21:25:24 +0000982 // Split catch block into two blocks after the original move-exception bytecode
983 // instruction, or at the beginning if not present. Returns the newly created,
984 // latter block, or nullptr if such block could not be created (must be dead
985 // in that case). Note that this method just updates raw block information,
986 // like predecessors, successors, dominators, and instruction list. It does not
987 // update the graph, reverse post order, loop information, nor make sure the
988 // blocks are consistent (for example ending with a control flow instruction).
989 HBasicBlock* SplitCatchBlockAfterMoveException();
990
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000991 // Merge `other` at the end of `this`. Successors and dominated blocks of
992 // `other` are changed to be successors and dominated blocks of `this`. Note
993 // that this method does not update the graph, reverse post order, loop
994 // information, nor make sure the blocks are consistent (for example ending
995 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100996 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997
998 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
999 // of `this` are moved to `other`.
1000 // Note that this method does not update the graph, reverse post order, loop
1001 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001002 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001003 void ReplaceWith(HBasicBlock* other);
1004
David Brazdil2d7352b2015-04-20 14:52:42 +01001005 // Merge `other` at the end of `this`. This method updates loops, reverse post
1006 // order, links to predecessors, successors, dominators and deletes the block
1007 // from the graph. The two blocks must be successive, i.e. `this` the only
1008 // predecessor of `other` and vice versa.
1009 void MergeWith(HBasicBlock* other);
1010
1011 // Disconnects `this` from all its predecessors, successors and dominator,
1012 // removes it from all loops it is included in and eventually from the graph.
1013 // The block must not dominate any other block. Predecessors and successors
1014 // are safely updated.
1015 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001016
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001018 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001019 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001020 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001021 // Replace instruction `initial` with `replacement` within this block.
1022 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1023 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001024 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001025 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001026 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001027 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1028 // instruction list. With 'ensure_safety' set to true, it verifies that the
1029 // instruction is not in use and removes it from the use lists of its inputs.
1030 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1031 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001032 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001033
1034 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001035 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001036 }
1037
Roland Levillain6b879dd2014-09-22 17:13:44 +01001038 bool IsLoopPreHeaderFirstPredecessor() const {
1039 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001040 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001041 }
1042
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001043 bool IsFirstPredecessorBackEdge() const {
1044 DCHECK(IsLoopHeader());
1045 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1046 }
1047
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001048 HLoopInformation* GetLoopInformation() const {
1049 return loop_information_;
1050 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001051
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001052 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001053 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001054 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001055 void SetInLoop(HLoopInformation* info) {
1056 if (IsLoopHeader()) {
1057 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001058 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001059 loop_information_ = info;
1060 } else if (loop_information_->Contains(*info->GetHeader())) {
1061 // Block is currently part of an outer loop. Make it part of this inner loop.
1062 // Note that a non loop header having a loop information means this loop information
1063 // has already been populated
1064 loop_information_ = info;
1065 } else {
1066 // Block is part of an inner loop. Do not update the loop information.
1067 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1068 // at this point, because this method is being called while populating `info`.
1069 }
1070 }
1071
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 // Raw update of the loop information.
1073 void SetLoopInformation(HLoopInformation* info) {
1074 loop_information_ = info;
1075 }
1076
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001077 bool IsInLoop() const { return loop_information_ != nullptr; }
1078
David Brazdilec16f792015-08-19 15:04:01 +01001079 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1080
1081 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1082 try_catch_information_ = try_catch_information;
1083 }
1084
1085 bool IsTryBlock() const {
1086 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1087 }
1088
1089 bool IsCatchBlock() const {
1090 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1091 }
David Brazdilffee3d32015-07-06 11:48:53 +01001092
1093 // Returns the try entry that this block's successors should have. They will
1094 // be in the same try, unless the block ends in a try boundary. In that case,
1095 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001096 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001097
David Brazdild7558da2015-09-22 13:04:14 +01001098 bool HasThrowingInstructions() const;
1099
David Brazdila4b8c212015-05-07 09:59:30 +01001100 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001101 bool Dominates(HBasicBlock* block) const;
1102
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001103 size_t GetLifetimeStart() const { return lifetime_start_; }
1104 size_t GetLifetimeEnd() const { return lifetime_end_; }
1105
1106 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1107 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1108
David Brazdil8d5b8b22015-03-24 10:51:52 +00001109 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001110 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001111 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001112 bool HasSinglePhi() const;
1113
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001114 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001115 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001116 ArenaVector<HBasicBlock*> predecessors_;
1117 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001118 HInstructionList instructions_;
1119 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001120 HLoopInformation* loop_information_;
1121 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001122 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001123 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001124 // The dex program counter of the first instruction of this block.
1125 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001126 size_t lifetime_start_;
1127 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001128 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001129
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001130 friend class HGraph;
1131 friend class HInstruction;
1132
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001133 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1134};
1135
David Brazdilb2bd1c52015-03-25 11:17:37 +00001136// Iterates over the LoopInformation of all loops which contain 'block'
1137// from the innermost to the outermost.
1138class HLoopInformationOutwardIterator : public ValueObject {
1139 public:
1140 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1141 : current_(block.GetLoopInformation()) {}
1142
1143 bool Done() const { return current_ == nullptr; }
1144
1145 void Advance() {
1146 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001147 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001148 }
1149
1150 HLoopInformation* Current() const {
1151 DCHECK(!Done());
1152 return current_;
1153 }
1154
1155 private:
1156 HLoopInformation* current_;
1157
1158 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1159};
1160
Alexandre Ramesef20f712015-06-09 10:29:30 +01001161#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001162 M(Above, Condition) \
1163 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001164 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001165 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001166 M(ArrayGet, Instruction) \
1167 M(ArrayLength, Instruction) \
1168 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001169 M(Below, Condition) \
1170 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001171 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001172 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001173 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001174 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001175 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001176 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001177 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001178 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001179 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001180 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001181 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001182 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001183 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001184 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001185 M(Exit, Instruction) \
1186 M(FloatConstant, Constant) \
1187 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001188 M(GreaterThan, Condition) \
1189 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001190 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001191 M(InstanceFieldGet, Instruction) \
1192 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001193 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001194 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001195 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001196 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001197 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001198 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(LessThan, Condition) \
1200 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001201 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001202 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001203 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001204 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001205 M(Local, Instruction) \
1206 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001207 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001208 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001209 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001210 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001211 M(Neg, UnaryOperation) \
1212 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001213 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001214 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001215 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001216 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001217 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001218 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001219 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001220 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001221 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001222 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001223 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001224 M(Return, Instruction) \
1225 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001226 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001227 M(Shl, BinaryOperation) \
1228 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001229 M(StaticFieldGet, Instruction) \
1230 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001231 M(UnresolvedInstanceFieldGet, Instruction) \
1232 M(UnresolvedInstanceFieldSet, Instruction) \
1233 M(UnresolvedStaticFieldGet, Instruction) \
1234 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001235 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(StoreLocal, Instruction) \
1237 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001239 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001240 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001241 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001242 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001243 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001244
Vladimir Markob4536b72015-11-24 13:45:23 +00001245#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001246#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001247#else
1248#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1249 M(ArmDexCacheArraysBase, Instruction)
1250#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001251
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001252#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001253#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001254#else
1255#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001256 M(Arm64DataProcWithShifterOp, Instruction) \
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00001257 M(Arm64IntermediateAddress, Instruction) \
1258 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001259#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001260
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001261#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1262
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001263#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1264
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001265#ifndef ART_ENABLE_CODEGEN_x86
1266#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1267#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001268#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1269 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001270 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001271 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001272 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001273#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001274
1275#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1276
1277#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1278 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1279 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1280 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001281 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001282 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001283 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1284 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1285
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001286#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1287 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001288 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001289 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001290 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001291 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001292
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001293#define FOR_EACH_INSTRUCTION(M) \
1294 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1295 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1296
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001297#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001298FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1299#undef FORWARD_DECLARATION
1300
Roland Levillainccc07a92014-09-16 14:48:16 +01001301#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001302 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001303 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001304 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001305 return other->Is##type(); \
1306 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001307 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001308
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001309#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1310 bool Is##type() const { return As##type() != nullptr; } \
1311 const H##type* As##type() const { return this; } \
1312 H##type* As##type() { return this; }
1313
David Brazdiled596192015-01-23 10:39:45 +00001314template <typename T> class HUseList;
1315
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001316template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001317class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001318 public:
David Brazdiled596192015-01-23 10:39:45 +00001319 HUseListNode* GetPrevious() const { return prev_; }
1320 HUseListNode* GetNext() const { return next_; }
1321 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001322 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001323 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001324
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001325 private:
David Brazdiled596192015-01-23 10:39:45 +00001326 HUseListNode(T user, size_t index)
1327 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1328
1329 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001330 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001331 HUseListNode<T>* prev_;
1332 HUseListNode<T>* next_;
1333
1334 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001335
1336 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1337};
1338
David Brazdiled596192015-01-23 10:39:45 +00001339template <typename T>
1340class HUseList : public ValueObject {
1341 public:
1342 HUseList() : first_(nullptr) {}
1343
1344 void Clear() {
1345 first_ = nullptr;
1346 }
1347
1348 // Adds a new entry at the beginning of the use list and returns
1349 // the newly created node.
1350 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001351 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001352 if (IsEmpty()) {
1353 first_ = new_node;
1354 } else {
1355 first_->prev_ = new_node;
1356 new_node->next_ = first_;
1357 first_ = new_node;
1358 }
1359 return new_node;
1360 }
1361
1362 HUseListNode<T>* GetFirst() const {
1363 return first_;
1364 }
1365
1366 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001367 DCHECK(node != nullptr);
1368 DCHECK(Contains(node));
1369
David Brazdiled596192015-01-23 10:39:45 +00001370 if (node->prev_ != nullptr) {
1371 node->prev_->next_ = node->next_;
1372 }
1373 if (node->next_ != nullptr) {
1374 node->next_->prev_ = node->prev_;
1375 }
1376 if (node == first_) {
1377 first_ = node->next_;
1378 }
1379 }
1380
David Brazdil1abb4192015-02-17 18:33:36 +00001381 bool Contains(const HUseListNode<T>* node) const {
1382 if (node == nullptr) {
1383 return false;
1384 }
1385 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1386 if (current == node) {
1387 return true;
1388 }
1389 }
1390 return false;
1391 }
1392
David Brazdiled596192015-01-23 10:39:45 +00001393 bool IsEmpty() const {
1394 return first_ == nullptr;
1395 }
1396
1397 bool HasOnlyOneUse() const {
1398 return first_ != nullptr && first_->next_ == nullptr;
1399 }
1400
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001401 size_t SizeSlow() const {
1402 size_t count = 0;
1403 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1404 ++count;
1405 }
1406 return count;
1407 }
1408
David Brazdiled596192015-01-23 10:39:45 +00001409 private:
1410 HUseListNode<T>* first_;
1411};
1412
1413template<typename T>
1414class HUseIterator : public ValueObject {
1415 public:
1416 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1417
1418 bool Done() const { return current_ == nullptr; }
1419
1420 void Advance() {
1421 DCHECK(!Done());
1422 current_ = current_->GetNext();
1423 }
1424
1425 HUseListNode<T>* Current() const {
1426 DCHECK(!Done());
1427 return current_;
1428 }
1429
1430 private:
1431 HUseListNode<T>* current_;
1432
1433 friend class HValue;
1434};
1435
David Brazdil1abb4192015-02-17 18:33:36 +00001436// This class is used by HEnvironment and HInstruction classes to record the
1437// instructions they use and pointers to the corresponding HUseListNodes kept
1438// by the used instructions.
1439template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001440class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001441 public:
1442 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1443 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1444
1445 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1446 : instruction_(old_record.instruction_), use_node_(use_node) {
1447 DCHECK(instruction_ != nullptr);
1448 DCHECK(use_node_ != nullptr);
1449 DCHECK(old_record.use_node_ == nullptr);
1450 }
1451
1452 HInstruction* GetInstruction() const { return instruction_; }
1453 HUseListNode<T>* GetUseNode() const { return use_node_; }
1454
1455 private:
1456 // Instruction used by the user.
1457 HInstruction* instruction_;
1458
1459 // Corresponding entry in the use list kept by 'instruction_'.
1460 HUseListNode<T>* use_node_;
1461};
1462
Aart Bik854a02b2015-07-14 16:07:00 -07001463/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001464 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001465 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001466 * For write/read dependences on fields/arrays, the dependence analysis uses
1467 * type disambiguation (e.g. a float field write cannot modify the value of an
1468 * integer field read) and the access type (e.g. a reference array write cannot
1469 * modify the value of a reference field read [although it may modify the
1470 * reference fetch prior to reading the field, which is represented by its own
1471 * write/read dependence]). The analysis makes conservative points-to
1472 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1473 * the same, and any reference read depends on any reference read without
1474 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001475 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001476 * The internal representation uses 38-bit and is described in the table below.
1477 * The first line indicates the side effect, and for field/array accesses the
1478 * second line indicates the type of the access (in the order of the
1479 * Primitive::Type enum).
1480 * The two numbered lines below indicate the bit position in the bitfield (read
1481 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001482 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001483 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1484 * +-------------+---------+---------+--------------+---------+---------+
1485 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1486 * | 3 |333333322|222222221| 1 |111111110|000000000|
1487 * | 7 |654321098|765432109| 8 |765432109|876543210|
1488 *
1489 * Note that, to ease the implementation, 'changes' bits are least significant
1490 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001491 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001492class SideEffects : public ValueObject {
1493 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001494 SideEffects() : flags_(0) {}
1495
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001496 static SideEffects None() {
1497 return SideEffects(0);
1498 }
1499
1500 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001501 return SideEffects(kAllChangeBits | kAllDependOnBits);
1502 }
1503
1504 static SideEffects AllChanges() {
1505 return SideEffects(kAllChangeBits);
1506 }
1507
1508 static SideEffects AllDependencies() {
1509 return SideEffects(kAllDependOnBits);
1510 }
1511
1512 static SideEffects AllExceptGCDependency() {
1513 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1514 }
1515
1516 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001517 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001518 }
1519
Aart Bik34c3ba92015-07-20 14:08:59 -07001520 static SideEffects AllWrites() {
1521 return SideEffects(kAllWrites);
1522 }
1523
1524 static SideEffects AllReads() {
1525 return SideEffects(kAllReads);
1526 }
1527
1528 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1529 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001530 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001531 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001532 }
1533
Aart Bik854a02b2015-07-14 16:07:00 -07001534 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1535 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536 }
1537
Aart Bik34c3ba92015-07-20 14:08:59 -07001538 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1539 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001540 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001541 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001542 }
1543
1544 static SideEffects ArrayReadOfType(Primitive::Type type) {
1545 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1546 }
1547
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001548 static SideEffects CanTriggerGC() {
1549 return SideEffects(1ULL << kCanTriggerGCBit);
1550 }
1551
1552 static SideEffects DependsOnGC() {
1553 return SideEffects(1ULL << kDependsOnGCBit);
1554 }
1555
Aart Bik854a02b2015-07-14 16:07:00 -07001556 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001557 SideEffects Union(SideEffects other) const {
1558 return SideEffects(flags_ | other.flags_);
1559 }
1560
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001561 SideEffects Exclusion(SideEffects other) const {
1562 return SideEffects(flags_ & ~other.flags_);
1563 }
1564
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001565 void Add(SideEffects other) {
1566 flags_ |= other.flags_;
1567 }
1568
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001569 bool Includes(SideEffects other) const {
1570 return (other.flags_ & flags_) == other.flags_;
1571 }
1572
1573 bool HasSideEffects() const {
1574 return (flags_ & kAllChangeBits);
1575 }
1576
1577 bool HasDependencies() const {
1578 return (flags_ & kAllDependOnBits);
1579 }
1580
1581 // Returns true if there are no side effects or dependencies.
1582 bool DoesNothing() const {
1583 return flags_ == 0;
1584 }
1585
Aart Bik854a02b2015-07-14 16:07:00 -07001586 // Returns true if something is written.
1587 bool DoesAnyWrite() const {
1588 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001589 }
1590
Aart Bik854a02b2015-07-14 16:07:00 -07001591 // Returns true if something is read.
1592 bool DoesAnyRead() const {
1593 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001594 }
1595
Aart Bik854a02b2015-07-14 16:07:00 -07001596 // Returns true if potentially everything is written and read
1597 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001598 bool DoesAllReadWrite() const {
1599 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1600 }
1601
Aart Bik854a02b2015-07-14 16:07:00 -07001602 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001604 }
1605
Roland Levillain0d5a2812015-11-13 10:07:31 +00001606 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001607 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001608 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1609 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001610 }
1611
1612 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001614 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001615 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 for (int s = kLastBit; s >= 0; s--) {
1617 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1618 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1619 // This is a bit for the GC side effect.
1620 if (current_bit_is_set) {
1621 flags += "GC";
1622 }
Aart Bik854a02b2015-07-14 16:07:00 -07001623 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001624 } else {
1625 // This is a bit for the array/field analysis.
1626 // The underscore character stands for the 'can trigger GC' bit.
1627 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1628 if (current_bit_is_set) {
1629 flags += kDebug[s];
1630 }
1631 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1632 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1633 flags += "|";
1634 }
1635 }
Aart Bik854a02b2015-07-14 16:07:00 -07001636 }
1637 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001638 }
1639
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001640 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 private:
1643 static constexpr int kFieldArrayAnalysisBits = 9;
1644
1645 static constexpr int kFieldWriteOffset = 0;
1646 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1647 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1648 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1649
1650 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1651
1652 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1653 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1654 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1655 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1656
1657 static constexpr int kLastBit = kDependsOnGCBit;
1658 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1659
1660 // Aliases.
1661
1662 static_assert(kChangeBits == kDependOnBits,
1663 "the 'change' bits should match the 'depend on' bits.");
1664
1665 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1666 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1667 static constexpr uint64_t kAllWrites =
1668 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1669 static constexpr uint64_t kAllReads =
1670 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001671
Aart Bik854a02b2015-07-14 16:07:00 -07001672 // Work around the fact that HIR aliases I/F and J/D.
1673 // TODO: remove this interceptor once HIR types are clean
1674 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1675 switch (type) {
1676 case Primitive::kPrimInt:
1677 case Primitive::kPrimFloat:
1678 return TypeFlag(Primitive::kPrimInt, offset) |
1679 TypeFlag(Primitive::kPrimFloat, offset);
1680 case Primitive::kPrimLong:
1681 case Primitive::kPrimDouble:
1682 return TypeFlag(Primitive::kPrimLong, offset) |
1683 TypeFlag(Primitive::kPrimDouble, offset);
1684 default:
1685 return TypeFlag(type, offset);
1686 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001687 }
1688
Aart Bik854a02b2015-07-14 16:07:00 -07001689 // Translates type to bit flag.
1690 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1691 CHECK_NE(type, Primitive::kPrimVoid);
1692 const uint64_t one = 1;
1693 const int shift = type; // 0-based consecutive enum
1694 DCHECK_LE(kFieldWriteOffset, shift);
1695 DCHECK_LT(shift, kArrayWriteOffset);
1696 return one << (type + offset);
1697 }
1698
1699 // Private constructor on direct flags value.
1700 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1701
1702 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001703};
1704
David Brazdiled596192015-01-23 10:39:45 +00001705// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001706class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001707 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001708 HEnvironment(ArenaAllocator* arena,
1709 size_t number_of_vregs,
1710 const DexFile& dex_file,
1711 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001712 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001713 InvokeType invoke_type,
1714 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001715 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1716 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001717 parent_(nullptr),
1718 dex_file_(dex_file),
1719 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001720 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001721 invoke_type_(invoke_type),
1722 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001723 }
1724
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001725 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001726 : HEnvironment(arena,
1727 to_copy.Size(),
1728 to_copy.GetDexFile(),
1729 to_copy.GetMethodIdx(),
1730 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001731 to_copy.GetInvokeType(),
1732 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001733
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001734 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001735 if (parent_ != nullptr) {
1736 parent_->SetAndCopyParentChain(allocator, parent);
1737 } else {
1738 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1739 parent_->CopyFrom(parent);
1740 if (parent->GetParent() != nullptr) {
1741 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1742 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001743 }
David Brazdiled596192015-01-23 10:39:45 +00001744 }
1745
Vladimir Marko71bf8092015-09-15 15:33:14 +01001746 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001747 void CopyFrom(HEnvironment* environment);
1748
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001749 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1750 // input to the loop phi instead. This is for inserting instructions that
1751 // require an environment (like HDeoptimization) in the loop pre-header.
1752 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001753
1754 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001755 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001756 }
1757
1758 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001759 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001760 }
1761
David Brazdil1abb4192015-02-17 18:33:36 +00001762 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001763
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001764 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001765
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001766 HEnvironment* GetParent() const { return parent_; }
1767
1768 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001769 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001770 }
1771
1772 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001773 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001774 }
1775
1776 uint32_t GetDexPc() const {
1777 return dex_pc_;
1778 }
1779
1780 uint32_t GetMethodIdx() const {
1781 return method_idx_;
1782 }
1783
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001784 InvokeType GetInvokeType() const {
1785 return invoke_type_;
1786 }
1787
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001788 const DexFile& GetDexFile() const {
1789 return dex_file_;
1790 }
1791
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001792 HInstruction* GetHolder() const {
1793 return holder_;
1794 }
1795
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001796
1797 bool IsFromInlinedInvoke() const {
1798 return GetParent() != nullptr;
1799 }
1800
David Brazdiled596192015-01-23 10:39:45 +00001801 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001802 // Record instructions' use entries of this environment for constant-time removal.
1803 // It should only be called by HInstruction when a new environment use is added.
1804 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1805 DCHECK(env_use->GetUser() == this);
1806 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001807 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001808 }
David Brazdiled596192015-01-23 10:39:45 +00001809
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001810 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1811 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001812 HEnvironment* parent_;
1813 const DexFile& dex_file_;
1814 const uint32_t method_idx_;
1815 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001816 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001817
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001818 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001819 HInstruction* const holder_;
1820
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001821 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001822
1823 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1824};
1825
Vladimir Markof9f64412015-09-02 14:05:49 +01001826class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001827 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001828 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001829 : previous_(nullptr),
1830 next_(nullptr),
1831 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001832 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001833 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001834 ssa_index_(-1),
David Brazdilb3e773e2016-01-26 11:28:37 +00001835 emitted_at_use_site_(false),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001836 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001837 locations_(nullptr),
1838 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001839 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001840 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001841 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001842
Dave Allison20dfc792014-06-16 20:44:29 -07001843 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001844
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001845#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001846 enum InstructionKind {
1847 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1848 };
1849#undef DECLARE_KIND
1850
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001851 HInstruction* GetNext() const { return next_; }
1852 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001853
Calin Juravle77520bc2015-01-12 18:45:46 +00001854 HInstruction* GetNextDisregardingMoves() const;
1855 HInstruction* GetPreviousDisregardingMoves() const;
1856
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001857 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001858 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001859 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001860 bool IsInBlock() const { return block_ != nullptr; }
1861 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001862 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1863 bool IsIrreducibleLoopHeaderPhi() const {
1864 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1865 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001866
Roland Levillain6b879dd2014-09-22 17:13:44 +01001867 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001868 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001869
1870 virtual void Accept(HGraphVisitor* visitor) = 0;
1871 virtual const char* DebugName() const = 0;
1872
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001874 void SetRawInputAt(size_t index, HInstruction* input) {
1875 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1876 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001879
1880 uint32_t GetDexPc() const { return dex_pc_; }
1881
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001882 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001883
Roland Levillaine161a2a2014-10-03 12:45:18 +01001884 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001885 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001886
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001887 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001888 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889
Calin Juravle10e244f2015-01-26 18:54:32 +00001890 // Does not apply for all instructions, but having this at top level greatly
1891 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001892 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001893 virtual bool CanBeNull() const {
1894 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1895 return true;
1896 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001897
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001898 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001899 return false;
1900 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001901
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001902 virtual bool IsActualObject() const {
1903 return GetType() == Primitive::kPrimNot;
1904 }
1905
Calin Juravle2e768302015-07-28 14:41:11 +00001906 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001907
Calin Juravle61d544b2015-02-23 16:46:57 +00001908 ReferenceTypeInfo GetReferenceTypeInfo() const {
1909 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1910 return reference_type_info_;
1911 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001912
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001914 DCHECK(user != nullptr);
1915 HUseListNode<HInstruction*>* use =
1916 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1917 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001918 }
1919
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001920 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001921 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001922 HUseListNode<HEnvironment*>* env_use =
1923 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1924 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001925 }
1926
David Brazdil1abb4192015-02-17 18:33:36 +00001927 void RemoveAsUserOfInput(size_t input) {
1928 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1929 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1930 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001931
David Brazdil1abb4192015-02-17 18:33:36 +00001932 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1933 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001934
David Brazdiled596192015-01-23 10:39:45 +00001935 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1936 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001937 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001938 bool HasOnlyOneNonEnvironmentUse() const {
1939 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1940 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001941
Roland Levillain6c82d402014-10-13 16:10:27 +01001942 // Does this instruction strictly dominate `other_instruction`?
1943 // Returns false if this instruction and `other_instruction` are the same.
1944 // Aborts if this instruction and `other_instruction` are both phis.
1945 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001946
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001947 int GetId() const { return id_; }
1948 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001949
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001950 int GetSsaIndex() const { return ssa_index_; }
1951 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1952 bool HasSsaIndex() const { return ssa_index_ != -1; }
1953
1954 bool HasEnvironment() const { return environment_ != nullptr; }
1955 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001956 // Set the `environment_` field. Raw because this method does not
1957 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001958 void SetRawEnvironment(HEnvironment* environment) {
1959 DCHECK(environment_ == nullptr);
1960 DCHECK_EQ(environment->GetHolder(), this);
1961 environment_ = environment;
1962 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001963
1964 // Set the environment of this instruction, copying it from `environment`. While
1965 // copying, the uses lists are being updated.
1966 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001967 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001968 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001969 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001970 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001971 if (environment->GetParent() != nullptr) {
1972 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1973 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001974 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001975
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001976 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1977 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001978 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001979 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001980 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001981 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001982 if (environment->GetParent() != nullptr) {
1983 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1984 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001985 }
1986
Nicolas Geoffray39468442014-09-02 15:17:15 +01001987 // Returns the number of entries in the environment. Typically, that is the
1988 // number of dex registers in a method. It could be more in case of inlining.
1989 size_t EnvironmentSize() const;
1990
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001991 LocationSummary* GetLocations() const { return locations_; }
1992 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001993
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001994 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001995 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001996
Alexandre Rames188d4312015-04-09 18:30:21 +01001997 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1998 // uses of this instruction by `other` are *not* updated.
1999 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2000 ReplaceWith(other);
2001 other->ReplaceInput(this, use_index);
2002 }
2003
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002004 // Move `this` instruction before `cursor`.
2005 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002006
Vladimir Markofb337ea2015-11-25 15:25:10 +00002007 // Move `this` before its first user and out of any loops. If there is no
2008 // out-of-loop user that dominates all other users, move the instruction
2009 // to the end of the out-of-loop common dominator of the user's blocks.
2010 //
2011 // This can be used only on non-throwing instructions with no side effects that
2012 // have at least one use but no environment uses.
2013 void MoveBeforeFirstUserAndOutOfLoops();
2014
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002015#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 bool Is##type() const; \
2017 const H##type* As##type() const; \
2018 H##type* As##type();
2019
2020 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2021#undef INSTRUCTION_TYPE_CHECK
2022
2023#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002024 bool Is##type() const { return (As##type() != nullptr); } \
2025 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002026 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002028#undef INSTRUCTION_TYPE_CHECK
2029
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002030 // Returns whether the instruction can be moved within the graph.
2031 virtual bool CanBeMoved() const { return false; }
2032
2033 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002034 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002035 return false;
2036 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002037
2038 // Returns whether any data encoded in the two instructions is equal.
2039 // This method does not look at the inputs. Both instructions must be
2040 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002041 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002042 return false;
2043 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002044
2045 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002046 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002047 // 2) Their inputs are identical.
2048 bool Equals(HInstruction* other) const;
2049
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002050 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2051 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2052 // the virtual function because the __attribute__((__pure__)) doesn't really
2053 // apply the strong requirement for virtual functions, preventing optimizations.
2054 InstructionKind GetKind() const PURE;
2055 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002056
2057 virtual size_t ComputeHashCode() const {
2058 size_t result = GetKind();
2059 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2060 result = (result * 31) + InputAt(i)->GetId();
2061 }
2062 return result;
2063 }
2064
2065 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002066 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002067
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002068 size_t GetLifetimePosition() const { return lifetime_position_; }
2069 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2070 LiveInterval* GetLiveInterval() const { return live_interval_; }
2071 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2072 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2073
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002074 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2075
2076 // Returns whether the code generation of the instruction will require to have access
2077 // to the current method. Such instructions are:
2078 // (1): Instructions that require an environment, as calling the runtime requires
2079 // to walk the stack and have the current method stored at a specific stack address.
2080 // (2): Object literals like classes and strings, that are loaded from the dex cache
2081 // fields of the current method.
2082 bool NeedsCurrentMethod() const {
2083 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2084 }
2085
Vladimir Markodc151b22015-10-15 18:02:30 +01002086 // Returns whether the code generation of the instruction will require to have access
2087 // to the dex cache of the current method's declaring class via the current method.
2088 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002089
Mark Mendellc4701932015-04-10 13:18:51 -04002090 // Does this instruction have any use in an environment before
2091 // control flow hits 'other'?
2092 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2093
2094 // Remove all references to environment uses of this instruction.
2095 // The caller must ensure that this is safe to do.
2096 void RemoveEnvironmentUsers();
2097
David Brazdilb3e773e2016-01-26 11:28:37 +00002098 bool IsEmittedAtUseSite() const { return emitted_at_use_site_; }
2099 void MarkEmittedAtUseSite() { emitted_at_use_site_ = true; }
2100
David Brazdil1abb4192015-02-17 18:33:36 +00002101 protected:
2102 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2103 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002104 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002105
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002106 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002107 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2108
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002109 HInstruction* previous_;
2110 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002111 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002112 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002114 // An instruction gets an id when it is added to the graph.
2115 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002116 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002117 int id_;
2118
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002119 // When doing liveness analysis, instructions that have uses get an SSA index.
2120 int ssa_index_;
2121
David Brazdilb3e773e2016-01-26 11:28:37 +00002122 // If set, the machine code for this instruction is assumed to be generated by
2123 // its users. Used by liveness analysis to compute use positions accordingly.
2124 bool emitted_at_use_site_;
2125
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002126 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002127 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002128
2129 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002130 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002131
Nicolas Geoffray39468442014-09-02 15:17:15 +01002132 // The environment associated with this instruction. Not null if the instruction
2133 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002134 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002135
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002136 // Set by the code generator.
2137 LocationSummary* locations_;
2138
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002139 // Set by the liveness analysis.
2140 LiveInterval* live_interval_;
2141
2142 // Set by the liveness analysis, this is the position in a linear
2143 // order of blocks where this instruction's live interval start.
2144 size_t lifetime_position_;
2145
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002146 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002147
Calin Juravleacf735c2015-02-12 15:25:22 +00002148 // TODO: for primitive types this should be marked as invalid.
2149 ReferenceTypeInfo reference_type_info_;
2150
David Brazdil1abb4192015-02-17 18:33:36 +00002151 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002153 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002154 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002155 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156
2157 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2158};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002159std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002160
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002161class HInputIterator : public ValueObject {
2162 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002163 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002164
2165 bool Done() const { return index_ == instruction_->InputCount(); }
2166 HInstruction* Current() const { return instruction_->InputAt(index_); }
2167 void Advance() { index_++; }
2168
2169 private:
2170 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002171 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002172
2173 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2174};
2175
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176class HInstructionIterator : public ValueObject {
2177 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002178 explicit HInstructionIterator(const HInstructionList& instructions)
2179 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002180 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181 }
2182
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002183 bool Done() const { return instruction_ == nullptr; }
2184 HInstruction* Current() const { return instruction_; }
2185 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002186 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002187 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002188 }
2189
2190 private:
2191 HInstruction* instruction_;
2192 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002193
2194 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195};
2196
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002197class HBackwardInstructionIterator : public ValueObject {
2198 public:
2199 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2200 : instruction_(instructions.last_instruction_) {
2201 next_ = Done() ? nullptr : instruction_->GetPrevious();
2202 }
2203
2204 bool Done() const { return instruction_ == nullptr; }
2205 HInstruction* Current() const { return instruction_; }
2206 void Advance() {
2207 instruction_ = next_;
2208 next_ = Done() ? nullptr : instruction_->GetPrevious();
2209 }
2210
2211 private:
2212 HInstruction* instruction_;
2213 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002214
2215 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002216};
2217
Vladimir Markof9f64412015-09-02 14:05:49 +01002218template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219class HTemplateInstruction: public HInstruction {
2220 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002221 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002222 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002223 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002224
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002225 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002227 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002228 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2229 DCHECK_LT(i, N);
2230 return inputs_[i];
2231 }
David Brazdil1abb4192015-02-17 18:33:36 +00002232
2233 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002234 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002235 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002236 }
2237
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002238 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002239 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002240
2241 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242};
2243
Vladimir Markof9f64412015-09-02 14:05:49 +01002244// HTemplateInstruction specialization for N=0.
2245template<>
2246class HTemplateInstruction<0>: public HInstruction {
2247 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002248 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002249 : HInstruction(side_effects, dex_pc) {}
2250
Vladimir Markof9f64412015-09-02 14:05:49 +01002251 virtual ~HTemplateInstruction() {}
2252
2253 size_t InputCount() const OVERRIDE { return 0; }
2254
2255 protected:
2256 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2257 LOG(FATAL) << "Unreachable";
2258 UNREACHABLE();
2259 }
2260
2261 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2262 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2263 LOG(FATAL) << "Unreachable";
2264 UNREACHABLE();
2265 }
2266
2267 private:
2268 friend class SsaBuilder;
2269};
2270
Dave Allison20dfc792014-06-16 20:44:29 -07002271template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002272class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002273 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002274 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002275 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002276 virtual ~HExpression() {}
2277
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002278 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002279
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002280 protected:
2281 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002282};
2283
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002284// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2285// instruction that branches to the exit block.
2286class HReturnVoid : public HTemplateInstruction<0> {
2287 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002288 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2289 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002290
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002291 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002292
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002293 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002294
2295 private:
2296 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2297};
2298
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002299// Represents dex's RETURN opcodes. A HReturn is a control flow
2300// instruction that branches to the exit block.
2301class HReturn : public HTemplateInstruction<1> {
2302 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2304 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002305 SetRawInputAt(0, value);
2306 }
2307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002308 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002309
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002310 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002311
2312 private:
2313 DISALLOW_COPY_AND_ASSIGN(HReturn);
2314};
2315
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002316// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002317// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318// exit block.
2319class HExit : public HTemplateInstruction<0> {
2320 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002321 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002323 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002324
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002325 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002326
2327 private:
2328 DISALLOW_COPY_AND_ASSIGN(HExit);
2329};
2330
2331// Jumps from one block to another.
2332class HGoto : public HTemplateInstruction<0> {
2333 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002334 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002335
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002336 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002337
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002338 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002339 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002340 }
2341
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002342 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002343
2344 private:
2345 DISALLOW_COPY_AND_ASSIGN(HGoto);
2346};
2347
Roland Levillain9867bc72015-08-05 10:21:34 +01002348class HConstant : public HExpression<0> {
2349 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002350 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2351 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002352
2353 bool CanBeMoved() const OVERRIDE { return true; }
2354
2355 virtual bool IsMinusOne() const { return false; }
2356 virtual bool IsZero() const { return false; }
2357 virtual bool IsOne() const { return false; }
2358
David Brazdil77a48ae2015-09-15 12:34:04 +00002359 virtual uint64_t GetValueAsUint64() const = 0;
2360
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002361 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002362
2363 private:
2364 DISALLOW_COPY_AND_ASSIGN(HConstant);
2365};
2366
2367class HNullConstant : public HConstant {
2368 public:
2369 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2370 return true;
2371 }
2372
David Brazdil77a48ae2015-09-15 12:34:04 +00002373 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2374
Roland Levillain9867bc72015-08-05 10:21:34 +01002375 size_t ComputeHashCode() const OVERRIDE { return 0; }
2376
2377 DECLARE_INSTRUCTION(NullConstant);
2378
2379 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002380 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002381
2382 friend class HGraph;
2383 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2384};
2385
2386// Constants of the type int. Those can be from Dex instructions, or
2387// synthesized (for example with the if-eqz instruction).
2388class HIntConstant : public HConstant {
2389 public:
2390 int32_t GetValue() const { return value_; }
2391
David Brazdil9f389d42015-10-01 14:32:56 +01002392 uint64_t GetValueAsUint64() const OVERRIDE {
2393 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2394 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002395
Roland Levillain9867bc72015-08-05 10:21:34 +01002396 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2397 DCHECK(other->IsIntConstant());
2398 return other->AsIntConstant()->value_ == value_;
2399 }
2400
2401 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2402
2403 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2404 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2405 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2406
2407 DECLARE_INSTRUCTION(IntConstant);
2408
2409 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002410 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2411 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2412 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2413 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002414
2415 const int32_t value_;
2416
2417 friend class HGraph;
2418 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2419 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2420 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2421};
2422
2423class HLongConstant : public HConstant {
2424 public:
2425 int64_t GetValue() const { return value_; }
2426
David Brazdil77a48ae2015-09-15 12:34:04 +00002427 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2428
Roland Levillain9867bc72015-08-05 10:21:34 +01002429 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2430 DCHECK(other->IsLongConstant());
2431 return other->AsLongConstant()->value_ == value_;
2432 }
2433
2434 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2435
2436 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2437 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2438 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2439
2440 DECLARE_INSTRUCTION(LongConstant);
2441
2442 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002443 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2444 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002445
2446 const int64_t value_;
2447
2448 friend class HGraph;
2449 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2450};
Dave Allison20dfc792014-06-16 20:44:29 -07002451
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002452// Conditional branch. A block ending with an HIf instruction must have
2453// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002454class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002455 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002456 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2457 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002458 SetRawInputAt(0, input);
2459 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002460
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002461 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002462
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002463 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002464 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002465 }
2466
2467 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002468 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002469 }
2470
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002471 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002472
2473 private:
2474 DISALLOW_COPY_AND_ASSIGN(HIf);
2475};
2476
David Brazdilfc6a86a2015-06-26 10:33:45 +00002477
2478// Abstract instruction which marks the beginning and/or end of a try block and
2479// links it to the respective exception handlers. Behaves the same as a Goto in
2480// non-exceptional control flow.
2481// Normal-flow successor is stored at index zero, exception handlers under
2482// higher indices in no particular order.
2483class HTryBoundary : public HTemplateInstruction<0> {
2484 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002485 enum BoundaryKind {
2486 kEntry,
2487 kExit,
2488 };
2489
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002490 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2491 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002492
2493 bool IsControlFlow() const OVERRIDE { return true; }
2494
2495 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002496 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002497
David Brazdild26a4112015-11-10 11:07:31 +00002498 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2499 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2500 }
2501
David Brazdilfc6a86a2015-06-26 10:33:45 +00002502 // Returns whether `handler` is among its exception handlers (non-zero index
2503 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002504 bool HasExceptionHandler(const HBasicBlock& handler) const {
2505 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002506 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002507 }
2508
2509 // If not present already, adds `handler` to its block's list of exception
2510 // handlers.
2511 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002512 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002513 GetBlock()->AddSuccessor(handler);
2514 }
2515 }
2516
David Brazdil56e1acc2015-06-30 15:41:36 +01002517 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002518
David Brazdilffee3d32015-07-06 11:48:53 +01002519 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2520
David Brazdilfc6a86a2015-06-26 10:33:45 +00002521 DECLARE_INSTRUCTION(TryBoundary);
2522
2523 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002524 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002525
2526 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2527};
2528
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002529// Deoptimize to interpreter, upon checking a condition.
2530class HDeoptimize : public HTemplateInstruction<1> {
2531 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002532 // We set CanTriggerGC to prevent any intermediate address to be live
2533 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002534 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002535 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002536 SetRawInputAt(0, cond);
2537 }
2538
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002539 bool CanBeMoved() const OVERRIDE { return true; }
2540 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2541 return true;
2542 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002543 bool NeedsEnvironment() const OVERRIDE { return true; }
2544 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002545
2546 DECLARE_INSTRUCTION(Deoptimize);
2547
2548 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002549 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2550};
2551
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002552// Represents the ArtMethod that was passed as a first argument to
2553// the method. It is used by instructions that depend on it, like
2554// instructions that work with the dex cache.
2555class HCurrentMethod : public HExpression<0> {
2556 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002557 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2558 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002559
2560 DECLARE_INSTRUCTION(CurrentMethod);
2561
2562 private:
2563 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2564};
2565
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002566// Fetches an ArtMethod from the virtual table or the interface method table
2567// of a class.
2568class HClassTableGet : public HExpression<1> {
2569 public:
2570 enum TableKind {
2571 kVTable,
2572 kIMTable,
2573 };
2574 HClassTableGet(HInstruction* cls,
2575 Primitive::Type type,
2576 TableKind kind,
2577 size_t index,
2578 uint32_t dex_pc)
2579 : HExpression(type, SideEffects::None(), dex_pc),
2580 index_(index),
2581 table_kind_(kind) {
2582 SetRawInputAt(0, cls);
2583 }
2584
2585 bool CanBeMoved() const OVERRIDE { return true; }
2586 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2587 return other->AsClassTableGet()->GetIndex() == index_ &&
2588 other->AsClassTableGet()->GetTableKind() == table_kind_;
2589 }
2590
2591 TableKind GetTableKind() const { return table_kind_; }
2592 size_t GetIndex() const { return index_; }
2593
2594 DECLARE_INSTRUCTION(ClassTableGet);
2595
2596 private:
2597 // The index of the ArtMethod in the table.
2598 const size_t index_;
2599 const TableKind table_kind_;
2600
2601 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2602};
2603
Mark Mendellfe57faa2015-09-18 09:26:15 -04002604// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2605// have one successor for each entry in the switch table, and the final successor
2606// will be the block containing the next Dex opcode.
2607class HPackedSwitch : public HTemplateInstruction<1> {
2608 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002609 HPackedSwitch(int32_t start_value,
2610 uint32_t num_entries,
2611 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002612 uint32_t dex_pc = kNoDexPc)
2613 : HTemplateInstruction(SideEffects::None(), dex_pc),
2614 start_value_(start_value),
2615 num_entries_(num_entries) {
2616 SetRawInputAt(0, input);
2617 }
2618
2619 bool IsControlFlow() const OVERRIDE { return true; }
2620
2621 int32_t GetStartValue() const { return start_value_; }
2622
Vladimir Marko211c2112015-09-24 16:52:33 +01002623 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002624
2625 HBasicBlock* GetDefaultBlock() const {
2626 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002627 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002628 }
2629 DECLARE_INSTRUCTION(PackedSwitch);
2630
2631 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002632 const int32_t start_value_;
2633 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002634
2635 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2636};
2637
Roland Levillain88cb1752014-10-20 16:36:47 +01002638class HUnaryOperation : public HExpression<1> {
2639 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002640 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2641 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002642 SetRawInputAt(0, input);
2643 }
2644
2645 HInstruction* GetInput() const { return InputAt(0); }
2646 Primitive::Type GetResultType() const { return GetType(); }
2647
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002648 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002649 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002650 return true;
2651 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002652
Roland Levillain9240d6a2014-10-20 16:47:04 +01002653 // Try to statically evaluate `operation` and return a HConstant
2654 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002655 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002656 HConstant* TryStaticEvaluation() const;
2657
2658 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002659 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2660 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002661
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002662 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002663
2664 private:
2665 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2666};
2667
Dave Allison20dfc792014-06-16 20:44:29 -07002668class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002669 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002670 HBinaryOperation(Primitive::Type result_type,
2671 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002672 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002673 SideEffects side_effects = SideEffects::None(),
2674 uint32_t dex_pc = kNoDexPc)
2675 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002676 SetRawInputAt(0, left);
2677 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002678 }
2679
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002680 HInstruction* GetLeft() const { return InputAt(0); }
2681 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002682 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002683
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002684 virtual bool IsCommutative() const { return false; }
2685
2686 // Put constant on the right.
2687 // Returns whether order is changed.
2688 bool OrderInputsWithConstantOnTheRight() {
2689 HInstruction* left = InputAt(0);
2690 HInstruction* right = InputAt(1);
2691 if (left->IsConstant() && !right->IsConstant()) {
2692 ReplaceInput(right, 0);
2693 ReplaceInput(left, 1);
2694 return true;
2695 }
2696 return false;
2697 }
2698
2699 // Order inputs by instruction id, but favor constant on the right side.
2700 // This helps GVN for commutative ops.
2701 void OrderInputs() {
2702 DCHECK(IsCommutative());
2703 HInstruction* left = InputAt(0);
2704 HInstruction* right = InputAt(1);
2705 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2706 return;
2707 }
2708 if (OrderInputsWithConstantOnTheRight()) {
2709 return;
2710 }
2711 // Order according to instruction id.
2712 if (left->GetId() > right->GetId()) {
2713 ReplaceInput(right, 0);
2714 ReplaceInput(left, 1);
2715 }
2716 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002717
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002718 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002719 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002720 return true;
2721 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002722
Roland Levillain9240d6a2014-10-20 16:47:04 +01002723 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002724 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002725 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002726 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002727
2728 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002729 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2730 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2731 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2732 HLongConstant* y ATTRIBUTE_UNUSED) const {
2733 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2734 return nullptr;
2735 }
2736 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2737 HIntConstant* y ATTRIBUTE_UNUSED) const {
2738 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2739 return nullptr;
2740 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002741 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2742 HNullConstant* y ATTRIBUTE_UNUSED) const {
2743 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2744 return nullptr;
2745 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002746
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002747 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002748 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002749 HConstant* GetConstantRight() const;
2750
2751 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002752 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002753 HInstruction* GetLeastConstantLeft() const;
2754
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002755 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002756
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002757 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002758 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2759};
2760
Mark Mendellc4701932015-04-10 13:18:51 -04002761// The comparison bias applies for floating point operations and indicates how NaN
2762// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002763enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002764 kNoBias, // bias is not applicable (i.e. for long operation)
2765 kGtBias, // return 1 for NaN comparisons
2766 kLtBias, // return -1 for NaN comparisons
2767};
2768
Dave Allison20dfc792014-06-16 20:44:29 -07002769class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002770 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002771 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2772 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002773 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002774
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002775 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002776 // `instruction`, and disregard moves in between.
2777 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002778
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002779 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002780
2781 virtual IfCondition GetCondition() const = 0;
2782
Mark Mendellc4701932015-04-10 13:18:51 -04002783 virtual IfCondition GetOppositeCondition() const = 0;
2784
Roland Levillain4fa13f62015-07-06 18:11:54 +01002785 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002786
2787 void SetBias(ComparisonBias bias) { bias_ = bias; }
2788
2789 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2790 return bias_ == other->AsCondition()->bias_;
2791 }
2792
Roland Levillain4fa13f62015-07-06 18:11:54 +01002793 bool IsFPConditionTrueIfNaN() const {
2794 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2795 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002796 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002797 }
2798
2799 bool IsFPConditionFalseIfNaN() const {
2800 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2801 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002802 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002803 }
2804
Dave Allison20dfc792014-06-16 20:44:29 -07002805 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002806 // Needed if we merge a HCompare into a HCondition.
2807 ComparisonBias bias_;
2808
Dave Allison20dfc792014-06-16 20:44:29 -07002809 DISALLOW_COPY_AND_ASSIGN(HCondition);
2810};
2811
2812// Instruction to check if two inputs are equal to each other.
2813class HEqual : public HCondition {
2814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002815 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2816 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002817
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002818 bool IsCommutative() const OVERRIDE { return true; }
2819
Roland Levillain9867bc72015-08-05 10:21:34 +01002820 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002821 return GetBlock()->GetGraph()->GetIntConstant(
2822 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002823 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002824 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002825 return GetBlock()->GetGraph()->GetIntConstant(
2826 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002827 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002828 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2829 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002830 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002831 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002832
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002833 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002834
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002835 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002836 return kCondEQ;
2837 }
2838
Mark Mendellc4701932015-04-10 13:18:51 -04002839 IfCondition GetOppositeCondition() const OVERRIDE {
2840 return kCondNE;
2841 }
2842
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002843 private:
Aart Bike9f37602015-10-09 11:15:55 -07002844 template <typename T> bool Compute(T x, T y) const { return x == y; }
2845
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002846 DISALLOW_COPY_AND_ASSIGN(HEqual);
2847};
2848
Dave Allison20dfc792014-06-16 20:44:29 -07002849class HNotEqual : public HCondition {
2850 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002851 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2852 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002853
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002854 bool IsCommutative() const OVERRIDE { return true; }
2855
Roland Levillain9867bc72015-08-05 10:21:34 +01002856 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002857 return GetBlock()->GetGraph()->GetIntConstant(
2858 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002859 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002860 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002861 return GetBlock()->GetGraph()->GetIntConstant(
2862 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002863 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002864 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2865 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002866 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002867 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002868
Dave Allison20dfc792014-06-16 20:44:29 -07002869 DECLARE_INSTRUCTION(NotEqual);
2870
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002871 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002872 return kCondNE;
2873 }
2874
Mark Mendellc4701932015-04-10 13:18:51 -04002875 IfCondition GetOppositeCondition() const OVERRIDE {
2876 return kCondEQ;
2877 }
2878
Dave Allison20dfc792014-06-16 20:44:29 -07002879 private:
Aart Bike9f37602015-10-09 11:15:55 -07002880 template <typename T> bool Compute(T x, T y) const { return x != y; }
2881
Dave Allison20dfc792014-06-16 20:44:29 -07002882 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2883};
2884
2885class HLessThan : public HCondition {
2886 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002887 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2888 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002889
Roland Levillain9867bc72015-08-05 10:21:34 +01002890 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002891 return GetBlock()->GetGraph()->GetIntConstant(
2892 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002893 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002894 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002895 return GetBlock()->GetGraph()->GetIntConstant(
2896 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002897 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002898
Dave Allison20dfc792014-06-16 20:44:29 -07002899 DECLARE_INSTRUCTION(LessThan);
2900
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002901 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002902 return kCondLT;
2903 }
2904
Mark Mendellc4701932015-04-10 13:18:51 -04002905 IfCondition GetOppositeCondition() const OVERRIDE {
2906 return kCondGE;
2907 }
2908
Dave Allison20dfc792014-06-16 20:44:29 -07002909 private:
Aart Bike9f37602015-10-09 11:15:55 -07002910 template <typename T> bool Compute(T x, T y) const { return x < y; }
2911
Dave Allison20dfc792014-06-16 20:44:29 -07002912 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2913};
2914
2915class HLessThanOrEqual : public HCondition {
2916 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002917 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2918 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002919
Roland Levillain9867bc72015-08-05 10:21:34 +01002920 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002921 return GetBlock()->GetGraph()->GetIntConstant(
2922 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002923 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002924 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002925 return GetBlock()->GetGraph()->GetIntConstant(
2926 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002927 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002928
Dave Allison20dfc792014-06-16 20:44:29 -07002929 DECLARE_INSTRUCTION(LessThanOrEqual);
2930
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002931 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002932 return kCondLE;
2933 }
2934
Mark Mendellc4701932015-04-10 13:18:51 -04002935 IfCondition GetOppositeCondition() const OVERRIDE {
2936 return kCondGT;
2937 }
2938
Dave Allison20dfc792014-06-16 20:44:29 -07002939 private:
Aart Bike9f37602015-10-09 11:15:55 -07002940 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2941
Dave Allison20dfc792014-06-16 20:44:29 -07002942 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2943};
2944
2945class HGreaterThan : public HCondition {
2946 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002947 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2948 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002949
Roland Levillain9867bc72015-08-05 10:21:34 +01002950 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002951 return GetBlock()->GetGraph()->GetIntConstant(
2952 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002953 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002954 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002955 return GetBlock()->GetGraph()->GetIntConstant(
2956 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002957 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002958
Dave Allison20dfc792014-06-16 20:44:29 -07002959 DECLARE_INSTRUCTION(GreaterThan);
2960
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002961 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002962 return kCondGT;
2963 }
2964
Mark Mendellc4701932015-04-10 13:18:51 -04002965 IfCondition GetOppositeCondition() const OVERRIDE {
2966 return kCondLE;
2967 }
2968
Dave Allison20dfc792014-06-16 20:44:29 -07002969 private:
Aart Bike9f37602015-10-09 11:15:55 -07002970 template <typename T> bool Compute(T x, T y) const { return x > y; }
2971
Dave Allison20dfc792014-06-16 20:44:29 -07002972 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2973};
2974
2975class HGreaterThanOrEqual : public HCondition {
2976 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002977 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2978 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002979
Roland Levillain9867bc72015-08-05 10:21:34 +01002980 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002981 return GetBlock()->GetGraph()->GetIntConstant(
2982 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002983 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002984 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002985 return GetBlock()->GetGraph()->GetIntConstant(
2986 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002987 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002988
Dave Allison20dfc792014-06-16 20:44:29 -07002989 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2990
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002991 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002992 return kCondGE;
2993 }
2994
Mark Mendellc4701932015-04-10 13:18:51 -04002995 IfCondition GetOppositeCondition() const OVERRIDE {
2996 return kCondLT;
2997 }
2998
Dave Allison20dfc792014-06-16 20:44:29 -07002999 private:
Aart Bike9f37602015-10-09 11:15:55 -07003000 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3001
Dave Allison20dfc792014-06-16 20:44:29 -07003002 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3003};
3004
Aart Bike9f37602015-10-09 11:15:55 -07003005class HBelow : public HCondition {
3006 public:
3007 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3008 : HCondition(first, second, dex_pc) {}
3009
3010 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3011 return GetBlock()->GetGraph()->GetIntConstant(
3012 Compute(static_cast<uint32_t>(x->GetValue()),
3013 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3014 }
3015 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3016 return GetBlock()->GetGraph()->GetIntConstant(
3017 Compute(static_cast<uint64_t>(x->GetValue()),
3018 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3019 }
3020
3021 DECLARE_INSTRUCTION(Below);
3022
3023 IfCondition GetCondition() const OVERRIDE {
3024 return kCondB;
3025 }
3026
3027 IfCondition GetOppositeCondition() const OVERRIDE {
3028 return kCondAE;
3029 }
3030
3031 private:
3032 template <typename T> bool Compute(T x, T y) const { return x < y; }
3033
3034 DISALLOW_COPY_AND_ASSIGN(HBelow);
3035};
3036
3037class HBelowOrEqual : public HCondition {
3038 public:
3039 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3040 : HCondition(first, second, dex_pc) {}
3041
3042 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3043 return GetBlock()->GetGraph()->GetIntConstant(
3044 Compute(static_cast<uint32_t>(x->GetValue()),
3045 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3046 }
3047 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3048 return GetBlock()->GetGraph()->GetIntConstant(
3049 Compute(static_cast<uint64_t>(x->GetValue()),
3050 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3051 }
3052
3053 DECLARE_INSTRUCTION(BelowOrEqual);
3054
3055 IfCondition GetCondition() const OVERRIDE {
3056 return kCondBE;
3057 }
3058
3059 IfCondition GetOppositeCondition() const OVERRIDE {
3060 return kCondA;
3061 }
3062
3063 private:
3064 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3065
3066 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3067};
3068
3069class HAbove : public HCondition {
3070 public:
3071 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3072 : HCondition(first, second, dex_pc) {}
3073
3074 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3075 return GetBlock()->GetGraph()->GetIntConstant(
3076 Compute(static_cast<uint32_t>(x->GetValue()),
3077 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3078 }
3079 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3080 return GetBlock()->GetGraph()->GetIntConstant(
3081 Compute(static_cast<uint64_t>(x->GetValue()),
3082 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3083 }
3084
3085 DECLARE_INSTRUCTION(Above);
3086
3087 IfCondition GetCondition() const OVERRIDE {
3088 return kCondA;
3089 }
3090
3091 IfCondition GetOppositeCondition() const OVERRIDE {
3092 return kCondBE;
3093 }
3094
3095 private:
3096 template <typename T> bool Compute(T x, T y) const { return x > y; }
3097
3098 DISALLOW_COPY_AND_ASSIGN(HAbove);
3099};
3100
3101class HAboveOrEqual : public HCondition {
3102 public:
3103 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3104 : HCondition(first, second, dex_pc) {}
3105
3106 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3107 return GetBlock()->GetGraph()->GetIntConstant(
3108 Compute(static_cast<uint32_t>(x->GetValue()),
3109 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3110 }
3111 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3112 return GetBlock()->GetGraph()->GetIntConstant(
3113 Compute(static_cast<uint64_t>(x->GetValue()),
3114 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3115 }
3116
3117 DECLARE_INSTRUCTION(AboveOrEqual);
3118
3119 IfCondition GetCondition() const OVERRIDE {
3120 return kCondAE;
3121 }
3122
3123 IfCondition GetOppositeCondition() const OVERRIDE {
3124 return kCondB;
3125 }
3126
3127 private:
3128 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3129
3130 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3131};
Dave Allison20dfc792014-06-16 20:44:29 -07003132
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003133// Instruction to check how two inputs compare to each other.
3134// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3135class HCompare : public HBinaryOperation {
3136 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003137 HCompare(Primitive::Type type,
3138 HInstruction* first,
3139 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003140 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003141 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003142 : HBinaryOperation(Primitive::kPrimInt,
3143 first,
3144 second,
3145 SideEffectsForArchRuntimeCalls(type),
3146 dex_pc),
3147 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003148 DCHECK_EQ(type, first->GetType());
3149 DCHECK_EQ(type, second->GetType());
3150 }
3151
Roland Levillain9867bc72015-08-05 10:21:34 +01003152 template <typename T>
3153 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003154
Roland Levillain9867bc72015-08-05 10:21:34 +01003155 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003156 return GetBlock()->GetGraph()->GetIntConstant(
3157 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003158 }
3159 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003160 return GetBlock()->GetGraph()->GetIntConstant(
3161 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003162 }
3163
Calin Juravleddb7df22014-11-25 20:56:51 +00003164 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3165 return bias_ == other->AsCompare()->bias_;
3166 }
3167
Mark Mendellc4701932015-04-10 13:18:51 -04003168 ComparisonBias GetBias() const { return bias_; }
3169
Roland Levillain4fa13f62015-07-06 18:11:54 +01003170 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003171
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003172
3173 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3174 // MIPS64 uses a runtime call for FP comparisons.
3175 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3176 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003177
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003178 DECLARE_INSTRUCTION(Compare);
3179
3180 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003181 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003182
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003183 DISALLOW_COPY_AND_ASSIGN(HCompare);
3184};
3185
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003186// A local in the graph. Corresponds to a Dex register.
3187class HLocal : public HTemplateInstruction<0> {
3188 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003189 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003190 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003191
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003192 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003193
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003194 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003195
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003196 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003197 // The Dex register number.
3198 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003199
3200 DISALLOW_COPY_AND_ASSIGN(HLocal);
3201};
3202
3203// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003204class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003206 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3207 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003208 SetRawInputAt(0, local);
3209 }
3210
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003211 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3212
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003213 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003214
3215 private:
3216 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3217};
3218
3219// Store a value in a given local. This instruction has two inputs: the value
3220// and the local.
3221class HStoreLocal : public HTemplateInstruction<2> {
3222 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003223 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3224 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003225 SetRawInputAt(0, local);
3226 SetRawInputAt(1, value);
3227 }
3228
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003229 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003231 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003232
3233 private:
3234 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3235};
3236
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003237class HFloatConstant : public HConstant {
3238 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003239 float GetValue() const { return value_; }
3240
David Brazdil77a48ae2015-09-15 12:34:04 +00003241 uint64_t GetValueAsUint64() const OVERRIDE {
3242 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3243 }
3244
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003245 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003246 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003247 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003248 }
3249
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003250 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003251
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003252 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003253 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003254 }
3255 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003256 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003257 }
3258 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003259 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3260 }
3261 bool IsNaN() const {
3262 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003263 }
3264
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003265 DECLARE_INSTRUCTION(FloatConstant);
3266
3267 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003268 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3269 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3270 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3271 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003272
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003273 const float value_;
3274
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003275 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003276 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003277 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003278 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3279};
3280
3281class HDoubleConstant : public HConstant {
3282 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003283 double GetValue() const { return value_; }
3284
David Brazdil77a48ae2015-09-15 12:34:04 +00003285 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3286
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003287 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003288 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003289 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003290 }
3291
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003292 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003293
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003294 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003295 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003296 }
3297 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003298 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003299 }
3300 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003301 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3302 }
3303 bool IsNaN() const {
3304 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003305 }
3306
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003307 DECLARE_INSTRUCTION(DoubleConstant);
3308
3309 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003310 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3311 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3312 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3313 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003314
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003315 const double value_;
3316
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003317 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003318 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003319 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003320 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3321};
3322
David Brazdil6de19382016-01-08 17:37:10 +00003323class HNewInstance : public HExpression<2> {
3324 public:
3325 HNewInstance(HInstruction* cls,
3326 HCurrentMethod* current_method,
3327 uint32_t dex_pc,
3328 uint16_t type_index,
3329 const DexFile& dex_file,
3330 bool can_throw,
3331 bool finalizable,
3332 QuickEntrypointEnum entrypoint)
3333 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3334 type_index_(type_index),
3335 dex_file_(dex_file),
3336 can_throw_(can_throw),
3337 finalizable_(finalizable),
3338 entrypoint_(entrypoint) {
3339 SetRawInputAt(0, cls);
3340 SetRawInputAt(1, current_method);
3341 }
3342
3343 uint16_t GetTypeIndex() const { return type_index_; }
3344 const DexFile& GetDexFile() const { return dex_file_; }
3345
3346 // Calls runtime so needs an environment.
3347 bool NeedsEnvironment() const OVERRIDE { return true; }
3348
3349 // It may throw when called on type that's not instantiable/accessible.
3350 // It can throw OOME.
3351 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3352 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3353
3354 bool IsFinalizable() const { return finalizable_; }
3355
3356 bool CanBeNull() const OVERRIDE { return false; }
3357
3358 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3359
3360 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3361 entrypoint_ = entrypoint;
3362 }
3363
3364 bool IsStringAlloc() const;
3365
3366 DECLARE_INSTRUCTION(NewInstance);
3367
3368 private:
3369 const uint16_t type_index_;
3370 const DexFile& dex_file_;
3371 const bool can_throw_;
3372 const bool finalizable_;
3373 QuickEntrypointEnum entrypoint_;
3374
3375 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3376};
3377
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003378enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003379#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3380 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003381#include "intrinsics_list.h"
3382 kNone,
3383 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3384#undef INTRINSICS_LIST
3385#undef OPTIMIZING_INTRINSICS
3386};
3387std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3388
Agi Csaki05f20562015-08-19 14:58:14 -07003389enum IntrinsicNeedsEnvironmentOrCache {
3390 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3391 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003392};
3393
Aart Bik5d75afe2015-12-14 11:57:01 -08003394enum IntrinsicSideEffects {
3395 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3396 kReadSideEffects, // Intrinsic may read heap memory.
3397 kWriteSideEffects, // Intrinsic may write heap memory.
3398 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3399};
3400
3401enum IntrinsicExceptions {
3402 kNoThrow, // Intrinsic does not throw any exceptions.
3403 kCanThrow // Intrinsic may throw exceptions.
3404};
3405
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003406class HInvoke : public HInstruction {
3407 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003408 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003409
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003410 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003411
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003412 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003413 SetRawInputAt(index, argument);
3414 }
3415
Roland Levillain3e3d7332015-04-28 11:00:54 +01003416 // Return the number of arguments. This number can be lower than
3417 // the number of inputs returned by InputCount(), as some invoke
3418 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3419 // inputs at the end of their list of inputs.
3420 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3421
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003422 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003423
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003424 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003425 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003426
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003427 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3428
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003429 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003430 return intrinsic_;
3431 }
3432
Aart Bik5d75afe2015-12-14 11:57:01 -08003433 void SetIntrinsic(Intrinsics intrinsic,
3434 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3435 IntrinsicSideEffects side_effects,
3436 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003437
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003438 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003439 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003440 }
3441
Aart Bik5d75afe2015-12-14 11:57:01 -08003442 bool CanThrow() const OVERRIDE { return can_throw_; }
3443
3444 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3445
3446 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3447 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3448 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003449
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003450 uint32_t* GetIntrinsicOptimizations() {
3451 return &intrinsic_optimizations_;
3452 }
3453
3454 const uint32_t* GetIntrinsicOptimizations() const {
3455 return &intrinsic_optimizations_;
3456 }
3457
3458 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3459
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003460 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003461
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003462 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003463 HInvoke(ArenaAllocator* arena,
3464 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003465 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003466 Primitive::Type return_type,
3467 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003468 uint32_t dex_method_index,
3469 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003470 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003471 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003472 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003473 inputs_(number_of_arguments + number_of_other_inputs,
3474 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003475 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003476 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003477 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003478 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003479 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003480 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003481 }
3482
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003483 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003484 return inputs_[index];
3485 }
3486
David Brazdil1abb4192015-02-17 18:33:36 +00003487 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003488 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003489 }
3490
Aart Bik5d75afe2015-12-14 11:57:01 -08003491 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3492
Roland Levillain3e3d7332015-04-28 11:00:54 +01003493 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003494 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003495 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003496 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003497 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003498 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003499 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003500
3501 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3502 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003503
3504 private:
3505 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3506};
3507
Calin Juravle175dc732015-08-25 15:42:32 +01003508class HInvokeUnresolved : public HInvoke {
3509 public:
3510 HInvokeUnresolved(ArenaAllocator* arena,
3511 uint32_t number_of_arguments,
3512 Primitive::Type return_type,
3513 uint32_t dex_pc,
3514 uint32_t dex_method_index,
3515 InvokeType invoke_type)
3516 : HInvoke(arena,
3517 number_of_arguments,
3518 0u /* number_of_other_inputs */,
3519 return_type,
3520 dex_pc,
3521 dex_method_index,
3522 invoke_type) {
3523 }
3524
3525 DECLARE_INSTRUCTION(InvokeUnresolved);
3526
3527 private:
3528 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3529};
3530
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003531class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003532 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003533 // Requirements of this method call regarding the class
3534 // initialization (clinit) check of its declaring class.
3535 enum class ClinitCheckRequirement {
3536 kNone, // Class already initialized.
3537 kExplicit, // Static call having explicit clinit check as last input.
3538 kImplicit, // Static call implicitly requiring a clinit check.
3539 };
3540
Vladimir Marko58155012015-08-19 12:49:41 +00003541 // Determines how to load the target ArtMethod*.
3542 enum class MethodLoadKind {
3543 // Use a String init ArtMethod* loaded from Thread entrypoints.
3544 kStringInit,
3545
3546 // Use the method's own ArtMethod* loaded by the register allocator.
3547 kRecursive,
3548
3549 // Use ArtMethod* at a known address, embed the direct address in the code.
3550 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3551 kDirectAddress,
3552
3553 // Use ArtMethod* at an address that will be known at link time, embed the direct
3554 // address in the code. If the image is relocatable, emit .patch_oat entry.
3555 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3556 // the image relocatable or not.
3557 kDirectAddressWithFixup,
3558
3559 // Load from resoved methods array in the dex cache using a PC-relative load.
3560 // Used when we need to use the dex cache, for example for invoke-static that
3561 // may cause class initialization (the entry may point to a resolution method),
3562 // and we know that we can access the dex cache arrays using a PC-relative load.
3563 kDexCachePcRelative,
3564
3565 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3566 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3567 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3568 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3569 kDexCacheViaMethod,
3570 };
3571
3572 // Determines the location of the code pointer.
3573 enum class CodePtrLocation {
3574 // Recursive call, use local PC-relative call instruction.
3575 kCallSelf,
3576
3577 // Use PC-relative call instruction patched at link time.
3578 // Used for calls within an oat file, boot->boot or app->app.
3579 kCallPCRelative,
3580
3581 // Call to a known target address, embed the direct address in code.
3582 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3583 kCallDirect,
3584
3585 // Call to a target address that will be known at link time, embed the direct
3586 // address in code. If the image is relocatable, emit .patch_oat entry.
3587 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3588 // the image relocatable or not.
3589 kCallDirectWithFixup,
3590
3591 // Use code pointer from the ArtMethod*.
3592 // Used when we don't know the target code. This is also the last-resort-kind used when
3593 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3594 kCallArtMethod,
3595 };
3596
3597 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003598 MethodLoadKind method_load_kind;
3599 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003600 // The method load data holds
3601 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3602 // Note that there are multiple string init methods, each having its own offset.
3603 // - the method address for kDirectAddress
3604 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003605 uint64_t method_load_data;
3606 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003607 };
3608
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003609 HInvokeStaticOrDirect(ArenaAllocator* arena,
3610 uint32_t number_of_arguments,
3611 Primitive::Type return_type,
3612 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003613 uint32_t method_index,
3614 MethodReference target_method,
3615 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003616 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003617 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003618 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003619 : HInvoke(arena,
3620 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003621 // There is potentially one extra argument for the HCurrentMethod node, and
3622 // potentially one other if the clinit check is explicit, and potentially
3623 // one other if the method is a string factory.
3624 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3625 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3626 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003627 return_type,
3628 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003629 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003630 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003631 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003632 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003633 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003634 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003635
Vladimir Markodc151b22015-10-15 18:02:30 +01003636 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003637 bool had_current_method_input = HasCurrentMethodInput();
3638 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3639
3640 // Using the current method is the default and once we find a better
3641 // method load kind, we should not go back to using the current method.
3642 DCHECK(had_current_method_input || !needs_current_method_input);
3643
3644 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003645 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3646 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003647 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003648 dispatch_info_ = dispatch_info;
3649 }
3650
Vladimir Markoc53c0792015-11-19 15:48:33 +00003651 void AddSpecialInput(HInstruction* input) {
3652 // We allow only one special input.
3653 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3654 DCHECK(InputCount() == GetSpecialInputIndex() ||
3655 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3656 InsertInputAt(GetSpecialInputIndex(), input);
3657 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003658
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003659 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003660 // We access the method via the dex cache so we can't do an implicit null check.
3661 // TODO: for intrinsics we can generate implicit null checks.
3662 return false;
3663 }
3664
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003665 bool CanBeNull() const OVERRIDE {
3666 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3667 }
3668
Vladimir Markoc53c0792015-11-19 15:48:33 +00003669 // Get the index of the special input, if any.
3670 //
David Brazdil6de19382016-01-08 17:37:10 +00003671 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3672 // method pointer; otherwise there may be one platform-specific special input,
3673 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003674 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003675 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003676
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003677 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3678 void SetOptimizedInvokeType(InvokeType invoke_type) {
3679 optimized_invoke_type_ = invoke_type;
3680 }
3681
Vladimir Marko58155012015-08-19 12:49:41 +00003682 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3683 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3684 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003685 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003686 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003687 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003688 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003689 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3690 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003691 bool HasCurrentMethodInput() const {
3692 // This function can be called only after the invoke has been fully initialized by the builder.
3693 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003694 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003695 return true;
3696 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003697 DCHECK(InputCount() == GetSpecialInputIndex() ||
3698 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003699 return false;
3700 }
3701 }
Vladimir Marko58155012015-08-19 12:49:41 +00003702 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3703 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003704 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003705
3706 int32_t GetStringInitOffset() const {
3707 DCHECK(IsStringInit());
3708 return dispatch_info_.method_load_data;
3709 }
3710
3711 uint64_t GetMethodAddress() const {
3712 DCHECK(HasMethodAddress());
3713 return dispatch_info_.method_load_data;
3714 }
3715
3716 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003717 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003718 return dispatch_info_.method_load_data;
3719 }
3720
3721 uint64_t GetDirectCodePtr() const {
3722 DCHECK(HasDirectCodePtr());
3723 return dispatch_info_.direct_code_ptr;
3724 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003725
Calin Juravle68ad6492015-08-18 17:08:12 +01003726 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3727
Roland Levillain4c0eb422015-04-24 16:43:49 +01003728 // Is this instruction a call to a static method?
3729 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003730 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003731 }
3732
Vladimir Markofbb184a2015-11-13 14:47:00 +00003733 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3734 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3735 // instruction; only relevant for static calls with explicit clinit check.
3736 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003737 DCHECK(IsStaticWithExplicitClinitCheck());
3738 size_t last_input_index = InputCount() - 1;
3739 HInstruction* last_input = InputAt(last_input_index);
3740 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003741 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003742 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003743 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003744 clinit_check_requirement_ = new_requirement;
3745 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003746 }
3747
David Brazdilbc9ab162016-01-20 14:50:19 +00003748 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003749 DCHECK(IsStringInit());
3750 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003751 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003752 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003753 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003754 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003755 }
3756
Roland Levillain4c0eb422015-04-24 16:43:49 +01003757 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003758 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003759 bool IsStaticWithExplicitClinitCheck() const {
3760 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3761 }
3762
3763 // Is this a call to a static method whose declaring class has an
3764 // implicit intialization check requirement?
3765 bool IsStaticWithImplicitClinitCheck() const {
3766 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3767 }
3768
Vladimir Markob554b5a2015-11-06 12:57:55 +00003769 // Does this method load kind need the current method as an input?
3770 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3771 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3772 }
3773
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003774 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003775
Roland Levillain4c0eb422015-04-24 16:43:49 +01003776 protected:
3777 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3778 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3779 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3780 HInstruction* input = input_record.GetInstruction();
3781 // `input` is the last input of a static invoke marked as having
3782 // an explicit clinit check. It must either be:
3783 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3784 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3785 DCHECK(input != nullptr);
3786 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3787 }
3788 return input_record;
3789 }
3790
Vladimir Markoc53c0792015-11-19 15:48:33 +00003791 void InsertInputAt(size_t index, HInstruction* input);
3792 void RemoveInputAt(size_t index);
3793
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003794 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003795 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003796 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003797 // The target method may refer to different dex file or method index than the original
3798 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3799 // in derived class to increase visibility.
3800 MethodReference target_method_;
3801 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003802
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003803 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003804};
Vladimir Markof64242a2015-12-01 14:58:23 +00003805std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003806std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003807
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003808class HInvokeVirtual : public HInvoke {
3809 public:
3810 HInvokeVirtual(ArenaAllocator* arena,
3811 uint32_t number_of_arguments,
3812 Primitive::Type return_type,
3813 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003814 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003815 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003816 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003817 vtable_index_(vtable_index) {}
3818
Calin Juravle641547a2015-04-21 22:08:51 +01003819 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003820 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003821 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003822 }
3823
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003824 uint32_t GetVTableIndex() const { return vtable_index_; }
3825
3826 DECLARE_INSTRUCTION(InvokeVirtual);
3827
3828 private:
3829 const uint32_t vtable_index_;
3830
3831 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3832};
3833
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003834class HInvokeInterface : public HInvoke {
3835 public:
3836 HInvokeInterface(ArenaAllocator* arena,
3837 uint32_t number_of_arguments,
3838 Primitive::Type return_type,
3839 uint32_t dex_pc,
3840 uint32_t dex_method_index,
3841 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003842 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003843 imt_index_(imt_index) {}
3844
Calin Juravle641547a2015-04-21 22:08:51 +01003845 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003846 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003847 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003848 }
3849
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003850 uint32_t GetImtIndex() const { return imt_index_; }
3851 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3852
3853 DECLARE_INSTRUCTION(InvokeInterface);
3854
3855 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003856 const uint32_t imt_index_;
3857
3858 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3859};
3860
Roland Levillain88cb1752014-10-20 16:36:47 +01003861class HNeg : public HUnaryOperation {
3862 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003863 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3864 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003865
Roland Levillain9867bc72015-08-05 10:21:34 +01003866 template <typename T> T Compute(T x) const { return -x; }
3867
3868 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003869 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003870 }
3871 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003873 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003874
Roland Levillain88cb1752014-10-20 16:36:47 +01003875 DECLARE_INSTRUCTION(Neg);
3876
3877 private:
3878 DISALLOW_COPY_AND_ASSIGN(HNeg);
3879};
3880
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003881class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003882 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003883 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003884 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003885 uint32_t dex_pc,
3886 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003887 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003888 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003889 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003890 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003891 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003892 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003893 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003894 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003895 }
3896
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003897 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003898 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003899
3900 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003901 bool NeedsEnvironment() const OVERRIDE { return true; }
3902
Mingyao Yang0c365e62015-03-31 15:09:29 -07003903 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3904 bool CanThrow() const OVERRIDE { return true; }
3905
Calin Juravle10e244f2015-01-26 18:54:32 +00003906 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003907
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003908 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3909
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003910 DECLARE_INSTRUCTION(NewArray);
3911
3912 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003913 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003914 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003915 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003916
3917 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3918};
3919
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003920class HAdd : public HBinaryOperation {
3921 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003922 HAdd(Primitive::Type result_type,
3923 HInstruction* left,
3924 HInstruction* right,
3925 uint32_t dex_pc = kNoDexPc)
3926 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003928 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003929
Roland Levillain9867bc72015-08-05 10:21:34 +01003930 template <typename T> T Compute(T x, T y) const { return x + y; }
3931
3932 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003933 return GetBlock()->GetGraph()->GetIntConstant(
3934 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003935 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003936 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003937 return GetBlock()->GetGraph()->GetLongConstant(
3938 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003939 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003940
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003941 DECLARE_INSTRUCTION(Add);
3942
3943 private:
3944 DISALLOW_COPY_AND_ASSIGN(HAdd);
3945};
3946
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003947class HSub : public HBinaryOperation {
3948 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003949 HSub(Primitive::Type result_type,
3950 HInstruction* left,
3951 HInstruction* right,
3952 uint32_t dex_pc = kNoDexPc)
3953 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003954
Roland Levillain9867bc72015-08-05 10:21:34 +01003955 template <typename T> T Compute(T x, T y) const { return x - y; }
3956
3957 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003958 return GetBlock()->GetGraph()->GetIntConstant(
3959 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003960 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003961 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003962 return GetBlock()->GetGraph()->GetLongConstant(
3963 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003964 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003965
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003966 DECLARE_INSTRUCTION(Sub);
3967
3968 private:
3969 DISALLOW_COPY_AND_ASSIGN(HSub);
3970};
3971
Calin Juravle34bacdf2014-10-07 20:23:36 +01003972class HMul : public HBinaryOperation {
3973 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003974 HMul(Primitive::Type result_type,
3975 HInstruction* left,
3976 HInstruction* right,
3977 uint32_t dex_pc = kNoDexPc)
3978 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003979
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003980 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003981
Roland Levillain9867bc72015-08-05 10:21:34 +01003982 template <typename T> T Compute(T x, T y) const { return x * y; }
3983
3984 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003985 return GetBlock()->GetGraph()->GetIntConstant(
3986 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003987 }
3988 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003989 return GetBlock()->GetGraph()->GetLongConstant(
3990 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003991 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003992
3993 DECLARE_INSTRUCTION(Mul);
3994
3995 private:
3996 DISALLOW_COPY_AND_ASSIGN(HMul);
3997};
3998
Calin Juravle7c4954d2014-10-28 16:57:40 +00003999class HDiv : public HBinaryOperation {
4000 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004001 HDiv(Primitive::Type result_type,
4002 HInstruction* left,
4003 HInstruction* right,
4004 uint32_t dex_pc)
4005 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004006
Roland Levillain9867bc72015-08-05 10:21:34 +01004007 template <typename T>
4008 T Compute(T x, T y) const {
4009 // Our graph structure ensures we never have 0 for `y` during
4010 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004011 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004012 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004013 return (y == -1) ? -x : x / y;
4014 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004015
Roland Levillain9867bc72015-08-05 10:21:34 +01004016 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004017 return GetBlock()->GetGraph()->GetIntConstant(
4018 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004019 }
4020 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004021 return GetBlock()->GetGraph()->GetLongConstant(
4022 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004023 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004024
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004025 static SideEffects SideEffectsForArchRuntimeCalls() {
4026 // The generated code can use a runtime call.
4027 return SideEffects::CanTriggerGC();
4028 }
4029
Calin Juravle7c4954d2014-10-28 16:57:40 +00004030 DECLARE_INSTRUCTION(Div);
4031
4032 private:
4033 DISALLOW_COPY_AND_ASSIGN(HDiv);
4034};
4035
Calin Juravlebacfec32014-11-14 15:54:36 +00004036class HRem : public HBinaryOperation {
4037 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004038 HRem(Primitive::Type result_type,
4039 HInstruction* left,
4040 HInstruction* right,
4041 uint32_t dex_pc)
4042 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004043
Roland Levillain9867bc72015-08-05 10:21:34 +01004044 template <typename T>
4045 T Compute(T x, T y) const {
4046 // Our graph structure ensures we never have 0 for `y` during
4047 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004048 DCHECK_NE(y, 0);
4049 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4050 return (y == -1) ? 0 : x % y;
4051 }
4052
Roland Levillain9867bc72015-08-05 10:21:34 +01004053 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004054 return GetBlock()->GetGraph()->GetIntConstant(
4055 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004056 }
4057 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004058 return GetBlock()->GetGraph()->GetLongConstant(
4059 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004060 }
4061
Calin Juravlebacfec32014-11-14 15:54:36 +00004062
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004063 static SideEffects SideEffectsForArchRuntimeCalls() {
4064 return SideEffects::CanTriggerGC();
4065 }
4066
Calin Juravlebacfec32014-11-14 15:54:36 +00004067 DECLARE_INSTRUCTION(Rem);
4068
4069 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004070 DISALLOW_COPY_AND_ASSIGN(HRem);
4071};
4072
Calin Juravled0d48522014-11-04 16:40:20 +00004073class HDivZeroCheck : public HExpression<1> {
4074 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004075 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4076 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004077 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004078 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004079 SetRawInputAt(0, value);
4080 }
4081
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004082 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4083
Calin Juravled0d48522014-11-04 16:40:20 +00004084 bool CanBeMoved() const OVERRIDE { return true; }
4085
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004086 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004087 return true;
4088 }
4089
4090 bool NeedsEnvironment() const OVERRIDE { return true; }
4091 bool CanThrow() const OVERRIDE { return true; }
4092
Calin Juravled0d48522014-11-04 16:40:20 +00004093 DECLARE_INSTRUCTION(DivZeroCheck);
4094
4095 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004096 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4097};
4098
Calin Juravle9aec02f2014-11-18 23:06:35 +00004099class HShl : public HBinaryOperation {
4100 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004101 HShl(Primitive::Type result_type,
4102 HInstruction* left,
4103 HInstruction* right,
4104 uint32_t dex_pc = kNoDexPc)
4105 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004106
Roland Levillain9867bc72015-08-05 10:21:34 +01004107 template <typename T, typename U, typename V>
4108 T Compute(T x, U y, V max_shift_value) const {
4109 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4110 "V is not the unsigned integer type corresponding to T");
4111 return x << (y & max_shift_value);
4112 }
4113
4114 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4115 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004116 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004117 }
4118 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4119 // case is handled as `x << static_cast<int>(y)`.
4120 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4121 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004122 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004123 }
4124 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4125 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004126 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004127 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004128
4129 DECLARE_INSTRUCTION(Shl);
4130
4131 private:
4132 DISALLOW_COPY_AND_ASSIGN(HShl);
4133};
4134
4135class HShr : public HBinaryOperation {
4136 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004137 HShr(Primitive::Type result_type,
4138 HInstruction* left,
4139 HInstruction* right,
4140 uint32_t dex_pc = kNoDexPc)
4141 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004142
Roland Levillain9867bc72015-08-05 10:21:34 +01004143 template <typename T, typename U, typename V>
4144 T Compute(T x, U y, V max_shift_value) const {
4145 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4146 "V is not the unsigned integer type corresponding to T");
4147 return x >> (y & max_shift_value);
4148 }
4149
4150 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4151 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004152 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004153 }
4154 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4155 // case is handled as `x >> static_cast<int>(y)`.
4156 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4157 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004158 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004159 }
4160 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4161 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004162 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004163 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004164
4165 DECLARE_INSTRUCTION(Shr);
4166
4167 private:
4168 DISALLOW_COPY_AND_ASSIGN(HShr);
4169};
4170
4171class HUShr : public HBinaryOperation {
4172 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004173 HUShr(Primitive::Type result_type,
4174 HInstruction* left,
4175 HInstruction* right,
4176 uint32_t dex_pc = kNoDexPc)
4177 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004178
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 template <typename T, typename U, typename V>
4180 T Compute(T x, U y, V max_shift_value) const {
4181 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4182 "V is not the unsigned integer type corresponding to T");
4183 V ux = static_cast<V>(x);
4184 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004185 }
4186
Roland Levillain9867bc72015-08-05 10:21:34 +01004187 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4188 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004189 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004190 }
4191 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4192 // case is handled as `x >>> static_cast<int>(y)`.
4193 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4194 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004196 }
4197 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4198 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004199 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004200 }
4201
4202 DECLARE_INSTRUCTION(UShr);
4203
4204 private:
4205 DISALLOW_COPY_AND_ASSIGN(HUShr);
4206};
4207
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004208class HAnd : public HBinaryOperation {
4209 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 HAnd(Primitive::Type result_type,
4211 HInstruction* left,
4212 HInstruction* right,
4213 uint32_t dex_pc = kNoDexPc)
4214 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004215
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004216 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004217
Roland Levillain9867bc72015-08-05 10:21:34 +01004218 template <typename T, typename U>
4219 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4220
4221 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004222 return GetBlock()->GetGraph()->GetIntConstant(
4223 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004224 }
4225 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004226 return GetBlock()->GetGraph()->GetLongConstant(
4227 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004228 }
4229 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 return GetBlock()->GetGraph()->GetLongConstant(
4231 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004232 }
4233 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004234 return GetBlock()->GetGraph()->GetLongConstant(
4235 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004236 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004237
4238 DECLARE_INSTRUCTION(And);
4239
4240 private:
4241 DISALLOW_COPY_AND_ASSIGN(HAnd);
4242};
4243
4244class HOr : public HBinaryOperation {
4245 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004246 HOr(Primitive::Type result_type,
4247 HInstruction* left,
4248 HInstruction* right,
4249 uint32_t dex_pc = kNoDexPc)
4250 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004251
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004252 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004253
Roland Levillain9867bc72015-08-05 10:21:34 +01004254 template <typename T, typename U>
4255 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4256
4257 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004258 return GetBlock()->GetGraph()->GetIntConstant(
4259 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004260 }
4261 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004262 return GetBlock()->GetGraph()->GetLongConstant(
4263 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004264 }
4265 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004266 return GetBlock()->GetGraph()->GetLongConstant(
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 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004273
4274 DECLARE_INSTRUCTION(Or);
4275
4276 private:
4277 DISALLOW_COPY_AND_ASSIGN(HOr);
4278};
4279
4280class HXor : public HBinaryOperation {
4281 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004282 HXor(Primitive::Type result_type,
4283 HInstruction* left,
4284 HInstruction* right,
4285 uint32_t dex_pc = kNoDexPc)
4286 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004287
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004288 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004289
Roland Levillain9867bc72015-08-05 10:21:34 +01004290 template <typename T, typename U>
4291 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4292
4293 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004294 return GetBlock()->GetGraph()->GetIntConstant(
4295 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004296 }
4297 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004298 return GetBlock()->GetGraph()->GetLongConstant(
4299 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004300 }
4301 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004302 return GetBlock()->GetGraph()->GetLongConstant(
4303 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004304 }
4305 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004306 return GetBlock()->GetGraph()->GetLongConstant(
4307 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004308 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004309
4310 DECLARE_INSTRUCTION(Xor);
4311
4312 private:
4313 DISALLOW_COPY_AND_ASSIGN(HXor);
4314};
4315
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004316class HRor : public HBinaryOperation {
4317 public:
4318 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4319 : HBinaryOperation(result_type, value, distance) {}
4320
4321 template <typename T, typename U, typename V>
4322 T Compute(T x, U y, V max_shift_value) const {
4323 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4324 "V is not the unsigned integer type corresponding to T");
4325 V ux = static_cast<V>(x);
4326 if ((y & max_shift_value) == 0) {
4327 return static_cast<T>(ux);
4328 } else {
4329 const V reg_bits = sizeof(T) * 8;
4330 return static_cast<T>(ux >> (y & max_shift_value)) |
4331 (x << (reg_bits - (y & max_shift_value)));
4332 }
4333 }
4334
4335 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4336 return GetBlock()->GetGraph()->GetIntConstant(
4337 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4338 }
4339 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4340 return GetBlock()->GetGraph()->GetLongConstant(
4341 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4342 }
4343 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4344 return GetBlock()->GetGraph()->GetLongConstant(
4345 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4346 }
4347
4348 DECLARE_INSTRUCTION(Ror);
4349
4350 private:
4351 DISALLOW_COPY_AND_ASSIGN(HRor);
4352};
4353
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004354// The value of a parameter in this method. Its location depends on
4355// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004356class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004357 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004358 HParameterValue(const DexFile& dex_file,
4359 uint16_t type_index,
4360 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004361 Primitive::Type parameter_type,
4362 bool is_this = false)
4363 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004364 dex_file_(dex_file),
4365 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004366 index_(index),
4367 is_this_(is_this),
4368 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004369
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004370 const DexFile& GetDexFile() const { return dex_file_; }
4371 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004372 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004373 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004374
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004375 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4376 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004377
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004378 DECLARE_INSTRUCTION(ParameterValue);
4379
4380 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004381 const DexFile& dex_file_;
4382 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004383 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004384 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004385 const uint8_t index_;
4386
Calin Juravle10e244f2015-01-26 18:54:32 +00004387 // Whether or not the parameter value corresponds to 'this' argument.
4388 const bool is_this_;
4389
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004390 bool can_be_null_;
4391
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004392 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4393};
4394
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004395class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004396 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004397 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4398 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004399
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004400 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004401 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004402 return true;
4403 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004404
Roland Levillain9867bc72015-08-05 10:21:34 +01004405 template <typename T> T Compute(T x) const { return ~x; }
4406
4407 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004408 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004409 }
4410 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004411 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004412 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004413
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004414 DECLARE_INSTRUCTION(Not);
4415
4416 private:
4417 DISALLOW_COPY_AND_ASSIGN(HNot);
4418};
4419
David Brazdil66d126e2015-04-03 16:02:44 +01004420class HBooleanNot : public HUnaryOperation {
4421 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004422 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4423 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004424
4425 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004426 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004427 return true;
4428 }
4429
Roland Levillain9867bc72015-08-05 10:21:34 +01004430 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004431 DCHECK(IsUint<1>(x));
4432 return !x;
4433 }
4434
Roland Levillain9867bc72015-08-05 10:21:34 +01004435 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004436 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004437 }
4438 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4439 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004440 UNREACHABLE();
4441 }
4442
4443 DECLARE_INSTRUCTION(BooleanNot);
4444
4445 private:
4446 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4447};
4448
Roland Levillaindff1f282014-11-05 14:15:05 +00004449class HTypeConversion : public HExpression<1> {
4450 public:
4451 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004452 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004453 : HExpression(result_type,
4454 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4455 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004456 SetRawInputAt(0, input);
4457 DCHECK_NE(input->GetType(), result_type);
4458 }
4459
4460 HInstruction* GetInput() const { return InputAt(0); }
4461 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4462 Primitive::Type GetResultType() const { return GetType(); }
4463
4464 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004465 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004466
Mark Mendelle82549b2015-05-06 10:55:34 -04004467 // Try to statically evaluate the conversion and return a HConstant
4468 // containing the result. If the input cannot be converted, return nullptr.
4469 HConstant* TryStaticEvaluation() const;
4470
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004471 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4472 Primitive::Type result_type) {
4473 // Some architectures may not require the 'GC' side effects, but at this point
4474 // in the compilation process we do not know what architecture we will
4475 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004476 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4477 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004478 return SideEffects::CanTriggerGC();
4479 }
4480 return SideEffects::None();
4481 }
4482
Roland Levillaindff1f282014-11-05 14:15:05 +00004483 DECLARE_INSTRUCTION(TypeConversion);
4484
4485 private:
4486 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4487};
4488
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004489static constexpr uint32_t kNoRegNumber = -1;
4490
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004491class HPhi : public HInstruction {
4492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 HPhi(ArenaAllocator* arena,
4494 uint32_t reg_number,
4495 size_t number_of_inputs,
4496 Primitive::Type type,
4497 uint32_t dex_pc = kNoDexPc)
4498 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004499 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004500 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004501 type_(ToPhiType(type)),
4502 // Phis are constructed live and marked dead if conflicting or unused.
4503 // Individual steps of SsaBuilder should assume that if a phi has been
4504 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4505 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004506 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004507 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004508 }
4509
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004510 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4511 static Primitive::Type ToPhiType(Primitive::Type type) {
4512 switch (type) {
4513 case Primitive::kPrimBoolean:
4514 case Primitive::kPrimByte:
4515 case Primitive::kPrimShort:
4516 case Primitive::kPrimChar:
4517 return Primitive::kPrimInt;
4518 default:
4519 return type;
4520 }
4521 }
4522
David Brazdilffee3d32015-07-06 11:48:53 +01004523 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4524
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004525 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004526
4527 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004528 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004529
Calin Juravle10e244f2015-01-26 18:54:32 +00004530 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004531 void SetType(Primitive::Type new_type) {
4532 // Make sure that only valid type changes occur. The following are allowed:
4533 // (1) int -> float/ref (primitive type propagation),
4534 // (2) long -> double (primitive type propagation).
4535 DCHECK(type_ == new_type ||
4536 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4537 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4538 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4539 type_ = new_type;
4540 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004541
Calin Juravle10e244f2015-01-26 18:54:32 +00004542 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4543 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4544
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004545 uint32_t GetRegNumber() const { return reg_number_; }
4546
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004547 void SetDead() { is_live_ = false; }
4548 void SetLive() { is_live_ = true; }
4549 bool IsDead() const { return !is_live_; }
4550 bool IsLive() const { return is_live_; }
4551
David Brazdil77a48ae2015-09-15 12:34:04 +00004552 bool IsVRegEquivalentOf(HInstruction* other) const {
4553 return other != nullptr
4554 && other->IsPhi()
4555 && other->AsPhi()->GetBlock() == GetBlock()
4556 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4557 }
4558
Calin Juravlea4f88312015-04-16 12:57:19 +01004559 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4560 // An equivalent phi is a phi having the same dex register and type.
4561 // It assumes that phis with the same dex register are adjacent.
4562 HPhi* GetNextEquivalentPhiWithSameType() {
4563 HInstruction* next = GetNext();
4564 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4565 if (next->GetType() == GetType()) {
4566 return next->AsPhi();
4567 }
4568 next = next->GetNext();
4569 }
4570 return nullptr;
4571 }
4572
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004573 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004574
David Brazdil1abb4192015-02-17 18:33:36 +00004575 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004576 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004577 return inputs_[index];
4578 }
David Brazdil1abb4192015-02-17 18:33:36 +00004579
4580 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004581 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004582 }
4583
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004584 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004585 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004586 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004587 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004588 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004589 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004590
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004591 DISALLOW_COPY_AND_ASSIGN(HPhi);
4592};
4593
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004594class HNullCheck : public HExpression<1> {
4595 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004596 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4597 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004599 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004600 SetRawInputAt(0, value);
4601 }
4602
Calin Juravle10e244f2015-01-26 18:54:32 +00004603 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004604 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004605 return true;
4606 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004607
Calin Juravle10e244f2015-01-26 18:54:32 +00004608 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004609
Calin Juravle10e244f2015-01-26 18:54:32 +00004610 bool CanThrow() const OVERRIDE { return true; }
4611
4612 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004613
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004614
4615 DECLARE_INSTRUCTION(NullCheck);
4616
4617 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004618 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4619};
4620
4621class FieldInfo : public ValueObject {
4622 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004623 FieldInfo(MemberOffset field_offset,
4624 Primitive::Type field_type,
4625 bool is_volatile,
4626 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004627 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004628 const DexFile& dex_file,
4629 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004630 : field_offset_(field_offset),
4631 field_type_(field_type),
4632 is_volatile_(is_volatile),
4633 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004634 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004635 dex_file_(dex_file),
4636 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004637
4638 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004639 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004640 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004641 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004642 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004643 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004644 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004645
4646 private:
4647 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004648 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004649 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004650 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004651 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004652 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004653 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004654};
4655
4656class HInstanceFieldGet : public HExpression<1> {
4657 public:
4658 HInstanceFieldGet(HInstruction* value,
4659 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004660 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004661 bool is_volatile,
4662 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004663 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004664 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004665 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004666 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004667 : HExpression(field_type,
4668 SideEffects::FieldReadOfType(field_type, is_volatile),
4669 dex_pc),
4670 field_info_(field_offset,
4671 field_type,
4672 is_volatile,
4673 field_idx,
4674 declaring_class_def_index,
4675 dex_file,
4676 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004677 SetRawInputAt(0, value);
4678 }
4679
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004680 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004681
4682 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4683 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4684 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004685 }
4686
Calin Juravle641547a2015-04-21 22:08:51 +01004687 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4688 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004689 }
4690
4691 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004692 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4693 }
4694
Calin Juravle52c48962014-12-16 17:02:57 +00004695 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004696 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004697 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004698 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004699
4700 DECLARE_INSTRUCTION(InstanceFieldGet);
4701
4702 private:
4703 const FieldInfo field_info_;
4704
4705 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4706};
4707
4708class HInstanceFieldSet : public HTemplateInstruction<2> {
4709 public:
4710 HInstanceFieldSet(HInstruction* object,
4711 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004712 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004713 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004714 bool is_volatile,
4715 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004716 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004717 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004718 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004719 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004720 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4721 dex_pc),
4722 field_info_(field_offset,
4723 field_type,
4724 is_volatile,
4725 field_idx,
4726 declaring_class_def_index,
4727 dex_file,
4728 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004729 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004730 SetRawInputAt(0, object);
4731 SetRawInputAt(1, value);
4732 }
4733
Calin Juravle641547a2015-04-21 22:08:51 +01004734 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4735 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004736 }
4737
Calin Juravle52c48962014-12-16 17:02:57 +00004738 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004739 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004740 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004741 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004742 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004743 bool GetValueCanBeNull() const { return value_can_be_null_; }
4744 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004745
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004746 DECLARE_INSTRUCTION(InstanceFieldSet);
4747
4748 private:
4749 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004750 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004751
4752 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4753};
4754
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004755class HArrayGet : public HExpression<2> {
4756 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004757 HArrayGet(HInstruction* array,
4758 HInstruction* index,
4759 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004760 uint32_t dex_pc,
4761 SideEffects additional_side_effects = SideEffects::None())
4762 : HExpression(type,
4763 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004764 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004765 SetRawInputAt(0, array);
4766 SetRawInputAt(1, index);
4767 }
4768
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004769 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004770 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004771 return true;
4772 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004773 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004774 // TODO: We can be smarter here.
4775 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4776 // which generates the implicit null check. There are cases when these can be removed
4777 // to produce better code. If we ever add optimizations to do so we should allow an
4778 // implicit check here (as long as the address falls in the first page).
4779 return false;
4780 }
4781
David Brazdil4833f5a2015-12-16 10:37:39 +00004782 bool IsEquivalentOf(HArrayGet* other) const {
4783 bool result = (GetDexPc() == other->GetDexPc());
4784 if (kIsDebugBuild && result) {
4785 DCHECK_EQ(GetBlock(), other->GetBlock());
4786 DCHECK_EQ(GetArray(), other->GetArray());
4787 DCHECK_EQ(GetIndex(), other->GetIndex());
4788 if (Primitive::IsIntOrLongType(GetType())) {
4789 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4790 } else {
4791 DCHECK(Primitive::IsFloatingPointType(GetType()));
4792 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4793 }
4794 }
4795 return result;
4796 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004797
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004798 HInstruction* GetArray() const { return InputAt(0); }
4799 HInstruction* GetIndex() const { return InputAt(1); }
4800
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 DECLARE_INSTRUCTION(ArrayGet);
4802
4803 private:
4804 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4805};
4806
4807class HArraySet : public HTemplateInstruction<3> {
4808 public:
4809 HArraySet(HInstruction* array,
4810 HInstruction* index,
4811 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004812 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004813 uint32_t dex_pc,
4814 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004815 : HTemplateInstruction(
4816 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004817 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4818 additional_side_effects),
4819 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004820 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004821 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004822 value_can_be_null_(true),
4823 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004824 SetRawInputAt(0, array);
4825 SetRawInputAt(1, index);
4826 SetRawInputAt(2, value);
4827 }
4828
Calin Juravle77520bc2015-01-12 18:45:46 +00004829 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00004830 // We call a runtime method to throw ArrayStoreException.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004831 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004832 }
4833
Mingyao Yang81014cb2015-06-02 03:16:27 -07004834 // Can throw ArrayStoreException.
4835 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4836
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004837 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004838 // TODO: Same as for ArrayGet.
4839 return false;
4840 }
4841
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004842 void ClearNeedsTypeCheck() {
4843 needs_type_check_ = false;
4844 }
4845
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004846 void ClearValueCanBeNull() {
4847 value_can_be_null_ = false;
4848 }
4849
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004850 void SetStaticTypeOfArrayIsObjectArray() {
4851 static_type_of_array_is_object_array_ = true;
4852 }
4853
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004854 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004855 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004856 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004857
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004858 HInstruction* GetArray() const { return InputAt(0); }
4859 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004860 HInstruction* GetValue() const { return InputAt(2); }
4861
4862 Primitive::Type GetComponentType() const {
4863 // The Dex format does not type floating point index operations. Since the
4864 // `expected_component_type_` is set during building and can therefore not
4865 // be correct, we also check what is the value type. If it is a floating
4866 // point type, we must use that type.
4867 Primitive::Type value_type = GetValue()->GetType();
4868 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4869 ? value_type
4870 : expected_component_type_;
4871 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004872
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004873 Primitive::Type GetRawExpectedComponentType() const {
4874 return expected_component_type_;
4875 }
4876
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004877 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4878 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4879 }
4880
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004881 DECLARE_INSTRUCTION(ArraySet);
4882
4883 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004884 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004885 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004886 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004887 // Cached information for the reference_type_info_ so that codegen
4888 // does not need to inspect the static type.
4889 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890
4891 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4892};
4893
4894class HArrayLength : public HExpression<1> {
4895 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004896 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004897 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004898 // Note that arrays do not change length, so the instruction does not
4899 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900 SetRawInputAt(0, array);
4901 }
4902
Calin Juravle77520bc2015-01-12 18:45:46 +00004903 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004904 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004905 return true;
4906 }
Calin Juravle641547a2015-04-21 22:08:51 +01004907 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4908 return obj == InputAt(0);
4909 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004910
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004911 DECLARE_INSTRUCTION(ArrayLength);
4912
4913 private:
4914 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4915};
4916
4917class HBoundsCheck : public HExpression<2> {
4918 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004919 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4920 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004921 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004922 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004923 DCHECK(index->GetType() == Primitive::kPrimInt);
4924 SetRawInputAt(0, index);
4925 SetRawInputAt(1, length);
4926 }
4927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004928 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004929 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004930 return true;
4931 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004933 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004935 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004936
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004937 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004938
4939 DECLARE_INSTRUCTION(BoundsCheck);
4940
4941 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004942 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4943};
4944
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004945class HSuspendCheck : public HTemplateInstruction<0> {
4946 public:
4947 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004948 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004949
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004950 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004951 return true;
4952 }
4953
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004954 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4955 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004956
4957 DECLARE_INSTRUCTION(SuspendCheck);
4958
4959 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004960 // Only used for code generation, in order to share the same slow path between back edges
4961 // of a same loop.
4962 SlowPathCode* slow_path_;
4963
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004964 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4965};
4966
David Srbecky0cf44932015-12-09 14:09:59 +00004967// Pseudo-instruction which provides the native debugger with mapping information.
4968// It ensures that we can generate line number and local variables at this point.
4969class HNativeDebugInfo : public HTemplateInstruction<0> {
4970 public:
4971 explicit HNativeDebugInfo(uint32_t dex_pc)
4972 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4973
4974 bool NeedsEnvironment() const OVERRIDE {
4975 return true;
4976 }
4977
4978 DECLARE_INSTRUCTION(NativeDebugInfo);
4979
4980 private:
4981 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4982};
4983
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004984/**
4985 * Instruction to load a Class object.
4986 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004987class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004988 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004989 HLoadClass(HCurrentMethod* current_method,
4990 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004991 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004992 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004993 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004994 bool needs_access_check,
4995 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004996 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004997 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004998 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004999 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00005000 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01005001 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005002 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00005003 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005004 // Referrers class should not need access check. We never inline unverified
5005 // methods so we can't possibly end up in this situation.
5006 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005007 SetRawInputAt(0, current_method);
5008 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005009
5010 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005011
5012 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005013 // Note that we don't need to test for generate_clinit_check_.
5014 // Whether or not we need to generate the clinit check is processed in
5015 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005016 return other->AsLoadClass()->type_index_ == type_index_ &&
5017 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005018 }
5019
5020 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5021
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005022 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005023 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005024 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005025
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005026 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005027 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005028 }
5029
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005030 bool MustGenerateClinitCheck() const {
5031 return generate_clinit_check_;
5032 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005033
Calin Juravle0ba218d2015-05-19 18:46:01 +01005034 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005035 // The entrypoint the code generator is going to call does not do
5036 // clinit of the class.
5037 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005038 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005039 }
5040
5041 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005042 return MustGenerateClinitCheck() ||
5043 (!is_referrers_class_ && !is_in_dex_cache_) ||
5044 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005045 }
5046
5047 bool NeedsAccessCheck() const {
5048 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005049 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005050
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005051 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005052 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005053 }
5054
Calin Juravleacf735c2015-02-12 15:25:22 +00005055 ReferenceTypeInfo GetLoadedClassRTI() {
5056 return loaded_class_rti_;
5057 }
5058
5059 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5060 // Make sure we only set exact types (the loaded class should never be merged).
5061 DCHECK(rti.IsExact());
5062 loaded_class_rti_ = rti;
5063 }
5064
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005065 const DexFile& GetDexFile() { return dex_file_; }
5066
Vladimir Markodc151b22015-10-15 18:02:30 +01005067 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005068
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005069 static SideEffects SideEffectsForArchRuntimeCalls() {
5070 return SideEffects::CanTriggerGC();
5071 }
5072
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005073 bool IsInDexCache() const { return is_in_dex_cache_; }
5074
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005075 DECLARE_INSTRUCTION(LoadClass);
5076
5077 private:
5078 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005079 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005080 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005081 // Whether this instruction must generate the initialization check.
5082 // Used for code generation.
5083 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005084 const bool needs_access_check_;
5085 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005086
Calin Juravleacf735c2015-02-12 15:25:22 +00005087 ReferenceTypeInfo loaded_class_rti_;
5088
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005089 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5090};
5091
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005092class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005093 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005094 HLoadString(HCurrentMethod* current_method,
5095 uint32_t string_index,
5096 uint32_t dex_pc,
5097 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005098 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005099 string_index_(string_index),
5100 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005101 SetRawInputAt(0, current_method);
5102 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005103
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005104 bool CanBeMoved() const OVERRIDE { return true; }
5105
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005106 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5107 return other->AsLoadString()->string_index_ == string_index_;
5108 }
5109
5110 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5111
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005112 uint32_t GetStringIndex() const { return string_index_; }
5113
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005114 // Will call the runtime if the string is not already in the dex cache.
5115 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5116
Vladimir Markodc151b22015-10-15 18:02:30 +01005117 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005118 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005119 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005120 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005121
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005122 static SideEffects SideEffectsForArchRuntimeCalls() {
5123 return SideEffects::CanTriggerGC();
5124 }
5125
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005126 DECLARE_INSTRUCTION(LoadString);
5127
5128 private:
5129 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005130 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005131
5132 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5133};
5134
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005135/**
5136 * Performs an initialization check on its Class object input.
5137 */
5138class HClinitCheck : public HExpression<1> {
5139 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005140 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005141 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005142 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005143 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5144 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005145 SetRawInputAt(0, constant);
5146 }
5147
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005148 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005149 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005150 return true;
5151 }
5152
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005153 bool NeedsEnvironment() const OVERRIDE {
5154 // May call runtime to initialize the class.
5155 return true;
5156 }
5157
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005158 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005159
5160 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5161
5162 DECLARE_INSTRUCTION(ClinitCheck);
5163
5164 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005165 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5166};
5167
5168class HStaticFieldGet : public HExpression<1> {
5169 public:
5170 HStaticFieldGet(HInstruction* cls,
5171 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005172 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005173 bool is_volatile,
5174 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005175 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005176 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005177 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005178 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005179 : HExpression(field_type,
5180 SideEffects::FieldReadOfType(field_type, is_volatile),
5181 dex_pc),
5182 field_info_(field_offset,
5183 field_type,
5184 is_volatile,
5185 field_idx,
5186 declaring_class_def_index,
5187 dex_file,
5188 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005189 SetRawInputAt(0, cls);
5190 }
5191
Calin Juravle52c48962014-12-16 17:02:57 +00005192
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005193 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005194
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005195 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005196 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5197 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005198 }
5199
5200 size_t ComputeHashCode() const OVERRIDE {
5201 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5202 }
5203
Calin Juravle52c48962014-12-16 17:02:57 +00005204 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005205 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5206 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005207 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005208
5209 DECLARE_INSTRUCTION(StaticFieldGet);
5210
5211 private:
5212 const FieldInfo field_info_;
5213
5214 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5215};
5216
5217class HStaticFieldSet : public HTemplateInstruction<2> {
5218 public:
5219 HStaticFieldSet(HInstruction* cls,
5220 HInstruction* value,
5221 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005222 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005223 bool is_volatile,
5224 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005225 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005226 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005227 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005228 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005229 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5230 dex_pc),
5231 field_info_(field_offset,
5232 field_type,
5233 is_volatile,
5234 field_idx,
5235 declaring_class_def_index,
5236 dex_file,
5237 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005238 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005239 SetRawInputAt(0, cls);
5240 SetRawInputAt(1, value);
5241 }
5242
Calin Juravle52c48962014-12-16 17:02:57 +00005243 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005244 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5245 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005246 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005247
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005248 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005249 bool GetValueCanBeNull() const { return value_can_be_null_; }
5250 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005251
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005252 DECLARE_INSTRUCTION(StaticFieldSet);
5253
5254 private:
5255 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005256 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005257
5258 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5259};
5260
Calin Juravlee460d1d2015-09-29 04:52:17 +01005261class HUnresolvedInstanceFieldGet : public HExpression<1> {
5262 public:
5263 HUnresolvedInstanceFieldGet(HInstruction* obj,
5264 Primitive::Type field_type,
5265 uint32_t field_index,
5266 uint32_t dex_pc)
5267 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5268 field_index_(field_index) {
5269 SetRawInputAt(0, obj);
5270 }
5271
5272 bool NeedsEnvironment() const OVERRIDE { return true; }
5273 bool CanThrow() const OVERRIDE { return true; }
5274
5275 Primitive::Type GetFieldType() const { return GetType(); }
5276 uint32_t GetFieldIndex() const { return field_index_; }
5277
5278 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5279
5280 private:
5281 const uint32_t field_index_;
5282
5283 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5284};
5285
5286class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5287 public:
5288 HUnresolvedInstanceFieldSet(HInstruction* obj,
5289 HInstruction* value,
5290 Primitive::Type field_type,
5291 uint32_t field_index,
5292 uint32_t dex_pc)
5293 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5294 field_type_(field_type),
5295 field_index_(field_index) {
5296 DCHECK_EQ(field_type, value->GetType());
5297 SetRawInputAt(0, obj);
5298 SetRawInputAt(1, value);
5299 }
5300
5301 bool NeedsEnvironment() const OVERRIDE { return true; }
5302 bool CanThrow() const OVERRIDE { return true; }
5303
5304 Primitive::Type GetFieldType() const { return field_type_; }
5305 uint32_t GetFieldIndex() const { return field_index_; }
5306
5307 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5308
5309 private:
5310 const Primitive::Type field_type_;
5311 const uint32_t field_index_;
5312
5313 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5314};
5315
5316class HUnresolvedStaticFieldGet : public HExpression<0> {
5317 public:
5318 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5319 uint32_t field_index,
5320 uint32_t dex_pc)
5321 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5322 field_index_(field_index) {
5323 }
5324
5325 bool NeedsEnvironment() const OVERRIDE { return true; }
5326 bool CanThrow() const OVERRIDE { return true; }
5327
5328 Primitive::Type GetFieldType() const { return GetType(); }
5329 uint32_t GetFieldIndex() const { return field_index_; }
5330
5331 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5332
5333 private:
5334 const uint32_t field_index_;
5335
5336 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5337};
5338
5339class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5340 public:
5341 HUnresolvedStaticFieldSet(HInstruction* value,
5342 Primitive::Type field_type,
5343 uint32_t field_index,
5344 uint32_t dex_pc)
5345 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5346 field_type_(field_type),
5347 field_index_(field_index) {
5348 DCHECK_EQ(field_type, value->GetType());
5349 SetRawInputAt(0, value);
5350 }
5351
5352 bool NeedsEnvironment() const OVERRIDE { return true; }
5353 bool CanThrow() const OVERRIDE { return true; }
5354
5355 Primitive::Type GetFieldType() const { return field_type_; }
5356 uint32_t GetFieldIndex() const { return field_index_; }
5357
5358 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5359
5360 private:
5361 const Primitive::Type field_type_;
5362 const uint32_t field_index_;
5363
5364 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5365};
5366
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005367// Implement the move-exception DEX instruction.
5368class HLoadException : public HExpression<0> {
5369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005370 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5371 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005372
David Brazdilbbd733e2015-08-18 17:48:17 +01005373 bool CanBeNull() const OVERRIDE { return false; }
5374
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005375 DECLARE_INSTRUCTION(LoadException);
5376
5377 private:
5378 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5379};
5380
David Brazdilcb1c0552015-08-04 16:22:25 +01005381// Implicit part of move-exception which clears thread-local exception storage.
5382// Must not be removed because the runtime expects the TLS to get cleared.
5383class HClearException : public HTemplateInstruction<0> {
5384 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005385 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5386 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005387
5388 DECLARE_INSTRUCTION(ClearException);
5389
5390 private:
5391 DISALLOW_COPY_AND_ASSIGN(HClearException);
5392};
5393
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005394class HThrow : public HTemplateInstruction<1> {
5395 public:
5396 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005397 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005398 SetRawInputAt(0, exception);
5399 }
5400
5401 bool IsControlFlow() const OVERRIDE { return true; }
5402
5403 bool NeedsEnvironment() const OVERRIDE { return true; }
5404
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005405 bool CanThrow() const OVERRIDE { return true; }
5406
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005407
5408 DECLARE_INSTRUCTION(Throw);
5409
5410 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005411 DISALLOW_COPY_AND_ASSIGN(HThrow);
5412};
5413
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005414/**
5415 * Implementation strategies for the code generator of a HInstanceOf
5416 * or `HCheckCast`.
5417 */
5418enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005419 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005420 kExactCheck, // Can do a single class compare.
5421 kClassHierarchyCheck, // Can just walk the super class chain.
5422 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5423 kInterfaceCheck, // No optimization yet when checking against an interface.
5424 kArrayObjectCheck, // Can just check if the array is not primitive.
5425 kArrayCheck // No optimization yet when checking against a generic array.
5426};
5427
Roland Levillain86503782016-02-11 19:07:30 +00005428std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5429
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005430class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005431 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005432 HInstanceOf(HInstruction* object,
5433 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005434 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005435 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005436 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005437 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005438 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005439 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005440 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005441 SetRawInputAt(0, object);
5442 SetRawInputAt(1, constant);
5443 }
5444
5445 bool CanBeMoved() const OVERRIDE { return true; }
5446
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005447 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005448 return true;
5449 }
5450
5451 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005452 return CanCallRuntime(check_kind_);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005453 }
5454
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005455 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5456
5457 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005458
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005459 // Used only in code generation.
5460 bool MustDoNullCheck() const { return must_do_null_check_; }
5461 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5462
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005463 static bool CanCallRuntime(TypeCheckKind check_kind) {
5464 // Mips currently does runtime calls for any other checks.
5465 return check_kind != TypeCheckKind::kExactCheck;
5466 }
5467
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005468 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005469 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005470 }
5471
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005472 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005473
5474 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005475 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005476 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005477
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005478 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5479};
5480
Calin Juravleb1498f62015-02-16 13:13:29 +00005481class HBoundType : public HExpression<1> {
5482 public:
David Brazdilf5552582015-12-27 13:36:12 +00005483 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005484 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005485 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5486 upper_can_be_null_(true),
5487 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005488 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005489 SetRawInputAt(0, input);
5490 }
5491
David Brazdilf5552582015-12-27 13:36:12 +00005492 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005493 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5494 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005495 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005496
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005497 void SetCanBeNull(bool can_be_null) {
5498 DCHECK(upper_can_be_null_ || !can_be_null);
5499 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005500 }
5501
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005502 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5503
Calin Juravleb1498f62015-02-16 13:13:29 +00005504 DECLARE_INSTRUCTION(BoundType);
5505
5506 private:
5507 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005508 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5509 // It is used to bound the type in cases like:
5510 // if (x instanceof ClassX) {
5511 // // uper_bound_ will be ClassX
5512 // }
David Brazdilf5552582015-12-27 13:36:12 +00005513 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005514 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5515 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005516 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005517 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005518
5519 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5520};
5521
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005522class HCheckCast : public HTemplateInstruction<2> {
5523 public:
5524 HCheckCast(HInstruction* object,
5525 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005526 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005527 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005528 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005529 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005530 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005531 SetRawInputAt(0, object);
5532 SetRawInputAt(1, constant);
5533 }
5534
5535 bool CanBeMoved() const OVERRIDE { return true; }
5536
5537 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5538 return true;
5539 }
5540
5541 bool NeedsEnvironment() const OVERRIDE {
5542 // Instruction may throw a CheckCastError.
5543 return true;
5544 }
5545
5546 bool CanThrow() const OVERRIDE { return true; }
5547
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005548 bool MustDoNullCheck() const { return must_do_null_check_; }
5549 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005550 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005551
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005552 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005553
5554 DECLARE_INSTRUCTION(CheckCast);
5555
5556 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005557 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005558 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005559
5560 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005561};
5562
Calin Juravle27df7582015-04-17 19:12:31 +01005563class HMemoryBarrier : public HTemplateInstruction<0> {
5564 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005565 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005566 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005567 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005568 barrier_kind_(barrier_kind) {}
5569
5570 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5571
5572 DECLARE_INSTRUCTION(MemoryBarrier);
5573
5574 private:
5575 const MemBarrierKind barrier_kind_;
5576
5577 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5578};
5579
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005580class HMonitorOperation : public HTemplateInstruction<1> {
5581 public:
5582 enum OperationKind {
5583 kEnter,
5584 kExit,
5585 };
5586
5587 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005588 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005589 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5590 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005591 SetRawInputAt(0, object);
5592 }
5593
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005594 // Instruction may go into runtime, so we need an environment.
5595 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00005596
5597 bool CanThrow() const OVERRIDE {
5598 // Verifier guarantees that monitor-exit cannot throw.
5599 // This is important because it allows the HGraphBuilder to remove
5600 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5601 return IsEnter();
5602 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005603
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005604
5605 bool IsEnter() const { return kind_ == kEnter; }
5606
5607 DECLARE_INSTRUCTION(MonitorOperation);
5608
Calin Juravle52c48962014-12-16 17:02:57 +00005609 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005610 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005611
5612 private:
5613 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5614};
5615
David Brazdil74eb1b22015-12-14 11:44:01 +00005616class HSelect : public HExpression<3> {
5617 public:
5618 HSelect(HInstruction* condition,
5619 HInstruction* true_value,
5620 HInstruction* false_value,
5621 uint32_t dex_pc)
5622 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
5623 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
5624
5625 // First input must be `true_value` or `false_value` to allow codegens to
5626 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
5627 // that architectures which implement HSelect as a conditional move also
5628 // will not need to invert the condition.
5629 SetRawInputAt(0, false_value);
5630 SetRawInputAt(1, true_value);
5631 SetRawInputAt(2, condition);
5632 }
5633
5634 HInstruction* GetFalseValue() const { return InputAt(0); }
5635 HInstruction* GetTrueValue() const { return InputAt(1); }
5636 HInstruction* GetCondition() const { return InputAt(2); }
5637
5638 bool CanBeMoved() const OVERRIDE { return true; }
5639 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
5640
5641 bool CanBeNull() const OVERRIDE {
5642 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
5643 }
5644
5645 DECLARE_INSTRUCTION(Select);
5646
5647 private:
5648 DISALLOW_COPY_AND_ASSIGN(HSelect);
5649};
5650
Vladimir Markof9f64412015-09-02 14:05:49 +01005651class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005652 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005653 MoveOperands(Location source,
5654 Location destination,
5655 Primitive::Type type,
5656 HInstruction* instruction)
5657 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005658
5659 Location GetSource() const { return source_; }
5660 Location GetDestination() const { return destination_; }
5661
5662 void SetSource(Location value) { source_ = value; }
5663 void SetDestination(Location value) { destination_ = value; }
5664
5665 // The parallel move resolver marks moves as "in-progress" by clearing the
5666 // destination (but not the source).
5667 Location MarkPending() {
5668 DCHECK(!IsPending());
5669 Location dest = destination_;
5670 destination_ = Location::NoLocation();
5671 return dest;
5672 }
5673
5674 void ClearPending(Location dest) {
5675 DCHECK(IsPending());
5676 destination_ = dest;
5677 }
5678
5679 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005680 DCHECK(source_.IsValid() || destination_.IsInvalid());
5681 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005682 }
5683
5684 // True if this blocks a move from the given location.
5685 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005686 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005687 }
5688
5689 // A move is redundant if it's been eliminated, if its source and
5690 // destination are the same, or if its destination is unneeded.
5691 bool IsRedundant() const {
5692 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5693 }
5694
5695 // We clear both operands to indicate move that's been eliminated.
5696 void Eliminate() {
5697 source_ = destination_ = Location::NoLocation();
5698 }
5699
5700 bool IsEliminated() const {
5701 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5702 return source_.IsInvalid();
5703 }
5704
Alexey Frunze4dda3372015-06-01 18:31:49 -07005705 Primitive::Type GetType() const { return type_; }
5706
Nicolas Geoffray90218252015-04-15 11:56:51 +01005707 bool Is64BitMove() const {
5708 return Primitive::Is64BitType(type_);
5709 }
5710
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005711 HInstruction* GetInstruction() const { return instruction_; }
5712
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005713 private:
5714 Location source_;
5715 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005716 // The type this move is for.
5717 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005718 // The instruction this move is assocatied with. Null when this move is
5719 // for moving an input in the expected locations of user (including a phi user).
5720 // This is only used in debug mode, to ensure we do not connect interval siblings
5721 // in the same parallel move.
5722 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005723};
5724
Roland Levillainc9285912015-12-18 10:38:42 +00005725std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5726
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005727static constexpr size_t kDefaultNumberOfMoves = 4;
5728
5729class HParallelMove : public HTemplateInstruction<0> {
5730 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005731 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005732 : HTemplateInstruction(SideEffects::None(), dex_pc),
5733 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5734 moves_.reserve(kDefaultNumberOfMoves);
5735 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005736
Nicolas Geoffray90218252015-04-15 11:56:51 +01005737 void AddMove(Location source,
5738 Location destination,
5739 Primitive::Type type,
5740 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005741 DCHECK(source.IsValid());
5742 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005743 if (kIsDebugBuild) {
5744 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005745 for (const MoveOperands& move : moves_) {
5746 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005747 // Special case the situation where the move is for the spill slot
5748 // of the instruction.
5749 if ((GetPrevious() == instruction)
5750 || ((GetPrevious() == nullptr)
5751 && instruction->IsPhi()
5752 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005753 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005754 << "Doing parallel moves for the same instruction.";
5755 } else {
5756 DCHECK(false) << "Doing parallel moves for the same instruction.";
5757 }
5758 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005759 }
5760 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005761 for (const MoveOperands& move : moves_) {
5762 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005763 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005764 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005765 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005766 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005767 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005768 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005769 }
5770
Vladimir Marko225b6462015-09-28 12:17:40 +01005771 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005772 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005773 }
5774
Vladimir Marko225b6462015-09-28 12:17:40 +01005775 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005776
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005777 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005778
5779 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005780 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005781
5782 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5783};
5784
Mark Mendell0616ae02015-04-17 12:49:27 -04005785} // namespace art
5786
Vladimir Markob4536b72015-11-24 13:45:23 +00005787#ifdef ART_ENABLE_CODEGEN_arm
5788#include "nodes_arm.h"
5789#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005790#ifdef ART_ENABLE_CODEGEN_arm64
5791#include "nodes_arm64.h"
5792#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005793#ifdef ART_ENABLE_CODEGEN_x86
5794#include "nodes_x86.h"
5795#endif
5796
5797namespace art {
5798
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005799class HGraphVisitor : public ValueObject {
5800 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005801 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5802 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005803
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005804 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005805 virtual void VisitBasicBlock(HBasicBlock* block);
5806
Roland Levillain633021e2014-10-01 14:12:25 +01005807 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005808 void VisitInsertionOrder();
5809
Roland Levillain633021e2014-10-01 14:12:25 +01005810 // Visit the graph following dominator tree reverse post-order.
5811 void VisitReversePostOrder();
5812
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005813 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005814
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005815 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005816#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005817 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5818
5819 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5820
5821#undef DECLARE_VISIT_INSTRUCTION
5822
5823 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005824 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005825
5826 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5827};
5828
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005829class HGraphDelegateVisitor : public HGraphVisitor {
5830 public:
5831 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5832 virtual ~HGraphDelegateVisitor() {}
5833
5834 // Visit functions that delegate to to super class.
5835#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005836 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005837
5838 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5839
5840#undef DECLARE_VISIT_INSTRUCTION
5841
5842 private:
5843 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5844};
5845
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005846class HInsertionOrderIterator : public ValueObject {
5847 public:
5848 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5849
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005850 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005851 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005852 void Advance() { ++index_; }
5853
5854 private:
5855 const HGraph& graph_;
5856 size_t index_;
5857
5858 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5859};
5860
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005861class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005862 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005863 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5864 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005865 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005866 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005867
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005868 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5869 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005870 void Advance() { ++index_; }
5871
5872 private:
5873 const HGraph& graph_;
5874 size_t index_;
5875
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005876 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005877};
5878
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005879class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005880 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005881 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005882 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005883 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005884 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005885 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005886
5887 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005888 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005889 void Advance() { --index_; }
5890
5891 private:
5892 const HGraph& graph_;
5893 size_t index_;
5894
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005895 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005896};
5897
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005898class HLinearPostOrderIterator : public ValueObject {
5899 public:
5900 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005901 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005902
5903 bool Done() const { return index_ == 0; }
5904
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005905 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005906
5907 void Advance() {
5908 --index_;
5909 DCHECK_GE(index_, 0U);
5910 }
5911
5912 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005913 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005914 size_t index_;
5915
5916 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5917};
5918
5919class HLinearOrderIterator : public ValueObject {
5920 public:
5921 explicit HLinearOrderIterator(const HGraph& graph)
5922 : order_(graph.GetLinearOrder()), index_(0) {}
5923
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005924 bool Done() const { return index_ == order_.size(); }
5925 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005926 void Advance() { ++index_; }
5927
5928 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005929 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005930 size_t index_;
5931
5932 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5933};
5934
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005935// Iterator over the blocks that art part of the loop. Includes blocks part
5936// of an inner loop. The order in which the blocks are iterated is on their
5937// block id.
5938class HBlocksInLoopIterator : public ValueObject {
5939 public:
5940 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5941 : blocks_in_loop_(info.GetBlocks()),
5942 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5943 index_(0) {
5944 if (!blocks_in_loop_.IsBitSet(index_)) {
5945 Advance();
5946 }
5947 }
5948
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005949 bool Done() const { return index_ == blocks_.size(); }
5950 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005951 void Advance() {
5952 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005953 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005954 if (blocks_in_loop_.IsBitSet(index_)) {
5955 break;
5956 }
5957 }
5958 }
5959
5960 private:
5961 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005962 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005963 size_t index_;
5964
5965 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5966};
5967
Mingyao Yang3584bce2015-05-19 16:01:59 -07005968// Iterator over the blocks that art part of the loop. Includes blocks part
5969// of an inner loop. The order in which the blocks are iterated is reverse
5970// post order.
5971class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5972 public:
5973 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5974 : blocks_in_loop_(info.GetBlocks()),
5975 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5976 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005977 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005978 Advance();
5979 }
5980 }
5981
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005982 bool Done() const { return index_ == blocks_.size(); }
5983 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005984 void Advance() {
5985 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005986 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5987 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005988 break;
5989 }
5990 }
5991 }
5992
5993 private:
5994 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005995 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005996 size_t index_;
5997
5998 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5999};
6000
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006001inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006002 if (constant->IsIntConstant()) {
6003 return constant->AsIntConstant()->GetValue();
6004 } else if (constant->IsLongConstant()) {
6005 return constant->AsLongConstant()->GetValue();
6006 } else {
6007 DCHECK(constant->IsNullConstant());
6008 return 0;
6009 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006010}
6011
Vladimir Marko58155012015-08-19 12:49:41 +00006012inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6013 // For the purposes of the compiler, the dex files must actually be the same object
6014 // if we want to safely treat them as the same. This is especially important for JIT
6015 // as custom class loaders can open the same underlying file (or memory) multiple
6016 // times and provide different class resolution but no two class loaders should ever
6017 // use the same DexFile object - doing so is an unsupported hack that can lead to
6018 // all sorts of weird failures.
6019 return &lhs == &rhs;
6020}
6021
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006022#define INSTRUCTION_TYPE_CHECK(type, super) \
6023 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6024 inline const H##type* HInstruction::As##type() const { \
6025 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6026 } \
6027 inline H##type* HInstruction::As##type() { \
6028 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6029 }
6030
6031 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6032#undef INSTRUCTION_TYPE_CHECK
6033
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006034class SwitchTable : public ValueObject {
6035 public:
6036 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6037 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6038 int32_t table_offset = instruction.VRegB_31t();
6039 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6040 if (sparse) {
6041 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6042 } else {
6043 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6044 }
6045 num_entries_ = table[1];
6046 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6047 }
6048
6049 uint16_t GetNumEntries() const {
6050 return num_entries_;
6051 }
6052
6053 void CheckIndex(size_t index) const {
6054 if (sparse_) {
6055 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6056 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6057 } else {
6058 // In a packed table, we have the starting key and num_entries_ values.
6059 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6060 }
6061 }
6062
6063 int32_t GetEntryAt(size_t index) const {
6064 CheckIndex(index);
6065 return values_[index];
6066 }
6067
6068 uint32_t GetDexPcForIndex(size_t index) const {
6069 CheckIndex(index);
6070 return dex_pc_ +
6071 (reinterpret_cast<const int16_t*>(values_ + index) -
6072 reinterpret_cast<const int16_t*>(&instruction_));
6073 }
6074
6075 // Index of the first value in the table.
6076 size_t GetFirstValueIndex() const {
6077 if (sparse_) {
6078 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6079 return num_entries_;
6080 } else {
6081 // In a packed table, we have the starting key and num_entries_ values.
6082 return 1;
6083 }
6084 }
6085
6086 private:
6087 const Instruction& instruction_;
6088 const uint32_t dex_pc_;
6089
6090 // Whether this is a sparse-switch table (or a packed-switch one).
6091 const bool sparse_;
6092
6093 // This can't be const as it needs to be computed off of the given instruction, and complicated
6094 // expressions in the initializer list seemed very ugly.
6095 uint16_t num_entries_;
6096
6097 const int32_t* values_;
6098
6099 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6100};
6101
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006102} // namespace art
6103
6104#endif // ART_COMPILER_OPTIMIZING_NODES_H_