blob: 2697af33eb59b8dd107b5d870cf8453fb22f9cdd [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 {
101 kAnalysisFailThrowCatchLoop,
102 kAnalysisFailAmbiguousArrayOp,
103 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000104};
105
Vladimir Markof9f64412015-09-02 14:05:49 +0100106class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 public:
108 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
109
110 void AddInstruction(HInstruction* instruction);
111 void RemoveInstruction(HInstruction* instruction);
112
David Brazdilc3d743f2015-04-22 13:40:50 +0100113 // Insert `instruction` before/after an existing instruction `cursor`.
114 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
115 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
116
Roland Levillain6b469232014-09-25 10:10:38 +0100117 // Return true if this list contains `instruction`.
118 bool Contains(HInstruction* instruction) const;
119
Roland Levillainccc07a92014-09-16 14:48:16 +0100120 // Return true if `instruction1` is found before `instruction2` in
121 // this instruction list and false otherwise. Abort if none
122 // of these instructions is found.
123 bool FoundBefore(const HInstruction* instruction1,
124 const HInstruction* instruction2) const;
125
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000126 bool IsEmpty() const { return first_instruction_ == nullptr; }
127 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
128
129 // Update the block of all instructions to be `block`.
130 void SetBlockOfInstructions(HBasicBlock* block) const;
131
132 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
133 void Add(const HInstructionList& instruction_list);
134
David Brazdil2d7352b2015-04-20 14:52:42 +0100135 // Return the number of instructions in the list. This is an expensive operation.
136 size_t CountSize() const;
137
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 private:
139 HInstruction* first_instruction_;
140 HInstruction* last_instruction_;
141
142 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 friend class HGraph;
144 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100146 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147
148 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
149};
150
David Brazdil4833f5a2015-12-16 10:37:39 +0000151class ReferenceTypeInfo : ValueObject {
152 public:
153 typedef Handle<mirror::Class> TypeHandle;
154
155 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
156 // The constructor will check that the type_handle is valid.
157 return ReferenceTypeInfo(type_handle, is_exact);
158 }
159
160 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
161
Vladimir Markof39745e2016-01-26 12:16:55 +0000162 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000163 return handle.GetReference() != nullptr;
164 }
165
166 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
167 return IsValidHandle(type_handle_);
168 }
169
170 bool IsExact() const { return is_exact_; }
171
172 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
173 DCHECK(IsValid());
174 return GetTypeHandle()->IsObjectClass();
175 }
176
177 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
178 DCHECK(IsValid());
179 return GetTypeHandle()->IsStringClass();
180 }
181
182 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
183 DCHECK(IsValid());
184 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
185 }
186
187 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
188 DCHECK(IsValid());
189 return GetTypeHandle()->IsInterface();
190 }
191
192 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
193 DCHECK(IsValid());
194 return GetTypeHandle()->IsArrayClass();
195 }
196
197 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
198 DCHECK(IsValid());
199 return GetTypeHandle()->IsPrimitiveArray();
200 }
201
202 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
203 DCHECK(IsValid());
204 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
205 }
206
207 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
208 DCHECK(IsValid());
209 if (!IsExact()) return false;
210 if (!IsArrayClass()) return false;
211 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
212 }
213
214 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 if (!IsExact()) return false;
217 if (!IsArrayClass()) return false;
218 if (!rti.IsArrayClass()) return false;
219 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
220 rti.GetTypeHandle()->GetComponentType());
221 }
222
223 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
224
225 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
226 DCHECK(IsValid());
227 DCHECK(rti.IsValid());
228 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
229 }
230
231 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
232 DCHECK(IsValid());
233 DCHECK(rti.IsValid());
234 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
235 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
236 }
237
238 // Returns true if the type information provide the same amount of details.
239 // Note that it does not mean that the instructions have the same actual type
240 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000241 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 if (!IsValid() && !rti.IsValid()) {
243 // Invalid types are equal.
244 return true;
245 }
246 if (!IsValid() || !rti.IsValid()) {
247 // One is valid, the other not.
248 return false;
249 }
250 return IsExact() == rti.IsExact()
251 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
252 }
253
254 private:
255 ReferenceTypeInfo();
256 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
257
258 // The class of the object.
259 TypeHandle type_handle_;
260 // Whether or not the type is exact or a superclass of the actual type.
261 // Whether or not we have any information about this type.
262 bool is_exact_;
263};
264
265std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
266
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000267// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100268class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000269 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100270 HGraph(ArenaAllocator* arena,
271 const DexFile& dex_file,
272 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100273 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100275 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100276 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000277 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100278 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000279 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100280 blocks_(arena->Adapter(kArenaAllocBlockList)),
281 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
282 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283 entry_block_(nullptr),
284 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100285 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100286 number_of_vregs_(0),
287 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000288 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400289 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000290 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000291 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000292 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000293 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100294 dex_file_(dex_file),
295 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100296 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100297 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100298 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000300 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100301 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
302 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
303 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
304 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000305 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000306 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
307 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 blocks_.reserve(kDefaultNumberOfBlocks);
309 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000311 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100312 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
313
David Brazdil69ba7b72015-06-23 18:27:30 +0100314 bool IsInSsaForm() const { return in_ssa_form_; }
315
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000316 HBasicBlock* GetEntryBlock() const { return entry_block_; }
317 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100318 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000319
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000320 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
321 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000322
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000323 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100324
David Brazdil4833f5a2015-12-16 10:37:39 +0000325 // Try building the SSA form of this graph, with dominance computation and
326 // loop recognition. Returns a code specifying that it was successful or the
327 // reason for failure.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000328 GraphAnalysisResult TryBuildingSsa(StackHandleScopeCollection* handles);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000329
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.
Calin Juravle2e768302015-07-28 14:41:11 +0000348 // Returns the instruction used to replace the invoke expression or null if the
349 // invoke is for a void method.
350 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000351
Mingyao Yang3584bce2015-05-19 16:01:59 -0700352 // Need to add a couple of blocks to test if the loop body is entered and
353 // put deoptimization instructions, etc.
354 void TransformLoopHeaderForBCE(HBasicBlock* header);
355
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000356 // Removes `block` from the graph. Assumes `block` has been disconnected from
357 // other blocks and has no instructions or phis.
358 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000359
David Brazdilfc6a86a2015-06-26 10:33:45 +0000360 // Splits the edge between `block` and `successor` while preserving the
361 // indices in the predecessor/successor lists. If there are multiple edges
362 // between the blocks, the lowest indices are used.
363 // Returns the new block which is empty and has the same dex pc as `successor`.
364 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
365
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100366 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
367 void SimplifyLoop(HBasicBlock* header);
368
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000369 int32_t GetNextInstructionId() {
370 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000371 return current_instruction_id_++;
372 }
373
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000374 int32_t GetCurrentInstructionId() const {
375 return current_instruction_id_;
376 }
377
378 void SetCurrentInstructionId(int32_t id) {
379 current_instruction_id_ = id;
380 }
381
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100382 uint16_t GetMaximumNumberOfOutVRegs() const {
383 return maximum_number_of_out_vregs_;
384 }
385
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000386 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
387 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100388 }
389
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100390 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
391 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
392 }
393
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000394 void UpdateTemporariesVRegSlots(size_t slots) {
395 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100396 }
397
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000398 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100399 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000400 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100401 }
402
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100403 void SetNumberOfVRegs(uint16_t number_of_vregs) {
404 number_of_vregs_ = number_of_vregs;
405 }
406
407 uint16_t GetNumberOfVRegs() const {
408 return number_of_vregs_;
409 }
410
411 void SetNumberOfInVRegs(uint16_t value) {
412 number_of_in_vregs_ = value;
413 }
414
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100415 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100416 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100417 return number_of_vregs_ - number_of_in_vregs_;
418 }
419
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100420 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100421 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100422 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100423
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100424 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100425 return linear_order_;
426 }
427
Mark Mendell1152c922015-04-24 17:06:35 -0400428 bool HasBoundsChecks() const {
429 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800430 }
431
Mark Mendell1152c922015-04-24 17:06:35 -0400432 void SetHasBoundsChecks(bool value) {
433 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800434 }
435
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100436 bool ShouldGenerateConstructorBarrier() const {
437 return should_generate_constructor_barrier_;
438 }
439
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000440 bool IsDebuggable() const { return debuggable_; }
441
David Brazdil8d5b8b22015-03-24 10:51:52 +0000442 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000443 // already, it is created and inserted into the graph. This method is only for
444 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600445 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000446
447 // TODO: This is problematic for the consistency of reference type propagation
448 // because it can be created anytime after the pass and thus it will be left
449 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600450 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000451
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600452 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
453 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000454 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600455 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
456 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000457 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600458 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
459 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000460 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600461 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
462 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000463 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000464
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100465 HCurrentMethod* GetCurrentMethod();
466
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100467 const DexFile& GetDexFile() const {
468 return dex_file_;
469 }
470
471 uint32_t GetMethodIdx() const {
472 return method_idx_;
473 }
474
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100475 InvokeType GetInvokeType() const {
476 return invoke_type_;
477 }
478
Mark Mendellc4701932015-04-10 13:18:51 -0400479 InstructionSet GetInstructionSet() const {
480 return instruction_set_;
481 }
482
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000483 bool IsCompilingOsr() const { return osr_; }
484
David Brazdil77a48ae2015-09-15 12:34:04 +0000485 bool HasTryCatch() const { return has_try_catch_; }
486 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100487
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000488 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
489 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
490
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100491 ArtMethod* GetArtMethod() const { return art_method_; }
492 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
493
Mark Mendellf6529172015-11-17 11:16:56 -0500494 // Returns an instruction with the opposite boolean value from 'cond'.
495 // The instruction has been inserted into the graph, either as a constant, or
496 // before cursor.
497 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
498
David Brazdil2d7352b2015-04-20 14:52:42 +0100499 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000500 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100501 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000502
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000503 template <class InstructionType, typename ValueType>
504 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600505 ArenaSafeMap<ValueType, InstructionType*>* cache,
506 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000507 // Try to find an existing constant of the given value.
508 InstructionType* constant = nullptr;
509 auto cached_constant = cache->find(value);
510 if (cached_constant != cache->end()) {
511 constant = cached_constant->second;
512 }
513
514 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100515 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000516 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000518 cache->Overwrite(value, constant);
519 InsertConstant(constant);
520 }
521 return constant;
522 }
523
David Brazdil8d5b8b22015-03-24 10:51:52 +0000524 void InsertConstant(HConstant* instruction);
525
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000526 // Cache a float constant into the graph. This method should only be
527 // called by the SsaBuilder when creating "equivalent" instructions.
528 void CacheFloatConstant(HFloatConstant* constant);
529
530 // See CacheFloatConstant comment.
531 void CacheDoubleConstant(HDoubleConstant* constant);
532
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000533 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000534
535 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100536 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000537
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100539 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000540
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100541 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100542 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100543
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000544 HBasicBlock* entry_block_;
545 HBasicBlock* exit_block_;
546
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100547 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100548 uint16_t maximum_number_of_out_vregs_;
549
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100550 // The number of virtual registers in this method. Contains the parameters.
551 uint16_t number_of_vregs_;
552
553 // The number of virtual registers used by parameters of this method.
554 uint16_t number_of_in_vregs_;
555
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000556 // Number of vreg size slots that the temporaries use (used in baseline compiler).
557 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100558
Mark Mendell1152c922015-04-24 17:06:35 -0400559 // Has bounds checks. We can totally skip BCE if it's false.
560 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800561
David Brazdil77a48ae2015-09-15 12:34:04 +0000562 // Flag whether there are any try/catch blocks in the graph. We will skip
563 // try/catch-related passes if false.
564 bool has_try_catch_;
565
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000566 // Flag whether there are any irreducible loops in the graph.
567 bool has_irreducible_loops_;
568
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000569 // Indicates whether the graph should be compiled in a way that
570 // ensures full debuggability. If false, we can apply more
571 // aggressive optimizations that may limit the level of debugging.
572 const bool debuggable_;
573
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000574 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000575 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000576
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100577 // The dex file from which the method is from.
578 const DexFile& dex_file_;
579
580 // The method index in the dex file.
581 const uint32_t method_idx_;
582
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100583 // If inlined, this encodes how the callee is being invoked.
584 const InvokeType invoke_type_;
585
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100586 // Whether the graph has been transformed to SSA form. Only used
587 // in debug mode to ensure we are not using properties only valid
588 // for non-SSA form (like the number of temporaries).
589 bool in_ssa_form_;
590
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100591 const bool should_generate_constructor_barrier_;
592
Mathieu Chartiere401d142015-04-22 13:56:20 -0700593 const InstructionSet instruction_set_;
594
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000595 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000596 HNullConstant* cached_null_constant_;
597 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000598 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000599 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000600 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000601
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100602 HCurrentMethod* cached_current_method_;
603
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100604 // The ArtMethod this graph is for. Note that for AOT, it may be null,
605 // for example for methods whose declaring class could not be resolved
606 // (such as when the superclass could not be found).
607 ArtMethod* art_method_;
608
David Brazdil4833f5a2015-12-16 10:37:39 +0000609 // Keep the RTI of inexact Object to avoid having to pass stack handle
610 // collection pointer to passes which may create NullConstant.
611 ReferenceTypeInfo inexact_object_rti_;
612
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000613 // Whether we are compiling this graph for on stack replacement: this will
614 // make all loops seen as irreducible and emit special stack maps to mark
615 // compiled code entries which the interpreter can directly jump to.
616 const bool osr_;
617
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000618 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100619 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000620 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000621 DISALLOW_COPY_AND_ASSIGN(HGraph);
622};
623
Vladimir Markof9f64412015-09-02 14:05:49 +0100624class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000625 public:
626 HLoopInformation(HBasicBlock* header, HGraph* graph)
627 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100628 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000629 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100630 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100631 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100632 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
633 back_edges_.reserve(kDefaultNumberOfBackEdges);
634 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100635
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000636 bool IsIrreducible() const { return irreducible_; }
637
638 void Dump(std::ostream& os);
639
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100640 HBasicBlock* GetHeader() const {
641 return header_;
642 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000644 void SetHeader(HBasicBlock* block) {
645 header_ = block;
646 }
647
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100648 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
649 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
650 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
651
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000652 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000654 }
655
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100656 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100657 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658 }
659
David Brazdil46e2a392015-03-16 17:31:52 +0000660 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100661 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 }
663
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000664 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100665 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000666 }
667
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100668 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100669
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100670 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100671 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100672 }
673
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100674 // Returns the lifetime position of the back edge that has the
675 // greatest lifetime position.
676 size_t GetLifetimeEnd() const;
677
678 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100680 }
681
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000682 // Finds blocks that are part of this loop.
683 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100684
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 // Returns whether this loop information contains `block`.
686 // Note that this loop information *must* be populated before entering this function.
687 bool Contains(const HBasicBlock& block) const;
688
689 // Returns whether this loop information is an inner loop of `other`.
690 // Note that `other` *must* be populated before entering this function.
691 bool IsIn(const HLoopInformation& other) const;
692
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800693 // Returns true if instruction is not defined within this loop.
694 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700695
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 const ArenaBitVector& GetBlocks() const { return blocks_; }
697
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000698 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000699 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000700
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000701 void ClearAllBlocks() {
702 blocks_.ClearAllBits();
703 }
704
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000705 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100706 // Internal recursive implementation of `Populate`.
707 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000708 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100709
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000710 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100711 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000712 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100713 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100714 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000715
716 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
717};
718
David Brazdilec16f792015-08-19 15:04:01 +0100719// Stores try/catch information for basic blocks.
720// Note that HGraph is constructed so that catch blocks cannot simultaneously
721// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100722class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100723 public:
724 // Try block information constructor.
725 explicit TryCatchInformation(const HTryBoundary& try_entry)
726 : try_entry_(&try_entry),
727 catch_dex_file_(nullptr),
728 catch_type_index_(DexFile::kDexNoIndex16) {
729 DCHECK(try_entry_ != nullptr);
730 }
731
732 // Catch block information constructor.
733 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
734 : try_entry_(nullptr),
735 catch_dex_file_(&dex_file),
736 catch_type_index_(catch_type_index) {}
737
738 bool IsTryBlock() const { return try_entry_ != nullptr; }
739
740 const HTryBoundary& GetTryEntry() const {
741 DCHECK(IsTryBlock());
742 return *try_entry_;
743 }
744
745 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
746
747 bool IsCatchAllTypeIndex() const {
748 DCHECK(IsCatchBlock());
749 return catch_type_index_ == DexFile::kDexNoIndex16;
750 }
751
752 uint16_t GetCatchTypeIndex() const {
753 DCHECK(IsCatchBlock());
754 return catch_type_index_;
755 }
756
757 const DexFile& GetCatchDexFile() const {
758 DCHECK(IsCatchBlock());
759 return *catch_dex_file_;
760 }
761
762 private:
763 // One of possibly several TryBoundary instructions entering the block's try.
764 // Only set for try blocks.
765 const HTryBoundary* try_entry_;
766
767 // Exception type information. Only set for catch blocks.
768 const DexFile* catch_dex_file_;
769 const uint16_t catch_type_index_;
770};
771
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100772static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100773static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100774
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000775// A block in a method. Contains the list of instructions represented
776// as a double linked list. Each block knows its predecessors and
777// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100778
Vladimir Markof9f64412015-09-02 14:05:49 +0100779class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600781 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000782 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000783 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
784 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000785 loop_information_(nullptr),
786 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000787 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100788 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100789 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100790 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000791 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000792 try_catch_information_(nullptr) {
793 predecessors_.reserve(kDefaultNumberOfPredecessors);
794 successors_.reserve(kDefaultNumberOfSuccessors);
795 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
796 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000797
Vladimir Marko60584552015-09-03 13:35:12 +0000798 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100799 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800 }
801
Vladimir Marko60584552015-09-03 13:35:12 +0000802 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100803 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000804 }
805
David Brazdild26a4112015-11-10 11:07:31 +0000806 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
807 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
808
Vladimir Marko60584552015-09-03 13:35:12 +0000809 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
810 return ContainsElement(successors_, block, start_from);
811 }
812
813 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100814 return dominated_blocks_;
815 }
816
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100817 bool IsEntryBlock() const {
818 return graph_->GetEntryBlock() == this;
819 }
820
821 bool IsExitBlock() const {
822 return graph_->GetExitBlock() == this;
823 }
824
David Brazdil46e2a392015-03-16 17:31:52 +0000825 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000826 bool IsSingleTryBoundary() const;
827
828 // Returns true if this block emits nothing but a jump.
829 bool IsSingleJump() const {
830 HLoopInformation* loop_info = GetLoopInformation();
831 return (IsSingleGoto() || IsSingleTryBoundary())
832 // Back edges generate a suspend check.
833 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
834 }
David Brazdil46e2a392015-03-16 17:31:52 +0000835
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000836 void AddBackEdge(HBasicBlock* back_edge) {
837 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000838 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000839 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100840 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000841 loop_information_->AddBackEdge(back_edge);
842 }
843
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000844 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000845 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000846
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100847 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000848 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600849 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000850
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000851 HBasicBlock* GetDominator() const { return dominator_; }
852 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000853 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
854
855 void RemoveDominatedBlock(HBasicBlock* block) {
856 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100857 }
Vladimir Marko60584552015-09-03 13:35:12 +0000858
859 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
860 ReplaceElement(dominated_blocks_, existing, new_block);
861 }
862
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100863 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000864
865 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100866 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000867 }
868
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100869 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
870 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100871 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100872 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100873 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
874 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000875
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000876 HInstruction* GetFirstInstructionDisregardMoves() const;
877
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000878 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000879 successors_.push_back(block);
880 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000881 }
882
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100883 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
884 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100885 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000886 new_block->predecessors_.push_back(this);
887 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000888 }
889
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000890 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
891 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000892 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000893 new_block->successors_.push_back(this);
894 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000895 }
896
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100897 // Insert `this` between `predecessor` and `successor. This method
898 // preserves the indicies, and will update the first edge found between
899 // `predecessor` and `successor`.
900 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
901 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100902 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000903 successor->predecessors_[predecessor_index] = this;
904 predecessor->successors_[successor_index] = this;
905 successors_.push_back(successor);
906 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100907 }
908
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100909 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000910 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100911 }
912
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000913 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000914 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000915 }
916
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100917 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000918 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100919 }
920
921 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000922 predecessors_.push_back(block);
923 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100924 }
925
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100926 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000927 DCHECK_EQ(predecessors_.size(), 2u);
928 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100929 }
930
David Brazdil769c9e52015-04-27 13:54:09 +0100931 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 DCHECK_EQ(successors_.size(), 2u);
933 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100934 }
935
David Brazdilfc6a86a2015-06-26 10:33:45 +0000936 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000937 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100938 }
939
David Brazdilfc6a86a2015-06-26 10:33:45 +0000940 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000941 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100942 }
943
David Brazdilfc6a86a2015-06-26 10:33:45 +0000944 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000945 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100946 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000947 }
948
949 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100951 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000952 }
953
954 // Returns whether the first occurrence of `predecessor` in the list of
955 // predecessors is at index `idx`.
956 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100957 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 return GetPredecessorIndexOf(predecessor) == idx;
959 }
960
David Brazdild7558da2015-09-22 13:04:14 +0100961 // Create a new block between this block and its predecessors. The new block
962 // is added to the graph, all predecessor edges are relinked to it and an edge
963 // is created to `this`. Returns the new empty block. Reverse post order or
964 // loop and try/catch information are not updated.
965 HBasicBlock* CreateImmediateDominator();
966
David Brazdilfc6a86a2015-06-26 10:33:45 +0000967 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100968 // created, latter block. Note that this method will add the block to the
969 // graph, create a Goto at the end of the former block and will create an edge
970 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100971 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 HBasicBlock* SplitBefore(HInstruction* cursor);
973
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000974 // Split the block into two blocks just after `cursor`. Returns the newly
975 // created block. Note that this method just updates raw block information,
976 // like predecessors, successors, dominators, and instruction list. It does not
977 // update the graph, reverse post order, loop information, nor make sure the
978 // blocks are consistent (for example ending with a control flow instruction).
979 HBasicBlock* SplitAfter(HInstruction* cursor);
980
David Brazdil9bc43612015-11-05 21:25:24 +0000981 // Split catch block into two blocks after the original move-exception bytecode
982 // instruction, or at the beginning if not present. Returns the newly created,
983 // latter block, or nullptr if such block could not be created (must be dead
984 // in that case). Note that this method just updates raw block information,
985 // like predecessors, successors, dominators, and instruction list. It does not
986 // update the graph, reverse post order, loop information, nor make sure the
987 // blocks are consistent (for example ending with a control flow instruction).
988 HBasicBlock* SplitCatchBlockAfterMoveException();
989
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000990 // Merge `other` at the end of `this`. Successors and dominated blocks of
991 // `other` are changed to be successors and dominated blocks of `this`. Note
992 // that this method does not update the graph, reverse post order, loop
993 // information, nor make sure the blocks are consistent (for example ending
994 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100995 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000996
997 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
998 // of `this` are moved to `other`.
999 // Note that this method does not update the graph, reverse post order, loop
1000 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001001 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001002 void ReplaceWith(HBasicBlock* other);
1003
David Brazdil2d7352b2015-04-20 14:52:42 +01001004 // Merge `other` at the end of `this`. This method updates loops, reverse post
1005 // order, links to predecessors, successors, dominators and deletes the block
1006 // from the graph. The two blocks must be successive, i.e. `this` the only
1007 // predecessor of `other` and vice versa.
1008 void MergeWith(HBasicBlock* other);
1009
1010 // Disconnects `this` from all its predecessors, successors and dominator,
1011 // removes it from all loops it is included in and eventually from the graph.
1012 // The block must not dominate any other block. Predecessors and successors
1013 // are safely updated.
1014 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001015
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001017 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001018 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001019 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001020 // Replace instruction `initial` with `replacement` within this block.
1021 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1022 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001023 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001024 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001025 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001026 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1027 // instruction list. With 'ensure_safety' set to true, it verifies that the
1028 // instruction is not in use and removes it from the use lists of its inputs.
1029 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1030 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001031 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001032
1033 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001034 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001035 }
1036
Roland Levillain6b879dd2014-09-22 17:13:44 +01001037 bool IsLoopPreHeaderFirstPredecessor() const {
1038 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001039 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001040 }
1041
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001042 bool IsFirstPredecessorBackEdge() const {
1043 DCHECK(IsLoopHeader());
1044 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1045 }
1046
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001047 HLoopInformation* GetLoopInformation() const {
1048 return loop_information_;
1049 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001050
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001051 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001052 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001053 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001054 void SetInLoop(HLoopInformation* info) {
1055 if (IsLoopHeader()) {
1056 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001057 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001058 loop_information_ = info;
1059 } else if (loop_information_->Contains(*info->GetHeader())) {
1060 // Block is currently part of an outer loop. Make it part of this inner loop.
1061 // Note that a non loop header having a loop information means this loop information
1062 // has already been populated
1063 loop_information_ = info;
1064 } else {
1065 // Block is part of an inner loop. Do not update the loop information.
1066 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1067 // at this point, because this method is being called while populating `info`.
1068 }
1069 }
1070
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001071 // Raw update of the loop information.
1072 void SetLoopInformation(HLoopInformation* info) {
1073 loop_information_ = info;
1074 }
1075
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001076 bool IsInLoop() const { return loop_information_ != nullptr; }
1077
David Brazdilec16f792015-08-19 15:04:01 +01001078 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1079
1080 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1081 try_catch_information_ = try_catch_information;
1082 }
1083
1084 bool IsTryBlock() const {
1085 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1086 }
1087
1088 bool IsCatchBlock() const {
1089 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1090 }
David Brazdilffee3d32015-07-06 11:48:53 +01001091
1092 // Returns the try entry that this block's successors should have. They will
1093 // be in the same try, unless the block ends in a try boundary. In that case,
1094 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001095 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001096
David Brazdild7558da2015-09-22 13:04:14 +01001097 bool HasThrowingInstructions() const;
1098
David Brazdila4b8c212015-05-07 09:59:30 +01001099 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001100 bool Dominates(HBasicBlock* block) const;
1101
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001102 size_t GetLifetimeStart() const { return lifetime_start_; }
1103 size_t GetLifetimeEnd() const { return lifetime_end_; }
1104
1105 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1106 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1107
David Brazdil8d5b8b22015-03-24 10:51:52 +00001108 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001109 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001110 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001111 bool HasSinglePhi() const;
1112
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001113 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001114 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001115 ArenaVector<HBasicBlock*> predecessors_;
1116 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001117 HInstructionList instructions_;
1118 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001119 HLoopInformation* loop_information_;
1120 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001121 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001122 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001123 // The dex program counter of the first instruction of this block.
1124 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001125 size_t lifetime_start_;
1126 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001127 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001128
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001129 friend class HGraph;
1130 friend class HInstruction;
1131
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001132 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1133};
1134
David Brazdilb2bd1c52015-03-25 11:17:37 +00001135// Iterates over the LoopInformation of all loops which contain 'block'
1136// from the innermost to the outermost.
1137class HLoopInformationOutwardIterator : public ValueObject {
1138 public:
1139 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1140 : current_(block.GetLoopInformation()) {}
1141
1142 bool Done() const { return current_ == nullptr; }
1143
1144 void Advance() {
1145 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001146 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001147 }
1148
1149 HLoopInformation* Current() const {
1150 DCHECK(!Done());
1151 return current_;
1152 }
1153
1154 private:
1155 HLoopInformation* current_;
1156
1157 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1158};
1159
Alexandre Ramesef20f712015-06-09 10:29:30 +01001160#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001161 M(Above, Condition) \
1162 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001163 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001164 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001165 M(ArrayGet, Instruction) \
1166 M(ArrayLength, Instruction) \
1167 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001168 M(Below, Condition) \
1169 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001170 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001171 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001172 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001173 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001174 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001175 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001176 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001177 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001178 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001179 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001180 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001181 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001182 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001183 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001184 M(Exit, Instruction) \
1185 M(FloatConstant, Constant) \
1186 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001187 M(GreaterThan, Condition) \
1188 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001189 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001190 M(InstanceFieldGet, Instruction) \
1191 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001192 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001193 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001194 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001195 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001196 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001197 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001198 M(LessThan, Condition) \
1199 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001200 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001201 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001202 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001203 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(Local, Instruction) \
1205 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001206 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001207 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001208 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001209 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 M(Neg, UnaryOperation) \
1211 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001212 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001213 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001214 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001215 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001216 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001217 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001218 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001220 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001222 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001223 M(Return, Instruction) \
1224 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001225 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001226 M(Shl, BinaryOperation) \
1227 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001228 M(StaticFieldGet, Instruction) \
1229 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001230 M(UnresolvedInstanceFieldGet, Instruction) \
1231 M(UnresolvedInstanceFieldSet, Instruction) \
1232 M(UnresolvedStaticFieldGet, Instruction) \
1233 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001234 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001235 M(StoreLocal, Instruction) \
1236 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001237 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001238 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001239 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001240 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001241 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001242 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001243
Vladimir Markob4536b72015-11-24 13:45:23 +00001244#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001245#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001246#else
1247#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1248 M(ArmDexCacheArraysBase, Instruction)
1249#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001250
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001251#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001252#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001253#else
1254#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001255 M(Arm64DataProcWithShifterOp, Instruction) \
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00001256 M(Arm64IntermediateAddress, Instruction) \
1257 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001258#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001259
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001260#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1261
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001262#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1263
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001264#ifndef ART_ENABLE_CODEGEN_x86
1265#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1266#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001267#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1268 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001269 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001270 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001271 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001272#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001273
1274#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1275
1276#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1277 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1278 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1279 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001280 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001281 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001282 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1283 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1284
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001285#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1286 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001287 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001288 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001289 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001290 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001291
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001292#define FOR_EACH_INSTRUCTION(M) \
1293 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1294 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1295
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001296#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001297FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1298#undef FORWARD_DECLARATION
1299
Roland Levillainccc07a92014-09-16 14:48:16 +01001300#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001301 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001302 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001303 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001304 return other->Is##type(); \
1305 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001306 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001307
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001308#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1309 bool Is##type() const { return As##type() != nullptr; } \
1310 const H##type* As##type() const { return this; } \
1311 H##type* As##type() { return this; }
1312
David Brazdiled596192015-01-23 10:39:45 +00001313template <typename T> class HUseList;
1314
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001315template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001316class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001317 public:
David Brazdiled596192015-01-23 10:39:45 +00001318 HUseListNode* GetPrevious() const { return prev_; }
1319 HUseListNode* GetNext() const { return next_; }
1320 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001321 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001322 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001323
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001324 private:
David Brazdiled596192015-01-23 10:39:45 +00001325 HUseListNode(T user, size_t index)
1326 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1327
1328 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001329 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001330 HUseListNode<T>* prev_;
1331 HUseListNode<T>* next_;
1332
1333 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001334
1335 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1336};
1337
David Brazdiled596192015-01-23 10:39:45 +00001338template <typename T>
1339class HUseList : public ValueObject {
1340 public:
1341 HUseList() : first_(nullptr) {}
1342
1343 void Clear() {
1344 first_ = nullptr;
1345 }
1346
1347 // Adds a new entry at the beginning of the use list and returns
1348 // the newly created node.
1349 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001350 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001351 if (IsEmpty()) {
1352 first_ = new_node;
1353 } else {
1354 first_->prev_ = new_node;
1355 new_node->next_ = first_;
1356 first_ = new_node;
1357 }
1358 return new_node;
1359 }
1360
1361 HUseListNode<T>* GetFirst() const {
1362 return first_;
1363 }
1364
1365 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001366 DCHECK(node != nullptr);
1367 DCHECK(Contains(node));
1368
David Brazdiled596192015-01-23 10:39:45 +00001369 if (node->prev_ != nullptr) {
1370 node->prev_->next_ = node->next_;
1371 }
1372 if (node->next_ != nullptr) {
1373 node->next_->prev_ = node->prev_;
1374 }
1375 if (node == first_) {
1376 first_ = node->next_;
1377 }
1378 }
1379
David Brazdil1abb4192015-02-17 18:33:36 +00001380 bool Contains(const HUseListNode<T>* node) const {
1381 if (node == nullptr) {
1382 return false;
1383 }
1384 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1385 if (current == node) {
1386 return true;
1387 }
1388 }
1389 return false;
1390 }
1391
David Brazdiled596192015-01-23 10:39:45 +00001392 bool IsEmpty() const {
1393 return first_ == nullptr;
1394 }
1395
1396 bool HasOnlyOneUse() const {
1397 return first_ != nullptr && first_->next_ == nullptr;
1398 }
1399
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001400 size_t SizeSlow() const {
1401 size_t count = 0;
1402 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1403 ++count;
1404 }
1405 return count;
1406 }
1407
David Brazdiled596192015-01-23 10:39:45 +00001408 private:
1409 HUseListNode<T>* first_;
1410};
1411
1412template<typename T>
1413class HUseIterator : public ValueObject {
1414 public:
1415 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1416
1417 bool Done() const { return current_ == nullptr; }
1418
1419 void Advance() {
1420 DCHECK(!Done());
1421 current_ = current_->GetNext();
1422 }
1423
1424 HUseListNode<T>* Current() const {
1425 DCHECK(!Done());
1426 return current_;
1427 }
1428
1429 private:
1430 HUseListNode<T>* current_;
1431
1432 friend class HValue;
1433};
1434
David Brazdil1abb4192015-02-17 18:33:36 +00001435// This class is used by HEnvironment and HInstruction classes to record the
1436// instructions they use and pointers to the corresponding HUseListNodes kept
1437// by the used instructions.
1438template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001439class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001440 public:
1441 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1442 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1443
1444 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1445 : instruction_(old_record.instruction_), use_node_(use_node) {
1446 DCHECK(instruction_ != nullptr);
1447 DCHECK(use_node_ != nullptr);
1448 DCHECK(old_record.use_node_ == nullptr);
1449 }
1450
1451 HInstruction* GetInstruction() const { return instruction_; }
1452 HUseListNode<T>* GetUseNode() const { return use_node_; }
1453
1454 private:
1455 // Instruction used by the user.
1456 HInstruction* instruction_;
1457
1458 // Corresponding entry in the use list kept by 'instruction_'.
1459 HUseListNode<T>* use_node_;
1460};
1461
Aart Bik854a02b2015-07-14 16:07:00 -07001462/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001463 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001464 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001465 * For write/read dependences on fields/arrays, the dependence analysis uses
1466 * type disambiguation (e.g. a float field write cannot modify the value of an
1467 * integer field read) and the access type (e.g. a reference array write cannot
1468 * modify the value of a reference field read [although it may modify the
1469 * reference fetch prior to reading the field, which is represented by its own
1470 * write/read dependence]). The analysis makes conservative points-to
1471 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1472 * the same, and any reference read depends on any reference read without
1473 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001474 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001475 * The internal representation uses 38-bit and is described in the table below.
1476 * The first line indicates the side effect, and for field/array accesses the
1477 * second line indicates the type of the access (in the order of the
1478 * Primitive::Type enum).
1479 * The two numbered lines below indicate the bit position in the bitfield (read
1480 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001481 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001482 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1483 * +-------------+---------+---------+--------------+---------+---------+
1484 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1485 * | 3 |333333322|222222221| 1 |111111110|000000000|
1486 * | 7 |654321098|765432109| 8 |765432109|876543210|
1487 *
1488 * Note that, to ease the implementation, 'changes' bits are least significant
1489 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001490 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491class SideEffects : public ValueObject {
1492 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001493 SideEffects() : flags_(0) {}
1494
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001495 static SideEffects None() {
1496 return SideEffects(0);
1497 }
1498
1499 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001500 return SideEffects(kAllChangeBits | kAllDependOnBits);
1501 }
1502
1503 static SideEffects AllChanges() {
1504 return SideEffects(kAllChangeBits);
1505 }
1506
1507 static SideEffects AllDependencies() {
1508 return SideEffects(kAllDependOnBits);
1509 }
1510
1511 static SideEffects AllExceptGCDependency() {
1512 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1513 }
1514
1515 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001516 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001517 }
1518
Aart Bik34c3ba92015-07-20 14:08:59 -07001519 static SideEffects AllWrites() {
1520 return SideEffects(kAllWrites);
1521 }
1522
1523 static SideEffects AllReads() {
1524 return SideEffects(kAllReads);
1525 }
1526
1527 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1528 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001529 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001530 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001531 }
1532
Aart Bik854a02b2015-07-14 16:07:00 -07001533 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1534 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001535 }
1536
Aart Bik34c3ba92015-07-20 14:08:59 -07001537 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1538 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001539 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001540 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001541 }
1542
1543 static SideEffects ArrayReadOfType(Primitive::Type type) {
1544 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1545 }
1546
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 static SideEffects CanTriggerGC() {
1548 return SideEffects(1ULL << kCanTriggerGCBit);
1549 }
1550
1551 static SideEffects DependsOnGC() {
1552 return SideEffects(1ULL << kDependsOnGCBit);
1553 }
1554
Aart Bik854a02b2015-07-14 16:07:00 -07001555 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001556 SideEffects Union(SideEffects other) const {
1557 return SideEffects(flags_ | other.flags_);
1558 }
1559
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001560 SideEffects Exclusion(SideEffects other) const {
1561 return SideEffects(flags_ & ~other.flags_);
1562 }
1563
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001564 void Add(SideEffects other) {
1565 flags_ |= other.flags_;
1566 }
1567
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001568 bool Includes(SideEffects other) const {
1569 return (other.flags_ & flags_) == other.flags_;
1570 }
1571
1572 bool HasSideEffects() const {
1573 return (flags_ & kAllChangeBits);
1574 }
1575
1576 bool HasDependencies() const {
1577 return (flags_ & kAllDependOnBits);
1578 }
1579
1580 // Returns true if there are no side effects or dependencies.
1581 bool DoesNothing() const {
1582 return flags_ == 0;
1583 }
1584
Aart Bik854a02b2015-07-14 16:07:00 -07001585 // Returns true if something is written.
1586 bool DoesAnyWrite() const {
1587 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001588 }
1589
Aart Bik854a02b2015-07-14 16:07:00 -07001590 // Returns true if something is read.
1591 bool DoesAnyRead() const {
1592 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001593 }
1594
Aart Bik854a02b2015-07-14 16:07:00 -07001595 // Returns true if potentially everything is written and read
1596 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001597 bool DoesAllReadWrite() const {
1598 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1599 }
1600
Aart Bik854a02b2015-07-14 16:07:00 -07001601 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001602 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001603 }
1604
Roland Levillain0d5a2812015-11-13 10:07:31 +00001605 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001606 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001607 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1608 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001609 }
1610
1611 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001612 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001613 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001614 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 for (int s = kLastBit; s >= 0; s--) {
1616 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1617 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1618 // This is a bit for the GC side effect.
1619 if (current_bit_is_set) {
1620 flags += "GC";
1621 }
Aart Bik854a02b2015-07-14 16:07:00 -07001622 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001623 } else {
1624 // This is a bit for the array/field analysis.
1625 // The underscore character stands for the 'can trigger GC' bit.
1626 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1627 if (current_bit_is_set) {
1628 flags += kDebug[s];
1629 }
1630 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1631 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1632 flags += "|";
1633 }
1634 }
Aart Bik854a02b2015-07-14 16:07:00 -07001635 }
1636 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001637 }
1638
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001639 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001640
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001641 private:
1642 static constexpr int kFieldArrayAnalysisBits = 9;
1643
1644 static constexpr int kFieldWriteOffset = 0;
1645 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1646 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1647 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1648
1649 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1650
1651 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1652 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1653 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1654 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1655
1656 static constexpr int kLastBit = kDependsOnGCBit;
1657 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1658
1659 // Aliases.
1660
1661 static_assert(kChangeBits == kDependOnBits,
1662 "the 'change' bits should match the 'depend on' bits.");
1663
1664 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1665 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1666 static constexpr uint64_t kAllWrites =
1667 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1668 static constexpr uint64_t kAllReads =
1669 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001670
Aart Bik854a02b2015-07-14 16:07:00 -07001671 // Work around the fact that HIR aliases I/F and J/D.
1672 // TODO: remove this interceptor once HIR types are clean
1673 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1674 switch (type) {
1675 case Primitive::kPrimInt:
1676 case Primitive::kPrimFloat:
1677 return TypeFlag(Primitive::kPrimInt, offset) |
1678 TypeFlag(Primitive::kPrimFloat, offset);
1679 case Primitive::kPrimLong:
1680 case Primitive::kPrimDouble:
1681 return TypeFlag(Primitive::kPrimLong, offset) |
1682 TypeFlag(Primitive::kPrimDouble, offset);
1683 default:
1684 return TypeFlag(type, offset);
1685 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001686 }
1687
Aart Bik854a02b2015-07-14 16:07:00 -07001688 // Translates type to bit flag.
1689 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1690 CHECK_NE(type, Primitive::kPrimVoid);
1691 const uint64_t one = 1;
1692 const int shift = type; // 0-based consecutive enum
1693 DCHECK_LE(kFieldWriteOffset, shift);
1694 DCHECK_LT(shift, kArrayWriteOffset);
1695 return one << (type + offset);
1696 }
1697
1698 // Private constructor on direct flags value.
1699 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1700
1701 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001702};
1703
David Brazdiled596192015-01-23 10:39:45 +00001704// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001705class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001706 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001707 HEnvironment(ArenaAllocator* arena,
1708 size_t number_of_vregs,
1709 const DexFile& dex_file,
1710 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001711 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001712 InvokeType invoke_type,
1713 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001714 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1715 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001716 parent_(nullptr),
1717 dex_file_(dex_file),
1718 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001719 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001720 invoke_type_(invoke_type),
1721 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001722 }
1723
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001724 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001725 : HEnvironment(arena,
1726 to_copy.Size(),
1727 to_copy.GetDexFile(),
1728 to_copy.GetMethodIdx(),
1729 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001730 to_copy.GetInvokeType(),
1731 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001732
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001733 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001734 if (parent_ != nullptr) {
1735 parent_->SetAndCopyParentChain(allocator, parent);
1736 } else {
1737 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1738 parent_->CopyFrom(parent);
1739 if (parent->GetParent() != nullptr) {
1740 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1741 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001742 }
David Brazdiled596192015-01-23 10:39:45 +00001743 }
1744
Vladimir Marko71bf8092015-09-15 15:33:14 +01001745 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001746 void CopyFrom(HEnvironment* environment);
1747
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001748 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1749 // input to the loop phi instead. This is for inserting instructions that
1750 // require an environment (like HDeoptimization) in the loop pre-header.
1751 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001752
1753 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001754 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001755 }
1756
1757 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001758 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001759 }
1760
David Brazdil1abb4192015-02-17 18:33:36 +00001761 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001762
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001763 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001764
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001765 HEnvironment* GetParent() const { return parent_; }
1766
1767 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001768 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001769 }
1770
1771 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001772 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001773 }
1774
1775 uint32_t GetDexPc() const {
1776 return dex_pc_;
1777 }
1778
1779 uint32_t GetMethodIdx() const {
1780 return method_idx_;
1781 }
1782
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001783 InvokeType GetInvokeType() const {
1784 return invoke_type_;
1785 }
1786
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001787 const DexFile& GetDexFile() const {
1788 return dex_file_;
1789 }
1790
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001791 HInstruction* GetHolder() const {
1792 return holder_;
1793 }
1794
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001795
1796 bool IsFromInlinedInvoke() const {
1797 return GetParent() != nullptr;
1798 }
1799
David Brazdiled596192015-01-23 10:39:45 +00001800 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001801 // Record instructions' use entries of this environment for constant-time removal.
1802 // It should only be called by HInstruction when a new environment use is added.
1803 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1804 DCHECK(env_use->GetUser() == this);
1805 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001806 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001807 }
David Brazdiled596192015-01-23 10:39:45 +00001808
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001809 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1810 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001811 HEnvironment* parent_;
1812 const DexFile& dex_file_;
1813 const uint32_t method_idx_;
1814 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001815 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001816
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001817 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001818 HInstruction* const holder_;
1819
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001820 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001821
1822 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1823};
1824
Vladimir Markof9f64412015-09-02 14:05:49 +01001825class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001827 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001828 : previous_(nullptr),
1829 next_(nullptr),
1830 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001831 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001832 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001833 ssa_index_(-1),
David Brazdilb3e773e2016-01-26 11:28:37 +00001834 emitted_at_use_site_(false),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001835 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001836 locations_(nullptr),
1837 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001838 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001839 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001840 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001841
Dave Allison20dfc792014-06-16 20:44:29 -07001842 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001843
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001844#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001845 enum InstructionKind {
1846 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1847 };
1848#undef DECLARE_KIND
1849
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001850 HInstruction* GetNext() const { return next_; }
1851 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001852
Calin Juravle77520bc2015-01-12 18:45:46 +00001853 HInstruction* GetNextDisregardingMoves() const;
1854 HInstruction* GetPreviousDisregardingMoves() const;
1855
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001856 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001857 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001858 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001859 bool IsInBlock() const { return block_ != nullptr; }
1860 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001861 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1862 bool IsIrreducibleLoopHeaderPhi() const {
1863 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1864 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001865
Roland Levillain6b879dd2014-09-22 17:13:44 +01001866 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001867 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001868
1869 virtual void Accept(HGraphVisitor* visitor) = 0;
1870 virtual const char* DebugName() const = 0;
1871
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001872 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001873 void SetRawInputAt(size_t index, HInstruction* input) {
1874 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1875 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001876
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001877 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001878
1879 uint32_t GetDexPc() const { return dex_pc_; }
1880
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001881 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001882
Roland Levillaine161a2a2014-10-03 12:45:18 +01001883 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001884 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001885
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001886 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001887 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001888
Calin Juravle10e244f2015-01-26 18:54:32 +00001889 // Does not apply for all instructions, but having this at top level greatly
1890 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001891 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001892 virtual bool CanBeNull() const {
1893 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1894 return true;
1895 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001896
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001897 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001898 return false;
1899 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001900
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001901 virtual bool IsActualObject() const {
1902 return GetType() == Primitive::kPrimNot;
1903 }
1904
Calin Juravle2e768302015-07-28 14:41:11 +00001905 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001906
Calin Juravle61d544b2015-02-23 16:46:57 +00001907 ReferenceTypeInfo GetReferenceTypeInfo() const {
1908 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1909 return reference_type_info_;
1910 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001911
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001912 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001913 DCHECK(user != nullptr);
1914 HUseListNode<HInstruction*>* use =
1915 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1916 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001917 }
1918
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001919 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001920 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001921 HUseListNode<HEnvironment*>* env_use =
1922 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1923 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001924 }
1925
David Brazdil1abb4192015-02-17 18:33:36 +00001926 void RemoveAsUserOfInput(size_t input) {
1927 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1928 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1929 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001930
David Brazdil1abb4192015-02-17 18:33:36 +00001931 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1932 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001933
David Brazdiled596192015-01-23 10:39:45 +00001934 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1935 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001936 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001937 bool HasOnlyOneNonEnvironmentUse() const {
1938 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1939 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001940
Roland Levillain6c82d402014-10-13 16:10:27 +01001941 // Does this instruction strictly dominate `other_instruction`?
1942 // Returns false if this instruction and `other_instruction` are the same.
1943 // Aborts if this instruction and `other_instruction` are both phis.
1944 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001945
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001946 int GetId() const { return id_; }
1947 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001948
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001949 int GetSsaIndex() const { return ssa_index_; }
1950 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1951 bool HasSsaIndex() const { return ssa_index_ != -1; }
1952
1953 bool HasEnvironment() const { return environment_ != nullptr; }
1954 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001955 // Set the `environment_` field. Raw because this method does not
1956 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001957 void SetRawEnvironment(HEnvironment* environment) {
1958 DCHECK(environment_ == nullptr);
1959 DCHECK_EQ(environment->GetHolder(), this);
1960 environment_ = environment;
1961 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001962
1963 // Set the environment of this instruction, copying it from `environment`. While
1964 // copying, the uses lists are being updated.
1965 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001966 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001967 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001968 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001969 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001970 if (environment->GetParent() != nullptr) {
1971 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1972 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001973 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001975 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1976 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001977 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001978 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001979 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001980 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001981 if (environment->GetParent() != nullptr) {
1982 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1983 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001984 }
1985
Nicolas Geoffray39468442014-09-02 15:17:15 +01001986 // Returns the number of entries in the environment. Typically, that is the
1987 // number of dex registers in a method. It could be more in case of inlining.
1988 size_t EnvironmentSize() const;
1989
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001990 LocationSummary* GetLocations() const { return locations_; }
1991 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001992
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001993 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001994 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001995
Alexandre Rames188d4312015-04-09 18:30:21 +01001996 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1997 // uses of this instruction by `other` are *not* updated.
1998 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1999 ReplaceWith(other);
2000 other->ReplaceInput(this, use_index);
2001 }
2002
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002003 // Move `this` instruction before `cursor`.
2004 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002005
Vladimir Markofb337ea2015-11-25 15:25:10 +00002006 // Move `this` before its first user and out of any loops. If there is no
2007 // out-of-loop user that dominates all other users, move the instruction
2008 // to the end of the out-of-loop common dominator of the user's blocks.
2009 //
2010 // This can be used only on non-throwing instructions with no side effects that
2011 // have at least one use but no environment uses.
2012 void MoveBeforeFirstUserAndOutOfLoops();
2013
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002014#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 bool Is##type() const; \
2016 const H##type* As##type() const; \
2017 H##type* As##type();
2018
2019 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2020#undef INSTRUCTION_TYPE_CHECK
2021
2022#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002023 bool Is##type() const { return (As##type() != nullptr); } \
2024 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002025 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002026 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002027#undef INSTRUCTION_TYPE_CHECK
2028
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002029 // Returns whether the instruction can be moved within the graph.
2030 virtual bool CanBeMoved() const { return false; }
2031
2032 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002033 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002034 return false;
2035 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002036
2037 // Returns whether any data encoded in the two instructions is equal.
2038 // This method does not look at the inputs. Both instructions must be
2039 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002040 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002041 return false;
2042 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002043
2044 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002045 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002046 // 2) Their inputs are identical.
2047 bool Equals(HInstruction* other) const;
2048
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002049 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2050 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2051 // the virtual function because the __attribute__((__pure__)) doesn't really
2052 // apply the strong requirement for virtual functions, preventing optimizations.
2053 InstructionKind GetKind() const PURE;
2054 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002055
2056 virtual size_t ComputeHashCode() const {
2057 size_t result = GetKind();
2058 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2059 result = (result * 31) + InputAt(i)->GetId();
2060 }
2061 return result;
2062 }
2063
2064 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002065 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002066
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002067 size_t GetLifetimePosition() const { return lifetime_position_; }
2068 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2069 LiveInterval* GetLiveInterval() const { return live_interval_; }
2070 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2071 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2072
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002073 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2074
2075 // Returns whether the code generation of the instruction will require to have access
2076 // to the current method. Such instructions are:
2077 // (1): Instructions that require an environment, as calling the runtime requires
2078 // to walk the stack and have the current method stored at a specific stack address.
2079 // (2): Object literals like classes and strings, that are loaded from the dex cache
2080 // fields of the current method.
2081 bool NeedsCurrentMethod() const {
2082 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2083 }
2084
Vladimir Markodc151b22015-10-15 18:02:30 +01002085 // Returns whether the code generation of the instruction will require to have access
2086 // to the dex cache of the current method's declaring class via the current method.
2087 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002088
Mark Mendellc4701932015-04-10 13:18:51 -04002089 // Does this instruction have any use in an environment before
2090 // control flow hits 'other'?
2091 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2092
2093 // Remove all references to environment uses of this instruction.
2094 // The caller must ensure that this is safe to do.
2095 void RemoveEnvironmentUsers();
2096
David Brazdilb3e773e2016-01-26 11:28:37 +00002097 bool IsEmittedAtUseSite() const { return emitted_at_use_site_; }
2098 void MarkEmittedAtUseSite() { emitted_at_use_site_ = true; }
2099
David Brazdil1abb4192015-02-17 18:33:36 +00002100 protected:
2101 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2102 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002103 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002104
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002105 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002106 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2107
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002108 HInstruction* previous_;
2109 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002110 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002111 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002112
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002113 // An instruction gets an id when it is added to the graph.
2114 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002115 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002116 int id_;
2117
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002118 // When doing liveness analysis, instructions that have uses get an SSA index.
2119 int ssa_index_;
2120
David Brazdilb3e773e2016-01-26 11:28:37 +00002121 // If set, the machine code for this instruction is assumed to be generated by
2122 // its users. Used by liveness analysis to compute use positions accordingly.
2123 bool emitted_at_use_site_;
2124
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002125 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002126 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002127
2128 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002129 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002130
Nicolas Geoffray39468442014-09-02 15:17:15 +01002131 // The environment associated with this instruction. Not null if the instruction
2132 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002133 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002134
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002135 // Set by the code generator.
2136 LocationSummary* locations_;
2137
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002138 // Set by the liveness analysis.
2139 LiveInterval* live_interval_;
2140
2141 // Set by the liveness analysis, this is the position in a linear
2142 // order of blocks where this instruction's live interval start.
2143 size_t lifetime_position_;
2144
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002145 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002146
Calin Juravleacf735c2015-02-12 15:25:22 +00002147 // TODO: for primitive types this should be marked as invalid.
2148 ReferenceTypeInfo reference_type_info_;
2149
David Brazdil1abb4192015-02-17 18:33:36 +00002150 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002152 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002153 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002154 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002155
2156 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2157};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002158std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160class HInputIterator : public ValueObject {
2161 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002162 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002163
2164 bool Done() const { return index_ == instruction_->InputCount(); }
2165 HInstruction* Current() const { return instruction_->InputAt(index_); }
2166 void Advance() { index_++; }
2167
2168 private:
2169 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002170 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002171
2172 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2173};
2174
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175class HInstructionIterator : public ValueObject {
2176 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002177 explicit HInstructionIterator(const HInstructionList& instructions)
2178 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002179 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002180 }
2181
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002182 bool Done() const { return instruction_ == nullptr; }
2183 HInstruction* Current() const { return instruction_; }
2184 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002186 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002187 }
2188
2189 private:
2190 HInstruction* instruction_;
2191 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002192
2193 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002194};
2195
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002196class HBackwardInstructionIterator : public ValueObject {
2197 public:
2198 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2199 : instruction_(instructions.last_instruction_) {
2200 next_ = Done() ? nullptr : instruction_->GetPrevious();
2201 }
2202
2203 bool Done() const { return instruction_ == nullptr; }
2204 HInstruction* Current() const { return instruction_; }
2205 void Advance() {
2206 instruction_ = next_;
2207 next_ = Done() ? nullptr : instruction_->GetPrevious();
2208 }
2209
2210 private:
2211 HInstruction* instruction_;
2212 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002213
2214 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002215};
2216
Vladimir Markof9f64412015-09-02 14:05:49 +01002217template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002218class HTemplateInstruction: public HInstruction {
2219 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002220 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002221 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002222 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002223
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002224 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002227 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2228 DCHECK_LT(i, N);
2229 return inputs_[i];
2230 }
David Brazdil1abb4192015-02-17 18:33:36 +00002231
2232 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002233 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002234 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002235 }
2236
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002237 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002238 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002239
2240 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241};
2242
Vladimir Markof9f64412015-09-02 14:05:49 +01002243// HTemplateInstruction specialization for N=0.
2244template<>
2245class HTemplateInstruction<0>: public HInstruction {
2246 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002247 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002248 : HInstruction(side_effects, dex_pc) {}
2249
Vladimir Markof9f64412015-09-02 14:05:49 +01002250 virtual ~HTemplateInstruction() {}
2251
2252 size_t InputCount() const OVERRIDE { return 0; }
2253
2254 protected:
2255 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2256 LOG(FATAL) << "Unreachable";
2257 UNREACHABLE();
2258 }
2259
2260 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2261 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2262 LOG(FATAL) << "Unreachable";
2263 UNREACHABLE();
2264 }
2265
2266 private:
2267 friend class SsaBuilder;
2268};
2269
Dave Allison20dfc792014-06-16 20:44:29 -07002270template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002271class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002272 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002273 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002274 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002275 virtual ~HExpression() {}
2276
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002277 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002278
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002279 protected:
2280 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002281};
2282
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002283// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2284// instruction that branches to the exit block.
2285class HReturnVoid : public HTemplateInstruction<0> {
2286 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002287 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2288 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002289
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002290 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002291
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002292 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002293
2294 private:
2295 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2296};
2297
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002298// Represents dex's RETURN opcodes. A HReturn is a control flow
2299// instruction that branches to the exit block.
2300class HReturn : public HTemplateInstruction<1> {
2301 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002302 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2303 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002304 SetRawInputAt(0, value);
2305 }
2306
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002307 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002308
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002309 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002310
2311 private:
2312 DISALLOW_COPY_AND_ASSIGN(HReturn);
2313};
2314
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002315// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002316// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002317// exit block.
2318class HExit : public HTemplateInstruction<0> {
2319 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002320 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002321
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002322 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002323
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002324 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002325
2326 private:
2327 DISALLOW_COPY_AND_ASSIGN(HExit);
2328};
2329
2330// Jumps from one block to another.
2331class HGoto : public HTemplateInstruction<0> {
2332 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002333 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002334
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002335 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002336
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002337 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002338 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002339 }
2340
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002341 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002342
2343 private:
2344 DISALLOW_COPY_AND_ASSIGN(HGoto);
2345};
2346
Roland Levillain9867bc72015-08-05 10:21:34 +01002347class HConstant : public HExpression<0> {
2348 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002349 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2350 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002351
2352 bool CanBeMoved() const OVERRIDE { return true; }
2353
2354 virtual bool IsMinusOne() const { return false; }
2355 virtual bool IsZero() const { return false; }
2356 virtual bool IsOne() const { return false; }
2357
David Brazdil77a48ae2015-09-15 12:34:04 +00002358 virtual uint64_t GetValueAsUint64() const = 0;
2359
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002360 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002361
2362 private:
2363 DISALLOW_COPY_AND_ASSIGN(HConstant);
2364};
2365
2366class HNullConstant : public HConstant {
2367 public:
2368 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2369 return true;
2370 }
2371
David Brazdil77a48ae2015-09-15 12:34:04 +00002372 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2373
Roland Levillain9867bc72015-08-05 10:21:34 +01002374 size_t ComputeHashCode() const OVERRIDE { return 0; }
2375
2376 DECLARE_INSTRUCTION(NullConstant);
2377
2378 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002379 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002380
2381 friend class HGraph;
2382 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2383};
2384
2385// Constants of the type int. Those can be from Dex instructions, or
2386// synthesized (for example with the if-eqz instruction).
2387class HIntConstant : public HConstant {
2388 public:
2389 int32_t GetValue() const { return value_; }
2390
David Brazdil9f389d42015-10-01 14:32:56 +01002391 uint64_t GetValueAsUint64() const OVERRIDE {
2392 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2393 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002394
Roland Levillain9867bc72015-08-05 10:21:34 +01002395 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2396 DCHECK(other->IsIntConstant());
2397 return other->AsIntConstant()->value_ == value_;
2398 }
2399
2400 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2401
2402 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2403 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2404 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2405
2406 DECLARE_INSTRUCTION(IntConstant);
2407
2408 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002409 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2410 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2411 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2412 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002413
2414 const int32_t value_;
2415
2416 friend class HGraph;
2417 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2418 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2419 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2420};
2421
2422class HLongConstant : public HConstant {
2423 public:
2424 int64_t GetValue() const { return value_; }
2425
David Brazdil77a48ae2015-09-15 12:34:04 +00002426 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2427
Roland Levillain9867bc72015-08-05 10:21:34 +01002428 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2429 DCHECK(other->IsLongConstant());
2430 return other->AsLongConstant()->value_ == value_;
2431 }
2432
2433 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2434
2435 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2436 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2437 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2438
2439 DECLARE_INSTRUCTION(LongConstant);
2440
2441 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002442 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2443 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002444
2445 const int64_t value_;
2446
2447 friend class HGraph;
2448 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2449};
Dave Allison20dfc792014-06-16 20:44:29 -07002450
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002451// Conditional branch. A block ending with an HIf instruction must have
2452// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002453class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002455 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2456 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002457 SetRawInputAt(0, input);
2458 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002459
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002460 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002461
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002462 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002463 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002464 }
2465
2466 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002467 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002468 }
2469
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002470 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002471
2472 private:
2473 DISALLOW_COPY_AND_ASSIGN(HIf);
2474};
2475
David Brazdilfc6a86a2015-06-26 10:33:45 +00002476
2477// Abstract instruction which marks the beginning and/or end of a try block and
2478// links it to the respective exception handlers. Behaves the same as a Goto in
2479// non-exceptional control flow.
2480// Normal-flow successor is stored at index zero, exception handlers under
2481// higher indices in no particular order.
2482class HTryBoundary : public HTemplateInstruction<0> {
2483 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002484 enum BoundaryKind {
2485 kEntry,
2486 kExit,
2487 };
2488
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002489 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2490 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002491
2492 bool IsControlFlow() const OVERRIDE { return true; }
2493
2494 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002495 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002496
David Brazdild26a4112015-11-10 11:07:31 +00002497 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2498 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2499 }
2500
David Brazdilfc6a86a2015-06-26 10:33:45 +00002501 // Returns whether `handler` is among its exception handlers (non-zero index
2502 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002503 bool HasExceptionHandler(const HBasicBlock& handler) const {
2504 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002505 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002506 }
2507
2508 // If not present already, adds `handler` to its block's list of exception
2509 // handlers.
2510 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002511 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002512 GetBlock()->AddSuccessor(handler);
2513 }
2514 }
2515
David Brazdil56e1acc2015-06-30 15:41:36 +01002516 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002517
David Brazdilffee3d32015-07-06 11:48:53 +01002518 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2519
David Brazdilfc6a86a2015-06-26 10:33:45 +00002520 DECLARE_INSTRUCTION(TryBoundary);
2521
2522 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002523 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002524
2525 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2526};
2527
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002528// Deoptimize to interpreter, upon checking a condition.
2529class HDeoptimize : public HTemplateInstruction<1> {
2530 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002531 // We set CanTriggerGC to prevent any intermediate address to be live
2532 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002533 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002534 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002535 SetRawInputAt(0, cond);
2536 }
2537
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002538 bool CanBeMoved() const OVERRIDE { return true; }
2539 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2540 return true;
2541 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002542 bool NeedsEnvironment() const OVERRIDE { return true; }
2543 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002544
2545 DECLARE_INSTRUCTION(Deoptimize);
2546
2547 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002548 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2549};
2550
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002551// Represents the ArtMethod that was passed as a first argument to
2552// the method. It is used by instructions that depend on it, like
2553// instructions that work with the dex cache.
2554class HCurrentMethod : public HExpression<0> {
2555 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002556 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2557 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002558
2559 DECLARE_INSTRUCTION(CurrentMethod);
2560
2561 private:
2562 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2563};
2564
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002565// Fetches an ArtMethod from the virtual table or the interface method table
2566// of a class.
2567class HClassTableGet : public HExpression<1> {
2568 public:
2569 enum TableKind {
2570 kVTable,
2571 kIMTable,
2572 };
2573 HClassTableGet(HInstruction* cls,
2574 Primitive::Type type,
2575 TableKind kind,
2576 size_t index,
2577 uint32_t dex_pc)
2578 : HExpression(type, SideEffects::None(), dex_pc),
2579 index_(index),
2580 table_kind_(kind) {
2581 SetRawInputAt(0, cls);
2582 }
2583
2584 bool CanBeMoved() const OVERRIDE { return true; }
2585 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2586 return other->AsClassTableGet()->GetIndex() == index_ &&
2587 other->AsClassTableGet()->GetTableKind() == table_kind_;
2588 }
2589
2590 TableKind GetTableKind() const { return table_kind_; }
2591 size_t GetIndex() const { return index_; }
2592
2593 DECLARE_INSTRUCTION(ClassTableGet);
2594
2595 private:
2596 // The index of the ArtMethod in the table.
2597 const size_t index_;
2598 const TableKind table_kind_;
2599
2600 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2601};
2602
Mark Mendellfe57faa2015-09-18 09:26:15 -04002603// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2604// have one successor for each entry in the switch table, and the final successor
2605// will be the block containing the next Dex opcode.
2606class HPackedSwitch : public HTemplateInstruction<1> {
2607 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002608 HPackedSwitch(int32_t start_value,
2609 uint32_t num_entries,
2610 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002611 uint32_t dex_pc = kNoDexPc)
2612 : HTemplateInstruction(SideEffects::None(), dex_pc),
2613 start_value_(start_value),
2614 num_entries_(num_entries) {
2615 SetRawInputAt(0, input);
2616 }
2617
2618 bool IsControlFlow() const OVERRIDE { return true; }
2619
2620 int32_t GetStartValue() const { return start_value_; }
2621
Vladimir Marko211c2112015-09-24 16:52:33 +01002622 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002623
2624 HBasicBlock* GetDefaultBlock() const {
2625 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002626 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002627 }
2628 DECLARE_INSTRUCTION(PackedSwitch);
2629
2630 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002631 const int32_t start_value_;
2632 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002633
2634 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2635};
2636
Roland Levillain88cb1752014-10-20 16:36:47 +01002637class HUnaryOperation : public HExpression<1> {
2638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002639 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2640 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002641 SetRawInputAt(0, input);
2642 }
2643
2644 HInstruction* GetInput() const { return InputAt(0); }
2645 Primitive::Type GetResultType() const { return GetType(); }
2646
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002647 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002648 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002649 return true;
2650 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002651
Roland Levillain9240d6a2014-10-20 16:47:04 +01002652 // Try to statically evaluate `operation` and return a HConstant
2653 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002654 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002655 HConstant* TryStaticEvaluation() const;
2656
2657 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002658 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2659 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002660
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002661 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002662
2663 private:
2664 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2665};
2666
Dave Allison20dfc792014-06-16 20:44:29 -07002667class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002668 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002669 HBinaryOperation(Primitive::Type result_type,
2670 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002671 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002672 SideEffects side_effects = SideEffects::None(),
2673 uint32_t dex_pc = kNoDexPc)
2674 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002675 SetRawInputAt(0, left);
2676 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002677 }
2678
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002679 HInstruction* GetLeft() const { return InputAt(0); }
2680 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002681 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002682
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002683 virtual bool IsCommutative() const { return false; }
2684
2685 // Put constant on the right.
2686 // Returns whether order is changed.
2687 bool OrderInputsWithConstantOnTheRight() {
2688 HInstruction* left = InputAt(0);
2689 HInstruction* right = InputAt(1);
2690 if (left->IsConstant() && !right->IsConstant()) {
2691 ReplaceInput(right, 0);
2692 ReplaceInput(left, 1);
2693 return true;
2694 }
2695 return false;
2696 }
2697
2698 // Order inputs by instruction id, but favor constant on the right side.
2699 // This helps GVN for commutative ops.
2700 void OrderInputs() {
2701 DCHECK(IsCommutative());
2702 HInstruction* left = InputAt(0);
2703 HInstruction* right = InputAt(1);
2704 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2705 return;
2706 }
2707 if (OrderInputsWithConstantOnTheRight()) {
2708 return;
2709 }
2710 // Order according to instruction id.
2711 if (left->GetId() > right->GetId()) {
2712 ReplaceInput(right, 0);
2713 ReplaceInput(left, 1);
2714 }
2715 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002716
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002717 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002718 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002719 return true;
2720 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002721
Roland Levillain9240d6a2014-10-20 16:47:04 +01002722 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002723 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002724 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002725 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002726
2727 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002728 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2729 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2730 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2731 HLongConstant* y ATTRIBUTE_UNUSED) const {
2732 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2733 return nullptr;
2734 }
2735 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2736 HIntConstant* y ATTRIBUTE_UNUSED) const {
2737 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2738 return nullptr;
2739 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002740 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2741 HNullConstant* y ATTRIBUTE_UNUSED) const {
2742 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2743 return nullptr;
2744 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002745
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002746 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002747 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002748 HConstant* GetConstantRight() const;
2749
2750 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002751 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002752 HInstruction* GetLeastConstantLeft() const;
2753
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002754 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002755
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002756 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002757 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2758};
2759
Mark Mendellc4701932015-04-10 13:18:51 -04002760// The comparison bias applies for floating point operations and indicates how NaN
2761// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002762enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002763 kNoBias, // bias is not applicable (i.e. for long operation)
2764 kGtBias, // return 1 for NaN comparisons
2765 kLtBias, // return -1 for NaN comparisons
2766};
2767
Dave Allison20dfc792014-06-16 20:44:29 -07002768class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002769 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2771 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002772 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002773
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002774 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002775 // `instruction`, and disregard moves in between.
2776 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002777
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002778 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002779
2780 virtual IfCondition GetCondition() const = 0;
2781
Mark Mendellc4701932015-04-10 13:18:51 -04002782 virtual IfCondition GetOppositeCondition() const = 0;
2783
Roland Levillain4fa13f62015-07-06 18:11:54 +01002784 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002785
2786 void SetBias(ComparisonBias bias) { bias_ = bias; }
2787
2788 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2789 return bias_ == other->AsCondition()->bias_;
2790 }
2791
Roland Levillain4fa13f62015-07-06 18:11:54 +01002792 bool IsFPConditionTrueIfNaN() const {
2793 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2794 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002795 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002796 }
2797
2798 bool IsFPConditionFalseIfNaN() const {
2799 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2800 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002801 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002802 }
2803
Dave Allison20dfc792014-06-16 20:44:29 -07002804 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002805 // Needed if we merge a HCompare into a HCondition.
2806 ComparisonBias bias_;
2807
Dave Allison20dfc792014-06-16 20:44:29 -07002808 DISALLOW_COPY_AND_ASSIGN(HCondition);
2809};
2810
2811// Instruction to check if two inputs are equal to each other.
2812class HEqual : public HCondition {
2813 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002814 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2815 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002816
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002817 bool IsCommutative() const OVERRIDE { return true; }
2818
Roland Levillain9867bc72015-08-05 10:21:34 +01002819 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 return GetBlock()->GetGraph()->GetIntConstant(
2821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002822 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002823 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002824 return GetBlock()->GetGraph()->GetIntConstant(
2825 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002826 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002827 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2828 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002829 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002830 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002831
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002832 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002833
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002834 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002835 return kCondEQ;
2836 }
2837
Mark Mendellc4701932015-04-10 13:18:51 -04002838 IfCondition GetOppositeCondition() const OVERRIDE {
2839 return kCondNE;
2840 }
2841
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002842 private:
Aart Bike9f37602015-10-09 11:15:55 -07002843 template <typename T> bool Compute(T x, T y) const { return x == y; }
2844
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002845 DISALLOW_COPY_AND_ASSIGN(HEqual);
2846};
2847
Dave Allison20dfc792014-06-16 20:44:29 -07002848class HNotEqual : public HCondition {
2849 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002850 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2851 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002852
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002853 bool IsCommutative() const OVERRIDE { return true; }
2854
Roland Levillain9867bc72015-08-05 10:21:34 +01002855 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002856 return GetBlock()->GetGraph()->GetIntConstant(
2857 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002858 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002859 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002860 return GetBlock()->GetGraph()->GetIntConstant(
2861 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002862 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002863 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2864 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002865 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002866 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002867
Dave Allison20dfc792014-06-16 20:44:29 -07002868 DECLARE_INSTRUCTION(NotEqual);
2869
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002870 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002871 return kCondNE;
2872 }
2873
Mark Mendellc4701932015-04-10 13:18:51 -04002874 IfCondition GetOppositeCondition() const OVERRIDE {
2875 return kCondEQ;
2876 }
2877
Dave Allison20dfc792014-06-16 20:44:29 -07002878 private:
Aart Bike9f37602015-10-09 11:15:55 -07002879 template <typename T> bool Compute(T x, T y) const { return x != y; }
2880
Dave Allison20dfc792014-06-16 20:44:29 -07002881 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2882};
2883
2884class HLessThan : public HCondition {
2885 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002886 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2887 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002888
Roland Levillain9867bc72015-08-05 10:21:34 +01002889 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002890 return GetBlock()->GetGraph()->GetIntConstant(
2891 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002892 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002893 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002894 return GetBlock()->GetGraph()->GetIntConstant(
2895 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002896 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002897
Dave Allison20dfc792014-06-16 20:44:29 -07002898 DECLARE_INSTRUCTION(LessThan);
2899
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002900 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002901 return kCondLT;
2902 }
2903
Mark Mendellc4701932015-04-10 13:18:51 -04002904 IfCondition GetOppositeCondition() const OVERRIDE {
2905 return kCondGE;
2906 }
2907
Dave Allison20dfc792014-06-16 20:44:29 -07002908 private:
Aart Bike9f37602015-10-09 11:15:55 -07002909 template <typename T> bool Compute(T x, T y) const { return x < y; }
2910
Dave Allison20dfc792014-06-16 20:44:29 -07002911 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2912};
2913
2914class HLessThanOrEqual : public HCondition {
2915 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002916 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2917 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002918
Roland Levillain9867bc72015-08-05 10:21:34 +01002919 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002920 return GetBlock()->GetGraph()->GetIntConstant(
2921 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002922 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002923 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002924 return GetBlock()->GetGraph()->GetIntConstant(
2925 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002926 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002927
Dave Allison20dfc792014-06-16 20:44:29 -07002928 DECLARE_INSTRUCTION(LessThanOrEqual);
2929
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002930 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002931 return kCondLE;
2932 }
2933
Mark Mendellc4701932015-04-10 13:18:51 -04002934 IfCondition GetOppositeCondition() const OVERRIDE {
2935 return kCondGT;
2936 }
2937
Dave Allison20dfc792014-06-16 20:44:29 -07002938 private:
Aart Bike9f37602015-10-09 11:15:55 -07002939 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2940
Dave Allison20dfc792014-06-16 20:44:29 -07002941 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2942};
2943
2944class HGreaterThan : public HCondition {
2945 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002946 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2947 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002948
Roland Levillain9867bc72015-08-05 10:21:34 +01002949 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002950 return GetBlock()->GetGraph()->GetIntConstant(
2951 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002952 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002953 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002954 return GetBlock()->GetGraph()->GetIntConstant(
2955 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002956 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002957
Dave Allison20dfc792014-06-16 20:44:29 -07002958 DECLARE_INSTRUCTION(GreaterThan);
2959
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002960 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002961 return kCondGT;
2962 }
2963
Mark Mendellc4701932015-04-10 13:18:51 -04002964 IfCondition GetOppositeCondition() const OVERRIDE {
2965 return kCondLE;
2966 }
2967
Dave Allison20dfc792014-06-16 20:44:29 -07002968 private:
Aart Bike9f37602015-10-09 11:15:55 -07002969 template <typename T> bool Compute(T x, T y) const { return x > y; }
2970
Dave Allison20dfc792014-06-16 20:44:29 -07002971 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2972};
2973
2974class HGreaterThanOrEqual : public HCondition {
2975 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002976 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2977 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002978
Roland Levillain9867bc72015-08-05 10:21:34 +01002979 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002980 return GetBlock()->GetGraph()->GetIntConstant(
2981 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002982 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002983 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002984 return GetBlock()->GetGraph()->GetIntConstant(
2985 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002986 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002987
Dave Allison20dfc792014-06-16 20:44:29 -07002988 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2989
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002990 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002991 return kCondGE;
2992 }
2993
Mark Mendellc4701932015-04-10 13:18:51 -04002994 IfCondition GetOppositeCondition() const OVERRIDE {
2995 return kCondLT;
2996 }
2997
Dave Allison20dfc792014-06-16 20:44:29 -07002998 private:
Aart Bike9f37602015-10-09 11:15:55 -07002999 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3000
Dave Allison20dfc792014-06-16 20:44:29 -07003001 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3002};
3003
Aart Bike9f37602015-10-09 11:15:55 -07003004class HBelow : public HCondition {
3005 public:
3006 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3007 : HCondition(first, second, dex_pc) {}
3008
3009 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3010 return GetBlock()->GetGraph()->GetIntConstant(
3011 Compute(static_cast<uint32_t>(x->GetValue()),
3012 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3013 }
3014 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3015 return GetBlock()->GetGraph()->GetIntConstant(
3016 Compute(static_cast<uint64_t>(x->GetValue()),
3017 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3018 }
3019
3020 DECLARE_INSTRUCTION(Below);
3021
3022 IfCondition GetCondition() const OVERRIDE {
3023 return kCondB;
3024 }
3025
3026 IfCondition GetOppositeCondition() const OVERRIDE {
3027 return kCondAE;
3028 }
3029
3030 private:
3031 template <typename T> bool Compute(T x, T y) const { return x < y; }
3032
3033 DISALLOW_COPY_AND_ASSIGN(HBelow);
3034};
3035
3036class HBelowOrEqual : public HCondition {
3037 public:
3038 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3039 : HCondition(first, second, dex_pc) {}
3040
3041 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3042 return GetBlock()->GetGraph()->GetIntConstant(
3043 Compute(static_cast<uint32_t>(x->GetValue()),
3044 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3045 }
3046 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3047 return GetBlock()->GetGraph()->GetIntConstant(
3048 Compute(static_cast<uint64_t>(x->GetValue()),
3049 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3050 }
3051
3052 DECLARE_INSTRUCTION(BelowOrEqual);
3053
3054 IfCondition GetCondition() const OVERRIDE {
3055 return kCondBE;
3056 }
3057
3058 IfCondition GetOppositeCondition() const OVERRIDE {
3059 return kCondA;
3060 }
3061
3062 private:
3063 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3064
3065 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3066};
3067
3068class HAbove : public HCondition {
3069 public:
3070 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3071 : HCondition(first, second, dex_pc) {}
3072
3073 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3074 return GetBlock()->GetGraph()->GetIntConstant(
3075 Compute(static_cast<uint32_t>(x->GetValue()),
3076 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3077 }
3078 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3079 return GetBlock()->GetGraph()->GetIntConstant(
3080 Compute(static_cast<uint64_t>(x->GetValue()),
3081 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3082 }
3083
3084 DECLARE_INSTRUCTION(Above);
3085
3086 IfCondition GetCondition() const OVERRIDE {
3087 return kCondA;
3088 }
3089
3090 IfCondition GetOppositeCondition() const OVERRIDE {
3091 return kCondBE;
3092 }
3093
3094 private:
3095 template <typename T> bool Compute(T x, T y) const { return x > y; }
3096
3097 DISALLOW_COPY_AND_ASSIGN(HAbove);
3098};
3099
3100class HAboveOrEqual : public HCondition {
3101 public:
3102 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3103 : HCondition(first, second, dex_pc) {}
3104
3105 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3106 return GetBlock()->GetGraph()->GetIntConstant(
3107 Compute(static_cast<uint32_t>(x->GetValue()),
3108 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3109 }
3110 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3111 return GetBlock()->GetGraph()->GetIntConstant(
3112 Compute(static_cast<uint64_t>(x->GetValue()),
3113 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3114 }
3115
3116 DECLARE_INSTRUCTION(AboveOrEqual);
3117
3118 IfCondition GetCondition() const OVERRIDE {
3119 return kCondAE;
3120 }
3121
3122 IfCondition GetOppositeCondition() const OVERRIDE {
3123 return kCondB;
3124 }
3125
3126 private:
3127 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3128
3129 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3130};
Dave Allison20dfc792014-06-16 20:44:29 -07003131
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003132// Instruction to check how two inputs compare to each other.
3133// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3134class HCompare : public HBinaryOperation {
3135 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003136 HCompare(Primitive::Type type,
3137 HInstruction* first,
3138 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003139 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003140 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003141 : HBinaryOperation(Primitive::kPrimInt,
3142 first,
3143 second,
3144 SideEffectsForArchRuntimeCalls(type),
3145 dex_pc),
3146 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003147 DCHECK_EQ(type, first->GetType());
3148 DCHECK_EQ(type, second->GetType());
3149 }
3150
Roland Levillain9867bc72015-08-05 10:21:34 +01003151 template <typename T>
3152 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003153
Roland Levillain9867bc72015-08-05 10:21:34 +01003154 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003155 return GetBlock()->GetGraph()->GetIntConstant(
3156 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003157 }
3158 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003159 return GetBlock()->GetGraph()->GetIntConstant(
3160 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003161 }
3162
Calin Juravleddb7df22014-11-25 20:56:51 +00003163 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3164 return bias_ == other->AsCompare()->bias_;
3165 }
3166
Mark Mendellc4701932015-04-10 13:18:51 -04003167 ComparisonBias GetBias() const { return bias_; }
3168
Roland Levillain4fa13f62015-07-06 18:11:54 +01003169 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003170
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003171
3172 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3173 // MIPS64 uses a runtime call for FP comparisons.
3174 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3175 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003176
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003177 DECLARE_INSTRUCTION(Compare);
3178
3179 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003180 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003181
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003182 DISALLOW_COPY_AND_ASSIGN(HCompare);
3183};
3184
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003185// A local in the graph. Corresponds to a Dex register.
3186class HLocal : public HTemplateInstruction<0> {
3187 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003188 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003189 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003190
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003191 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003192
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003193 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003194
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003195 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003196 // The Dex register number.
3197 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003198
3199 DISALLOW_COPY_AND_ASSIGN(HLocal);
3200};
3201
3202// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003203class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003204 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003205 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3206 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003207 SetRawInputAt(0, local);
3208 }
3209
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003210 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3211
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003212 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003213
3214 private:
3215 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3216};
3217
3218// Store a value in a given local. This instruction has two inputs: the value
3219// and the local.
3220class HStoreLocal : public HTemplateInstruction<2> {
3221 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003222 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3223 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003224 SetRawInputAt(0, local);
3225 SetRawInputAt(1, value);
3226 }
3227
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003228 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3229
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003230 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003231
3232 private:
3233 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3234};
3235
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003236class HFloatConstant : public HConstant {
3237 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003238 float GetValue() const { return value_; }
3239
David Brazdil77a48ae2015-09-15 12:34:04 +00003240 uint64_t GetValueAsUint64() const OVERRIDE {
3241 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3242 }
3243
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003244 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003245 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003246 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003247 }
3248
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003249 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003250
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003251 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003252 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003253 }
3254 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003255 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003256 }
3257 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003258 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3259 }
3260 bool IsNaN() const {
3261 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003262 }
3263
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003264 DECLARE_INSTRUCTION(FloatConstant);
3265
3266 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003267 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3268 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3269 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3270 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003271
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003272 const float value_;
3273
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003274 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003275 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003276 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003277 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3278};
3279
3280class HDoubleConstant : public HConstant {
3281 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003282 double GetValue() const { return value_; }
3283
David Brazdil77a48ae2015-09-15 12:34:04 +00003284 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3285
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003286 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003287 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003288 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003289 }
3290
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003291 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003292
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003293 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003294 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003295 }
3296 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003297 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003298 }
3299 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003300 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3301 }
3302 bool IsNaN() const {
3303 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003304 }
3305
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003306 DECLARE_INSTRUCTION(DoubleConstant);
3307
3308 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003309 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3310 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3311 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3312 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003313
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003314 const double value_;
3315
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003316 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003317 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003318 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003319 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3320};
3321
David Brazdil6de19382016-01-08 17:37:10 +00003322class HNewInstance : public HExpression<2> {
3323 public:
3324 HNewInstance(HInstruction* cls,
3325 HCurrentMethod* current_method,
3326 uint32_t dex_pc,
3327 uint16_t type_index,
3328 const DexFile& dex_file,
3329 bool can_throw,
3330 bool finalizable,
3331 QuickEntrypointEnum entrypoint)
3332 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3333 type_index_(type_index),
3334 dex_file_(dex_file),
3335 can_throw_(can_throw),
3336 finalizable_(finalizable),
3337 entrypoint_(entrypoint) {
3338 SetRawInputAt(0, cls);
3339 SetRawInputAt(1, current_method);
3340 }
3341
3342 uint16_t GetTypeIndex() const { return type_index_; }
3343 const DexFile& GetDexFile() const { return dex_file_; }
3344
3345 // Calls runtime so needs an environment.
3346 bool NeedsEnvironment() const OVERRIDE { return true; }
3347
3348 // It may throw when called on type that's not instantiable/accessible.
3349 // It can throw OOME.
3350 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3351 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3352
3353 bool IsFinalizable() const { return finalizable_; }
3354
3355 bool CanBeNull() const OVERRIDE { return false; }
3356
3357 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3358
3359 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3360 entrypoint_ = entrypoint;
3361 }
3362
3363 bool IsStringAlloc() const;
3364
3365 DECLARE_INSTRUCTION(NewInstance);
3366
3367 private:
3368 const uint16_t type_index_;
3369 const DexFile& dex_file_;
3370 const bool can_throw_;
3371 const bool finalizable_;
3372 QuickEntrypointEnum entrypoint_;
3373
3374 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3375};
3376
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003377enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003378#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3379 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003380#include "intrinsics_list.h"
3381 kNone,
3382 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3383#undef INTRINSICS_LIST
3384#undef OPTIMIZING_INTRINSICS
3385};
3386std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3387
Agi Csaki05f20562015-08-19 14:58:14 -07003388enum IntrinsicNeedsEnvironmentOrCache {
3389 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3390 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003391};
3392
Aart Bik5d75afe2015-12-14 11:57:01 -08003393enum IntrinsicSideEffects {
3394 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3395 kReadSideEffects, // Intrinsic may read heap memory.
3396 kWriteSideEffects, // Intrinsic may write heap memory.
3397 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3398};
3399
3400enum IntrinsicExceptions {
3401 kNoThrow, // Intrinsic does not throw any exceptions.
3402 kCanThrow // Intrinsic may throw exceptions.
3403};
3404
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003405class HInvoke : public HInstruction {
3406 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003407 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003408
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003409 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003410
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003411 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003412 SetRawInputAt(index, argument);
3413 }
3414
Roland Levillain3e3d7332015-04-28 11:00:54 +01003415 // Return the number of arguments. This number can be lower than
3416 // the number of inputs returned by InputCount(), as some invoke
3417 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3418 // inputs at the end of their list of inputs.
3419 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3420
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003421 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003422
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003423 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003424 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003425
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003426 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3427
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003428 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003429 return intrinsic_;
3430 }
3431
Aart Bik5d75afe2015-12-14 11:57:01 -08003432 void SetIntrinsic(Intrinsics intrinsic,
3433 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3434 IntrinsicSideEffects side_effects,
3435 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003436
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003437 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003438 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003439 }
3440
Aart Bik5d75afe2015-12-14 11:57:01 -08003441 bool CanThrow() const OVERRIDE { return can_throw_; }
3442
3443 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3444
3445 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3446 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3447 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003448
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003449 uint32_t* GetIntrinsicOptimizations() {
3450 return &intrinsic_optimizations_;
3451 }
3452
3453 const uint32_t* GetIntrinsicOptimizations() const {
3454 return &intrinsic_optimizations_;
3455 }
3456
3457 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3458
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003459 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003460
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003461 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003462 HInvoke(ArenaAllocator* arena,
3463 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003464 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003465 Primitive::Type return_type,
3466 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003467 uint32_t dex_method_index,
3468 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003469 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003470 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003471 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003472 inputs_(number_of_arguments + number_of_other_inputs,
3473 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003474 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003475 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003476 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003477 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003478 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003479 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003480 }
3481
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003482 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003483 return inputs_[index];
3484 }
3485
David Brazdil1abb4192015-02-17 18:33:36 +00003486 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003487 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003488 }
3489
Aart Bik5d75afe2015-12-14 11:57:01 -08003490 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3491
Roland Levillain3e3d7332015-04-28 11:00:54 +01003492 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003493 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003494 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003495 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003496 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003497 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003498 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003499
3500 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3501 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003502
3503 private:
3504 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3505};
3506
Calin Juravle175dc732015-08-25 15:42:32 +01003507class HInvokeUnresolved : public HInvoke {
3508 public:
3509 HInvokeUnresolved(ArenaAllocator* arena,
3510 uint32_t number_of_arguments,
3511 Primitive::Type return_type,
3512 uint32_t dex_pc,
3513 uint32_t dex_method_index,
3514 InvokeType invoke_type)
3515 : HInvoke(arena,
3516 number_of_arguments,
3517 0u /* number_of_other_inputs */,
3518 return_type,
3519 dex_pc,
3520 dex_method_index,
3521 invoke_type) {
3522 }
3523
3524 DECLARE_INSTRUCTION(InvokeUnresolved);
3525
3526 private:
3527 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3528};
3529
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003530class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003531 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003532 // Requirements of this method call regarding the class
3533 // initialization (clinit) check of its declaring class.
3534 enum class ClinitCheckRequirement {
3535 kNone, // Class already initialized.
3536 kExplicit, // Static call having explicit clinit check as last input.
3537 kImplicit, // Static call implicitly requiring a clinit check.
3538 };
3539
Vladimir Marko58155012015-08-19 12:49:41 +00003540 // Determines how to load the target ArtMethod*.
3541 enum class MethodLoadKind {
3542 // Use a String init ArtMethod* loaded from Thread entrypoints.
3543 kStringInit,
3544
3545 // Use the method's own ArtMethod* loaded by the register allocator.
3546 kRecursive,
3547
3548 // Use ArtMethod* at a known address, embed the direct address in the code.
3549 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3550 kDirectAddress,
3551
3552 // Use ArtMethod* at an address that will be known at link time, embed the direct
3553 // address in the code. If the image is relocatable, emit .patch_oat entry.
3554 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3555 // the image relocatable or not.
3556 kDirectAddressWithFixup,
3557
3558 // Load from resoved methods array in the dex cache using a PC-relative load.
3559 // Used when we need to use the dex cache, for example for invoke-static that
3560 // may cause class initialization (the entry may point to a resolution method),
3561 // and we know that we can access the dex cache arrays using a PC-relative load.
3562 kDexCachePcRelative,
3563
3564 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3565 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3566 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3567 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3568 kDexCacheViaMethod,
3569 };
3570
3571 // Determines the location of the code pointer.
3572 enum class CodePtrLocation {
3573 // Recursive call, use local PC-relative call instruction.
3574 kCallSelf,
3575
3576 // Use PC-relative call instruction patched at link time.
3577 // Used for calls within an oat file, boot->boot or app->app.
3578 kCallPCRelative,
3579
3580 // Call to a known target address, embed the direct address in code.
3581 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3582 kCallDirect,
3583
3584 // Call to a target address that will be known at link time, embed the direct
3585 // address in code. If the image is relocatable, emit .patch_oat entry.
3586 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3587 // the image relocatable or not.
3588 kCallDirectWithFixup,
3589
3590 // Use code pointer from the ArtMethod*.
3591 // Used when we don't know the target code. This is also the last-resort-kind used when
3592 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3593 kCallArtMethod,
3594 };
3595
3596 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003597 MethodLoadKind method_load_kind;
3598 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003599 // The method load data holds
3600 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3601 // Note that there are multiple string init methods, each having its own offset.
3602 // - the method address for kDirectAddress
3603 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003604 uint64_t method_load_data;
3605 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003606 };
3607
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003608 HInvokeStaticOrDirect(ArenaAllocator* arena,
3609 uint32_t number_of_arguments,
3610 Primitive::Type return_type,
3611 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003612 uint32_t method_index,
3613 MethodReference target_method,
3614 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003615 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003616 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003617 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003618 : HInvoke(arena,
3619 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003620 // There is potentially one extra argument for the HCurrentMethod node, and
3621 // potentially one other if the clinit check is explicit, and potentially
3622 // one other if the method is a string factory.
3623 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3624 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3625 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003626 return_type,
3627 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003628 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003629 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003630 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003631 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003632 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003633 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003634
Vladimir Markodc151b22015-10-15 18:02:30 +01003635 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003636 bool had_current_method_input = HasCurrentMethodInput();
3637 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3638
3639 // Using the current method is the default and once we find a better
3640 // method load kind, we should not go back to using the current method.
3641 DCHECK(had_current_method_input || !needs_current_method_input);
3642
3643 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003644 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3645 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003646 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003647 dispatch_info_ = dispatch_info;
3648 }
3649
Vladimir Markoc53c0792015-11-19 15:48:33 +00003650 void AddSpecialInput(HInstruction* input) {
3651 // We allow only one special input.
3652 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3653 DCHECK(InputCount() == GetSpecialInputIndex() ||
3654 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3655 InsertInputAt(GetSpecialInputIndex(), input);
3656 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003657
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003658 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003659 // We access the method via the dex cache so we can't do an implicit null check.
3660 // TODO: for intrinsics we can generate implicit null checks.
3661 return false;
3662 }
3663
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003664 bool CanBeNull() const OVERRIDE {
3665 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3666 }
3667
Vladimir Markoc53c0792015-11-19 15:48:33 +00003668 // Get the index of the special input, if any.
3669 //
David Brazdil6de19382016-01-08 17:37:10 +00003670 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3671 // method pointer; otherwise there may be one platform-specific special input,
3672 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003673 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3674
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003675 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3676 void SetOptimizedInvokeType(InvokeType invoke_type) {
3677 optimized_invoke_type_ = invoke_type;
3678 }
3679
Vladimir Marko58155012015-08-19 12:49:41 +00003680 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3681 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3682 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003683 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003684 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003685 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003686 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003687 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3688 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003689 bool HasCurrentMethodInput() const {
3690 // This function can be called only after the invoke has been fully initialized by the builder.
3691 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003692 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003693 return true;
3694 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003695 DCHECK(InputCount() == GetSpecialInputIndex() ||
3696 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003697 return false;
3698 }
3699 }
Vladimir Marko58155012015-08-19 12:49:41 +00003700 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3701 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003702 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003703
3704 int32_t GetStringInitOffset() const {
3705 DCHECK(IsStringInit());
3706 return dispatch_info_.method_load_data;
3707 }
3708
3709 uint64_t GetMethodAddress() const {
3710 DCHECK(HasMethodAddress());
3711 return dispatch_info_.method_load_data;
3712 }
3713
3714 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003715 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003716 return dispatch_info_.method_load_data;
3717 }
3718
3719 uint64_t GetDirectCodePtr() const {
3720 DCHECK(HasDirectCodePtr());
3721 return dispatch_info_.direct_code_ptr;
3722 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003723
Calin Juravle68ad6492015-08-18 17:08:12 +01003724 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3725
Roland Levillain4c0eb422015-04-24 16:43:49 +01003726 // Is this instruction a call to a static method?
3727 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003728 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003729 }
3730
Vladimir Markofbb184a2015-11-13 14:47:00 +00003731 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3732 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3733 // instruction; only relevant for static calls with explicit clinit check.
3734 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003735 DCHECK(IsStaticWithExplicitClinitCheck());
3736 size_t last_input_index = InputCount() - 1;
3737 HInstruction* last_input = InputAt(last_input_index);
3738 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003739 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003740 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003741 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003742 clinit_check_requirement_ = new_requirement;
3743 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003744 }
3745
David Brazdilbc9ab162016-01-20 14:50:19 +00003746 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003747 DCHECK(IsStringInit());
3748 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003749 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003750 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003751 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003752 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003753 }
3754
Roland Levillain4c0eb422015-04-24 16:43:49 +01003755 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003756 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003757 bool IsStaticWithExplicitClinitCheck() const {
3758 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3759 }
3760
3761 // Is this a call to a static method whose declaring class has an
3762 // implicit intialization check requirement?
3763 bool IsStaticWithImplicitClinitCheck() const {
3764 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3765 }
3766
Vladimir Markob554b5a2015-11-06 12:57:55 +00003767 // Does this method load kind need the current method as an input?
3768 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3769 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3770 }
3771
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003772 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003773
Roland Levillain4c0eb422015-04-24 16:43:49 +01003774 protected:
3775 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3776 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3777 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3778 HInstruction* input = input_record.GetInstruction();
3779 // `input` is the last input of a static invoke marked as having
3780 // an explicit clinit check. It must either be:
3781 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3782 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3783 DCHECK(input != nullptr);
3784 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3785 }
3786 return input_record;
3787 }
3788
Vladimir Markoc53c0792015-11-19 15:48:33 +00003789 void InsertInputAt(size_t index, HInstruction* input);
3790 void RemoveInputAt(size_t index);
3791
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003792 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003793 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003794 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003795 // The target method may refer to different dex file or method index than the original
3796 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3797 // in derived class to increase visibility.
3798 MethodReference target_method_;
3799 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003800
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003801 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003802};
Vladimir Markof64242a2015-12-01 14:58:23 +00003803std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003804std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003805
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003806class HInvokeVirtual : public HInvoke {
3807 public:
3808 HInvokeVirtual(ArenaAllocator* arena,
3809 uint32_t number_of_arguments,
3810 Primitive::Type return_type,
3811 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003812 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003813 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003814 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003815 vtable_index_(vtable_index) {}
3816
Calin Juravle641547a2015-04-21 22:08:51 +01003817 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003818 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003819 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003820 }
3821
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003822 uint32_t GetVTableIndex() const { return vtable_index_; }
3823
3824 DECLARE_INSTRUCTION(InvokeVirtual);
3825
3826 private:
3827 const uint32_t vtable_index_;
3828
3829 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3830};
3831
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003832class HInvokeInterface : public HInvoke {
3833 public:
3834 HInvokeInterface(ArenaAllocator* arena,
3835 uint32_t number_of_arguments,
3836 Primitive::Type return_type,
3837 uint32_t dex_pc,
3838 uint32_t dex_method_index,
3839 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003840 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003841 imt_index_(imt_index) {}
3842
Calin Juravle641547a2015-04-21 22:08:51 +01003843 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003844 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003845 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003846 }
3847
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003848 uint32_t GetImtIndex() const { return imt_index_; }
3849 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3850
3851 DECLARE_INSTRUCTION(InvokeInterface);
3852
3853 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003854 const uint32_t imt_index_;
3855
3856 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3857};
3858
Roland Levillain88cb1752014-10-20 16:36:47 +01003859class HNeg : public HUnaryOperation {
3860 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003861 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3862 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003863
Roland Levillain9867bc72015-08-05 10:21:34 +01003864 template <typename T> T Compute(T x) const { return -x; }
3865
3866 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003867 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003868 }
3869 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003870 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003871 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003872
Roland Levillain88cb1752014-10-20 16:36:47 +01003873 DECLARE_INSTRUCTION(Neg);
3874
3875 private:
3876 DISALLOW_COPY_AND_ASSIGN(HNeg);
3877};
3878
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003879class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003880 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003881 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003882 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003883 uint32_t dex_pc,
3884 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003885 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003886 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003887 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003888 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003889 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003890 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003891 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003892 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003893 }
3894
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003895 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003896 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003897
3898 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003899 bool NeedsEnvironment() const OVERRIDE { return true; }
3900
Mingyao Yang0c365e62015-03-31 15:09:29 -07003901 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3902 bool CanThrow() const OVERRIDE { return true; }
3903
Calin Juravle10e244f2015-01-26 18:54:32 +00003904 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003905
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003906 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3907
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003908 DECLARE_INSTRUCTION(NewArray);
3909
3910 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003911 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003912 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003913 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003914
3915 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3916};
3917
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003918class HAdd : public HBinaryOperation {
3919 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003920 HAdd(Primitive::Type result_type,
3921 HInstruction* left,
3922 HInstruction* right,
3923 uint32_t dex_pc = kNoDexPc)
3924 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003925
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003926 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003927
Roland Levillain9867bc72015-08-05 10:21:34 +01003928 template <typename T> T Compute(T x, T y) const { return x + y; }
3929
3930 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003931 return GetBlock()->GetGraph()->GetIntConstant(
3932 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003933 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003934 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003935 return GetBlock()->GetGraph()->GetLongConstant(
3936 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003937 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003938
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003939 DECLARE_INSTRUCTION(Add);
3940
3941 private:
3942 DISALLOW_COPY_AND_ASSIGN(HAdd);
3943};
3944
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003945class HSub : public HBinaryOperation {
3946 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003947 HSub(Primitive::Type result_type,
3948 HInstruction* left,
3949 HInstruction* right,
3950 uint32_t dex_pc = kNoDexPc)
3951 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003952
Roland Levillain9867bc72015-08-05 10:21:34 +01003953 template <typename T> T Compute(T x, T y) const { return x - y; }
3954
3955 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003956 return GetBlock()->GetGraph()->GetIntConstant(
3957 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003958 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003959 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003960 return GetBlock()->GetGraph()->GetLongConstant(
3961 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003962 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003963
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003964 DECLARE_INSTRUCTION(Sub);
3965
3966 private:
3967 DISALLOW_COPY_AND_ASSIGN(HSub);
3968};
3969
Calin Juravle34bacdf2014-10-07 20:23:36 +01003970class HMul : public HBinaryOperation {
3971 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003972 HMul(Primitive::Type result_type,
3973 HInstruction* left,
3974 HInstruction* right,
3975 uint32_t dex_pc = kNoDexPc)
3976 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003977
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003978 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003979
Roland Levillain9867bc72015-08-05 10:21:34 +01003980 template <typename T> T Compute(T x, T y) const { return x * y; }
3981
3982 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003983 return GetBlock()->GetGraph()->GetIntConstant(
3984 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003985 }
3986 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003987 return GetBlock()->GetGraph()->GetLongConstant(
3988 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003989 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003990
3991 DECLARE_INSTRUCTION(Mul);
3992
3993 private:
3994 DISALLOW_COPY_AND_ASSIGN(HMul);
3995};
3996
Calin Juravle7c4954d2014-10-28 16:57:40 +00003997class HDiv : public HBinaryOperation {
3998 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003999 HDiv(Primitive::Type result_type,
4000 HInstruction* left,
4001 HInstruction* right,
4002 uint32_t dex_pc)
4003 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004004
Roland Levillain9867bc72015-08-05 10:21:34 +01004005 template <typename T>
4006 T Compute(T x, T y) const {
4007 // Our graph structure ensures we never have 0 for `y` during
4008 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004009 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004010 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004011 return (y == -1) ? -x : x / y;
4012 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004013
Roland Levillain9867bc72015-08-05 10:21:34 +01004014 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004015 return GetBlock()->GetGraph()->GetIntConstant(
4016 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004017 }
4018 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004019 return GetBlock()->GetGraph()->GetLongConstant(
4020 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004021 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004022
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004023 static SideEffects SideEffectsForArchRuntimeCalls() {
4024 // The generated code can use a runtime call.
4025 return SideEffects::CanTriggerGC();
4026 }
4027
Calin Juravle7c4954d2014-10-28 16:57:40 +00004028 DECLARE_INSTRUCTION(Div);
4029
4030 private:
4031 DISALLOW_COPY_AND_ASSIGN(HDiv);
4032};
4033
Calin Juravlebacfec32014-11-14 15:54:36 +00004034class HRem : public HBinaryOperation {
4035 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004036 HRem(Primitive::Type result_type,
4037 HInstruction* left,
4038 HInstruction* right,
4039 uint32_t dex_pc)
4040 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004041
Roland Levillain9867bc72015-08-05 10:21:34 +01004042 template <typename T>
4043 T Compute(T x, T y) const {
4044 // Our graph structure ensures we never have 0 for `y` during
4045 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004046 DCHECK_NE(y, 0);
4047 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4048 return (y == -1) ? 0 : x % y;
4049 }
4050
Roland Levillain9867bc72015-08-05 10:21:34 +01004051 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004052 return GetBlock()->GetGraph()->GetIntConstant(
4053 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004054 }
4055 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004056 return GetBlock()->GetGraph()->GetLongConstant(
4057 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004058 }
4059
Calin Juravlebacfec32014-11-14 15:54:36 +00004060
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004061 static SideEffects SideEffectsForArchRuntimeCalls() {
4062 return SideEffects::CanTriggerGC();
4063 }
4064
Calin Juravlebacfec32014-11-14 15:54:36 +00004065 DECLARE_INSTRUCTION(Rem);
4066
4067 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004068 DISALLOW_COPY_AND_ASSIGN(HRem);
4069};
4070
Calin Juravled0d48522014-11-04 16:40:20 +00004071class HDivZeroCheck : public HExpression<1> {
4072 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004073 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4074 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004075 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004076 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004077 SetRawInputAt(0, value);
4078 }
4079
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004080 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4081
Calin Juravled0d48522014-11-04 16:40:20 +00004082 bool CanBeMoved() const OVERRIDE { return true; }
4083
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004084 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004085 return true;
4086 }
4087
4088 bool NeedsEnvironment() const OVERRIDE { return true; }
4089 bool CanThrow() const OVERRIDE { return true; }
4090
Calin Juravled0d48522014-11-04 16:40:20 +00004091 DECLARE_INSTRUCTION(DivZeroCheck);
4092
4093 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004094 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4095};
4096
Calin Juravle9aec02f2014-11-18 23:06:35 +00004097class HShl : public HBinaryOperation {
4098 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004099 HShl(Primitive::Type result_type,
4100 HInstruction* left,
4101 HInstruction* right,
4102 uint32_t dex_pc = kNoDexPc)
4103 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004104
Roland Levillain9867bc72015-08-05 10:21:34 +01004105 template <typename T, typename U, typename V>
4106 T Compute(T x, U y, V max_shift_value) const {
4107 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4108 "V is not the unsigned integer type corresponding to T");
4109 return x << (y & max_shift_value);
4110 }
4111
4112 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4113 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004114 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004115 }
4116 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4117 // case is handled as `x << static_cast<int>(y)`.
4118 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4119 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004120 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004121 }
4122 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4123 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004124 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004125 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004126
4127 DECLARE_INSTRUCTION(Shl);
4128
4129 private:
4130 DISALLOW_COPY_AND_ASSIGN(HShl);
4131};
4132
4133class HShr : public HBinaryOperation {
4134 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004135 HShr(Primitive::Type result_type,
4136 HInstruction* left,
4137 HInstruction* right,
4138 uint32_t dex_pc = kNoDexPc)
4139 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004140
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 template <typename T, typename U, typename V>
4142 T Compute(T x, U y, V max_shift_value) const {
4143 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4144 "V is not the unsigned integer type corresponding to T");
4145 return x >> (y & max_shift_value);
4146 }
4147
4148 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4149 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004150 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004151 }
4152 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4153 // case is handled as `x >> static_cast<int>(y)`.
4154 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4155 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004156 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004157 }
4158 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4159 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004160 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004161 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004162
4163 DECLARE_INSTRUCTION(Shr);
4164
4165 private:
4166 DISALLOW_COPY_AND_ASSIGN(HShr);
4167};
4168
4169class HUShr : public HBinaryOperation {
4170 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004171 HUShr(Primitive::Type result_type,
4172 HInstruction* left,
4173 HInstruction* right,
4174 uint32_t dex_pc = kNoDexPc)
4175 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004176
Roland Levillain9867bc72015-08-05 10:21:34 +01004177 template <typename T, typename U, typename V>
4178 T Compute(T x, U y, V max_shift_value) const {
4179 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4180 "V is not the unsigned integer type corresponding to T");
4181 V ux = static_cast<V>(x);
4182 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004183 }
4184
Roland Levillain9867bc72015-08-05 10:21:34 +01004185 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4186 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004187 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004188 }
4189 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4190 // case is handled as `x >>> static_cast<int>(y)`.
4191 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4192 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004193 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004194 }
4195 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4196 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004197 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004198 }
4199
4200 DECLARE_INSTRUCTION(UShr);
4201
4202 private:
4203 DISALLOW_COPY_AND_ASSIGN(HUShr);
4204};
4205
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004206class HAnd : public HBinaryOperation {
4207 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004208 HAnd(Primitive::Type result_type,
4209 HInstruction* left,
4210 HInstruction* right,
4211 uint32_t dex_pc = kNoDexPc)
4212 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004213
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004214 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004215
Roland Levillain9867bc72015-08-05 10:21:34 +01004216 template <typename T, typename U>
4217 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4218
4219 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004220 return GetBlock()->GetGraph()->GetIntConstant(
4221 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004222 }
4223 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004224 return GetBlock()->GetGraph()->GetLongConstant(
4225 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004226 }
4227 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004228 return GetBlock()->GetGraph()->GetLongConstant(
4229 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004230 }
4231 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004232 return GetBlock()->GetGraph()->GetLongConstant(
4233 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004234 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004235
4236 DECLARE_INSTRUCTION(And);
4237
4238 private:
4239 DISALLOW_COPY_AND_ASSIGN(HAnd);
4240};
4241
4242class HOr : public HBinaryOperation {
4243 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004244 HOr(Primitive::Type result_type,
4245 HInstruction* left,
4246 HInstruction* right,
4247 uint32_t dex_pc = kNoDexPc)
4248 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004249
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004250 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004251
Roland Levillain9867bc72015-08-05 10:21:34 +01004252 template <typename T, typename U>
4253 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4254
4255 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004256 return GetBlock()->GetGraph()->GetIntConstant(
4257 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004258 }
4259 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004260 return GetBlock()->GetGraph()->GetLongConstant(
4261 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004262 }
4263 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004264 return GetBlock()->GetGraph()->GetLongConstant(
4265 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004266 }
4267 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004268 return GetBlock()->GetGraph()->GetLongConstant(
4269 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004270 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004271
4272 DECLARE_INSTRUCTION(Or);
4273
4274 private:
4275 DISALLOW_COPY_AND_ASSIGN(HOr);
4276};
4277
4278class HXor : public HBinaryOperation {
4279 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004280 HXor(Primitive::Type result_type,
4281 HInstruction* left,
4282 HInstruction* right,
4283 uint32_t dex_pc = kNoDexPc)
4284 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004285
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004286 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004287
Roland Levillain9867bc72015-08-05 10:21:34 +01004288 template <typename T, typename U>
4289 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4290
4291 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004292 return GetBlock()->GetGraph()->GetIntConstant(
4293 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004294 }
4295 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004296 return GetBlock()->GetGraph()->GetLongConstant(
4297 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004298 }
4299 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004300 return GetBlock()->GetGraph()->GetLongConstant(
4301 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004302 }
4303 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004304 return GetBlock()->GetGraph()->GetLongConstant(
4305 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004306 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004307
4308 DECLARE_INSTRUCTION(Xor);
4309
4310 private:
4311 DISALLOW_COPY_AND_ASSIGN(HXor);
4312};
4313
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004314class HRor : public HBinaryOperation {
4315 public:
4316 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4317 : HBinaryOperation(result_type, value, distance) {}
4318
4319 template <typename T, typename U, typename V>
4320 T Compute(T x, U y, V max_shift_value) const {
4321 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4322 "V is not the unsigned integer type corresponding to T");
4323 V ux = static_cast<V>(x);
4324 if ((y & max_shift_value) == 0) {
4325 return static_cast<T>(ux);
4326 } else {
4327 const V reg_bits = sizeof(T) * 8;
4328 return static_cast<T>(ux >> (y & max_shift_value)) |
4329 (x << (reg_bits - (y & max_shift_value)));
4330 }
4331 }
4332
4333 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4334 return GetBlock()->GetGraph()->GetIntConstant(
4335 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4336 }
4337 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4338 return GetBlock()->GetGraph()->GetLongConstant(
4339 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4340 }
4341 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4342 return GetBlock()->GetGraph()->GetLongConstant(
4343 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4344 }
4345
4346 DECLARE_INSTRUCTION(Ror);
4347
4348 private:
4349 DISALLOW_COPY_AND_ASSIGN(HRor);
4350};
4351
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004352// The value of a parameter in this method. Its location depends on
4353// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004354class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004355 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004356 HParameterValue(const DexFile& dex_file,
4357 uint16_t type_index,
4358 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004359 Primitive::Type parameter_type,
4360 bool is_this = false)
4361 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004362 dex_file_(dex_file),
4363 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004364 index_(index),
4365 is_this_(is_this),
4366 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004367
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004368 const DexFile& GetDexFile() const { return dex_file_; }
4369 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004370 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004371 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004372
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004373 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4374 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004375
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004376 DECLARE_INSTRUCTION(ParameterValue);
4377
4378 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004379 const DexFile& dex_file_;
4380 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004381 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004382 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004383 const uint8_t index_;
4384
Calin Juravle10e244f2015-01-26 18:54:32 +00004385 // Whether or not the parameter value corresponds to 'this' argument.
4386 const bool is_this_;
4387
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004388 bool can_be_null_;
4389
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004390 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4391};
4392
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004393class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004394 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004395 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4396 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004397
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004398 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004399 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004400 return true;
4401 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004402
Roland Levillain9867bc72015-08-05 10:21:34 +01004403 template <typename T> T Compute(T x) const { return ~x; }
4404
4405 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004406 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004407 }
4408 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004409 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004410 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004411
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004412 DECLARE_INSTRUCTION(Not);
4413
4414 private:
4415 DISALLOW_COPY_AND_ASSIGN(HNot);
4416};
4417
David Brazdil66d126e2015-04-03 16:02:44 +01004418class HBooleanNot : public HUnaryOperation {
4419 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004420 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4421 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004422
4423 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004424 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004425 return true;
4426 }
4427
Roland Levillain9867bc72015-08-05 10:21:34 +01004428 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004429 DCHECK(IsUint<1>(x));
4430 return !x;
4431 }
4432
Roland Levillain9867bc72015-08-05 10:21:34 +01004433 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004434 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004435 }
4436 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4437 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004438 UNREACHABLE();
4439 }
4440
4441 DECLARE_INSTRUCTION(BooleanNot);
4442
4443 private:
4444 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4445};
4446
Roland Levillaindff1f282014-11-05 14:15:05 +00004447class HTypeConversion : public HExpression<1> {
4448 public:
4449 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004450 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004451 : HExpression(result_type,
4452 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4453 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004454 SetRawInputAt(0, input);
4455 DCHECK_NE(input->GetType(), result_type);
4456 }
4457
4458 HInstruction* GetInput() const { return InputAt(0); }
4459 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4460 Primitive::Type GetResultType() const { return GetType(); }
4461
4462 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004463 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004464
Mark Mendelle82549b2015-05-06 10:55:34 -04004465 // Try to statically evaluate the conversion and return a HConstant
4466 // containing the result. If the input cannot be converted, return nullptr.
4467 HConstant* TryStaticEvaluation() const;
4468
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004469 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4470 Primitive::Type result_type) {
4471 // Some architectures may not require the 'GC' side effects, but at this point
4472 // in the compilation process we do not know what architecture we will
4473 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004474 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4475 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004476 return SideEffects::CanTriggerGC();
4477 }
4478 return SideEffects::None();
4479 }
4480
Roland Levillaindff1f282014-11-05 14:15:05 +00004481 DECLARE_INSTRUCTION(TypeConversion);
4482
4483 private:
4484 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4485};
4486
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004487static constexpr uint32_t kNoRegNumber = -1;
4488
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004489class HPhi : public HInstruction {
4490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004491 HPhi(ArenaAllocator* arena,
4492 uint32_t reg_number,
4493 size_t number_of_inputs,
4494 Primitive::Type type,
4495 uint32_t dex_pc = kNoDexPc)
4496 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004497 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004498 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004499 type_(ToPhiType(type)),
4500 // Phis are constructed live and marked dead if conflicting or unused.
4501 // Individual steps of SsaBuilder should assume that if a phi has been
4502 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4503 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004504 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004505 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004506 }
4507
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004508 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4509 static Primitive::Type ToPhiType(Primitive::Type type) {
4510 switch (type) {
4511 case Primitive::kPrimBoolean:
4512 case Primitive::kPrimByte:
4513 case Primitive::kPrimShort:
4514 case Primitive::kPrimChar:
4515 return Primitive::kPrimInt;
4516 default:
4517 return type;
4518 }
4519 }
4520
David Brazdilffee3d32015-07-06 11:48:53 +01004521 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4522
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004523 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004524
4525 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004526 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004527
Calin Juravle10e244f2015-01-26 18:54:32 +00004528 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004529 void SetType(Primitive::Type new_type) {
4530 // Make sure that only valid type changes occur. The following are allowed:
4531 // (1) int -> float/ref (primitive type propagation),
4532 // (2) long -> double (primitive type propagation).
4533 DCHECK(type_ == new_type ||
4534 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4535 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4536 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4537 type_ = new_type;
4538 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004539
Calin Juravle10e244f2015-01-26 18:54:32 +00004540 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4541 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4542
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004543 uint32_t GetRegNumber() const { return reg_number_; }
4544
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004545 void SetDead() { is_live_ = false; }
4546 void SetLive() { is_live_ = true; }
4547 bool IsDead() const { return !is_live_; }
4548 bool IsLive() const { return is_live_; }
4549
David Brazdil77a48ae2015-09-15 12:34:04 +00004550 bool IsVRegEquivalentOf(HInstruction* other) const {
4551 return other != nullptr
4552 && other->IsPhi()
4553 && other->AsPhi()->GetBlock() == GetBlock()
4554 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4555 }
4556
Calin Juravlea4f88312015-04-16 12:57:19 +01004557 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4558 // An equivalent phi is a phi having the same dex register and type.
4559 // It assumes that phis with the same dex register are adjacent.
4560 HPhi* GetNextEquivalentPhiWithSameType() {
4561 HInstruction* next = GetNext();
4562 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4563 if (next->GetType() == GetType()) {
4564 return next->AsPhi();
4565 }
4566 next = next->GetNext();
4567 }
4568 return nullptr;
4569 }
4570
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004571 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004572
David Brazdil1abb4192015-02-17 18:33:36 +00004573 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004574 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004575 return inputs_[index];
4576 }
David Brazdil1abb4192015-02-17 18:33:36 +00004577
4578 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004579 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004580 }
4581
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004582 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004583 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004584 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004585 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004586 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004587 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004588
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004589 DISALLOW_COPY_AND_ASSIGN(HPhi);
4590};
4591
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004592class HNullCheck : public HExpression<1> {
4593 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004594 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4595 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004596 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004597 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598 SetRawInputAt(0, value);
4599 }
4600
Calin Juravle10e244f2015-01-26 18:54:32 +00004601 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004602 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004603 return true;
4604 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004605
Calin Juravle10e244f2015-01-26 18:54:32 +00004606 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607
Calin Juravle10e244f2015-01-26 18:54:32 +00004608 bool CanThrow() const OVERRIDE { return true; }
4609
4610 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004611
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004612
4613 DECLARE_INSTRUCTION(NullCheck);
4614
4615 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004616 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4617};
4618
4619class FieldInfo : public ValueObject {
4620 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004621 FieldInfo(MemberOffset field_offset,
4622 Primitive::Type field_type,
4623 bool is_volatile,
4624 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004625 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004626 const DexFile& dex_file,
4627 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004628 : field_offset_(field_offset),
4629 field_type_(field_type),
4630 is_volatile_(is_volatile),
4631 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004632 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004633 dex_file_(dex_file),
4634 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004635
4636 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004637 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004638 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004639 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004640 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004641 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004642 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643
4644 private:
4645 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004646 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004647 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004648 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004649 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004650 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004651 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004652};
4653
4654class HInstanceFieldGet : public HExpression<1> {
4655 public:
4656 HInstanceFieldGet(HInstruction* value,
4657 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004658 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004659 bool is_volatile,
4660 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004661 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004662 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004664 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004665 : HExpression(field_type,
4666 SideEffects::FieldReadOfType(field_type, is_volatile),
4667 dex_pc),
4668 field_info_(field_offset,
4669 field_type,
4670 is_volatile,
4671 field_idx,
4672 declaring_class_def_index,
4673 dex_file,
4674 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004675 SetRawInputAt(0, value);
4676 }
4677
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004678 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004679
4680 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4681 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4682 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004683 }
4684
Calin Juravle641547a2015-04-21 22:08:51 +01004685 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4686 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004687 }
4688
4689 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004690 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4691 }
4692
Calin Juravle52c48962014-12-16 17:02:57 +00004693 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004694 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004695 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004696 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004697
4698 DECLARE_INSTRUCTION(InstanceFieldGet);
4699
4700 private:
4701 const FieldInfo field_info_;
4702
4703 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4704};
4705
4706class HInstanceFieldSet : public HTemplateInstruction<2> {
4707 public:
4708 HInstanceFieldSet(HInstruction* object,
4709 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004710 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004711 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004712 bool is_volatile,
4713 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004714 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004715 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004716 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004717 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004718 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4719 dex_pc),
4720 field_info_(field_offset,
4721 field_type,
4722 is_volatile,
4723 field_idx,
4724 declaring_class_def_index,
4725 dex_file,
4726 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004727 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004728 SetRawInputAt(0, object);
4729 SetRawInputAt(1, value);
4730 }
4731
Calin Juravle641547a2015-04-21 22:08:51 +01004732 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4733 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004734 }
4735
Calin Juravle52c48962014-12-16 17:02:57 +00004736 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004737 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004738 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004739 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004740 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004741 bool GetValueCanBeNull() const { return value_can_be_null_; }
4742 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004743
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004744 DECLARE_INSTRUCTION(InstanceFieldSet);
4745
4746 private:
4747 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004748 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004749
4750 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4751};
4752
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004753class HArrayGet : public HExpression<2> {
4754 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004755 HArrayGet(HInstruction* array,
4756 HInstruction* index,
4757 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004758 uint32_t dex_pc,
4759 SideEffects additional_side_effects = SideEffects::None())
4760 : HExpression(type,
4761 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004762 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004763 SetRawInputAt(0, array);
4764 SetRawInputAt(1, index);
4765 }
4766
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004767 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004768 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004769 return true;
4770 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004771 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004772 // TODO: We can be smarter here.
4773 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4774 // which generates the implicit null check. There are cases when these can be removed
4775 // to produce better code. If we ever add optimizations to do so we should allow an
4776 // implicit check here (as long as the address falls in the first page).
4777 return false;
4778 }
4779
David Brazdil4833f5a2015-12-16 10:37:39 +00004780 bool IsEquivalentOf(HArrayGet* other) const {
4781 bool result = (GetDexPc() == other->GetDexPc());
4782 if (kIsDebugBuild && result) {
4783 DCHECK_EQ(GetBlock(), other->GetBlock());
4784 DCHECK_EQ(GetArray(), other->GetArray());
4785 DCHECK_EQ(GetIndex(), other->GetIndex());
4786 if (Primitive::IsIntOrLongType(GetType())) {
4787 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4788 } else {
4789 DCHECK(Primitive::IsFloatingPointType(GetType()));
4790 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4791 }
4792 }
4793 return result;
4794 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004795
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004796 HInstruction* GetArray() const { return InputAt(0); }
4797 HInstruction* GetIndex() const { return InputAt(1); }
4798
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004799 DECLARE_INSTRUCTION(ArrayGet);
4800
4801 private:
4802 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4803};
4804
4805class HArraySet : public HTemplateInstruction<3> {
4806 public:
4807 HArraySet(HInstruction* array,
4808 HInstruction* index,
4809 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004810 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004811 uint32_t dex_pc,
4812 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004813 : HTemplateInstruction(
4814 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004815 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4816 additional_side_effects),
4817 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004818 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004819 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004820 value_can_be_null_(true),
4821 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004822 SetRawInputAt(0, array);
4823 SetRawInputAt(1, index);
4824 SetRawInputAt(2, value);
4825 }
4826
Calin Juravle77520bc2015-01-12 18:45:46 +00004827 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00004828 // We call a runtime method to throw ArrayStoreException.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004829 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004830 }
4831
Mingyao Yang81014cb2015-06-02 03:16:27 -07004832 // Can throw ArrayStoreException.
4833 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4834
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004835 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004836 // TODO: Same as for ArrayGet.
4837 return false;
4838 }
4839
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004840 void ClearNeedsTypeCheck() {
4841 needs_type_check_ = false;
4842 }
4843
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004844 void ClearValueCanBeNull() {
4845 value_can_be_null_ = false;
4846 }
4847
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004848 void SetStaticTypeOfArrayIsObjectArray() {
4849 static_type_of_array_is_object_array_ = true;
4850 }
4851
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004852 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004853 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004854 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004855
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004856 HInstruction* GetArray() const { return InputAt(0); }
4857 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004858 HInstruction* GetValue() const { return InputAt(2); }
4859
4860 Primitive::Type GetComponentType() const {
4861 // The Dex format does not type floating point index operations. Since the
4862 // `expected_component_type_` is set during building and can therefore not
4863 // be correct, we also check what is the value type. If it is a floating
4864 // point type, we must use that type.
4865 Primitive::Type value_type = GetValue()->GetType();
4866 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4867 ? value_type
4868 : expected_component_type_;
4869 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004870
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004871 Primitive::Type GetRawExpectedComponentType() const {
4872 return expected_component_type_;
4873 }
4874
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004875 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4876 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4877 }
4878
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879 DECLARE_INSTRUCTION(ArraySet);
4880
4881 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004882 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004883 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004884 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004885 // Cached information for the reference_type_info_ so that codegen
4886 // does not need to inspect the static type.
4887 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004888
4889 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4890};
4891
4892class HArrayLength : public HExpression<1> {
4893 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004894 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004895 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004896 // Note that arrays do not change length, so the instruction does not
4897 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004898 SetRawInputAt(0, array);
4899 }
4900
Calin Juravle77520bc2015-01-12 18:45:46 +00004901 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004902 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004903 return true;
4904 }
Calin Juravle641547a2015-04-21 22:08:51 +01004905 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4906 return obj == InputAt(0);
4907 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004908
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004909 DECLARE_INSTRUCTION(ArrayLength);
4910
4911 private:
4912 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4913};
4914
4915class HBoundsCheck : public HExpression<2> {
4916 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004917 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4918 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004919 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004920 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004921 DCHECK(index->GetType() == Primitive::kPrimInt);
4922 SetRawInputAt(0, index);
4923 SetRawInputAt(1, length);
4924 }
4925
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004926 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004927 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004928 return true;
4929 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004930
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004931 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004933 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004934
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004935 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004936
4937 DECLARE_INSTRUCTION(BoundsCheck);
4938
4939 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004940 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4941};
4942
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004943class HSuspendCheck : public HTemplateInstruction<0> {
4944 public:
4945 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004946 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004947
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004948 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004949 return true;
4950 }
4951
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004952 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4953 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004954
4955 DECLARE_INSTRUCTION(SuspendCheck);
4956
4957 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004958 // Only used for code generation, in order to share the same slow path between back edges
4959 // of a same loop.
4960 SlowPathCode* slow_path_;
4961
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004962 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4963};
4964
David Srbecky0cf44932015-12-09 14:09:59 +00004965// Pseudo-instruction which provides the native debugger with mapping information.
4966// It ensures that we can generate line number and local variables at this point.
4967class HNativeDebugInfo : public HTemplateInstruction<0> {
4968 public:
4969 explicit HNativeDebugInfo(uint32_t dex_pc)
4970 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4971
4972 bool NeedsEnvironment() const OVERRIDE {
4973 return true;
4974 }
4975
4976 DECLARE_INSTRUCTION(NativeDebugInfo);
4977
4978 private:
4979 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4980};
4981
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004982/**
4983 * Instruction to load a Class object.
4984 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004985class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004986 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004987 HLoadClass(HCurrentMethod* current_method,
4988 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004989 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004990 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004991 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004992 bool needs_access_check,
4993 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004994 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004995 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004996 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004997 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004998 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004999 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005000 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00005001 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005002 // Referrers class should not need access check. We never inline unverified
5003 // methods so we can't possibly end up in this situation.
5004 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005005 SetRawInputAt(0, current_method);
5006 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005007
5008 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005009
5010 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005011 // Note that we don't need to test for generate_clinit_check_.
5012 // Whether or not we need to generate the clinit check is processed in
5013 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005014 return other->AsLoadClass()->type_index_ == type_index_ &&
5015 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005016 }
5017
5018 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5019
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005020 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005021 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005022 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005023
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005024 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005025 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005026 }
5027
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005028 bool MustGenerateClinitCheck() const {
5029 return generate_clinit_check_;
5030 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005031
Calin Juravle0ba218d2015-05-19 18:46:01 +01005032 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005033 // The entrypoint the code generator is going to call does not do
5034 // clinit of the class.
5035 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005036 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005037 }
5038
5039 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005040 return MustGenerateClinitCheck() ||
5041 (!is_referrers_class_ && !is_in_dex_cache_) ||
5042 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005043 }
5044
5045 bool NeedsAccessCheck() const {
5046 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005047 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005048
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005049 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005050 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005051 }
5052
Calin Juravleacf735c2015-02-12 15:25:22 +00005053 ReferenceTypeInfo GetLoadedClassRTI() {
5054 return loaded_class_rti_;
5055 }
5056
5057 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5058 // Make sure we only set exact types (the loaded class should never be merged).
5059 DCHECK(rti.IsExact());
5060 loaded_class_rti_ = rti;
5061 }
5062
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005063 const DexFile& GetDexFile() { return dex_file_; }
5064
Vladimir Markodc151b22015-10-15 18:02:30 +01005065 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005066
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005067 static SideEffects SideEffectsForArchRuntimeCalls() {
5068 return SideEffects::CanTriggerGC();
5069 }
5070
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005071 bool IsInDexCache() const { return is_in_dex_cache_; }
5072
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005073 DECLARE_INSTRUCTION(LoadClass);
5074
5075 private:
5076 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005077 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005078 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005079 // Whether this instruction must generate the initialization check.
5080 // Used for code generation.
5081 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005082 const bool needs_access_check_;
5083 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005084
Calin Juravleacf735c2015-02-12 15:25:22 +00005085 ReferenceTypeInfo loaded_class_rti_;
5086
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005087 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5088};
5089
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005090class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005091 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005092 HLoadString(HCurrentMethod* current_method,
5093 uint32_t string_index,
5094 uint32_t dex_pc,
5095 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005096 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005097 string_index_(string_index),
5098 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005099 SetRawInputAt(0, current_method);
5100 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005101
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005102 bool CanBeMoved() const OVERRIDE { return true; }
5103
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005104 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5105 return other->AsLoadString()->string_index_ == string_index_;
5106 }
5107
5108 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5109
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005110 uint32_t GetStringIndex() const { return string_index_; }
5111
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005112 // Will call the runtime if the string is not already in the dex cache.
5113 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5114
Vladimir Markodc151b22015-10-15 18:02:30 +01005115 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005116 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005117 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005118 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005119
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005120 static SideEffects SideEffectsForArchRuntimeCalls() {
5121 return SideEffects::CanTriggerGC();
5122 }
5123
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005124 DECLARE_INSTRUCTION(LoadString);
5125
5126 private:
5127 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005128 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005129
5130 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5131};
5132
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005133/**
5134 * Performs an initialization check on its Class object input.
5135 */
5136class HClinitCheck : public HExpression<1> {
5137 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005138 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005139 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005140 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005141 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5142 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005143 SetRawInputAt(0, constant);
5144 }
5145
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005146 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005147 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005148 return true;
5149 }
5150
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005151 bool NeedsEnvironment() const OVERRIDE {
5152 // May call runtime to initialize the class.
5153 return true;
5154 }
5155
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005156 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005157
5158 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5159
5160 DECLARE_INSTRUCTION(ClinitCheck);
5161
5162 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005163 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5164};
5165
5166class HStaticFieldGet : public HExpression<1> {
5167 public:
5168 HStaticFieldGet(HInstruction* cls,
5169 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005170 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005171 bool is_volatile,
5172 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005173 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005174 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005175 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005176 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005177 : HExpression(field_type,
5178 SideEffects::FieldReadOfType(field_type, is_volatile),
5179 dex_pc),
5180 field_info_(field_offset,
5181 field_type,
5182 is_volatile,
5183 field_idx,
5184 declaring_class_def_index,
5185 dex_file,
5186 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005187 SetRawInputAt(0, cls);
5188 }
5189
Calin Juravle52c48962014-12-16 17:02:57 +00005190
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005191 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005192
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005193 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005194 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5195 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005196 }
5197
5198 size_t ComputeHashCode() const OVERRIDE {
5199 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5200 }
5201
Calin Juravle52c48962014-12-16 17:02:57 +00005202 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005203 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5204 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005205 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005206
5207 DECLARE_INSTRUCTION(StaticFieldGet);
5208
5209 private:
5210 const FieldInfo field_info_;
5211
5212 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5213};
5214
5215class HStaticFieldSet : public HTemplateInstruction<2> {
5216 public:
5217 HStaticFieldSet(HInstruction* cls,
5218 HInstruction* value,
5219 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005220 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005221 bool is_volatile,
5222 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005223 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005224 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005225 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005226 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005227 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5228 dex_pc),
5229 field_info_(field_offset,
5230 field_type,
5231 is_volatile,
5232 field_idx,
5233 declaring_class_def_index,
5234 dex_file,
5235 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005236 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005237 SetRawInputAt(0, cls);
5238 SetRawInputAt(1, value);
5239 }
5240
Calin Juravle52c48962014-12-16 17:02:57 +00005241 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005242 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5243 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005244 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005245
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005246 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005247 bool GetValueCanBeNull() const { return value_can_be_null_; }
5248 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005249
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005250 DECLARE_INSTRUCTION(StaticFieldSet);
5251
5252 private:
5253 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005254 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005255
5256 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5257};
5258
Calin Juravlee460d1d2015-09-29 04:52:17 +01005259class HUnresolvedInstanceFieldGet : public HExpression<1> {
5260 public:
5261 HUnresolvedInstanceFieldGet(HInstruction* obj,
5262 Primitive::Type field_type,
5263 uint32_t field_index,
5264 uint32_t dex_pc)
5265 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5266 field_index_(field_index) {
5267 SetRawInputAt(0, obj);
5268 }
5269
5270 bool NeedsEnvironment() const OVERRIDE { return true; }
5271 bool CanThrow() const OVERRIDE { return true; }
5272
5273 Primitive::Type GetFieldType() const { return GetType(); }
5274 uint32_t GetFieldIndex() const { return field_index_; }
5275
5276 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5277
5278 private:
5279 const uint32_t field_index_;
5280
5281 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5282};
5283
5284class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5285 public:
5286 HUnresolvedInstanceFieldSet(HInstruction* obj,
5287 HInstruction* value,
5288 Primitive::Type field_type,
5289 uint32_t field_index,
5290 uint32_t dex_pc)
5291 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5292 field_type_(field_type),
5293 field_index_(field_index) {
5294 DCHECK_EQ(field_type, value->GetType());
5295 SetRawInputAt(0, obj);
5296 SetRawInputAt(1, value);
5297 }
5298
5299 bool NeedsEnvironment() const OVERRIDE { return true; }
5300 bool CanThrow() const OVERRIDE { return true; }
5301
5302 Primitive::Type GetFieldType() const { return field_type_; }
5303 uint32_t GetFieldIndex() const { return field_index_; }
5304
5305 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5306
5307 private:
5308 const Primitive::Type field_type_;
5309 const uint32_t field_index_;
5310
5311 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5312};
5313
5314class HUnresolvedStaticFieldGet : public HExpression<0> {
5315 public:
5316 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5317 uint32_t field_index,
5318 uint32_t dex_pc)
5319 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5320 field_index_(field_index) {
5321 }
5322
5323 bool NeedsEnvironment() const OVERRIDE { return true; }
5324 bool CanThrow() const OVERRIDE { return true; }
5325
5326 Primitive::Type GetFieldType() const { return GetType(); }
5327 uint32_t GetFieldIndex() const { return field_index_; }
5328
5329 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5330
5331 private:
5332 const uint32_t field_index_;
5333
5334 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5335};
5336
5337class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5338 public:
5339 HUnresolvedStaticFieldSet(HInstruction* value,
5340 Primitive::Type field_type,
5341 uint32_t field_index,
5342 uint32_t dex_pc)
5343 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5344 field_type_(field_type),
5345 field_index_(field_index) {
5346 DCHECK_EQ(field_type, value->GetType());
5347 SetRawInputAt(0, value);
5348 }
5349
5350 bool NeedsEnvironment() const OVERRIDE { return true; }
5351 bool CanThrow() const OVERRIDE { return true; }
5352
5353 Primitive::Type GetFieldType() const { return field_type_; }
5354 uint32_t GetFieldIndex() const { return field_index_; }
5355
5356 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5357
5358 private:
5359 const Primitive::Type field_type_;
5360 const uint32_t field_index_;
5361
5362 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5363};
5364
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005365// Implement the move-exception DEX instruction.
5366class HLoadException : public HExpression<0> {
5367 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005368 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5369 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005370
David Brazdilbbd733e2015-08-18 17:48:17 +01005371 bool CanBeNull() const OVERRIDE { return false; }
5372
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005373 DECLARE_INSTRUCTION(LoadException);
5374
5375 private:
5376 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5377};
5378
David Brazdilcb1c0552015-08-04 16:22:25 +01005379// Implicit part of move-exception which clears thread-local exception storage.
5380// Must not be removed because the runtime expects the TLS to get cleared.
5381class HClearException : public HTemplateInstruction<0> {
5382 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005383 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5384 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005385
5386 DECLARE_INSTRUCTION(ClearException);
5387
5388 private:
5389 DISALLOW_COPY_AND_ASSIGN(HClearException);
5390};
5391
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005392class HThrow : public HTemplateInstruction<1> {
5393 public:
5394 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005395 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005396 SetRawInputAt(0, exception);
5397 }
5398
5399 bool IsControlFlow() const OVERRIDE { return true; }
5400
5401 bool NeedsEnvironment() const OVERRIDE { return true; }
5402
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005403 bool CanThrow() const OVERRIDE { return true; }
5404
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005405
5406 DECLARE_INSTRUCTION(Throw);
5407
5408 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005409 DISALLOW_COPY_AND_ASSIGN(HThrow);
5410};
5411
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005412/**
5413 * Implementation strategies for the code generator of a HInstanceOf
5414 * or `HCheckCast`.
5415 */
5416enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005417 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005418 kExactCheck, // Can do a single class compare.
5419 kClassHierarchyCheck, // Can just walk the super class chain.
5420 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5421 kInterfaceCheck, // No optimization yet when checking against an interface.
5422 kArrayObjectCheck, // Can just check if the array is not primitive.
5423 kArrayCheck // No optimization yet when checking against a generic array.
5424};
5425
Roland Levillain86503782016-02-11 19:07:30 +00005426std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5427
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005428class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005429 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005430 HInstanceOf(HInstruction* object,
5431 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005432 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005433 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005434 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005435 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005436 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005437 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005438 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005439 SetRawInputAt(0, object);
5440 SetRawInputAt(1, constant);
5441 }
5442
5443 bool CanBeMoved() const OVERRIDE { return true; }
5444
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005445 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005446 return true;
5447 }
5448
5449 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005450 return CanCallRuntime(check_kind_);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005451 }
5452
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005453 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5454
5455 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005456
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005457 // Used only in code generation.
5458 bool MustDoNullCheck() const { return must_do_null_check_; }
5459 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5460
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005461 static bool CanCallRuntime(TypeCheckKind check_kind) {
5462 // Mips currently does runtime calls for any other checks.
5463 return check_kind != TypeCheckKind::kExactCheck;
5464 }
5465
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005466 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005467 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005468 }
5469
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005470 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005471
5472 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005473 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005474 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005475
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005476 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5477};
5478
Calin Juravleb1498f62015-02-16 13:13:29 +00005479class HBoundType : public HExpression<1> {
5480 public:
David Brazdilf5552582015-12-27 13:36:12 +00005481 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005482 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005483 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5484 upper_can_be_null_(true),
5485 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005486 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005487 SetRawInputAt(0, input);
5488 }
5489
David Brazdilf5552582015-12-27 13:36:12 +00005490 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005491 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5492 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005493 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005494
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005495 void SetCanBeNull(bool can_be_null) {
5496 DCHECK(upper_can_be_null_ || !can_be_null);
5497 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005498 }
5499
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005500 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5501
Calin Juravleb1498f62015-02-16 13:13:29 +00005502 DECLARE_INSTRUCTION(BoundType);
5503
5504 private:
5505 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005506 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5507 // It is used to bound the type in cases like:
5508 // if (x instanceof ClassX) {
5509 // // uper_bound_ will be ClassX
5510 // }
David Brazdilf5552582015-12-27 13:36:12 +00005511 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005512 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5513 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005514 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005515 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005516
5517 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5518};
5519
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005520class HCheckCast : public HTemplateInstruction<2> {
5521 public:
5522 HCheckCast(HInstruction* object,
5523 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005524 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005525 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005526 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005527 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005528 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005529 SetRawInputAt(0, object);
5530 SetRawInputAt(1, constant);
5531 }
5532
5533 bool CanBeMoved() const OVERRIDE { return true; }
5534
5535 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5536 return true;
5537 }
5538
5539 bool NeedsEnvironment() const OVERRIDE {
5540 // Instruction may throw a CheckCastError.
5541 return true;
5542 }
5543
5544 bool CanThrow() const OVERRIDE { return true; }
5545
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005546 bool MustDoNullCheck() const { return must_do_null_check_; }
5547 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005548 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005549
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005550 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005551
5552 DECLARE_INSTRUCTION(CheckCast);
5553
5554 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005555 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005556 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005557
5558 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005559};
5560
Calin Juravle27df7582015-04-17 19:12:31 +01005561class HMemoryBarrier : public HTemplateInstruction<0> {
5562 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005563 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005564 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005565 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005566 barrier_kind_(barrier_kind) {}
5567
5568 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5569
5570 DECLARE_INSTRUCTION(MemoryBarrier);
5571
5572 private:
5573 const MemBarrierKind barrier_kind_;
5574
5575 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5576};
5577
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005578class HMonitorOperation : public HTemplateInstruction<1> {
5579 public:
5580 enum OperationKind {
5581 kEnter,
5582 kExit,
5583 };
5584
5585 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005586 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005587 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5588 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005589 SetRawInputAt(0, object);
5590 }
5591
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005592 // Instruction may go into runtime, so we need an environment.
5593 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00005594
5595 bool CanThrow() const OVERRIDE {
5596 // Verifier guarantees that monitor-exit cannot throw.
5597 // This is important because it allows the HGraphBuilder to remove
5598 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5599 return IsEnter();
5600 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005601
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005602
5603 bool IsEnter() const { return kind_ == kEnter; }
5604
5605 DECLARE_INSTRUCTION(MonitorOperation);
5606
Calin Juravle52c48962014-12-16 17:02:57 +00005607 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005608 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005609
5610 private:
5611 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5612};
5613
David Brazdil74eb1b22015-12-14 11:44:01 +00005614class HSelect : public HExpression<3> {
5615 public:
5616 HSelect(HInstruction* condition,
5617 HInstruction* true_value,
5618 HInstruction* false_value,
5619 uint32_t dex_pc)
5620 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
5621 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
5622
5623 // First input must be `true_value` or `false_value` to allow codegens to
5624 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
5625 // that architectures which implement HSelect as a conditional move also
5626 // will not need to invert the condition.
5627 SetRawInputAt(0, false_value);
5628 SetRawInputAt(1, true_value);
5629 SetRawInputAt(2, condition);
5630 }
5631
5632 HInstruction* GetFalseValue() const { return InputAt(0); }
5633 HInstruction* GetTrueValue() const { return InputAt(1); }
5634 HInstruction* GetCondition() const { return InputAt(2); }
5635
5636 bool CanBeMoved() const OVERRIDE { return true; }
5637 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
5638
5639 bool CanBeNull() const OVERRIDE {
5640 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
5641 }
5642
5643 DECLARE_INSTRUCTION(Select);
5644
5645 private:
5646 DISALLOW_COPY_AND_ASSIGN(HSelect);
5647};
5648
Vladimir Markof9f64412015-09-02 14:05:49 +01005649class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005650 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005651 MoveOperands(Location source,
5652 Location destination,
5653 Primitive::Type type,
5654 HInstruction* instruction)
5655 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005656
5657 Location GetSource() const { return source_; }
5658 Location GetDestination() const { return destination_; }
5659
5660 void SetSource(Location value) { source_ = value; }
5661 void SetDestination(Location value) { destination_ = value; }
5662
5663 // The parallel move resolver marks moves as "in-progress" by clearing the
5664 // destination (but not the source).
5665 Location MarkPending() {
5666 DCHECK(!IsPending());
5667 Location dest = destination_;
5668 destination_ = Location::NoLocation();
5669 return dest;
5670 }
5671
5672 void ClearPending(Location dest) {
5673 DCHECK(IsPending());
5674 destination_ = dest;
5675 }
5676
5677 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005678 DCHECK(source_.IsValid() || destination_.IsInvalid());
5679 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005680 }
5681
5682 // True if this blocks a move from the given location.
5683 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005684 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005685 }
5686
5687 // A move is redundant if it's been eliminated, if its source and
5688 // destination are the same, or if its destination is unneeded.
5689 bool IsRedundant() const {
5690 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5691 }
5692
5693 // We clear both operands to indicate move that's been eliminated.
5694 void Eliminate() {
5695 source_ = destination_ = Location::NoLocation();
5696 }
5697
5698 bool IsEliminated() const {
5699 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5700 return source_.IsInvalid();
5701 }
5702
Alexey Frunze4dda3372015-06-01 18:31:49 -07005703 Primitive::Type GetType() const { return type_; }
5704
Nicolas Geoffray90218252015-04-15 11:56:51 +01005705 bool Is64BitMove() const {
5706 return Primitive::Is64BitType(type_);
5707 }
5708
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005709 HInstruction* GetInstruction() const { return instruction_; }
5710
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005711 private:
5712 Location source_;
5713 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005714 // The type this move is for.
5715 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005716 // The instruction this move is assocatied with. Null when this move is
5717 // for moving an input in the expected locations of user (including a phi user).
5718 // This is only used in debug mode, to ensure we do not connect interval siblings
5719 // in the same parallel move.
5720 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005721};
5722
Roland Levillainc9285912015-12-18 10:38:42 +00005723std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5724
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005725static constexpr size_t kDefaultNumberOfMoves = 4;
5726
5727class HParallelMove : public HTemplateInstruction<0> {
5728 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005729 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005730 : HTemplateInstruction(SideEffects::None(), dex_pc),
5731 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5732 moves_.reserve(kDefaultNumberOfMoves);
5733 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005734
Nicolas Geoffray90218252015-04-15 11:56:51 +01005735 void AddMove(Location source,
5736 Location destination,
5737 Primitive::Type type,
5738 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005739 DCHECK(source.IsValid());
5740 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005741 if (kIsDebugBuild) {
5742 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005743 for (const MoveOperands& move : moves_) {
5744 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005745 // Special case the situation where the move is for the spill slot
5746 // of the instruction.
5747 if ((GetPrevious() == instruction)
5748 || ((GetPrevious() == nullptr)
5749 && instruction->IsPhi()
5750 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005751 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005752 << "Doing parallel moves for the same instruction.";
5753 } else {
5754 DCHECK(false) << "Doing parallel moves for the same instruction.";
5755 }
5756 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005757 }
5758 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005759 for (const MoveOperands& move : moves_) {
5760 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005761 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005762 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005763 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005764 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005765 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005766 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005767 }
5768
Vladimir Marko225b6462015-09-28 12:17:40 +01005769 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005770 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005771 }
5772
Vladimir Marko225b6462015-09-28 12:17:40 +01005773 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005774
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005775 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005776
5777 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005778 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005779
5780 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5781};
5782
Mark Mendell0616ae02015-04-17 12:49:27 -04005783} // namespace art
5784
Vladimir Markob4536b72015-11-24 13:45:23 +00005785#ifdef ART_ENABLE_CODEGEN_arm
5786#include "nodes_arm.h"
5787#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005788#ifdef ART_ENABLE_CODEGEN_arm64
5789#include "nodes_arm64.h"
5790#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005791#ifdef ART_ENABLE_CODEGEN_x86
5792#include "nodes_x86.h"
5793#endif
5794
5795namespace art {
5796
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005797class HGraphVisitor : public ValueObject {
5798 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005799 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5800 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005801
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005802 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005803 virtual void VisitBasicBlock(HBasicBlock* block);
5804
Roland Levillain633021e2014-10-01 14:12:25 +01005805 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005806 void VisitInsertionOrder();
5807
Roland Levillain633021e2014-10-01 14:12:25 +01005808 // Visit the graph following dominator tree reverse post-order.
5809 void VisitReversePostOrder();
5810
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005811 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005812
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005813 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005814#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005815 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5816
5817 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5818
5819#undef DECLARE_VISIT_INSTRUCTION
5820
5821 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005822 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005823
5824 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5825};
5826
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005827class HGraphDelegateVisitor : public HGraphVisitor {
5828 public:
5829 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5830 virtual ~HGraphDelegateVisitor() {}
5831
5832 // Visit functions that delegate to to super class.
5833#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005834 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005835
5836 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5837
5838#undef DECLARE_VISIT_INSTRUCTION
5839
5840 private:
5841 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5842};
5843
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005844class HInsertionOrderIterator : public ValueObject {
5845 public:
5846 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5847
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005848 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005849 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005850 void Advance() { ++index_; }
5851
5852 private:
5853 const HGraph& graph_;
5854 size_t index_;
5855
5856 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5857};
5858
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005859class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005860 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005861 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5862 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005863 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005864 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005865
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005866 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5867 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005868 void Advance() { ++index_; }
5869
5870 private:
5871 const HGraph& graph_;
5872 size_t index_;
5873
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005874 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005875};
5876
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005877class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005878 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005879 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005880 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005881 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005882 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005883 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005884
5885 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005886 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005887 void Advance() { --index_; }
5888
5889 private:
5890 const HGraph& graph_;
5891 size_t index_;
5892
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005893 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005894};
5895
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005896class HLinearPostOrderIterator : public ValueObject {
5897 public:
5898 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005899 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005900
5901 bool Done() const { return index_ == 0; }
5902
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005903 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005904
5905 void Advance() {
5906 --index_;
5907 DCHECK_GE(index_, 0U);
5908 }
5909
5910 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005911 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005912 size_t index_;
5913
5914 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5915};
5916
5917class HLinearOrderIterator : public ValueObject {
5918 public:
5919 explicit HLinearOrderIterator(const HGraph& graph)
5920 : order_(graph.GetLinearOrder()), index_(0) {}
5921
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005922 bool Done() const { return index_ == order_.size(); }
5923 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005924 void Advance() { ++index_; }
5925
5926 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005927 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005928 size_t index_;
5929
5930 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5931};
5932
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005933// Iterator over the blocks that art part of the loop. Includes blocks part
5934// of an inner loop. The order in which the blocks are iterated is on their
5935// block id.
5936class HBlocksInLoopIterator : public ValueObject {
5937 public:
5938 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5939 : blocks_in_loop_(info.GetBlocks()),
5940 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5941 index_(0) {
5942 if (!blocks_in_loop_.IsBitSet(index_)) {
5943 Advance();
5944 }
5945 }
5946
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005947 bool Done() const { return index_ == blocks_.size(); }
5948 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005949 void Advance() {
5950 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005951 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005952 if (blocks_in_loop_.IsBitSet(index_)) {
5953 break;
5954 }
5955 }
5956 }
5957
5958 private:
5959 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005960 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005961 size_t index_;
5962
5963 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5964};
5965
Mingyao Yang3584bce2015-05-19 16:01:59 -07005966// Iterator over the blocks that art part of the loop. Includes blocks part
5967// of an inner loop. The order in which the blocks are iterated is reverse
5968// post order.
5969class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5970 public:
5971 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5972 : blocks_in_loop_(info.GetBlocks()),
5973 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5974 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005975 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005976 Advance();
5977 }
5978 }
5979
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005980 bool Done() const { return index_ == blocks_.size(); }
5981 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005982 void Advance() {
5983 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005984 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5985 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005986 break;
5987 }
5988 }
5989 }
5990
5991 private:
5992 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005993 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005994 size_t index_;
5995
5996 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5997};
5998
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005999inline int64_t Int64FromConstant(HConstant* constant) {
6000 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
6001 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
6002 : constant->AsLongConstant()->GetValue();
6003}
6004
Vladimir Marko58155012015-08-19 12:49:41 +00006005inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6006 // For the purposes of the compiler, the dex files must actually be the same object
6007 // if we want to safely treat them as the same. This is especially important for JIT
6008 // as custom class loaders can open the same underlying file (or memory) multiple
6009 // times and provide different class resolution but no two class loaders should ever
6010 // use the same DexFile object - doing so is an unsupported hack that can lead to
6011 // all sorts of weird failures.
6012 return &lhs == &rhs;
6013}
6014
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006015#define INSTRUCTION_TYPE_CHECK(type, super) \
6016 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6017 inline const H##type* HInstruction::As##type() const { \
6018 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6019 } \
6020 inline H##type* HInstruction::As##type() { \
6021 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6022 }
6023
6024 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6025#undef INSTRUCTION_TYPE_CHECK
6026
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006027class SwitchTable : public ValueObject {
6028 public:
6029 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6030 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6031 int32_t table_offset = instruction.VRegB_31t();
6032 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6033 if (sparse) {
6034 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6035 } else {
6036 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6037 }
6038 num_entries_ = table[1];
6039 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6040 }
6041
6042 uint16_t GetNumEntries() const {
6043 return num_entries_;
6044 }
6045
6046 void CheckIndex(size_t index) const {
6047 if (sparse_) {
6048 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6049 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6050 } else {
6051 // In a packed table, we have the starting key and num_entries_ values.
6052 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6053 }
6054 }
6055
6056 int32_t GetEntryAt(size_t index) const {
6057 CheckIndex(index);
6058 return values_[index];
6059 }
6060
6061 uint32_t GetDexPcForIndex(size_t index) const {
6062 CheckIndex(index);
6063 return dex_pc_ +
6064 (reinterpret_cast<const int16_t*>(values_ + index) -
6065 reinterpret_cast<const int16_t*>(&instruction_));
6066 }
6067
6068 // Index of the first value in the table.
6069 size_t GetFirstValueIndex() const {
6070 if (sparse_) {
6071 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6072 return num_entries_;
6073 } else {
6074 // In a packed table, we have the starting key and num_entries_ values.
6075 return 1;
6076 }
6077 }
6078
6079 private:
6080 const Instruction& instruction_;
6081 const uint32_t dex_pc_;
6082
6083 // Whether this is a sparse-switch table (or a packed-switch one).
6084 const bool sparse_;
6085
6086 // This can't be const as it needs to be computed off of the given instruction, and complicated
6087 // expressions in the initializer list seemed very ugly.
6088 uint16_t num_entries_;
6089
6090 const int32_t* values_;
6091
6092 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6093};
6094
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006095} // namespace art
6096
6097#endif // ART_COMPILER_OPTIMIZING_NODES_H_