blob: e2f3b93f918ac5c99fce1042c4e949c440f3ecf0 [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
David Brazdil4833f5a2015-12-16 10:37:39 +0000100enum BuildSsaResult {
101 kBuildSsaFailNonNaturalLoop,
102 kBuildSsaFailThrowCatchLoop,
David Brazdil15693bf2015-12-16 10:30:45 +0000103 kBuildSsaFailAmbiguousArrayOp,
David Brazdil4833f5a2015-12-16 10:37:39 +0000104 kBuildSsaSuccess,
105};
106
Vladimir Markof9f64412015-09-02 14:05:49 +0100107class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100108 public:
109 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
110
111 void AddInstruction(HInstruction* instruction);
112 void RemoveInstruction(HInstruction* instruction);
113
David Brazdilc3d743f2015-04-22 13:40:50 +0100114 // Insert `instruction` before/after an existing instruction `cursor`.
115 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
116 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
117
Roland Levillain6b469232014-09-25 10:10:38 +0100118 // Return true if this list contains `instruction`.
119 bool Contains(HInstruction* instruction) const;
120
Roland Levillainccc07a92014-09-16 14:48:16 +0100121 // Return true if `instruction1` is found before `instruction2` in
122 // this instruction list and false otherwise. Abort if none
123 // of these instructions is found.
124 bool FoundBefore(const HInstruction* instruction1,
125 const HInstruction* instruction2) const;
126
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000127 bool IsEmpty() const { return first_instruction_ == nullptr; }
128 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
129
130 // Update the block of all instructions to be `block`.
131 void SetBlockOfInstructions(HBasicBlock* block) const;
132
133 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
134 void Add(const HInstructionList& instruction_list);
135
David Brazdil2d7352b2015-04-20 14:52:42 +0100136 // Return the number of instructions in the list. This is an expensive operation.
137 size_t CountSize() const;
138
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 private:
140 HInstruction* first_instruction_;
141 HInstruction* last_instruction_;
142
143 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000144 friend class HGraph;
145 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100146 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100147 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148
149 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
150};
151
David Brazdil4833f5a2015-12-16 10:37:39 +0000152class ReferenceTypeInfo : ValueObject {
153 public:
154 typedef Handle<mirror::Class> TypeHandle;
155
156 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
157 // The constructor will check that the type_handle is valid.
158 return ReferenceTypeInfo(type_handle, is_exact);
159 }
160
161 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
162
163 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
164 return handle.GetReference() != nullptr;
165 }
166
167 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
168 return IsValidHandle(type_handle_);
169 }
170
171 bool IsExact() const { return is_exact_; }
172
173 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
174 DCHECK(IsValid());
175 return GetTypeHandle()->IsObjectClass();
176 }
177
178 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
179 DCHECK(IsValid());
180 return GetTypeHandle()->IsStringClass();
181 }
182
183 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
184 DCHECK(IsValid());
185 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
186 }
187
188 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
189 DCHECK(IsValid());
190 return GetTypeHandle()->IsInterface();
191 }
192
193 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
194 DCHECK(IsValid());
195 return GetTypeHandle()->IsArrayClass();
196 }
197
198 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
199 DCHECK(IsValid());
200 return GetTypeHandle()->IsPrimitiveArray();
201 }
202
203 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
204 DCHECK(IsValid());
205 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
206 }
207
208 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
209 DCHECK(IsValid());
210 if (!IsExact()) return false;
211 if (!IsArrayClass()) return false;
212 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
213 }
214
215 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
216 DCHECK(IsValid());
217 if (!IsExact()) return false;
218 if (!IsArrayClass()) return false;
219 if (!rti.IsArrayClass()) return false;
220 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
221 rti.GetTypeHandle()->GetComponentType());
222 }
223
224 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
225
226 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
227 DCHECK(IsValid());
228 DCHECK(rti.IsValid());
229 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
230 }
231
232 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
233 DCHECK(IsValid());
234 DCHECK(rti.IsValid());
235 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
236 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
237 }
238
239 // Returns true if the type information provide the same amount of details.
240 // Note that it does not mean that the instructions have the same actual type
241 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000242 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 if (!IsValid() && !rti.IsValid()) {
244 // Invalid types are equal.
245 return true;
246 }
247 if (!IsValid() || !rti.IsValid()) {
248 // One is valid, the other not.
249 return false;
250 }
251 return IsExact() == rti.IsExact()
252 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
253 }
254
255 private:
256 ReferenceTypeInfo();
257 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
258
259 // The class of the object.
260 TypeHandle type_handle_;
261 // Whether or not the type is exact or a superclass of the actual type.
262 // Whether or not we have any information about this type.
263 bool is_exact_;
264};
265
266std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
267
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000268// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100269class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000270 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100271 HGraph(ArenaAllocator* arena,
272 const DexFile& dex_file,
273 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100274 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100276 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100277 bool debuggable = false,
278 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 Geoffraye0fe7ae2015-03-09 10:02:49 +0000291 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000292 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100293 dex_file_(dex_file),
294 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100295 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100296 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100297 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000299 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100300 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
301 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
302 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
303 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000304 cached_current_method_(nullptr),
305 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 blocks_.reserve(kDefaultNumberOfBlocks);
307 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000309 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
311
David Brazdil69ba7b72015-06-23 18:27:30 +0100312 bool IsInSsaForm() const { return in_ssa_form_; }
313
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000314 HBasicBlock* GetEntryBlock() const { return entry_block_; }
315 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100316 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000317
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000318 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
319 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000320
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000321 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100322
David Brazdil4833f5a2015-12-16 10:37:39 +0000323 // Try building the SSA form of this graph, with dominance computation and
324 // loop recognition. Returns a code specifying that it was successful or the
325 // reason for failure.
326 BuildSsaResult TryBuildingSsa(StackHandleScopeCollection* handles);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000327
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100328 void ComputeDominanceInformation();
329 void ClearDominanceInformation();
330
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100332 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100333 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334
David Brazdil4833f5a2015-12-16 10:37:39 +0000335 // Analyze all natural loops in this graph. Returns a code specifying that it
336 // was successful or the reason for failure. The method will fail if a loop
337 // is not natural, that is the header does not dominate a back edge, or if it
338 // is a throw-catch loop, i.e. the header is a catch block.
339 BuildSsaResult AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100340
David Brazdilffee3d32015-07-06 11:48:53 +0100341 // Iterate over blocks to compute try block membership. Needs reverse post
342 // order and loop information.
343 void ComputeTryBlockInformation();
344
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000345 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000346 // Returns the instruction used to replace the invoke expression or null if the
347 // invoke is for a void method.
348 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000349
Mingyao Yang3584bce2015-05-19 16:01:59 -0700350 // Need to add a couple of blocks to test if the loop body is entered and
351 // put deoptimization instructions, etc.
352 void TransformLoopHeaderForBCE(HBasicBlock* header);
353
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000354 // Removes `block` from the graph. Assumes `block` has been disconnected from
355 // other blocks and has no instructions or phis.
356 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000357
David Brazdilfc6a86a2015-06-26 10:33:45 +0000358 // Splits the edge between `block` and `successor` while preserving the
359 // indices in the predecessor/successor lists. If there are multiple edges
360 // between the blocks, the lowest indices are used.
361 // Returns the new block which is empty and has the same dex pc as `successor`.
362 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
363
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
365 void SimplifyLoop(HBasicBlock* header);
366
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000367 int32_t GetNextInstructionId() {
368 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000369 return current_instruction_id_++;
370 }
371
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000372 int32_t GetCurrentInstructionId() const {
373 return current_instruction_id_;
374 }
375
376 void SetCurrentInstructionId(int32_t id) {
377 current_instruction_id_ = id;
378 }
379
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100380 uint16_t GetMaximumNumberOfOutVRegs() const {
381 return maximum_number_of_out_vregs_;
382 }
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
385 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100386 }
387
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100388 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
389 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
390 }
391
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000392 void UpdateTemporariesVRegSlots(size_t slots) {
393 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100394 }
395
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000396 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100397 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000398 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100399 }
400
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100401 void SetNumberOfVRegs(uint16_t number_of_vregs) {
402 number_of_vregs_ = number_of_vregs;
403 }
404
405 uint16_t GetNumberOfVRegs() const {
406 return number_of_vregs_;
407 }
408
409 void SetNumberOfInVRegs(uint16_t value) {
410 number_of_in_vregs_ = value;
411 }
412
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100413 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100414 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100415 return number_of_vregs_ - number_of_in_vregs_;
416 }
417
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100420 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100421
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100422 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100423 return linear_order_;
424 }
425
Mark Mendell1152c922015-04-24 17:06:35 -0400426 bool HasBoundsChecks() const {
427 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800428 }
429
Mark Mendell1152c922015-04-24 17:06:35 -0400430 void SetHasBoundsChecks(bool value) {
431 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800432 }
433
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100434 bool ShouldGenerateConstructorBarrier() const {
435 return should_generate_constructor_barrier_;
436 }
437
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000438 bool IsDebuggable() const { return debuggable_; }
439
David Brazdil8d5b8b22015-03-24 10:51:52 +0000440 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000441 // already, it is created and inserted into the graph. This method is only for
442 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600443 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000444
445 // TODO: This is problematic for the consistency of reference type propagation
446 // because it can be created anytime after the pass and thus it will be left
447 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600448 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000449
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600450 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
451 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000452 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600453 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
454 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600456 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
457 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000458 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600459 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
460 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000461 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000462
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100463 HCurrentMethod* GetCurrentMethod();
464
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100465 const DexFile& GetDexFile() const {
466 return dex_file_;
467 }
468
469 uint32_t GetMethodIdx() const {
470 return method_idx_;
471 }
472
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100473 InvokeType GetInvokeType() const {
474 return invoke_type_;
475 }
476
Mark Mendellc4701932015-04-10 13:18:51 -0400477 InstructionSet GetInstructionSet() const {
478 return instruction_set_;
479 }
480
David Brazdil77a48ae2015-09-15 12:34:04 +0000481 bool HasTryCatch() const { return has_try_catch_; }
482 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100483
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100484 ArtMethod* GetArtMethod() const { return art_method_; }
485 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
486
Mark Mendellf6529172015-11-17 11:16:56 -0500487 // Returns an instruction with the opposite boolean value from 'cond'.
488 // The instruction has been inserted into the graph, either as a constant, or
489 // before cursor.
490 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
491
David Brazdil2d7352b2015-04-20 14:52:42 +0100492 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100493 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000494 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100495 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000496
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000497 template <class InstructionType, typename ValueType>
498 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600499 ArenaSafeMap<ValueType, InstructionType*>* cache,
500 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000501 // Try to find an existing constant of the given value.
502 InstructionType* constant = nullptr;
503 auto cached_constant = cache->find(value);
504 if (cached_constant != cache->end()) {
505 constant = cached_constant->second;
506 }
507
508 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100509 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000510 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600511 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000512 cache->Overwrite(value, constant);
513 InsertConstant(constant);
514 }
515 return constant;
516 }
517
David Brazdil8d5b8b22015-03-24 10:51:52 +0000518 void InsertConstant(HConstant* instruction);
519
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000520 // Cache a float constant into the graph. This method should only be
521 // called by the SsaBuilder when creating "equivalent" instructions.
522 void CacheFloatConstant(HFloatConstant* constant);
523
524 // See CacheFloatConstant comment.
525 void CacheDoubleConstant(HDoubleConstant* constant);
526
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000527 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000528
529 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100530 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000531
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100532 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100533 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000534
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100535 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100536 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100537
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000538 HBasicBlock* entry_block_;
539 HBasicBlock* exit_block_;
540
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100541 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100542 uint16_t maximum_number_of_out_vregs_;
543
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100544 // The number of virtual registers in this method. Contains the parameters.
545 uint16_t number_of_vregs_;
546
547 // The number of virtual registers used by parameters of this method.
548 uint16_t number_of_in_vregs_;
549
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000550 // Number of vreg size slots that the temporaries use (used in baseline compiler).
551 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100552
Mark Mendell1152c922015-04-24 17:06:35 -0400553 // Has bounds checks. We can totally skip BCE if it's false.
554 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800555
David Brazdil77a48ae2015-09-15 12:34:04 +0000556 // Flag whether there are any try/catch blocks in the graph. We will skip
557 // try/catch-related passes if false.
558 bool has_try_catch_;
559
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000560 // Indicates whether the graph should be compiled in a way that
561 // ensures full debuggability. If false, we can apply more
562 // aggressive optimizations that may limit the level of debugging.
563 const bool debuggable_;
564
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000565 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000566 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000567
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100568 // The dex file from which the method is from.
569 const DexFile& dex_file_;
570
571 // The method index in the dex file.
572 const uint32_t method_idx_;
573
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100574 // If inlined, this encodes how the callee is being invoked.
575 const InvokeType invoke_type_;
576
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100577 // Whether the graph has been transformed to SSA form. Only used
578 // in debug mode to ensure we are not using properties only valid
579 // for non-SSA form (like the number of temporaries).
580 bool in_ssa_form_;
581
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100582 const bool should_generate_constructor_barrier_;
583
Mathieu Chartiere401d142015-04-22 13:56:20 -0700584 const InstructionSet instruction_set_;
585
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000586 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000587 HNullConstant* cached_null_constant_;
588 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000589 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000590 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000591 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000592
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100593 HCurrentMethod* cached_current_method_;
594
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100595 // The ArtMethod this graph is for. Note that for AOT, it may be null,
596 // for example for methods whose declaring class could not be resolved
597 // (such as when the superclass could not be found).
598 ArtMethod* art_method_;
599
David Brazdil4833f5a2015-12-16 10:37:39 +0000600 // Keep the RTI of inexact Object to avoid having to pass stack handle
601 // collection pointer to passes which may create NullConstant.
602 ReferenceTypeInfo inexact_object_rti_;
603
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000604 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100605 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000606 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000607 DISALLOW_COPY_AND_ASSIGN(HGraph);
608};
609
Vladimir Markof9f64412015-09-02 14:05:49 +0100610class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000611 public:
612 HLoopInformation(HBasicBlock* header, HGraph* graph)
613 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100614 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100615 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100616 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100617 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
618 back_edges_.reserve(kDefaultNumberOfBackEdges);
619 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100620
621 HBasicBlock* GetHeader() const {
622 return header_;
623 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000624
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000625 void SetHeader(HBasicBlock* block) {
626 header_ = block;
627 }
628
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100629 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
630 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
631 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
632
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000633 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100634 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000635 }
636
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100637 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100638 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100639 }
640
David Brazdil46e2a392015-03-16 17:31:52 +0000641 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100642 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100643 }
644
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000645 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100646 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000647 }
648
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100649 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100650
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100651 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100652 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100653 }
654
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100655 // Returns the lifetime position of the back edge that has the
656 // greatest lifetime position.
657 size_t GetLifetimeEnd() const;
658
659 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100660 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100661 }
662
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100663 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100664 // that is the header dominates the back edge.
665 bool Populate();
666
David Brazdila4b8c212015-05-07 09:59:30 +0100667 // Reanalyzes the loop by removing loop info from its blocks and re-running
668 // Populate(). If there are no back edges left, the loop info is completely
669 // removed as well as its SuspendCheck instruction. It must be run on nested
670 // inner loops first.
671 void Update();
672
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100673 // Returns whether this loop information contains `block`.
674 // Note that this loop information *must* be populated before entering this function.
675 bool Contains(const HBasicBlock& block) const;
676
677 // Returns whether this loop information is an inner loop of `other`.
678 // Note that `other` *must* be populated before entering this function.
679 bool IsIn(const HLoopInformation& other) const;
680
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800681 // Returns true if instruction is not defined within this loop.
682 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700683
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100684 const ArenaBitVector& GetBlocks() const { return blocks_; }
685
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000686 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000687 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000688
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000689 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100690 // Internal recursive implementation of `Populate`.
691 void PopulateRecursive(HBasicBlock* block);
692
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100694 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100695 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697
698 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
699};
700
David Brazdilec16f792015-08-19 15:04:01 +0100701// Stores try/catch information for basic blocks.
702// Note that HGraph is constructed so that catch blocks cannot simultaneously
703// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100704class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100705 public:
706 // Try block information constructor.
707 explicit TryCatchInformation(const HTryBoundary& try_entry)
708 : try_entry_(&try_entry),
709 catch_dex_file_(nullptr),
710 catch_type_index_(DexFile::kDexNoIndex16) {
711 DCHECK(try_entry_ != nullptr);
712 }
713
714 // Catch block information constructor.
715 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
716 : try_entry_(nullptr),
717 catch_dex_file_(&dex_file),
718 catch_type_index_(catch_type_index) {}
719
720 bool IsTryBlock() const { return try_entry_ != nullptr; }
721
722 const HTryBoundary& GetTryEntry() const {
723 DCHECK(IsTryBlock());
724 return *try_entry_;
725 }
726
727 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
728
729 bool IsCatchAllTypeIndex() const {
730 DCHECK(IsCatchBlock());
731 return catch_type_index_ == DexFile::kDexNoIndex16;
732 }
733
734 uint16_t GetCatchTypeIndex() const {
735 DCHECK(IsCatchBlock());
736 return catch_type_index_;
737 }
738
739 const DexFile& GetCatchDexFile() const {
740 DCHECK(IsCatchBlock());
741 return *catch_dex_file_;
742 }
743
744 private:
745 // One of possibly several TryBoundary instructions entering the block's try.
746 // Only set for try blocks.
747 const HTryBoundary* try_entry_;
748
749 // Exception type information. Only set for catch blocks.
750 const DexFile* catch_dex_file_;
751 const uint16_t catch_type_index_;
752};
753
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100754static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100755static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100756
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000757// A block in a method. Contains the list of instructions represented
758// as a double linked list. Each block knows its predecessors and
759// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100760
Vladimir Markof9f64412015-09-02 14:05:49 +0100761class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600763 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000764 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000765 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
766 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000767 loop_information_(nullptr),
768 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000769 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100770 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100771 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100772 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000773 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000774 try_catch_information_(nullptr) {
775 predecessors_.reserve(kDefaultNumberOfPredecessors);
776 successors_.reserve(kDefaultNumberOfSuccessors);
777 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
778 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000779
Vladimir Marko60584552015-09-03 13:35:12 +0000780 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100781 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000782 }
783
Vladimir Marko60584552015-09-03 13:35:12 +0000784 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000786 }
787
David Brazdild26a4112015-11-10 11:07:31 +0000788 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
789 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
790
Vladimir Marko60584552015-09-03 13:35:12 +0000791 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
792 return ContainsElement(successors_, block, start_from);
793 }
794
795 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100796 return dominated_blocks_;
797 }
798
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100799 bool IsEntryBlock() const {
800 return graph_->GetEntryBlock() == this;
801 }
802
803 bool IsExitBlock() const {
804 return graph_->GetExitBlock() == this;
805 }
806
David Brazdil46e2a392015-03-16 17:31:52 +0000807 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000808 bool IsSingleTryBoundary() const;
809
810 // Returns true if this block emits nothing but a jump.
811 bool IsSingleJump() const {
812 HLoopInformation* loop_info = GetLoopInformation();
813 return (IsSingleGoto() || IsSingleTryBoundary())
814 // Back edges generate a suspend check.
815 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
816 }
David Brazdil46e2a392015-03-16 17:31:52 +0000817
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000818 void AddBackEdge(HBasicBlock* back_edge) {
819 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000820 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000821 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100822 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000823 loop_information_->AddBackEdge(back_edge);
824 }
825
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000826 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000827 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000828
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100829 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000830 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600831 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000832
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000833 HBasicBlock* GetDominator() const { return dominator_; }
834 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000835 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
836
837 void RemoveDominatedBlock(HBasicBlock* block) {
838 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100839 }
Vladimir Marko60584552015-09-03 13:35:12 +0000840
841 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
842 ReplaceElement(dominated_blocks_, existing, new_block);
843 }
844
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100845 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000846
847 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100848 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000849 }
850
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100851 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
852 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100853 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100854 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100855 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
856 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000857
858 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000859 successors_.push_back(block);
860 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000861 }
862
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100863 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
864 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100865 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000866 new_block->predecessors_.push_back(this);
867 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000868 }
869
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000870 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
871 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000872 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000873 new_block->successors_.push_back(this);
874 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000875 }
876
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100877 // Insert `this` between `predecessor` and `successor. This method
878 // preserves the indicies, and will update the first edge found between
879 // `predecessor` and `successor`.
880 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
881 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100882 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000883 successor->predecessors_[predecessor_index] = this;
884 predecessor->successors_[successor_index] = this;
885 successors_.push_back(successor);
886 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100887 }
888
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100889 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000890 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100891 }
892
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000893 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000894 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000895 }
896
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100897 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000898 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100899 }
900
901 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000902 predecessors_.push_back(block);
903 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 }
905
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100906 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000907 DCHECK_EQ(predecessors_.size(), 2u);
908 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100909 }
910
David Brazdil769c9e52015-04-27 13:54:09 +0100911 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000912 DCHECK_EQ(successors_.size(), 2u);
913 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100914 }
915
David Brazdilfc6a86a2015-06-26 10:33:45 +0000916 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000917 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100918 }
919
David Brazdilfc6a86a2015-06-26 10:33:45 +0000920 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000921 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100922 }
923
David Brazdilfc6a86a2015-06-26 10:33:45 +0000924 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000925 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100926 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000927 }
928
929 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000930 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100931 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000932 }
933
934 // Returns whether the first occurrence of `predecessor` in the list of
935 // predecessors is at index `idx`.
936 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100937 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000938 return GetPredecessorIndexOf(predecessor) == idx;
939 }
940
David Brazdild7558da2015-09-22 13:04:14 +0100941 // Create a new block between this block and its predecessors. The new block
942 // is added to the graph, all predecessor edges are relinked to it and an edge
943 // is created to `this`. Returns the new empty block. Reverse post order or
944 // loop and try/catch information are not updated.
945 HBasicBlock* CreateImmediateDominator();
946
David Brazdilfc6a86a2015-06-26 10:33:45 +0000947 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100948 // created, latter block. Note that this method will add the block to the
949 // graph, create a Goto at the end of the former block and will create an edge
950 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100951 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000952 HBasicBlock* SplitBefore(HInstruction* cursor);
953
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000954 // Split the block into two blocks just after `cursor`. Returns the newly
955 // created block. Note that this method just updates raw block information,
956 // like predecessors, successors, dominators, and instruction list. It does not
957 // update the graph, reverse post order, loop information, nor make sure the
958 // blocks are consistent (for example ending with a control flow instruction).
959 HBasicBlock* SplitAfter(HInstruction* cursor);
960
David Brazdil9bc43612015-11-05 21:25:24 +0000961 // Split catch block into two blocks after the original move-exception bytecode
962 // instruction, or at the beginning if not present. Returns the newly created,
963 // latter block, or nullptr if such block could not be created (must be dead
964 // in that case). Note that this method just updates raw block information,
965 // like predecessors, successors, dominators, and instruction list. It does not
966 // update the graph, reverse post order, loop information, nor make sure the
967 // blocks are consistent (for example ending with a control flow instruction).
968 HBasicBlock* SplitCatchBlockAfterMoveException();
969
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000970 // Merge `other` at the end of `this`. Successors and dominated blocks of
971 // `other` are changed to be successors and dominated blocks of `this`. Note
972 // that this method does not update the graph, reverse post order, loop
973 // information, nor make sure the blocks are consistent (for example ending
974 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100975 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000976
977 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
978 // of `this` are moved to `other`.
979 // Note that this method does not update the graph, reverse post order, loop
980 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000981 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000982 void ReplaceWith(HBasicBlock* other);
983
David Brazdil2d7352b2015-04-20 14:52:42 +0100984 // Merge `other` at the end of `this`. This method updates loops, reverse post
985 // order, links to predecessors, successors, dominators and deletes the block
986 // from the graph. The two blocks must be successive, i.e. `this` the only
987 // predecessor of `other` and vice versa.
988 void MergeWith(HBasicBlock* other);
989
990 // Disconnects `this` from all its predecessors, successors and dominator,
991 // removes it from all loops it is included in and eventually from the graph.
992 // The block must not dominate any other block. Predecessors and successors
993 // are safely updated.
994 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000995
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100997 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100998 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100999 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001000 // Replace instruction `initial` with `replacement` within this block.
1001 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1002 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001003 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001004 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001005 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1006 // instruction list. With 'ensure_safety' set to true, it verifies that the
1007 // instruction is not in use and removes it from the use lists of its inputs.
1008 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1009 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001010 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001011
1012 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001013 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001014 }
1015
Roland Levillain6b879dd2014-09-22 17:13:44 +01001016 bool IsLoopPreHeaderFirstPredecessor() const {
1017 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001018 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001019 }
1020
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001021 HLoopInformation* GetLoopInformation() const {
1022 return loop_information_;
1023 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001024
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001026 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001027 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001028 void SetInLoop(HLoopInformation* info) {
1029 if (IsLoopHeader()) {
1030 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001031 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001032 loop_information_ = info;
1033 } else if (loop_information_->Contains(*info->GetHeader())) {
1034 // Block is currently part of an outer loop. Make it part of this inner loop.
1035 // Note that a non loop header having a loop information means this loop information
1036 // has already been populated
1037 loop_information_ = info;
1038 } else {
1039 // Block is part of an inner loop. Do not update the loop information.
1040 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1041 // at this point, because this method is being called while populating `info`.
1042 }
1043 }
1044
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001045 // Raw update of the loop information.
1046 void SetLoopInformation(HLoopInformation* info) {
1047 loop_information_ = info;
1048 }
1049
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001050 bool IsInLoop() const { return loop_information_ != nullptr; }
1051
David Brazdilec16f792015-08-19 15:04:01 +01001052 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1053
1054 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1055 try_catch_information_ = try_catch_information;
1056 }
1057
1058 bool IsTryBlock() const {
1059 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1060 }
1061
1062 bool IsCatchBlock() const {
1063 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1064 }
David Brazdilffee3d32015-07-06 11:48:53 +01001065
1066 // Returns the try entry that this block's successors should have. They will
1067 // be in the same try, unless the block ends in a try boundary. In that case,
1068 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001069 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001070
David Brazdild7558da2015-09-22 13:04:14 +01001071 bool HasThrowingInstructions() const;
1072
David Brazdila4b8c212015-05-07 09:59:30 +01001073 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001074 bool Dominates(HBasicBlock* block) const;
1075
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001076 size_t GetLifetimeStart() const { return lifetime_start_; }
1077 size_t GetLifetimeEnd() const { return lifetime_end_; }
1078
1079 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1080 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1081
David Brazdil8d5b8b22015-03-24 10:51:52 +00001082 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001083 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001084 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001085 bool HasSinglePhi() const;
1086
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001087 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001088 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001089 ArenaVector<HBasicBlock*> predecessors_;
1090 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001091 HInstructionList instructions_;
1092 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001093 HLoopInformation* loop_information_;
1094 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001095 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001096 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001097 // The dex program counter of the first instruction of this block.
1098 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001099 size_t lifetime_start_;
1100 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001101 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001102
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001103 friend class HGraph;
1104 friend class HInstruction;
1105
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001106 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1107};
1108
David Brazdilb2bd1c52015-03-25 11:17:37 +00001109// Iterates over the LoopInformation of all loops which contain 'block'
1110// from the innermost to the outermost.
1111class HLoopInformationOutwardIterator : public ValueObject {
1112 public:
1113 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1114 : current_(block.GetLoopInformation()) {}
1115
1116 bool Done() const { return current_ == nullptr; }
1117
1118 void Advance() {
1119 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001120 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001121 }
1122
1123 HLoopInformation* Current() const {
1124 DCHECK(!Done());
1125 return current_;
1126 }
1127
1128 private:
1129 HLoopInformation* current_;
1130
1131 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1132};
1133
Alexandre Ramesef20f712015-06-09 10:29:30 +01001134#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001135 M(Above, Condition) \
1136 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001137 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001138 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001139 M(ArrayGet, Instruction) \
1140 M(ArrayLength, Instruction) \
1141 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001142 M(Below, Condition) \
1143 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001144 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001145 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001146 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001147 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001148 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001149 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001150 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001151 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001152 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001153 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001154 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001155 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001156 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001157 M(Exit, Instruction) \
1158 M(FloatConstant, Constant) \
1159 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001160 M(GreaterThan, Condition) \
1161 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001162 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001163 M(InstanceFieldGet, Instruction) \
1164 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001165 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001166 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001167 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001168 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001169 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001170 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001171 M(LessThan, Condition) \
1172 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001173 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001174 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001175 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001176 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001177 M(Local, Instruction) \
1178 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001179 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001180 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001181 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001182 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001183 M(Neg, UnaryOperation) \
1184 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001185 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001186 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001187 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001188 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001189 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001190 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001191 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001192 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001194 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001195 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001196 M(Return, Instruction) \
1197 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001198 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001199 M(Shl, BinaryOperation) \
1200 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001201 M(StaticFieldGet, Instruction) \
1202 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001203 M(UnresolvedInstanceFieldGet, Instruction) \
1204 M(UnresolvedInstanceFieldSet, Instruction) \
1205 M(UnresolvedStaticFieldGet, Instruction) \
1206 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001207 M(StoreLocal, Instruction) \
1208 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001211 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001212 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001213 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001214 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001215 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001216
Vladimir Markob4536b72015-11-24 13:45:23 +00001217#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001218#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001219#else
1220#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1221 M(ArmDexCacheArraysBase, Instruction)
1222#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001223
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001224#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001225#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001226#else
1227#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001228 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001229 M(Arm64IntermediateAddress, Instruction) \
1230 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001231#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001232
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001233#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1234
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001235#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1236
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001237#ifndef ART_ENABLE_CODEGEN_x86
1238#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1239#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001240#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1241 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001242 M(X86LoadFromConstantTable, Instruction) \
1243 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001244#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001245
1246#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1247
1248#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1249 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1250 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1251 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001252 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001253 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001254 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1255 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1256
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001257#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1258 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001259 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001260 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001261 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001262 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001263
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001264#define FOR_EACH_INSTRUCTION(M) \
1265 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1266 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1267
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001268#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001269FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1270#undef FORWARD_DECLARATION
1271
Roland Levillainccc07a92014-09-16 14:48:16 +01001272#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001273 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001274 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001275 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001276 return other->Is##type(); \
1277 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001278 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001279
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001280#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1281 bool Is##type() const { return As##type() != nullptr; } \
1282 const H##type* As##type() const { return this; } \
1283 H##type* As##type() { return this; }
1284
David Brazdiled596192015-01-23 10:39:45 +00001285template <typename T> class HUseList;
1286
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001287template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001288class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001289 public:
David Brazdiled596192015-01-23 10:39:45 +00001290 HUseListNode* GetPrevious() const { return prev_; }
1291 HUseListNode* GetNext() const { return next_; }
1292 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001293 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001294 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001295
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001296 private:
David Brazdiled596192015-01-23 10:39:45 +00001297 HUseListNode(T user, size_t index)
1298 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1299
1300 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001301 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001302 HUseListNode<T>* prev_;
1303 HUseListNode<T>* next_;
1304
1305 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001306
1307 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1308};
1309
David Brazdiled596192015-01-23 10:39:45 +00001310template <typename T>
1311class HUseList : public ValueObject {
1312 public:
1313 HUseList() : first_(nullptr) {}
1314
1315 void Clear() {
1316 first_ = nullptr;
1317 }
1318
1319 // Adds a new entry at the beginning of the use list and returns
1320 // the newly created node.
1321 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001322 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001323 if (IsEmpty()) {
1324 first_ = new_node;
1325 } else {
1326 first_->prev_ = new_node;
1327 new_node->next_ = first_;
1328 first_ = new_node;
1329 }
1330 return new_node;
1331 }
1332
1333 HUseListNode<T>* GetFirst() const {
1334 return first_;
1335 }
1336
1337 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001338 DCHECK(node != nullptr);
1339 DCHECK(Contains(node));
1340
David Brazdiled596192015-01-23 10:39:45 +00001341 if (node->prev_ != nullptr) {
1342 node->prev_->next_ = node->next_;
1343 }
1344 if (node->next_ != nullptr) {
1345 node->next_->prev_ = node->prev_;
1346 }
1347 if (node == first_) {
1348 first_ = node->next_;
1349 }
1350 }
1351
David Brazdil1abb4192015-02-17 18:33:36 +00001352 bool Contains(const HUseListNode<T>* node) const {
1353 if (node == nullptr) {
1354 return false;
1355 }
1356 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1357 if (current == node) {
1358 return true;
1359 }
1360 }
1361 return false;
1362 }
1363
David Brazdiled596192015-01-23 10:39:45 +00001364 bool IsEmpty() const {
1365 return first_ == nullptr;
1366 }
1367
1368 bool HasOnlyOneUse() const {
1369 return first_ != nullptr && first_->next_ == nullptr;
1370 }
1371
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001372 size_t SizeSlow() const {
1373 size_t count = 0;
1374 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1375 ++count;
1376 }
1377 return count;
1378 }
1379
David Brazdiled596192015-01-23 10:39:45 +00001380 private:
1381 HUseListNode<T>* first_;
1382};
1383
1384template<typename T>
1385class HUseIterator : public ValueObject {
1386 public:
1387 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1388
1389 bool Done() const { return current_ == nullptr; }
1390
1391 void Advance() {
1392 DCHECK(!Done());
1393 current_ = current_->GetNext();
1394 }
1395
1396 HUseListNode<T>* Current() const {
1397 DCHECK(!Done());
1398 return current_;
1399 }
1400
1401 private:
1402 HUseListNode<T>* current_;
1403
1404 friend class HValue;
1405};
1406
David Brazdil1abb4192015-02-17 18:33:36 +00001407// This class is used by HEnvironment and HInstruction classes to record the
1408// instructions they use and pointers to the corresponding HUseListNodes kept
1409// by the used instructions.
1410template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001411class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001412 public:
1413 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1414 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1415
1416 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1417 : instruction_(old_record.instruction_), use_node_(use_node) {
1418 DCHECK(instruction_ != nullptr);
1419 DCHECK(use_node_ != nullptr);
1420 DCHECK(old_record.use_node_ == nullptr);
1421 }
1422
1423 HInstruction* GetInstruction() const { return instruction_; }
1424 HUseListNode<T>* GetUseNode() const { return use_node_; }
1425
1426 private:
1427 // Instruction used by the user.
1428 HInstruction* instruction_;
1429
1430 // Corresponding entry in the use list kept by 'instruction_'.
1431 HUseListNode<T>* use_node_;
1432};
1433
Aart Bik854a02b2015-07-14 16:07:00 -07001434/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001435 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001436 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001437 * For write/read dependences on fields/arrays, the dependence analysis uses
1438 * type disambiguation (e.g. a float field write cannot modify the value of an
1439 * integer field read) and the access type (e.g. a reference array write cannot
1440 * modify the value of a reference field read [although it may modify the
1441 * reference fetch prior to reading the field, which is represented by its own
1442 * write/read dependence]). The analysis makes conservative points-to
1443 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1444 * the same, and any reference read depends on any reference read without
1445 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001446 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 * The internal representation uses 38-bit and is described in the table below.
1448 * The first line indicates the side effect, and for field/array accesses the
1449 * second line indicates the type of the access (in the order of the
1450 * Primitive::Type enum).
1451 * The two numbered lines below indicate the bit position in the bitfield (read
1452 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001453 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001454 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1455 * +-------------+---------+---------+--------------+---------+---------+
1456 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1457 * | 3 |333333322|222222221| 1 |111111110|000000000|
1458 * | 7 |654321098|765432109| 8 |765432109|876543210|
1459 *
1460 * Note that, to ease the implementation, 'changes' bits are least significant
1461 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001462 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001463class SideEffects : public ValueObject {
1464 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001465 SideEffects() : flags_(0) {}
1466
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001467 static SideEffects None() {
1468 return SideEffects(0);
1469 }
1470
1471 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001472 return SideEffects(kAllChangeBits | kAllDependOnBits);
1473 }
1474
1475 static SideEffects AllChanges() {
1476 return SideEffects(kAllChangeBits);
1477 }
1478
1479 static SideEffects AllDependencies() {
1480 return SideEffects(kAllDependOnBits);
1481 }
1482
1483 static SideEffects AllExceptGCDependency() {
1484 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1485 }
1486
1487 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001488 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001489 }
1490
Aart Bik34c3ba92015-07-20 14:08:59 -07001491 static SideEffects AllWrites() {
1492 return SideEffects(kAllWrites);
1493 }
1494
1495 static SideEffects AllReads() {
1496 return SideEffects(kAllReads);
1497 }
1498
1499 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1500 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001501 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001502 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001503 }
1504
Aart Bik854a02b2015-07-14 16:07:00 -07001505 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1506 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001507 }
1508
Aart Bik34c3ba92015-07-20 14:08:59 -07001509 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1510 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001511 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001512 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001513 }
1514
1515 static SideEffects ArrayReadOfType(Primitive::Type type) {
1516 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1517 }
1518
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001519 static SideEffects CanTriggerGC() {
1520 return SideEffects(1ULL << kCanTriggerGCBit);
1521 }
1522
1523 static SideEffects DependsOnGC() {
1524 return SideEffects(1ULL << kDependsOnGCBit);
1525 }
1526
Aart Bik854a02b2015-07-14 16:07:00 -07001527 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001528 SideEffects Union(SideEffects other) const {
1529 return SideEffects(flags_ | other.flags_);
1530 }
1531
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001532 SideEffects Exclusion(SideEffects other) const {
1533 return SideEffects(flags_ & ~other.flags_);
1534 }
1535
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001536 void Add(SideEffects other) {
1537 flags_ |= other.flags_;
1538 }
1539
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001540 bool Includes(SideEffects other) const {
1541 return (other.flags_ & flags_) == other.flags_;
1542 }
1543
1544 bool HasSideEffects() const {
1545 return (flags_ & kAllChangeBits);
1546 }
1547
1548 bool HasDependencies() const {
1549 return (flags_ & kAllDependOnBits);
1550 }
1551
1552 // Returns true if there are no side effects or dependencies.
1553 bool DoesNothing() const {
1554 return flags_ == 0;
1555 }
1556
Aart Bik854a02b2015-07-14 16:07:00 -07001557 // Returns true if something is written.
1558 bool DoesAnyWrite() const {
1559 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001560 }
1561
Aart Bik854a02b2015-07-14 16:07:00 -07001562 // Returns true if something is read.
1563 bool DoesAnyRead() const {
1564 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001565 }
1566
Aart Bik854a02b2015-07-14 16:07:00 -07001567 // Returns true if potentially everything is written and read
1568 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001569 bool DoesAllReadWrite() const {
1570 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1571 }
1572
Aart Bik854a02b2015-07-14 16:07:00 -07001573 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001574 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001575 }
1576
Roland Levillain0d5a2812015-11-13 10:07:31 +00001577 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001578 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1580 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001581 }
1582
1583 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001585 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001586 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001587 for (int s = kLastBit; s >= 0; s--) {
1588 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1589 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1590 // This is a bit for the GC side effect.
1591 if (current_bit_is_set) {
1592 flags += "GC";
1593 }
Aart Bik854a02b2015-07-14 16:07:00 -07001594 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 } else {
1596 // This is a bit for the array/field analysis.
1597 // The underscore character stands for the 'can trigger GC' bit.
1598 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1599 if (current_bit_is_set) {
1600 flags += kDebug[s];
1601 }
1602 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1603 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1604 flags += "|";
1605 }
1606 }
Aart Bik854a02b2015-07-14 16:07:00 -07001607 }
1608 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001609 }
1610
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001611 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001612
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 private:
1614 static constexpr int kFieldArrayAnalysisBits = 9;
1615
1616 static constexpr int kFieldWriteOffset = 0;
1617 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1618 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1619 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1620
1621 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1622
1623 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1624 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1625 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1626 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1627
1628 static constexpr int kLastBit = kDependsOnGCBit;
1629 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1630
1631 // Aliases.
1632
1633 static_assert(kChangeBits == kDependOnBits,
1634 "the 'change' bits should match the 'depend on' bits.");
1635
1636 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1637 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1638 static constexpr uint64_t kAllWrites =
1639 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1640 static constexpr uint64_t kAllReads =
1641 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001642
Aart Bik854a02b2015-07-14 16:07:00 -07001643 // Work around the fact that HIR aliases I/F and J/D.
1644 // TODO: remove this interceptor once HIR types are clean
1645 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1646 switch (type) {
1647 case Primitive::kPrimInt:
1648 case Primitive::kPrimFloat:
1649 return TypeFlag(Primitive::kPrimInt, offset) |
1650 TypeFlag(Primitive::kPrimFloat, offset);
1651 case Primitive::kPrimLong:
1652 case Primitive::kPrimDouble:
1653 return TypeFlag(Primitive::kPrimLong, offset) |
1654 TypeFlag(Primitive::kPrimDouble, offset);
1655 default:
1656 return TypeFlag(type, offset);
1657 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001658 }
1659
Aart Bik854a02b2015-07-14 16:07:00 -07001660 // Translates type to bit flag.
1661 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1662 CHECK_NE(type, Primitive::kPrimVoid);
1663 const uint64_t one = 1;
1664 const int shift = type; // 0-based consecutive enum
1665 DCHECK_LE(kFieldWriteOffset, shift);
1666 DCHECK_LT(shift, kArrayWriteOffset);
1667 return one << (type + offset);
1668 }
1669
1670 // Private constructor on direct flags value.
1671 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1672
1673 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001674};
1675
David Brazdiled596192015-01-23 10:39:45 +00001676// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001677class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001678 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001679 HEnvironment(ArenaAllocator* arena,
1680 size_t number_of_vregs,
1681 const DexFile& dex_file,
1682 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001683 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001684 InvokeType invoke_type,
1685 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001686 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1687 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001688 parent_(nullptr),
1689 dex_file_(dex_file),
1690 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001691 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001692 invoke_type_(invoke_type),
1693 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001694 }
1695
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001696 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001697 : HEnvironment(arena,
1698 to_copy.Size(),
1699 to_copy.GetDexFile(),
1700 to_copy.GetMethodIdx(),
1701 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001702 to_copy.GetInvokeType(),
1703 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001704
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001705 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001706 if (parent_ != nullptr) {
1707 parent_->SetAndCopyParentChain(allocator, parent);
1708 } else {
1709 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1710 parent_->CopyFrom(parent);
1711 if (parent->GetParent() != nullptr) {
1712 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1713 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001714 }
David Brazdiled596192015-01-23 10:39:45 +00001715 }
1716
Vladimir Marko71bf8092015-09-15 15:33:14 +01001717 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001718 void CopyFrom(HEnvironment* environment);
1719
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001720 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1721 // input to the loop phi instead. This is for inserting instructions that
1722 // require an environment (like HDeoptimization) in the loop pre-header.
1723 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001724
1725 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001726 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001727 }
1728
1729 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001730 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001731 }
1732
David Brazdil1abb4192015-02-17 18:33:36 +00001733 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001734
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001735 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001736
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001737 HEnvironment* GetParent() const { return parent_; }
1738
1739 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001740 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001741 }
1742
1743 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001744 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001745 }
1746
1747 uint32_t GetDexPc() const {
1748 return dex_pc_;
1749 }
1750
1751 uint32_t GetMethodIdx() const {
1752 return method_idx_;
1753 }
1754
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001755 InvokeType GetInvokeType() const {
1756 return invoke_type_;
1757 }
1758
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001759 const DexFile& GetDexFile() const {
1760 return dex_file_;
1761 }
1762
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001763 HInstruction* GetHolder() const {
1764 return holder_;
1765 }
1766
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001767
1768 bool IsFromInlinedInvoke() const {
1769 return GetParent() != nullptr;
1770 }
1771
David Brazdiled596192015-01-23 10:39:45 +00001772 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001773 // Record instructions' use entries of this environment for constant-time removal.
1774 // It should only be called by HInstruction when a new environment use is added.
1775 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1776 DCHECK(env_use->GetUser() == this);
1777 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001778 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001779 }
David Brazdiled596192015-01-23 10:39:45 +00001780
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001781 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1782 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001783 HEnvironment* parent_;
1784 const DexFile& dex_file_;
1785 const uint32_t method_idx_;
1786 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001787 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001788
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001789 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001790 HInstruction* const holder_;
1791
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001792 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001793
1794 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1795};
1796
Vladimir Markof9f64412015-09-02 14:05:49 +01001797class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001798 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001799 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001800 : previous_(nullptr),
1801 next_(nullptr),
1802 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001803 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001805 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001806 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001807 locations_(nullptr),
1808 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001809 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001810 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001811 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812
Dave Allison20dfc792014-06-16 20:44:29 -07001813 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001814
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001815#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001816 enum InstructionKind {
1817 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1818 };
1819#undef DECLARE_KIND
1820
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001821 HInstruction* GetNext() const { return next_; }
1822 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001823
Calin Juravle77520bc2015-01-12 18:45:46 +00001824 HInstruction* GetNextDisregardingMoves() const;
1825 HInstruction* GetPreviousDisregardingMoves() const;
1826
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001827 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001828 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001829 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001830 bool IsInBlock() const { return block_ != nullptr; }
1831 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001832 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001833
Roland Levillain6b879dd2014-09-22 17:13:44 +01001834 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001835 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001836
1837 virtual void Accept(HGraphVisitor* visitor) = 0;
1838 virtual const char* DebugName() const = 0;
1839
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001840 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001841 void SetRawInputAt(size_t index, HInstruction* input) {
1842 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1843 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001845 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001846
1847 uint32_t GetDexPc() const { return dex_pc_; }
1848
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001849 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001850
Roland Levillaine161a2a2014-10-03 12:45:18 +01001851 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001852 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001853
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001854 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001855 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001856
Calin Juravle10e244f2015-01-26 18:54:32 +00001857 // Does not apply for all instructions, but having this at top level greatly
1858 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001859 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001860 virtual bool CanBeNull() const {
1861 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1862 return true;
1863 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001864
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001865 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001866 return false;
1867 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001868
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001869 virtual bool IsActualObject() const {
1870 return GetType() == Primitive::kPrimNot;
1871 }
1872
Calin Juravle2e768302015-07-28 14:41:11 +00001873 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001874
Calin Juravle61d544b2015-02-23 16:46:57 +00001875 ReferenceTypeInfo GetReferenceTypeInfo() const {
1876 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1877 return reference_type_info_;
1878 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001879
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001880 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001881 DCHECK(user != nullptr);
1882 HUseListNode<HInstruction*>* use =
1883 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1884 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001885 }
1886
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001887 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001888 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001889 HUseListNode<HEnvironment*>* env_use =
1890 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1891 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001892 }
1893
David Brazdil1abb4192015-02-17 18:33:36 +00001894 void RemoveAsUserOfInput(size_t input) {
1895 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1896 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1897 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001898
David Brazdil1abb4192015-02-17 18:33:36 +00001899 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1900 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001901
David Brazdiled596192015-01-23 10:39:45 +00001902 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1903 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001904 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001905 bool HasOnlyOneNonEnvironmentUse() const {
1906 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1907 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001908
Roland Levillain6c82d402014-10-13 16:10:27 +01001909 // Does this instruction strictly dominate `other_instruction`?
1910 // Returns false if this instruction and `other_instruction` are the same.
1911 // Aborts if this instruction and `other_instruction` are both phis.
1912 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001913
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001914 int GetId() const { return id_; }
1915 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001916
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001917 int GetSsaIndex() const { return ssa_index_; }
1918 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1919 bool HasSsaIndex() const { return ssa_index_ != -1; }
1920
1921 bool HasEnvironment() const { return environment_ != nullptr; }
1922 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001923 // Set the `environment_` field. Raw because this method does not
1924 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001925 void SetRawEnvironment(HEnvironment* environment) {
1926 DCHECK(environment_ == nullptr);
1927 DCHECK_EQ(environment->GetHolder(), this);
1928 environment_ = environment;
1929 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001930
1931 // Set the environment of this instruction, copying it from `environment`. While
1932 // copying, the uses lists are being updated.
1933 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001934 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001935 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001936 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001937 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001938 if (environment->GetParent() != nullptr) {
1939 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1940 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001941 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001942
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001943 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1944 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001945 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001946 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001947 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001948 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001949 if (environment->GetParent() != nullptr) {
1950 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1951 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001952 }
1953
Nicolas Geoffray39468442014-09-02 15:17:15 +01001954 // Returns the number of entries in the environment. Typically, that is the
1955 // number of dex registers in a method. It could be more in case of inlining.
1956 size_t EnvironmentSize() const;
1957
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001958 LocationSummary* GetLocations() const { return locations_; }
1959 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001960
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001962 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001963
Alexandre Rames188d4312015-04-09 18:30:21 +01001964 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1965 // uses of this instruction by `other` are *not* updated.
1966 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1967 ReplaceWith(other);
1968 other->ReplaceInput(this, use_index);
1969 }
1970
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001971 // Move `this` instruction before `cursor`.
1972 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001973
Vladimir Markofb337ea2015-11-25 15:25:10 +00001974 // Move `this` before its first user and out of any loops. If there is no
1975 // out-of-loop user that dominates all other users, move the instruction
1976 // to the end of the out-of-loop common dominator of the user's blocks.
1977 //
1978 // This can be used only on non-throwing instructions with no side effects that
1979 // have at least one use but no environment uses.
1980 void MoveBeforeFirstUserAndOutOfLoops();
1981
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001982#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 bool Is##type() const; \
1984 const H##type* As##type() const; \
1985 H##type* As##type();
1986
1987 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1988#undef INSTRUCTION_TYPE_CHECK
1989
1990#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001991 bool Is##type() const { return (As##type() != nullptr); } \
1992 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001993 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001994 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001995#undef INSTRUCTION_TYPE_CHECK
1996
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001997 // Returns whether the instruction can be moved within the graph.
1998 virtual bool CanBeMoved() const { return false; }
1999
2000 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002001 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002002 return false;
2003 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002004
2005 // Returns whether any data encoded in the two instructions is equal.
2006 // This method does not look at the inputs. Both instructions must be
2007 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002008 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002009 return false;
2010 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002011
2012 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002013 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002014 // 2) Their inputs are identical.
2015 bool Equals(HInstruction* other) const;
2016
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002017 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2018 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2019 // the virtual function because the __attribute__((__pure__)) doesn't really
2020 // apply the strong requirement for virtual functions, preventing optimizations.
2021 InstructionKind GetKind() const PURE;
2022 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002023
2024 virtual size_t ComputeHashCode() const {
2025 size_t result = GetKind();
2026 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2027 result = (result * 31) + InputAt(i)->GetId();
2028 }
2029 return result;
2030 }
2031
2032 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002033 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002034
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002035 size_t GetLifetimePosition() const { return lifetime_position_; }
2036 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2037 LiveInterval* GetLiveInterval() const { return live_interval_; }
2038 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2039 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2040
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002041 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2042
2043 // Returns whether the code generation of the instruction will require to have access
2044 // to the current method. Such instructions are:
2045 // (1): Instructions that require an environment, as calling the runtime requires
2046 // to walk the stack and have the current method stored at a specific stack address.
2047 // (2): Object literals like classes and strings, that are loaded from the dex cache
2048 // fields of the current method.
2049 bool NeedsCurrentMethod() const {
2050 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2051 }
2052
Vladimir Markodc151b22015-10-15 18:02:30 +01002053 // Returns whether the code generation of the instruction will require to have access
2054 // to the dex cache of the current method's declaring class via the current method.
2055 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002056
Mark Mendellc4701932015-04-10 13:18:51 -04002057 // Does this instruction have any use in an environment before
2058 // control flow hits 'other'?
2059 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2060
2061 // Remove all references to environment uses of this instruction.
2062 // The caller must ensure that this is safe to do.
2063 void RemoveEnvironmentUsers();
2064
David Brazdil1abb4192015-02-17 18:33:36 +00002065 protected:
2066 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2067 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002068 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002069
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002070 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002071 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2072
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002073 HInstruction* previous_;
2074 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002075 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002076 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002077
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002078 // An instruction gets an id when it is added to the graph.
2079 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002080 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002081 int id_;
2082
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002083 // When doing liveness analysis, instructions that have uses get an SSA index.
2084 int ssa_index_;
2085
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002086 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002087 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002088
2089 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002090 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002091
Nicolas Geoffray39468442014-09-02 15:17:15 +01002092 // The environment associated with this instruction. Not null if the instruction
2093 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002094 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002095
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002096 // Set by the code generator.
2097 LocationSummary* locations_;
2098
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002099 // Set by the liveness analysis.
2100 LiveInterval* live_interval_;
2101
2102 // Set by the liveness analysis, this is the position in a linear
2103 // order of blocks where this instruction's live interval start.
2104 size_t lifetime_position_;
2105
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002106 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002107
Calin Juravleacf735c2015-02-12 15:25:22 +00002108 // TODO: for primitive types this should be marked as invalid.
2109 ReferenceTypeInfo reference_type_info_;
2110
David Brazdil1abb4192015-02-17 18:33:36 +00002111 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002112 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002113 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002114 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002115 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002116
2117 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2118};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002119std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002120
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002121class HInputIterator : public ValueObject {
2122 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002123 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002124
2125 bool Done() const { return index_ == instruction_->InputCount(); }
2126 HInstruction* Current() const { return instruction_->InputAt(index_); }
2127 void Advance() { index_++; }
2128
2129 private:
2130 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002131 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002132
2133 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2134};
2135
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002136class HInstructionIterator : public ValueObject {
2137 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002138 explicit HInstructionIterator(const HInstructionList& instructions)
2139 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002140 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002141 }
2142
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002143 bool Done() const { return instruction_ == nullptr; }
2144 HInstruction* Current() const { return instruction_; }
2145 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002147 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002148 }
2149
2150 private:
2151 HInstruction* instruction_;
2152 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002153
2154 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002155};
2156
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002157class HBackwardInstructionIterator : public ValueObject {
2158 public:
2159 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2160 : instruction_(instructions.last_instruction_) {
2161 next_ = Done() ? nullptr : instruction_->GetPrevious();
2162 }
2163
2164 bool Done() const { return instruction_ == nullptr; }
2165 HInstruction* Current() const { return instruction_; }
2166 void Advance() {
2167 instruction_ = next_;
2168 next_ = Done() ? nullptr : instruction_->GetPrevious();
2169 }
2170
2171 private:
2172 HInstruction* instruction_;
2173 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002174
2175 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002176};
2177
Vladimir Markof9f64412015-09-02 14:05:49 +01002178template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002179class HTemplateInstruction: public HInstruction {
2180 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002181 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002182 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002183 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002184
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002185 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002186
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002187 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002188 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2189 DCHECK_LT(i, N);
2190 return inputs_[i];
2191 }
David Brazdil1abb4192015-02-17 18:33:36 +00002192
2193 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002194 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002195 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002196 }
2197
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002198 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002199 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002200
2201 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002202};
2203
Vladimir Markof9f64412015-09-02 14:05:49 +01002204// HTemplateInstruction specialization for N=0.
2205template<>
2206class HTemplateInstruction<0>: public HInstruction {
2207 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002208 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002209 : HInstruction(side_effects, dex_pc) {}
2210
Vladimir Markof9f64412015-09-02 14:05:49 +01002211 virtual ~HTemplateInstruction() {}
2212
2213 size_t InputCount() const OVERRIDE { return 0; }
2214
2215 protected:
2216 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2217 LOG(FATAL) << "Unreachable";
2218 UNREACHABLE();
2219 }
2220
2221 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2222 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2223 LOG(FATAL) << "Unreachable";
2224 UNREACHABLE();
2225 }
2226
2227 private:
2228 friend class SsaBuilder;
2229};
2230
Dave Allison20dfc792014-06-16 20:44:29 -07002231template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002232class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002233 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002234 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002235 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002236 virtual ~HExpression() {}
2237
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002238 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002239
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002240 protected:
2241 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002242};
2243
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002244// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2245// instruction that branches to the exit block.
2246class HReturnVoid : public HTemplateInstruction<0> {
2247 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002248 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2249 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002250
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002251 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002252
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002253 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002254
2255 private:
2256 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2257};
2258
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002259// Represents dex's RETURN opcodes. A HReturn is a control flow
2260// instruction that branches to the exit block.
2261class HReturn : public HTemplateInstruction<1> {
2262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002263 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2264 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002265 SetRawInputAt(0, value);
2266 }
2267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002268 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002269
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002270 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002271
2272 private:
2273 DISALLOW_COPY_AND_ASSIGN(HReturn);
2274};
2275
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002276// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002277// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002278// exit block.
2279class HExit : public HTemplateInstruction<0> {
2280 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002281 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002282
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002283 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002284
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002285 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002286
2287 private:
2288 DISALLOW_COPY_AND_ASSIGN(HExit);
2289};
2290
2291// Jumps from one block to another.
2292class HGoto : public HTemplateInstruction<0> {
2293 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002294 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002295
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002296 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002297
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002298 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002299 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002300 }
2301
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002302 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303
2304 private:
2305 DISALLOW_COPY_AND_ASSIGN(HGoto);
2306};
2307
Roland Levillain9867bc72015-08-05 10:21:34 +01002308class HConstant : public HExpression<0> {
2309 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002310 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2311 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002312
2313 bool CanBeMoved() const OVERRIDE { return true; }
2314
2315 virtual bool IsMinusOne() const { return false; }
2316 virtual bool IsZero() const { return false; }
2317 virtual bool IsOne() const { return false; }
2318
David Brazdil77a48ae2015-09-15 12:34:04 +00002319 virtual uint64_t GetValueAsUint64() const = 0;
2320
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002321 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002322
2323 private:
2324 DISALLOW_COPY_AND_ASSIGN(HConstant);
2325};
2326
2327class HNullConstant : public HConstant {
2328 public:
2329 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2330 return true;
2331 }
2332
David Brazdil77a48ae2015-09-15 12:34:04 +00002333 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2334
Roland Levillain9867bc72015-08-05 10:21:34 +01002335 size_t ComputeHashCode() const OVERRIDE { return 0; }
2336
2337 DECLARE_INSTRUCTION(NullConstant);
2338
2339 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002340 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002341
2342 friend class HGraph;
2343 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2344};
2345
2346// Constants of the type int. Those can be from Dex instructions, or
2347// synthesized (for example with the if-eqz instruction).
2348class HIntConstant : public HConstant {
2349 public:
2350 int32_t GetValue() const { return value_; }
2351
David Brazdil9f389d42015-10-01 14:32:56 +01002352 uint64_t GetValueAsUint64() const OVERRIDE {
2353 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2354 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002355
Roland Levillain9867bc72015-08-05 10:21:34 +01002356 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2357 DCHECK(other->IsIntConstant());
2358 return other->AsIntConstant()->value_ == value_;
2359 }
2360
2361 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2362
2363 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2364 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2365 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2366
2367 DECLARE_INSTRUCTION(IntConstant);
2368
2369 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002370 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2371 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2372 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2373 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002374
2375 const int32_t value_;
2376
2377 friend class HGraph;
2378 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2379 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2380 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2381};
2382
2383class HLongConstant : public HConstant {
2384 public:
2385 int64_t GetValue() const { return value_; }
2386
David Brazdil77a48ae2015-09-15 12:34:04 +00002387 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2388
Roland Levillain9867bc72015-08-05 10:21:34 +01002389 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2390 DCHECK(other->IsLongConstant());
2391 return other->AsLongConstant()->value_ == value_;
2392 }
2393
2394 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2395
2396 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2397 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2398 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2399
2400 DECLARE_INSTRUCTION(LongConstant);
2401
2402 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002403 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2404 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002405
2406 const int64_t value_;
2407
2408 friend class HGraph;
2409 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2410};
Dave Allison20dfc792014-06-16 20:44:29 -07002411
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002412// Conditional branch. A block ending with an HIf instruction must have
2413// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002414class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002415 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002416 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2417 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002418 SetRawInputAt(0, input);
2419 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002420
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002421 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002422
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002423 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002424 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002425 }
2426
2427 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002428 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002429 }
2430
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002431 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002432
2433 private:
2434 DISALLOW_COPY_AND_ASSIGN(HIf);
2435};
2436
David Brazdilfc6a86a2015-06-26 10:33:45 +00002437
2438// Abstract instruction which marks the beginning and/or end of a try block and
2439// links it to the respective exception handlers. Behaves the same as a Goto in
2440// non-exceptional control flow.
2441// Normal-flow successor is stored at index zero, exception handlers under
2442// higher indices in no particular order.
2443class HTryBoundary : public HTemplateInstruction<0> {
2444 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002445 enum BoundaryKind {
2446 kEntry,
2447 kExit,
2448 };
2449
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002450 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2451 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002452
2453 bool IsControlFlow() const OVERRIDE { return true; }
2454
2455 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002456 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002457
David Brazdild26a4112015-11-10 11:07:31 +00002458 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2459 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2460 }
2461
David Brazdilfc6a86a2015-06-26 10:33:45 +00002462 // Returns whether `handler` is among its exception handlers (non-zero index
2463 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002464 bool HasExceptionHandler(const HBasicBlock& handler) const {
2465 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002466 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002467 }
2468
2469 // If not present already, adds `handler` to its block's list of exception
2470 // handlers.
2471 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002472 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002473 GetBlock()->AddSuccessor(handler);
2474 }
2475 }
2476
David Brazdil56e1acc2015-06-30 15:41:36 +01002477 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002478
David Brazdilffee3d32015-07-06 11:48:53 +01002479 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2480
David Brazdilfc6a86a2015-06-26 10:33:45 +00002481 DECLARE_INSTRUCTION(TryBoundary);
2482
2483 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002484 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002485
2486 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2487};
2488
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002489// Deoptimize to interpreter, upon checking a condition.
2490class HDeoptimize : public HTemplateInstruction<1> {
2491 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002492 // We set CanTriggerGC to prevent any intermediate address to be live
2493 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002494 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002495 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002496 SetRawInputAt(0, cond);
2497 }
2498
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002499 bool CanBeMoved() const OVERRIDE { return true; }
2500 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2501 return true;
2502 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002503 bool NeedsEnvironment() const OVERRIDE { return true; }
2504 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002505
2506 DECLARE_INSTRUCTION(Deoptimize);
2507
2508 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002509 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2510};
2511
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002512// Represents the ArtMethod that was passed as a first argument to
2513// the method. It is used by instructions that depend on it, like
2514// instructions that work with the dex cache.
2515class HCurrentMethod : public HExpression<0> {
2516 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002517 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2518 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002519
2520 DECLARE_INSTRUCTION(CurrentMethod);
2521
2522 private:
2523 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2524};
2525
Mark Mendellfe57faa2015-09-18 09:26:15 -04002526// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2527// have one successor for each entry in the switch table, and the final successor
2528// will be the block containing the next Dex opcode.
2529class HPackedSwitch : public HTemplateInstruction<1> {
2530 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002531 HPackedSwitch(int32_t start_value,
2532 uint32_t num_entries,
2533 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002534 uint32_t dex_pc = kNoDexPc)
2535 : HTemplateInstruction(SideEffects::None(), dex_pc),
2536 start_value_(start_value),
2537 num_entries_(num_entries) {
2538 SetRawInputAt(0, input);
2539 }
2540
2541 bool IsControlFlow() const OVERRIDE { return true; }
2542
2543 int32_t GetStartValue() const { return start_value_; }
2544
Vladimir Marko211c2112015-09-24 16:52:33 +01002545 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002546
2547 HBasicBlock* GetDefaultBlock() const {
2548 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002549 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002550 }
2551 DECLARE_INSTRUCTION(PackedSwitch);
2552
2553 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002554 const int32_t start_value_;
2555 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002556
2557 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2558};
2559
Roland Levillain88cb1752014-10-20 16:36:47 +01002560class HUnaryOperation : public HExpression<1> {
2561 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002562 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2563 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002564 SetRawInputAt(0, input);
2565 }
2566
2567 HInstruction* GetInput() const { return InputAt(0); }
2568 Primitive::Type GetResultType() const { return GetType(); }
2569
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002570 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002571 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002572 return true;
2573 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002574
Roland Levillain9240d6a2014-10-20 16:47:04 +01002575 // Try to statically evaluate `operation` and return a HConstant
2576 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002577 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002578 HConstant* TryStaticEvaluation() const;
2579
2580 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002581 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2582 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002583
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002584 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002585
2586 private:
2587 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2588};
2589
Dave Allison20dfc792014-06-16 20:44:29 -07002590class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002591 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002592 HBinaryOperation(Primitive::Type result_type,
2593 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002594 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002595 SideEffects side_effects = SideEffects::None(),
2596 uint32_t dex_pc = kNoDexPc)
2597 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002598 SetRawInputAt(0, left);
2599 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002600 }
2601
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002602 HInstruction* GetLeft() const { return InputAt(0); }
2603 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002604 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002605
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002606 virtual bool IsCommutative() const { return false; }
2607
2608 // Put constant on the right.
2609 // Returns whether order is changed.
2610 bool OrderInputsWithConstantOnTheRight() {
2611 HInstruction* left = InputAt(0);
2612 HInstruction* right = InputAt(1);
2613 if (left->IsConstant() && !right->IsConstant()) {
2614 ReplaceInput(right, 0);
2615 ReplaceInput(left, 1);
2616 return true;
2617 }
2618 return false;
2619 }
2620
2621 // Order inputs by instruction id, but favor constant on the right side.
2622 // This helps GVN for commutative ops.
2623 void OrderInputs() {
2624 DCHECK(IsCommutative());
2625 HInstruction* left = InputAt(0);
2626 HInstruction* right = InputAt(1);
2627 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2628 return;
2629 }
2630 if (OrderInputsWithConstantOnTheRight()) {
2631 return;
2632 }
2633 // Order according to instruction id.
2634 if (left->GetId() > right->GetId()) {
2635 ReplaceInput(right, 0);
2636 ReplaceInput(left, 1);
2637 }
2638 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002639
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002640 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002641 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002642 return true;
2643 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002644
Roland Levillain9240d6a2014-10-20 16:47:04 +01002645 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002646 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002647 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002648 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002649
2650 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002651 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2652 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2653 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2654 HLongConstant* y ATTRIBUTE_UNUSED) const {
2655 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2656 return nullptr;
2657 }
2658 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2659 HIntConstant* y ATTRIBUTE_UNUSED) const {
2660 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2661 return nullptr;
2662 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002663 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2664 HNullConstant* y ATTRIBUTE_UNUSED) const {
2665 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2666 return nullptr;
2667 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002668
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002669 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002670 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002671 HConstant* GetConstantRight() const;
2672
2673 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002674 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002675 HInstruction* GetLeastConstantLeft() const;
2676
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002677 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002678
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002679 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002680 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2681};
2682
Mark Mendellc4701932015-04-10 13:18:51 -04002683// The comparison bias applies for floating point operations and indicates how NaN
2684// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002685enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002686 kNoBias, // bias is not applicable (i.e. for long operation)
2687 kGtBias, // return 1 for NaN comparisons
2688 kLtBias, // return -1 for NaN comparisons
2689};
2690
Dave Allison20dfc792014-06-16 20:44:29 -07002691class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2694 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002695 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002696 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002697
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002698 bool NeedsMaterialization() const { return needs_materialization_; }
2699 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002700
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002701 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002702 // `instruction`, and disregard moves in between.
2703 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002704
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002705 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002706
2707 virtual IfCondition GetCondition() const = 0;
2708
Mark Mendellc4701932015-04-10 13:18:51 -04002709 virtual IfCondition GetOppositeCondition() const = 0;
2710
Roland Levillain4fa13f62015-07-06 18:11:54 +01002711 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002712
2713 void SetBias(ComparisonBias bias) { bias_ = bias; }
2714
2715 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2716 return bias_ == other->AsCondition()->bias_;
2717 }
2718
Roland Levillain4fa13f62015-07-06 18:11:54 +01002719 bool IsFPConditionTrueIfNaN() const {
2720 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2721 IfCondition if_cond = GetCondition();
2722 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2723 }
2724
2725 bool IsFPConditionFalseIfNaN() const {
2726 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2727 IfCondition if_cond = GetCondition();
2728 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2729 }
2730
Dave Allison20dfc792014-06-16 20:44:29 -07002731 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002732 // For register allocation purposes, returns whether this instruction needs to be
2733 // materialized (that is, not just be in the processor flags).
2734 bool needs_materialization_;
2735
Mark Mendellc4701932015-04-10 13:18:51 -04002736 // Needed if we merge a HCompare into a HCondition.
2737 ComparisonBias bias_;
2738
Dave Allison20dfc792014-06-16 20:44:29 -07002739 DISALLOW_COPY_AND_ASSIGN(HCondition);
2740};
2741
2742// Instruction to check if two inputs are equal to each other.
2743class HEqual : public HCondition {
2744 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002745 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2746 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002747
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002748 bool IsCommutative() const OVERRIDE { return true; }
2749
Roland Levillain9867bc72015-08-05 10:21:34 +01002750 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002751 return GetBlock()->GetGraph()->GetIntConstant(
2752 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002753 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002754 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002755 return GetBlock()->GetGraph()->GetIntConstant(
2756 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002757 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002758 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2759 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002760 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002761 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002762
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002763 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002764
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002765 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002766 return kCondEQ;
2767 }
2768
Mark Mendellc4701932015-04-10 13:18:51 -04002769 IfCondition GetOppositeCondition() const OVERRIDE {
2770 return kCondNE;
2771 }
2772
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002773 private:
Aart Bike9f37602015-10-09 11:15:55 -07002774 template <typename T> bool Compute(T x, T y) const { return x == y; }
2775
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002776 DISALLOW_COPY_AND_ASSIGN(HEqual);
2777};
2778
Dave Allison20dfc792014-06-16 20:44:29 -07002779class HNotEqual : public HCondition {
2780 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002781 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2782 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002783
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002784 bool IsCommutative() const OVERRIDE { return true; }
2785
Roland Levillain9867bc72015-08-05 10:21:34 +01002786 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002787 return GetBlock()->GetGraph()->GetIntConstant(
2788 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002789 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002790 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002791 return GetBlock()->GetGraph()->GetIntConstant(
2792 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002793 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002794 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2795 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002796 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002797 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002798
Dave Allison20dfc792014-06-16 20:44:29 -07002799 DECLARE_INSTRUCTION(NotEqual);
2800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002801 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002802 return kCondNE;
2803 }
2804
Mark Mendellc4701932015-04-10 13:18:51 -04002805 IfCondition GetOppositeCondition() const OVERRIDE {
2806 return kCondEQ;
2807 }
2808
Dave Allison20dfc792014-06-16 20:44:29 -07002809 private:
Aart Bike9f37602015-10-09 11:15:55 -07002810 template <typename T> bool Compute(T x, T y) const { return x != y; }
2811
Dave Allison20dfc792014-06-16 20:44:29 -07002812 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2813};
2814
2815class HLessThan : public HCondition {
2816 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002817 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2818 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002819
Roland Levillain9867bc72015-08-05 10:21:34 +01002820 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002821 return GetBlock()->GetGraph()->GetIntConstant(
2822 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002823 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002824 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002825 return GetBlock()->GetGraph()->GetIntConstant(
2826 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002827 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002828
Dave Allison20dfc792014-06-16 20:44:29 -07002829 DECLARE_INSTRUCTION(LessThan);
2830
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002831 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002832 return kCondLT;
2833 }
2834
Mark Mendellc4701932015-04-10 13:18:51 -04002835 IfCondition GetOppositeCondition() const OVERRIDE {
2836 return kCondGE;
2837 }
2838
Dave Allison20dfc792014-06-16 20:44:29 -07002839 private:
Aart Bike9f37602015-10-09 11:15:55 -07002840 template <typename T> bool Compute(T x, T y) const { return x < y; }
2841
Dave Allison20dfc792014-06-16 20:44:29 -07002842 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2843};
2844
2845class HLessThanOrEqual : public HCondition {
2846 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002847 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2848 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002849
Roland Levillain9867bc72015-08-05 10:21:34 +01002850 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002851 return GetBlock()->GetGraph()->GetIntConstant(
2852 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002853 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002854 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002855 return GetBlock()->GetGraph()->GetIntConstant(
2856 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002857 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002858
Dave Allison20dfc792014-06-16 20:44:29 -07002859 DECLARE_INSTRUCTION(LessThanOrEqual);
2860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002861 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002862 return kCondLE;
2863 }
2864
Mark Mendellc4701932015-04-10 13:18:51 -04002865 IfCondition GetOppositeCondition() const OVERRIDE {
2866 return kCondGT;
2867 }
2868
Dave Allison20dfc792014-06-16 20:44:29 -07002869 private:
Aart Bike9f37602015-10-09 11:15:55 -07002870 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2871
Dave Allison20dfc792014-06-16 20:44:29 -07002872 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2873};
2874
2875class HGreaterThan : public HCondition {
2876 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002877 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2878 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002879
Roland Levillain9867bc72015-08-05 10:21:34 +01002880 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002881 return GetBlock()->GetGraph()->GetIntConstant(
2882 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002883 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002885 return GetBlock()->GetGraph()->GetIntConstant(
2886 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002887 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002888
Dave Allison20dfc792014-06-16 20:44:29 -07002889 DECLARE_INSTRUCTION(GreaterThan);
2890
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002891 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002892 return kCondGT;
2893 }
2894
Mark Mendellc4701932015-04-10 13:18:51 -04002895 IfCondition GetOppositeCondition() const OVERRIDE {
2896 return kCondLE;
2897 }
2898
Dave Allison20dfc792014-06-16 20:44:29 -07002899 private:
Aart Bike9f37602015-10-09 11:15:55 -07002900 template <typename T> bool Compute(T x, T y) const { return x > y; }
2901
Dave Allison20dfc792014-06-16 20:44:29 -07002902 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2903};
2904
2905class HGreaterThanOrEqual : public HCondition {
2906 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002907 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2908 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002909
Roland Levillain9867bc72015-08-05 10:21:34 +01002910 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002911 return GetBlock()->GetGraph()->GetIntConstant(
2912 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002913 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002914 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002915 return GetBlock()->GetGraph()->GetIntConstant(
2916 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002917 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002918
Dave Allison20dfc792014-06-16 20:44:29 -07002919 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2920
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002921 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002922 return kCondGE;
2923 }
2924
Mark Mendellc4701932015-04-10 13:18:51 -04002925 IfCondition GetOppositeCondition() const OVERRIDE {
2926 return kCondLT;
2927 }
2928
Dave Allison20dfc792014-06-16 20:44:29 -07002929 private:
Aart Bike9f37602015-10-09 11:15:55 -07002930 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2931
Dave Allison20dfc792014-06-16 20:44:29 -07002932 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2933};
2934
Aart Bike9f37602015-10-09 11:15:55 -07002935class HBelow : public HCondition {
2936 public:
2937 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2938 : HCondition(first, second, dex_pc) {}
2939
2940 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2941 return GetBlock()->GetGraph()->GetIntConstant(
2942 Compute(static_cast<uint32_t>(x->GetValue()),
2943 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2944 }
2945 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2946 return GetBlock()->GetGraph()->GetIntConstant(
2947 Compute(static_cast<uint64_t>(x->GetValue()),
2948 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2949 }
2950
2951 DECLARE_INSTRUCTION(Below);
2952
2953 IfCondition GetCondition() const OVERRIDE {
2954 return kCondB;
2955 }
2956
2957 IfCondition GetOppositeCondition() const OVERRIDE {
2958 return kCondAE;
2959 }
2960
2961 private:
2962 template <typename T> bool Compute(T x, T y) const { return x < y; }
2963
2964 DISALLOW_COPY_AND_ASSIGN(HBelow);
2965};
2966
2967class HBelowOrEqual : public HCondition {
2968 public:
2969 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2970 : HCondition(first, second, dex_pc) {}
2971
2972 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2973 return GetBlock()->GetGraph()->GetIntConstant(
2974 Compute(static_cast<uint32_t>(x->GetValue()),
2975 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2976 }
2977 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2978 return GetBlock()->GetGraph()->GetIntConstant(
2979 Compute(static_cast<uint64_t>(x->GetValue()),
2980 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2981 }
2982
2983 DECLARE_INSTRUCTION(BelowOrEqual);
2984
2985 IfCondition GetCondition() const OVERRIDE {
2986 return kCondBE;
2987 }
2988
2989 IfCondition GetOppositeCondition() const OVERRIDE {
2990 return kCondA;
2991 }
2992
2993 private:
2994 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2995
2996 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2997};
2998
2999class HAbove : public HCondition {
3000 public:
3001 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3002 : HCondition(first, second, dex_pc) {}
3003
3004 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3005 return GetBlock()->GetGraph()->GetIntConstant(
3006 Compute(static_cast<uint32_t>(x->GetValue()),
3007 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3008 }
3009 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3010 return GetBlock()->GetGraph()->GetIntConstant(
3011 Compute(static_cast<uint64_t>(x->GetValue()),
3012 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3013 }
3014
3015 DECLARE_INSTRUCTION(Above);
3016
3017 IfCondition GetCondition() const OVERRIDE {
3018 return kCondA;
3019 }
3020
3021 IfCondition GetOppositeCondition() const OVERRIDE {
3022 return kCondBE;
3023 }
3024
3025 private:
3026 template <typename T> bool Compute(T x, T y) const { return x > y; }
3027
3028 DISALLOW_COPY_AND_ASSIGN(HAbove);
3029};
3030
3031class HAboveOrEqual : public HCondition {
3032 public:
3033 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3034 : HCondition(first, second, dex_pc) {}
3035
3036 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3037 return GetBlock()->GetGraph()->GetIntConstant(
3038 Compute(static_cast<uint32_t>(x->GetValue()),
3039 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3040 }
3041 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3042 return GetBlock()->GetGraph()->GetIntConstant(
3043 Compute(static_cast<uint64_t>(x->GetValue()),
3044 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3045 }
3046
3047 DECLARE_INSTRUCTION(AboveOrEqual);
3048
3049 IfCondition GetCondition() const OVERRIDE {
3050 return kCondAE;
3051 }
3052
3053 IfCondition GetOppositeCondition() const OVERRIDE {
3054 return kCondB;
3055 }
3056
3057 private:
3058 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3059
3060 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3061};
Dave Allison20dfc792014-06-16 20:44:29 -07003062
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003063// Instruction to check how two inputs compare to each other.
3064// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3065class HCompare : public HBinaryOperation {
3066 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003067 HCompare(Primitive::Type type,
3068 HInstruction* first,
3069 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003070 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003071 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003072 : HBinaryOperation(Primitive::kPrimInt,
3073 first,
3074 second,
3075 SideEffectsForArchRuntimeCalls(type),
3076 dex_pc),
3077 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003078 DCHECK_EQ(type, first->GetType());
3079 DCHECK_EQ(type, second->GetType());
3080 }
3081
Roland Levillain9867bc72015-08-05 10:21:34 +01003082 template <typename T>
3083 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003084
Roland Levillain9867bc72015-08-05 10:21:34 +01003085 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003086 return GetBlock()->GetGraph()->GetIntConstant(
3087 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003088 }
3089 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003090 return GetBlock()->GetGraph()->GetIntConstant(
3091 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003092 }
3093
Calin Juravleddb7df22014-11-25 20:56:51 +00003094 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3095 return bias_ == other->AsCompare()->bias_;
3096 }
3097
Mark Mendellc4701932015-04-10 13:18:51 -04003098 ComparisonBias GetBias() const { return bias_; }
3099
Roland Levillain4fa13f62015-07-06 18:11:54 +01003100 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003101
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003102
3103 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3104 // MIPS64 uses a runtime call for FP comparisons.
3105 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3106 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003107
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003108 DECLARE_INSTRUCTION(Compare);
3109
3110 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003111 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003112
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003113 DISALLOW_COPY_AND_ASSIGN(HCompare);
3114};
3115
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003116// A local in the graph. Corresponds to a Dex register.
3117class HLocal : public HTemplateInstruction<0> {
3118 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003119 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003120 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003121
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003122 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003123
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003124 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003125
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003126 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003127 // The Dex register number.
3128 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003129
3130 DISALLOW_COPY_AND_ASSIGN(HLocal);
3131};
3132
3133// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003134class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003135 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003136 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3137 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003138 SetRawInputAt(0, local);
3139 }
3140
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003141 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3142
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003143 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003144
3145 private:
3146 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3147};
3148
3149// Store a value in a given local. This instruction has two inputs: the value
3150// and the local.
3151class HStoreLocal : public HTemplateInstruction<2> {
3152 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003153 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3154 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003155 SetRawInputAt(0, local);
3156 SetRawInputAt(1, value);
3157 }
3158
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003159 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3160
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003161 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003162
3163 private:
3164 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3165};
3166
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003167class HFloatConstant : public HConstant {
3168 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003169 float GetValue() const { return value_; }
3170
David Brazdil77a48ae2015-09-15 12:34:04 +00003171 uint64_t GetValueAsUint64() const OVERRIDE {
3172 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3173 }
3174
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003175 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003176 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003177 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003178 }
3179
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003180 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003181
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003182 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003183 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003184 }
3185 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003186 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003187 }
3188 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003189 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3190 }
3191 bool IsNaN() const {
3192 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003193 }
3194
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003195 DECLARE_INSTRUCTION(FloatConstant);
3196
3197 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003198 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3199 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3200 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3201 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003202
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003203 const float value_;
3204
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003205 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003206 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003207 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003208 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3209};
3210
3211class HDoubleConstant : public HConstant {
3212 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003213 double GetValue() const { return value_; }
3214
David Brazdil77a48ae2015-09-15 12:34:04 +00003215 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003217 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003218 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003219 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003220 }
3221
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003222 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003223
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003224 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003225 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003226 }
3227 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003228 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003229 }
3230 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003231 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3232 }
3233 bool IsNaN() const {
3234 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003235 }
3236
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003237 DECLARE_INSTRUCTION(DoubleConstant);
3238
3239 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003240 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3241 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3242 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3243 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003244
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003245 const double value_;
3246
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003247 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003248 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003249 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003250 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3251};
3252
David Brazdil6de19382016-01-08 17:37:10 +00003253class HNewInstance : public HExpression<2> {
3254 public:
3255 HNewInstance(HInstruction* cls,
3256 HCurrentMethod* current_method,
3257 uint32_t dex_pc,
3258 uint16_t type_index,
3259 const DexFile& dex_file,
3260 bool can_throw,
3261 bool finalizable,
3262 QuickEntrypointEnum entrypoint)
3263 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3264 type_index_(type_index),
3265 dex_file_(dex_file),
3266 can_throw_(can_throw),
3267 finalizable_(finalizable),
3268 entrypoint_(entrypoint) {
3269 SetRawInputAt(0, cls);
3270 SetRawInputAt(1, current_method);
3271 }
3272
3273 uint16_t GetTypeIndex() const { return type_index_; }
3274 const DexFile& GetDexFile() const { return dex_file_; }
3275
3276 // Calls runtime so needs an environment.
3277 bool NeedsEnvironment() const OVERRIDE { return true; }
3278
3279 // It may throw when called on type that's not instantiable/accessible.
3280 // It can throw OOME.
3281 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3282 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3283
3284 bool IsFinalizable() const { return finalizable_; }
3285
3286 bool CanBeNull() const OVERRIDE { return false; }
3287
3288 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3289
3290 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3291 entrypoint_ = entrypoint;
3292 }
3293
3294 bool IsStringAlloc() const;
3295
3296 DECLARE_INSTRUCTION(NewInstance);
3297
3298 private:
3299 const uint16_t type_index_;
3300 const DexFile& dex_file_;
3301 const bool can_throw_;
3302 const bool finalizable_;
3303 QuickEntrypointEnum entrypoint_;
3304
3305 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3306};
3307
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003308enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003309#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3310 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003311#include "intrinsics_list.h"
3312 kNone,
3313 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3314#undef INTRINSICS_LIST
3315#undef OPTIMIZING_INTRINSICS
3316};
3317std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3318
Agi Csaki05f20562015-08-19 14:58:14 -07003319enum IntrinsicNeedsEnvironmentOrCache {
3320 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3321 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003322};
3323
Aart Bik5d75afe2015-12-14 11:57:01 -08003324enum IntrinsicSideEffects {
3325 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3326 kReadSideEffects, // Intrinsic may read heap memory.
3327 kWriteSideEffects, // Intrinsic may write heap memory.
3328 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3329};
3330
3331enum IntrinsicExceptions {
3332 kNoThrow, // Intrinsic does not throw any exceptions.
3333 kCanThrow // Intrinsic may throw exceptions.
3334};
3335
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003336class HInvoke : public HInstruction {
3337 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003338 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003339
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003340 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003341
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003342 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003343 SetRawInputAt(index, argument);
3344 }
3345
Roland Levillain3e3d7332015-04-28 11:00:54 +01003346 // Return the number of arguments. This number can be lower than
3347 // the number of inputs returned by InputCount(), as some invoke
3348 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3349 // inputs at the end of their list of inputs.
3350 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3351
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003352 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003353
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003354 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003355 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003356
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003357 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3358
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003359 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003360 return intrinsic_;
3361 }
3362
Aart Bik5d75afe2015-12-14 11:57:01 -08003363 void SetIntrinsic(Intrinsics intrinsic,
3364 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3365 IntrinsicSideEffects side_effects,
3366 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003367
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003368 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003369 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003370 }
3371
Aart Bik5d75afe2015-12-14 11:57:01 -08003372 bool CanThrow() const OVERRIDE { return can_throw_; }
3373
3374 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3375
3376 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3377 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3378 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003379
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003380 uint32_t* GetIntrinsicOptimizations() {
3381 return &intrinsic_optimizations_;
3382 }
3383
3384 const uint32_t* GetIntrinsicOptimizations() const {
3385 return &intrinsic_optimizations_;
3386 }
3387
3388 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3389
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003390 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003391
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003392 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003393 HInvoke(ArenaAllocator* arena,
3394 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003395 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003396 Primitive::Type return_type,
3397 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003398 uint32_t dex_method_index,
3399 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003400 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003401 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003402 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003403 inputs_(number_of_arguments + number_of_other_inputs,
3404 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003405 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003406 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003407 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003408 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003409 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003410 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003411 }
3412
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003413 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003414 return inputs_[index];
3415 }
3416
David Brazdil1abb4192015-02-17 18:33:36 +00003417 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003418 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003419 }
3420
Aart Bik5d75afe2015-12-14 11:57:01 -08003421 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3422
Roland Levillain3e3d7332015-04-28 11:00:54 +01003423 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003424 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003425 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003426 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003427 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003428 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003429 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003430
3431 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3432 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003433
3434 private:
3435 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3436};
3437
Calin Juravle175dc732015-08-25 15:42:32 +01003438class HInvokeUnresolved : public HInvoke {
3439 public:
3440 HInvokeUnresolved(ArenaAllocator* arena,
3441 uint32_t number_of_arguments,
3442 Primitive::Type return_type,
3443 uint32_t dex_pc,
3444 uint32_t dex_method_index,
3445 InvokeType invoke_type)
3446 : HInvoke(arena,
3447 number_of_arguments,
3448 0u /* number_of_other_inputs */,
3449 return_type,
3450 dex_pc,
3451 dex_method_index,
3452 invoke_type) {
3453 }
3454
3455 DECLARE_INSTRUCTION(InvokeUnresolved);
3456
3457 private:
3458 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3459};
3460
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003461class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003462 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003463 // Requirements of this method call regarding the class
3464 // initialization (clinit) check of its declaring class.
3465 enum class ClinitCheckRequirement {
3466 kNone, // Class already initialized.
3467 kExplicit, // Static call having explicit clinit check as last input.
3468 kImplicit, // Static call implicitly requiring a clinit check.
3469 };
3470
Vladimir Marko58155012015-08-19 12:49:41 +00003471 // Determines how to load the target ArtMethod*.
3472 enum class MethodLoadKind {
3473 // Use a String init ArtMethod* loaded from Thread entrypoints.
3474 kStringInit,
3475
3476 // Use the method's own ArtMethod* loaded by the register allocator.
3477 kRecursive,
3478
3479 // Use ArtMethod* at a known address, embed the direct address in the code.
3480 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3481 kDirectAddress,
3482
3483 // Use ArtMethod* at an address that will be known at link time, embed the direct
3484 // address in the code. If the image is relocatable, emit .patch_oat entry.
3485 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3486 // the image relocatable or not.
3487 kDirectAddressWithFixup,
3488
3489 // Load from resoved methods array in the dex cache using a PC-relative load.
3490 // Used when we need to use the dex cache, for example for invoke-static that
3491 // may cause class initialization (the entry may point to a resolution method),
3492 // and we know that we can access the dex cache arrays using a PC-relative load.
3493 kDexCachePcRelative,
3494
3495 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3496 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3497 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3498 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3499 kDexCacheViaMethod,
3500 };
3501
3502 // Determines the location of the code pointer.
3503 enum class CodePtrLocation {
3504 // Recursive call, use local PC-relative call instruction.
3505 kCallSelf,
3506
3507 // Use PC-relative call instruction patched at link time.
3508 // Used for calls within an oat file, boot->boot or app->app.
3509 kCallPCRelative,
3510
3511 // Call to a known target address, embed the direct address in code.
3512 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3513 kCallDirect,
3514
3515 // Call to a target address that will be known at link time, embed the direct
3516 // address in code. If the image is relocatable, emit .patch_oat entry.
3517 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3518 // the image relocatable or not.
3519 kCallDirectWithFixup,
3520
3521 // Use code pointer from the ArtMethod*.
3522 // Used when we don't know the target code. This is also the last-resort-kind used when
3523 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3524 kCallArtMethod,
3525 };
3526
3527 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003528 MethodLoadKind method_load_kind;
3529 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003530 // The method load data holds
3531 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3532 // Note that there are multiple string init methods, each having its own offset.
3533 // - the method address for kDirectAddress
3534 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003535 uint64_t method_load_data;
3536 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003537 };
3538
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003539 HInvokeStaticOrDirect(ArenaAllocator* arena,
3540 uint32_t number_of_arguments,
3541 Primitive::Type return_type,
3542 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003543 uint32_t method_index,
3544 MethodReference target_method,
3545 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003546 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003547 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003548 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003549 : HInvoke(arena,
3550 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003551 // There is potentially one extra argument for the HCurrentMethod node, and
3552 // potentially one other if the clinit check is explicit, and potentially
3553 // one other if the method is a string factory.
3554 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3555 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3556 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003557 return_type,
3558 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003559 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003560 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003561 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003562 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003563 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003564 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003565
Vladimir Markodc151b22015-10-15 18:02:30 +01003566 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003567 bool had_current_method_input = HasCurrentMethodInput();
3568 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3569
3570 // Using the current method is the default and once we find a better
3571 // method load kind, we should not go back to using the current method.
3572 DCHECK(had_current_method_input || !needs_current_method_input);
3573
3574 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003575 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3576 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003577 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003578 dispatch_info_ = dispatch_info;
3579 }
3580
Vladimir Markoc53c0792015-11-19 15:48:33 +00003581 void AddSpecialInput(HInstruction* input) {
3582 // We allow only one special input.
3583 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3584 DCHECK(InputCount() == GetSpecialInputIndex() ||
3585 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3586 InsertInputAt(GetSpecialInputIndex(), input);
3587 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003588
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003589 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003590 // We access the method via the dex cache so we can't do an implicit null check.
3591 // TODO: for intrinsics we can generate implicit null checks.
3592 return false;
3593 }
3594
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003595 bool CanBeNull() const OVERRIDE {
3596 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3597 }
3598
Vladimir Markoc53c0792015-11-19 15:48:33 +00003599 // Get the index of the special input, if any.
3600 //
David Brazdil6de19382016-01-08 17:37:10 +00003601 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3602 // method pointer; otherwise there may be one platform-specific special input,
3603 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003604 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3605
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003606 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3607 void SetOptimizedInvokeType(InvokeType invoke_type) {
3608 optimized_invoke_type_ = invoke_type;
3609 }
3610
Vladimir Marko58155012015-08-19 12:49:41 +00003611 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3612 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3613 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003614 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003615 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003616 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003617 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003618 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3619 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003620 bool HasCurrentMethodInput() const {
3621 // This function can be called only after the invoke has been fully initialized by the builder.
3622 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003623 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003624 return true;
3625 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003626 DCHECK(InputCount() == GetSpecialInputIndex() ||
3627 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003628 return false;
3629 }
3630 }
Vladimir Marko58155012015-08-19 12:49:41 +00003631 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3632 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003633 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003634
3635 int32_t GetStringInitOffset() const {
3636 DCHECK(IsStringInit());
3637 return dispatch_info_.method_load_data;
3638 }
3639
3640 uint64_t GetMethodAddress() const {
3641 DCHECK(HasMethodAddress());
3642 return dispatch_info_.method_load_data;
3643 }
3644
3645 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003646 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003647 return dispatch_info_.method_load_data;
3648 }
3649
3650 uint64_t GetDirectCodePtr() const {
3651 DCHECK(HasDirectCodePtr());
3652 return dispatch_info_.direct_code_ptr;
3653 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003654
Calin Juravle68ad6492015-08-18 17:08:12 +01003655 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3656
Roland Levillain4c0eb422015-04-24 16:43:49 +01003657 // Is this instruction a call to a static method?
3658 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003659 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003660 }
3661
Vladimir Markofbb184a2015-11-13 14:47:00 +00003662 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3663 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3664 // instruction; only relevant for static calls with explicit clinit check.
3665 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003666 DCHECK(IsStaticWithExplicitClinitCheck());
3667 size_t last_input_index = InputCount() - 1;
3668 HInstruction* last_input = InputAt(last_input_index);
3669 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003670 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003671 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003672 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003673 clinit_check_requirement_ = new_requirement;
3674 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003675 }
3676
David Brazdil6de19382016-01-08 17:37:10 +00003677 HNewInstance* GetThisArgumentOfStringInit() const {
3678 DCHECK(IsStringInit());
3679 size_t index = InputCount() - 1;
3680 DCHECK(InputAt(index)->IsNewInstance());
3681 return InputAt(index)->AsNewInstance();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003682 }
3683
David Brazdil6de19382016-01-08 17:37:10 +00003684 void RemoveThisArgumentOfStringInit() {
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003685 DCHECK(IsStringInit());
David Brazdil6de19382016-01-08 17:37:10 +00003686 size_t index = InputCount() - 1;
3687 DCHECK(InputAt(index)->IsNewInstance());
3688 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003689 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003690 }
3691
Roland Levillain4c0eb422015-04-24 16:43:49 +01003692 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003693 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003694 bool IsStaticWithExplicitClinitCheck() const {
3695 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3696 }
3697
3698 // Is this a call to a static method whose declaring class has an
3699 // implicit intialization check requirement?
3700 bool IsStaticWithImplicitClinitCheck() const {
3701 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3702 }
3703
Vladimir Markob554b5a2015-11-06 12:57:55 +00003704 // Does this method load kind need the current method as an input?
3705 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3706 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3707 }
3708
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003709 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003710
Roland Levillain4c0eb422015-04-24 16:43:49 +01003711 protected:
3712 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3713 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3714 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3715 HInstruction* input = input_record.GetInstruction();
3716 // `input` is the last input of a static invoke marked as having
3717 // an explicit clinit check. It must either be:
3718 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3719 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3720 DCHECK(input != nullptr);
3721 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3722 }
3723 return input_record;
3724 }
3725
Vladimir Markoc53c0792015-11-19 15:48:33 +00003726 void InsertInputAt(size_t index, HInstruction* input);
3727 void RemoveInputAt(size_t index);
3728
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003729 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003730 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003731 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003732 // The target method may refer to different dex file or method index than the original
3733 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3734 // in derived class to increase visibility.
3735 MethodReference target_method_;
3736 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003737
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003738 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003739};
Vladimir Markof64242a2015-12-01 14:58:23 +00003740std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003741std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003742
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003743class HInvokeVirtual : public HInvoke {
3744 public:
3745 HInvokeVirtual(ArenaAllocator* arena,
3746 uint32_t number_of_arguments,
3747 Primitive::Type return_type,
3748 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003749 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003750 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003751 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003752 vtable_index_(vtable_index) {}
3753
Calin Juravle641547a2015-04-21 22:08:51 +01003754 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003755 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003756 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003757 }
3758
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003759 uint32_t GetVTableIndex() const { return vtable_index_; }
3760
3761 DECLARE_INSTRUCTION(InvokeVirtual);
3762
3763 private:
3764 const uint32_t vtable_index_;
3765
3766 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3767};
3768
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003769class HInvokeInterface : public HInvoke {
3770 public:
3771 HInvokeInterface(ArenaAllocator* arena,
3772 uint32_t number_of_arguments,
3773 Primitive::Type return_type,
3774 uint32_t dex_pc,
3775 uint32_t dex_method_index,
3776 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003777 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003778 imt_index_(imt_index) {}
3779
Calin Juravle641547a2015-04-21 22:08:51 +01003780 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003781 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003782 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003783 }
3784
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003785 uint32_t GetImtIndex() const { return imt_index_; }
3786 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3787
3788 DECLARE_INSTRUCTION(InvokeInterface);
3789
3790 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003791 const uint32_t imt_index_;
3792
3793 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3794};
3795
Roland Levillain88cb1752014-10-20 16:36:47 +01003796class HNeg : public HUnaryOperation {
3797 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003798 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3799 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003800
Roland Levillain9867bc72015-08-05 10:21:34 +01003801 template <typename T> T Compute(T x) const { return -x; }
3802
3803 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003804 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003805 }
3806 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003807 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003808 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003809
Roland Levillain88cb1752014-10-20 16:36:47 +01003810 DECLARE_INSTRUCTION(Neg);
3811
3812 private:
3813 DISALLOW_COPY_AND_ASSIGN(HNeg);
3814};
3815
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003816class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003817 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003818 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003819 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003820 uint32_t dex_pc,
3821 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003822 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003823 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003824 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003825 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003826 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003827 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003828 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003829 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003830 }
3831
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003832 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003833 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003834
3835 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003836 bool NeedsEnvironment() const OVERRIDE { return true; }
3837
Mingyao Yang0c365e62015-03-31 15:09:29 -07003838 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3839 bool CanThrow() const OVERRIDE { return true; }
3840
Calin Juravle10e244f2015-01-26 18:54:32 +00003841 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003842
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003843 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3844
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003845 DECLARE_INSTRUCTION(NewArray);
3846
3847 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003848 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003849 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003850 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003851
3852 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3853};
3854
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003855class HAdd : public HBinaryOperation {
3856 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003857 HAdd(Primitive::Type result_type,
3858 HInstruction* left,
3859 HInstruction* right,
3860 uint32_t dex_pc = kNoDexPc)
3861 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003862
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003863 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003864
Roland Levillain9867bc72015-08-05 10:21:34 +01003865 template <typename T> T Compute(T x, T y) const { return x + y; }
3866
3867 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003868 return GetBlock()->GetGraph()->GetIntConstant(
3869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003870 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003871 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 return GetBlock()->GetGraph()->GetLongConstant(
3873 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003874 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003875
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003876 DECLARE_INSTRUCTION(Add);
3877
3878 private:
3879 DISALLOW_COPY_AND_ASSIGN(HAdd);
3880};
3881
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003882class HSub : public HBinaryOperation {
3883 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003884 HSub(Primitive::Type result_type,
3885 HInstruction* left,
3886 HInstruction* right,
3887 uint32_t dex_pc = kNoDexPc)
3888 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003889
Roland Levillain9867bc72015-08-05 10:21:34 +01003890 template <typename T> T Compute(T x, T y) const { return x - y; }
3891
3892 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003893 return GetBlock()->GetGraph()->GetIntConstant(
3894 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003895 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003896 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003897 return GetBlock()->GetGraph()->GetLongConstant(
3898 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003899 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003900
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003901 DECLARE_INSTRUCTION(Sub);
3902
3903 private:
3904 DISALLOW_COPY_AND_ASSIGN(HSub);
3905};
3906
Calin Juravle34bacdf2014-10-07 20:23:36 +01003907class HMul : public HBinaryOperation {
3908 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003909 HMul(Primitive::Type result_type,
3910 HInstruction* left,
3911 HInstruction* right,
3912 uint32_t dex_pc = kNoDexPc)
3913 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003914
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003915 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003916
Roland Levillain9867bc72015-08-05 10:21:34 +01003917 template <typename T> T Compute(T x, T y) const { return x * y; }
3918
3919 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003920 return GetBlock()->GetGraph()->GetIntConstant(
3921 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003922 }
3923 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003924 return GetBlock()->GetGraph()->GetLongConstant(
3925 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003926 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003927
3928 DECLARE_INSTRUCTION(Mul);
3929
3930 private:
3931 DISALLOW_COPY_AND_ASSIGN(HMul);
3932};
3933
Calin Juravle7c4954d2014-10-28 16:57:40 +00003934class HDiv : public HBinaryOperation {
3935 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003936 HDiv(Primitive::Type result_type,
3937 HInstruction* left,
3938 HInstruction* right,
3939 uint32_t dex_pc)
3940 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003941
Roland Levillain9867bc72015-08-05 10:21:34 +01003942 template <typename T>
3943 T Compute(T x, T y) const {
3944 // Our graph structure ensures we never have 0 for `y` during
3945 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003946 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003947 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003948 return (y == -1) ? -x : x / y;
3949 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003950
Roland Levillain9867bc72015-08-05 10:21:34 +01003951 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003952 return GetBlock()->GetGraph()->GetIntConstant(
3953 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003954 }
3955 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003956 return GetBlock()->GetGraph()->GetLongConstant(
3957 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003958 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003959
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003960 static SideEffects SideEffectsForArchRuntimeCalls() {
3961 // The generated code can use a runtime call.
3962 return SideEffects::CanTriggerGC();
3963 }
3964
Calin Juravle7c4954d2014-10-28 16:57:40 +00003965 DECLARE_INSTRUCTION(Div);
3966
3967 private:
3968 DISALLOW_COPY_AND_ASSIGN(HDiv);
3969};
3970
Calin Juravlebacfec32014-11-14 15:54:36 +00003971class HRem : public HBinaryOperation {
3972 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003973 HRem(Primitive::Type result_type,
3974 HInstruction* left,
3975 HInstruction* right,
3976 uint32_t dex_pc)
3977 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003978
Roland Levillain9867bc72015-08-05 10:21:34 +01003979 template <typename T>
3980 T Compute(T x, T y) const {
3981 // Our graph structure ensures we never have 0 for `y` during
3982 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003983 DCHECK_NE(y, 0);
3984 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3985 return (y == -1) ? 0 : x % y;
3986 }
3987
Roland Levillain9867bc72015-08-05 10:21:34 +01003988 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003989 return GetBlock()->GetGraph()->GetIntConstant(
3990 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003991 }
3992 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003993 return GetBlock()->GetGraph()->GetLongConstant(
3994 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003995 }
3996
Calin Juravlebacfec32014-11-14 15:54:36 +00003997
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003998 static SideEffects SideEffectsForArchRuntimeCalls() {
3999 return SideEffects::CanTriggerGC();
4000 }
4001
Calin Juravlebacfec32014-11-14 15:54:36 +00004002 DECLARE_INSTRUCTION(Rem);
4003
4004 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004005 DISALLOW_COPY_AND_ASSIGN(HRem);
4006};
4007
Calin Juravled0d48522014-11-04 16:40:20 +00004008class HDivZeroCheck : public HExpression<1> {
4009 public:
4010 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004011 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004012 SetRawInputAt(0, value);
4013 }
4014
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004015 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4016
Calin Juravled0d48522014-11-04 16:40:20 +00004017 bool CanBeMoved() const OVERRIDE { return true; }
4018
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004019 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004020 return true;
4021 }
4022
4023 bool NeedsEnvironment() const OVERRIDE { return true; }
4024 bool CanThrow() const OVERRIDE { return true; }
4025
Calin Juravled0d48522014-11-04 16:40:20 +00004026 DECLARE_INSTRUCTION(DivZeroCheck);
4027
4028 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004029 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4030};
4031
Calin Juravle9aec02f2014-11-18 23:06:35 +00004032class HShl : public HBinaryOperation {
4033 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004034 HShl(Primitive::Type result_type,
4035 HInstruction* left,
4036 HInstruction* right,
4037 uint32_t dex_pc = kNoDexPc)
4038 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004039
Roland Levillain9867bc72015-08-05 10:21:34 +01004040 template <typename T, typename U, typename V>
4041 T Compute(T x, U y, V max_shift_value) const {
4042 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4043 "V is not the unsigned integer type corresponding to T");
4044 return x << (y & max_shift_value);
4045 }
4046
4047 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4048 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004049 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004050 }
4051 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4052 // case is handled as `x << static_cast<int>(y)`.
4053 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4054 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004055 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004056 }
4057 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4058 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004059 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004060 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004061
4062 DECLARE_INSTRUCTION(Shl);
4063
4064 private:
4065 DISALLOW_COPY_AND_ASSIGN(HShl);
4066};
4067
4068class HShr : public HBinaryOperation {
4069 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 HShr(Primitive::Type result_type,
4071 HInstruction* left,
4072 HInstruction* right,
4073 uint32_t dex_pc = kNoDexPc)
4074 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004075
Roland Levillain9867bc72015-08-05 10:21:34 +01004076 template <typename T, typename U, typename V>
4077 T Compute(T x, U y, V max_shift_value) const {
4078 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4079 "V is not the unsigned integer type corresponding to T");
4080 return x >> (y & max_shift_value);
4081 }
4082
4083 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4084 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004085 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004086 }
4087 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4088 // case is handled as `x >> static_cast<int>(y)`.
4089 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4090 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004091 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004092 }
4093 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4094 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004095 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004096 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004097
4098 DECLARE_INSTRUCTION(Shr);
4099
4100 private:
4101 DISALLOW_COPY_AND_ASSIGN(HShr);
4102};
4103
4104class HUShr : public HBinaryOperation {
4105 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004106 HUShr(Primitive::Type result_type,
4107 HInstruction* left,
4108 HInstruction* right,
4109 uint32_t dex_pc = kNoDexPc)
4110 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004111
Roland Levillain9867bc72015-08-05 10:21:34 +01004112 template <typename T, typename U, typename V>
4113 T Compute(T x, U y, V max_shift_value) const {
4114 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4115 "V is not the unsigned integer type corresponding to T");
4116 V ux = static_cast<V>(x);
4117 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004118 }
4119
Roland Levillain9867bc72015-08-05 10:21:34 +01004120 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4121 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004122 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004123 }
4124 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4125 // case is handled as `x >>> static_cast<int>(y)`.
4126 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4127 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004128 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004129 }
4130 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4131 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004132 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004133 }
4134
4135 DECLARE_INSTRUCTION(UShr);
4136
4137 private:
4138 DISALLOW_COPY_AND_ASSIGN(HUShr);
4139};
4140
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004141class HAnd : public HBinaryOperation {
4142 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 HAnd(Primitive::Type result_type,
4144 HInstruction* left,
4145 HInstruction* right,
4146 uint32_t dex_pc = kNoDexPc)
4147 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004148
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004149 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004150
Roland Levillain9867bc72015-08-05 10:21:34 +01004151 template <typename T, typename U>
4152 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4153
4154 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004155 return GetBlock()->GetGraph()->GetIntConstant(
4156 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004157 }
4158 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004159 return GetBlock()->GetGraph()->GetLongConstant(
4160 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004161 }
4162 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004163 return GetBlock()->GetGraph()->GetLongConstant(
4164 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004165 }
4166 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004167 return GetBlock()->GetGraph()->GetLongConstant(
4168 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004169 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004170
4171 DECLARE_INSTRUCTION(And);
4172
4173 private:
4174 DISALLOW_COPY_AND_ASSIGN(HAnd);
4175};
4176
4177class HOr : public HBinaryOperation {
4178 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004179 HOr(Primitive::Type result_type,
4180 HInstruction* left,
4181 HInstruction* right,
4182 uint32_t dex_pc = kNoDexPc)
4183 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004184
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004185 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004186
Roland Levillain9867bc72015-08-05 10:21:34 +01004187 template <typename T, typename U>
4188 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4189
4190 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004191 return GetBlock()->GetGraph()->GetIntConstant(
4192 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004193 }
4194 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 return GetBlock()->GetGraph()->GetLongConstant(
4196 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004197 }
4198 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004199 return GetBlock()->GetGraph()->GetLongConstant(
4200 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004201 }
4202 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004203 return GetBlock()->GetGraph()->GetLongConstant(
4204 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004205 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004206
4207 DECLARE_INSTRUCTION(Or);
4208
4209 private:
4210 DISALLOW_COPY_AND_ASSIGN(HOr);
4211};
4212
4213class HXor : public HBinaryOperation {
4214 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004215 HXor(Primitive::Type result_type,
4216 HInstruction* left,
4217 HInstruction* right,
4218 uint32_t dex_pc = kNoDexPc)
4219 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004220
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004221 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004222
Roland Levillain9867bc72015-08-05 10:21:34 +01004223 template <typename T, typename U>
4224 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4225
4226 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004227 return GetBlock()->GetGraph()->GetIntConstant(
4228 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004229 }
4230 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004231 return GetBlock()->GetGraph()->GetLongConstant(
4232 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004233 }
4234 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004235 return GetBlock()->GetGraph()->GetLongConstant(
4236 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004237 }
4238 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004239 return GetBlock()->GetGraph()->GetLongConstant(
4240 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004241 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004242
4243 DECLARE_INSTRUCTION(Xor);
4244
4245 private:
4246 DISALLOW_COPY_AND_ASSIGN(HXor);
4247};
4248
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004249class HRor : public HBinaryOperation {
4250 public:
4251 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4252 : HBinaryOperation(result_type, value, distance) {}
4253
4254 template <typename T, typename U, typename V>
4255 T Compute(T x, U y, V max_shift_value) const {
4256 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4257 "V is not the unsigned integer type corresponding to T");
4258 V ux = static_cast<V>(x);
4259 if ((y & max_shift_value) == 0) {
4260 return static_cast<T>(ux);
4261 } else {
4262 const V reg_bits = sizeof(T) * 8;
4263 return static_cast<T>(ux >> (y & max_shift_value)) |
4264 (x << (reg_bits - (y & max_shift_value)));
4265 }
4266 }
4267
4268 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4269 return GetBlock()->GetGraph()->GetIntConstant(
4270 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4271 }
4272 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4273 return GetBlock()->GetGraph()->GetLongConstant(
4274 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4275 }
4276 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4277 return GetBlock()->GetGraph()->GetLongConstant(
4278 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4279 }
4280
4281 DECLARE_INSTRUCTION(Ror);
4282
4283 private:
4284 DISALLOW_COPY_AND_ASSIGN(HRor);
4285};
4286
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004287// The value of a parameter in this method. Its location depends on
4288// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004289class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004290 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004291 HParameterValue(const DexFile& dex_file,
4292 uint16_t type_index,
4293 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004294 Primitive::Type parameter_type,
4295 bool is_this = false)
4296 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004297 dex_file_(dex_file),
4298 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004299 index_(index),
4300 is_this_(is_this),
4301 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004302
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004303 const DexFile& GetDexFile() const { return dex_file_; }
4304 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004305 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004306 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004307
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004308 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4309 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004310
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004311 DECLARE_INSTRUCTION(ParameterValue);
4312
4313 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004314 const DexFile& dex_file_;
4315 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004316 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004317 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004318 const uint8_t index_;
4319
Calin Juravle10e244f2015-01-26 18:54:32 +00004320 // Whether or not the parameter value corresponds to 'this' argument.
4321 const bool is_this_;
4322
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004323 bool can_be_null_;
4324
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004325 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4326};
4327
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004328class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004329 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004330 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4331 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004332
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004333 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004334 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004335 return true;
4336 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004337
Roland Levillain9867bc72015-08-05 10:21:34 +01004338 template <typename T> T Compute(T x) const { return ~x; }
4339
4340 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004341 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004342 }
4343 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004344 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004345 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004346
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004347 DECLARE_INSTRUCTION(Not);
4348
4349 private:
4350 DISALLOW_COPY_AND_ASSIGN(HNot);
4351};
4352
David Brazdil66d126e2015-04-03 16:02:44 +01004353class HBooleanNot : public HUnaryOperation {
4354 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4356 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004357
4358 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004359 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004360 return true;
4361 }
4362
Roland Levillain9867bc72015-08-05 10:21:34 +01004363 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004364 DCHECK(IsUint<1>(x));
4365 return !x;
4366 }
4367
Roland Levillain9867bc72015-08-05 10:21:34 +01004368 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004369 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004370 }
4371 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4372 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004373 UNREACHABLE();
4374 }
4375
4376 DECLARE_INSTRUCTION(BooleanNot);
4377
4378 private:
4379 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4380};
4381
Roland Levillaindff1f282014-11-05 14:15:05 +00004382class HTypeConversion : public HExpression<1> {
4383 public:
4384 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004385 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004386 : HExpression(result_type,
4387 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4388 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004389 SetRawInputAt(0, input);
4390 DCHECK_NE(input->GetType(), result_type);
4391 }
4392
4393 HInstruction* GetInput() const { return InputAt(0); }
4394 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4395 Primitive::Type GetResultType() const { return GetType(); }
4396
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004397 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004398 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004399
Roland Levillaindff1f282014-11-05 14:15:05 +00004400 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004401 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004402
Mark Mendelle82549b2015-05-06 10:55:34 -04004403 // Try to statically evaluate the conversion and return a HConstant
4404 // containing the result. If the input cannot be converted, return nullptr.
4405 HConstant* TryStaticEvaluation() const;
4406
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004407 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4408 Primitive::Type result_type) {
4409 // Some architectures may not require the 'GC' side effects, but at this point
4410 // in the compilation process we do not know what architecture we will
4411 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004412 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4413 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004414 return SideEffects::CanTriggerGC();
4415 }
4416 return SideEffects::None();
4417 }
4418
Roland Levillaindff1f282014-11-05 14:15:05 +00004419 DECLARE_INSTRUCTION(TypeConversion);
4420
4421 private:
4422 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4423};
4424
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004425static constexpr uint32_t kNoRegNumber = -1;
4426
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004427class HPhi : public HInstruction {
4428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004429 HPhi(ArenaAllocator* arena,
4430 uint32_t reg_number,
4431 size_t number_of_inputs,
4432 Primitive::Type type,
4433 uint32_t dex_pc = kNoDexPc)
4434 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004435 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004436 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004437 type_(ToPhiType(type)),
4438 // Phis are constructed live and marked dead if conflicting or unused.
4439 // Individual steps of SsaBuilder should assume that if a phi has been
4440 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4441 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004442 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004443 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004444 }
4445
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004446 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4447 static Primitive::Type ToPhiType(Primitive::Type type) {
4448 switch (type) {
4449 case Primitive::kPrimBoolean:
4450 case Primitive::kPrimByte:
4451 case Primitive::kPrimShort:
4452 case Primitive::kPrimChar:
4453 return Primitive::kPrimInt;
4454 default:
4455 return type;
4456 }
4457 }
4458
David Brazdilffee3d32015-07-06 11:48:53 +01004459 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4460
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004461 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004462
4463 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004464 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004465
Calin Juravle10e244f2015-01-26 18:54:32 +00004466 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004467 void SetType(Primitive::Type new_type) {
4468 // Make sure that only valid type changes occur. The following are allowed:
4469 // (1) int -> float/ref (primitive type propagation),
4470 // (2) long -> double (primitive type propagation).
4471 DCHECK(type_ == new_type ||
4472 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4473 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4474 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4475 type_ = new_type;
4476 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004477
Calin Juravle10e244f2015-01-26 18:54:32 +00004478 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4479 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4480
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004481 uint32_t GetRegNumber() const { return reg_number_; }
4482
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004483 void SetDead() { is_live_ = false; }
4484 void SetLive() { is_live_ = true; }
4485 bool IsDead() const { return !is_live_; }
4486 bool IsLive() const { return is_live_; }
4487
David Brazdil77a48ae2015-09-15 12:34:04 +00004488 bool IsVRegEquivalentOf(HInstruction* other) const {
4489 return other != nullptr
4490 && other->IsPhi()
4491 && other->AsPhi()->GetBlock() == GetBlock()
4492 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4493 }
4494
Calin Juravlea4f88312015-04-16 12:57:19 +01004495 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4496 // An equivalent phi is a phi having the same dex register and type.
4497 // It assumes that phis with the same dex register are adjacent.
4498 HPhi* GetNextEquivalentPhiWithSameType() {
4499 HInstruction* next = GetNext();
4500 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4501 if (next->GetType() == GetType()) {
4502 return next->AsPhi();
4503 }
4504 next = next->GetNext();
4505 }
4506 return nullptr;
4507 }
4508
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004509 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004510
David Brazdil1abb4192015-02-17 18:33:36 +00004511 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004512 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004513 return inputs_[index];
4514 }
David Brazdil1abb4192015-02-17 18:33:36 +00004515
4516 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004517 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004518 }
4519
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004520 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004521 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004522 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004523 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004524 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004525 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004526
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004527 DISALLOW_COPY_AND_ASSIGN(HPhi);
4528};
4529
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004530class HNullCheck : public HExpression<1> {
4531 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004532 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4533 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004534 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004535 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004536 SetRawInputAt(0, value);
4537 }
4538
Calin Juravle10e244f2015-01-26 18:54:32 +00004539 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004540 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004541 return true;
4542 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004543
Calin Juravle10e244f2015-01-26 18:54:32 +00004544 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004545
Calin Juravle10e244f2015-01-26 18:54:32 +00004546 bool CanThrow() const OVERRIDE { return true; }
4547
4548 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004549
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004550
4551 DECLARE_INSTRUCTION(NullCheck);
4552
4553 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004554 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4555};
4556
4557class FieldInfo : public ValueObject {
4558 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004559 FieldInfo(MemberOffset field_offset,
4560 Primitive::Type field_type,
4561 bool is_volatile,
4562 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004563 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004564 const DexFile& dex_file,
4565 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004566 : field_offset_(field_offset),
4567 field_type_(field_type),
4568 is_volatile_(is_volatile),
4569 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004570 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004571 dex_file_(dex_file),
4572 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004573
4574 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004575 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004576 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004577 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004578 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004579 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004580 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004581
4582 private:
4583 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004584 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004585 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004586 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004587 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004588 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004589 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004590};
4591
4592class HInstanceFieldGet : public HExpression<1> {
4593 public:
4594 HInstanceFieldGet(HInstruction* value,
4595 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004596 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004597 bool is_volatile,
4598 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004599 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004600 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004601 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004602 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004603 : HExpression(field_type,
4604 SideEffects::FieldReadOfType(field_type, is_volatile),
4605 dex_pc),
4606 field_info_(field_offset,
4607 field_type,
4608 is_volatile,
4609 field_idx,
4610 declaring_class_def_index,
4611 dex_file,
4612 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004613 SetRawInputAt(0, value);
4614 }
4615
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004616 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004617
4618 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4619 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4620 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004621 }
4622
Calin Juravle641547a2015-04-21 22:08:51 +01004623 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4624 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004625 }
4626
4627 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004628 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4629 }
4630
Calin Juravle52c48962014-12-16 17:02:57 +00004631 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004632 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004633 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004634 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004635
4636 DECLARE_INSTRUCTION(InstanceFieldGet);
4637
4638 private:
4639 const FieldInfo field_info_;
4640
4641 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4642};
4643
4644class HInstanceFieldSet : public HTemplateInstruction<2> {
4645 public:
4646 HInstanceFieldSet(HInstruction* object,
4647 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004648 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004649 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004650 bool is_volatile,
4651 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004652 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004653 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004655 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004656 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4657 dex_pc),
4658 field_info_(field_offset,
4659 field_type,
4660 is_volatile,
4661 field_idx,
4662 declaring_class_def_index,
4663 dex_file,
4664 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004665 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004666 SetRawInputAt(0, object);
4667 SetRawInputAt(1, value);
4668 }
4669
Calin Juravle641547a2015-04-21 22:08:51 +01004670 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4671 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004672 }
4673
Calin Juravle52c48962014-12-16 17:02:57 +00004674 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004675 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004676 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004677 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004678 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004679 bool GetValueCanBeNull() const { return value_can_be_null_; }
4680 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004681
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004682 DECLARE_INSTRUCTION(InstanceFieldSet);
4683
4684 private:
4685 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004686 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004687
4688 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4689};
4690
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004691class HArrayGet : public HExpression<2> {
4692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004693 HArrayGet(HInstruction* array,
4694 HInstruction* index,
4695 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004696 uint32_t dex_pc,
4697 SideEffects additional_side_effects = SideEffects::None())
4698 : HExpression(type,
4699 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004700 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004701 SetRawInputAt(0, array);
4702 SetRawInputAt(1, index);
4703 }
4704
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004705 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004706 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004707 return true;
4708 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004709 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004710 // TODO: We can be smarter here.
4711 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4712 // which generates the implicit null check. There are cases when these can be removed
4713 // to produce better code. If we ever add optimizations to do so we should allow an
4714 // implicit check here (as long as the address falls in the first page).
4715 return false;
4716 }
4717
David Brazdil4833f5a2015-12-16 10:37:39 +00004718 bool IsEquivalentOf(HArrayGet* other) const {
4719 bool result = (GetDexPc() == other->GetDexPc());
4720 if (kIsDebugBuild && result) {
4721 DCHECK_EQ(GetBlock(), other->GetBlock());
4722 DCHECK_EQ(GetArray(), other->GetArray());
4723 DCHECK_EQ(GetIndex(), other->GetIndex());
4724 if (Primitive::IsIntOrLongType(GetType())) {
4725 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4726 } else {
4727 DCHECK(Primitive::IsFloatingPointType(GetType()));
4728 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4729 }
4730 }
4731 return result;
4732 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004733
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004734 HInstruction* GetArray() const { return InputAt(0); }
4735 HInstruction* GetIndex() const { return InputAt(1); }
4736
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737 DECLARE_INSTRUCTION(ArrayGet);
4738
4739 private:
4740 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4741};
4742
4743class HArraySet : public HTemplateInstruction<3> {
4744 public:
4745 HArraySet(HInstruction* array,
4746 HInstruction* index,
4747 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004748 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004749 uint32_t dex_pc,
4750 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004751 : HTemplateInstruction(
4752 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004753 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4754 additional_side_effects),
4755 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004756 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004757 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004758 value_can_be_null_(true),
4759 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004760 SetRawInputAt(0, array);
4761 SetRawInputAt(1, index);
4762 SetRawInputAt(2, value);
4763 }
4764
Calin Juravle77520bc2015-01-12 18:45:46 +00004765 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004766 // We currently always call a runtime method to catch array store
4767 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004768 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004769 }
4770
Mingyao Yang81014cb2015-06-02 03:16:27 -07004771 // Can throw ArrayStoreException.
4772 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4773
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004774 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004775 // TODO: Same as for ArrayGet.
4776 return false;
4777 }
4778
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004779 void ClearNeedsTypeCheck() {
4780 needs_type_check_ = false;
4781 }
4782
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004783 void ClearValueCanBeNull() {
4784 value_can_be_null_ = false;
4785 }
4786
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004787 void SetStaticTypeOfArrayIsObjectArray() {
4788 static_type_of_array_is_object_array_ = true;
4789 }
4790
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004791 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004792 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004793 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004794
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004795 HInstruction* GetArray() const { return InputAt(0); }
4796 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004797 HInstruction* GetValue() const { return InputAt(2); }
4798
4799 Primitive::Type GetComponentType() const {
4800 // The Dex format does not type floating point index operations. Since the
4801 // `expected_component_type_` is set during building and can therefore not
4802 // be correct, we also check what is the value type. If it is a floating
4803 // point type, we must use that type.
4804 Primitive::Type value_type = GetValue()->GetType();
4805 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4806 ? value_type
4807 : expected_component_type_;
4808 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004809
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004810 Primitive::Type GetRawExpectedComponentType() const {
4811 return expected_component_type_;
4812 }
4813
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004814 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4815 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4816 }
4817
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004818 DECLARE_INSTRUCTION(ArraySet);
4819
4820 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004821 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004822 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004823 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004824 // Cached information for the reference_type_info_ so that codegen
4825 // does not need to inspect the static type.
4826 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004827
4828 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4829};
4830
4831class HArrayLength : public HExpression<1> {
4832 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004833 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004834 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004835 // Note that arrays do not change length, so the instruction does not
4836 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004837 SetRawInputAt(0, array);
4838 }
4839
Calin Juravle77520bc2015-01-12 18:45:46 +00004840 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004841 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004842 return true;
4843 }
Calin Juravle641547a2015-04-21 22:08:51 +01004844 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4845 return obj == InputAt(0);
4846 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004847
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004848 DECLARE_INSTRUCTION(ArrayLength);
4849
4850 private:
4851 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4852};
4853
4854class HBoundsCheck : public HExpression<2> {
4855 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004856 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4857 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004858 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004859 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004860 DCHECK(index->GetType() == Primitive::kPrimInt);
4861 SetRawInputAt(0, index);
4862 SetRawInputAt(1, length);
4863 }
4864
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004865 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004866 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004867 return true;
4868 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004869
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004870 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004871
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004872 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004873
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004874 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004875
4876 DECLARE_INSTRUCTION(BoundsCheck);
4877
4878 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4880};
4881
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004882/**
4883 * Some DEX instructions are folded into multiple HInstructions that need
4884 * to stay live until the last HInstruction. This class
4885 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004886 * HInstruction stays live. `index` represents the stack location index of the
4887 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004888 */
4889class HTemporary : public HTemplateInstruction<0> {
4890 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004891 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4892 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004893
4894 size_t GetIndex() const { return index_; }
4895
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004896 Primitive::Type GetType() const OVERRIDE {
4897 // The previous instruction is the one that will be stored in the temporary location.
4898 DCHECK(GetPrevious() != nullptr);
4899 return GetPrevious()->GetType();
4900 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004901
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004902 DECLARE_INSTRUCTION(Temporary);
4903
4904 private:
4905 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004906 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4907};
4908
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004909class HSuspendCheck : public HTemplateInstruction<0> {
4910 public:
4911 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004912 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004913
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004914 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004915 return true;
4916 }
4917
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004918 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4919 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004920
4921 DECLARE_INSTRUCTION(SuspendCheck);
4922
4923 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004924 // Only used for code generation, in order to share the same slow path between back edges
4925 // of a same loop.
4926 SlowPathCode* slow_path_;
4927
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004928 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4929};
4930
David Srbecky0cf44932015-12-09 14:09:59 +00004931// Pseudo-instruction which provides the native debugger with mapping information.
4932// It ensures that we can generate line number and local variables at this point.
4933class HNativeDebugInfo : public HTemplateInstruction<0> {
4934 public:
4935 explicit HNativeDebugInfo(uint32_t dex_pc)
4936 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4937
4938 bool NeedsEnvironment() const OVERRIDE {
4939 return true;
4940 }
4941
4942 DECLARE_INSTRUCTION(NativeDebugInfo);
4943
4944 private:
4945 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4946};
4947
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004948/**
4949 * Instruction to load a Class object.
4950 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004951class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004952 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004953 HLoadClass(HCurrentMethod* current_method,
4954 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004955 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004956 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004957 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004958 bool needs_access_check,
4959 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004960 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004961 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004962 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004963 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004964 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004965 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004966 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004967 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004968 // Referrers class should not need access check. We never inline unverified
4969 // methods so we can't possibly end up in this situation.
4970 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004971 SetRawInputAt(0, current_method);
4972 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004973
4974 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004975
4976 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004977 // Note that we don't need to test for generate_clinit_check_.
4978 // Whether or not we need to generate the clinit check is processed in
4979 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004980 return other->AsLoadClass()->type_index_ == type_index_ &&
4981 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004982 }
4983
4984 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4985
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004986 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004987 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004988 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004989
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004990 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004991 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004992 }
4993
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004994 bool MustGenerateClinitCheck() const {
4995 return generate_clinit_check_;
4996 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004997
Calin Juravle0ba218d2015-05-19 18:46:01 +01004998 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004999 // The entrypoint the code generator is going to call does not do
5000 // clinit of the class.
5001 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005002 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005003 }
5004
5005 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005006 return MustGenerateClinitCheck() ||
5007 (!is_referrers_class_ && !is_in_dex_cache_) ||
5008 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005009 }
5010
5011 bool NeedsAccessCheck() const {
5012 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005013 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005014
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005015 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005016 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005017 }
5018
Calin Juravleacf735c2015-02-12 15:25:22 +00005019 ReferenceTypeInfo GetLoadedClassRTI() {
5020 return loaded_class_rti_;
5021 }
5022
5023 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5024 // Make sure we only set exact types (the loaded class should never be merged).
5025 DCHECK(rti.IsExact());
5026 loaded_class_rti_ = rti;
5027 }
5028
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005029 const DexFile& GetDexFile() { return dex_file_; }
5030
Vladimir Markodc151b22015-10-15 18:02:30 +01005031 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005032
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005033 static SideEffects SideEffectsForArchRuntimeCalls() {
5034 return SideEffects::CanTriggerGC();
5035 }
5036
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005037 bool IsInDexCache() const { return is_in_dex_cache_; }
5038
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005039 DECLARE_INSTRUCTION(LoadClass);
5040
5041 private:
5042 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005043 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005044 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005045 // Whether this instruction must generate the initialization check.
5046 // Used for code generation.
5047 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005048 const bool needs_access_check_;
5049 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005050
Calin Juravleacf735c2015-02-12 15:25:22 +00005051 ReferenceTypeInfo loaded_class_rti_;
5052
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005053 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5054};
5055
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005056class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005057 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005058 HLoadString(HCurrentMethod* current_method,
5059 uint32_t string_index,
5060 uint32_t dex_pc,
5061 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005062 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005063 string_index_(string_index),
5064 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005065 SetRawInputAt(0, current_method);
5066 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005067
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005068 bool CanBeMoved() const OVERRIDE { return true; }
5069
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005070 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5071 return other->AsLoadString()->string_index_ == string_index_;
5072 }
5073
5074 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5075
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005076 uint32_t GetStringIndex() const { return string_index_; }
5077
5078 // TODO: Can we deopt or debug when we resolve a string?
5079 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005080 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005081 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005082 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005083
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005084 static SideEffects SideEffectsForArchRuntimeCalls() {
5085 return SideEffects::CanTriggerGC();
5086 }
5087
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005088 DECLARE_INSTRUCTION(LoadString);
5089
5090 private:
5091 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005092 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005093
5094 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5095};
5096
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005097/**
5098 * Performs an initialization check on its Class object input.
5099 */
5100class HClinitCheck : public HExpression<1> {
5101 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005102 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005103 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005104 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005105 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5106 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005107 SetRawInputAt(0, constant);
5108 }
5109
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005110 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005111 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005112 return true;
5113 }
5114
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005115 bool NeedsEnvironment() const OVERRIDE {
5116 // May call runtime to initialize the class.
5117 return true;
5118 }
5119
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005120 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005121
5122 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5123
5124 DECLARE_INSTRUCTION(ClinitCheck);
5125
5126 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005127 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5128};
5129
5130class HStaticFieldGet : public HExpression<1> {
5131 public:
5132 HStaticFieldGet(HInstruction* cls,
5133 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005134 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005135 bool is_volatile,
5136 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005137 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005138 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005139 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005140 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005141 : HExpression(field_type,
5142 SideEffects::FieldReadOfType(field_type, is_volatile),
5143 dex_pc),
5144 field_info_(field_offset,
5145 field_type,
5146 is_volatile,
5147 field_idx,
5148 declaring_class_def_index,
5149 dex_file,
5150 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005151 SetRawInputAt(0, cls);
5152 }
5153
Calin Juravle52c48962014-12-16 17:02:57 +00005154
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005155 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005156
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005157 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005158 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5159 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005160 }
5161
5162 size_t ComputeHashCode() const OVERRIDE {
5163 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5164 }
5165
Calin Juravle52c48962014-12-16 17:02:57 +00005166 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005167 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5168 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005169 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005170
5171 DECLARE_INSTRUCTION(StaticFieldGet);
5172
5173 private:
5174 const FieldInfo field_info_;
5175
5176 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5177};
5178
5179class HStaticFieldSet : public HTemplateInstruction<2> {
5180 public:
5181 HStaticFieldSet(HInstruction* cls,
5182 HInstruction* value,
5183 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005184 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005185 bool is_volatile,
5186 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005187 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005188 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005189 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005190 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005191 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5192 dex_pc),
5193 field_info_(field_offset,
5194 field_type,
5195 is_volatile,
5196 field_idx,
5197 declaring_class_def_index,
5198 dex_file,
5199 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005200 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005201 SetRawInputAt(0, cls);
5202 SetRawInputAt(1, value);
5203 }
5204
Calin Juravle52c48962014-12-16 17:02:57 +00005205 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005206 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5207 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005208 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005209
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005210 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005211 bool GetValueCanBeNull() const { return value_can_be_null_; }
5212 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005213
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005214 DECLARE_INSTRUCTION(StaticFieldSet);
5215
5216 private:
5217 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005218 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005219
5220 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5221};
5222
Calin Juravlee460d1d2015-09-29 04:52:17 +01005223class HUnresolvedInstanceFieldGet : public HExpression<1> {
5224 public:
5225 HUnresolvedInstanceFieldGet(HInstruction* obj,
5226 Primitive::Type field_type,
5227 uint32_t field_index,
5228 uint32_t dex_pc)
5229 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5230 field_index_(field_index) {
5231 SetRawInputAt(0, obj);
5232 }
5233
5234 bool NeedsEnvironment() const OVERRIDE { return true; }
5235 bool CanThrow() const OVERRIDE { return true; }
5236
5237 Primitive::Type GetFieldType() const { return GetType(); }
5238 uint32_t GetFieldIndex() const { return field_index_; }
5239
5240 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5241
5242 private:
5243 const uint32_t field_index_;
5244
5245 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5246};
5247
5248class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5249 public:
5250 HUnresolvedInstanceFieldSet(HInstruction* obj,
5251 HInstruction* value,
5252 Primitive::Type field_type,
5253 uint32_t field_index,
5254 uint32_t dex_pc)
5255 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5256 field_type_(field_type),
5257 field_index_(field_index) {
5258 DCHECK_EQ(field_type, value->GetType());
5259 SetRawInputAt(0, obj);
5260 SetRawInputAt(1, value);
5261 }
5262
5263 bool NeedsEnvironment() const OVERRIDE { return true; }
5264 bool CanThrow() const OVERRIDE { return true; }
5265
5266 Primitive::Type GetFieldType() const { return field_type_; }
5267 uint32_t GetFieldIndex() const { return field_index_; }
5268
5269 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5270
5271 private:
5272 const Primitive::Type field_type_;
5273 const uint32_t field_index_;
5274
5275 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5276};
5277
5278class HUnresolvedStaticFieldGet : public HExpression<0> {
5279 public:
5280 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5281 uint32_t field_index,
5282 uint32_t dex_pc)
5283 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5284 field_index_(field_index) {
5285 }
5286
5287 bool NeedsEnvironment() const OVERRIDE { return true; }
5288 bool CanThrow() const OVERRIDE { return true; }
5289
5290 Primitive::Type GetFieldType() const { return GetType(); }
5291 uint32_t GetFieldIndex() const { return field_index_; }
5292
5293 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5294
5295 private:
5296 const uint32_t field_index_;
5297
5298 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5299};
5300
5301class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5302 public:
5303 HUnresolvedStaticFieldSet(HInstruction* value,
5304 Primitive::Type field_type,
5305 uint32_t field_index,
5306 uint32_t dex_pc)
5307 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5308 field_type_(field_type),
5309 field_index_(field_index) {
5310 DCHECK_EQ(field_type, value->GetType());
5311 SetRawInputAt(0, value);
5312 }
5313
5314 bool NeedsEnvironment() const OVERRIDE { return true; }
5315 bool CanThrow() const OVERRIDE { return true; }
5316
5317 Primitive::Type GetFieldType() const { return field_type_; }
5318 uint32_t GetFieldIndex() const { return field_index_; }
5319
5320 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5321
5322 private:
5323 const Primitive::Type field_type_;
5324 const uint32_t field_index_;
5325
5326 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5327};
5328
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005329// Implement the move-exception DEX instruction.
5330class HLoadException : public HExpression<0> {
5331 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005332 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5333 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005334
David Brazdilbbd733e2015-08-18 17:48:17 +01005335 bool CanBeNull() const OVERRIDE { return false; }
5336
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005337 DECLARE_INSTRUCTION(LoadException);
5338
5339 private:
5340 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5341};
5342
David Brazdilcb1c0552015-08-04 16:22:25 +01005343// Implicit part of move-exception which clears thread-local exception storage.
5344// Must not be removed because the runtime expects the TLS to get cleared.
5345class HClearException : public HTemplateInstruction<0> {
5346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005347 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5348 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005349
5350 DECLARE_INSTRUCTION(ClearException);
5351
5352 private:
5353 DISALLOW_COPY_AND_ASSIGN(HClearException);
5354};
5355
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005356class HThrow : public HTemplateInstruction<1> {
5357 public:
5358 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005359 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005360 SetRawInputAt(0, exception);
5361 }
5362
5363 bool IsControlFlow() const OVERRIDE { return true; }
5364
5365 bool NeedsEnvironment() const OVERRIDE { return true; }
5366
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005367 bool CanThrow() const OVERRIDE { return true; }
5368
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005369
5370 DECLARE_INSTRUCTION(Throw);
5371
5372 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005373 DISALLOW_COPY_AND_ASSIGN(HThrow);
5374};
5375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005376/**
5377 * Implementation strategies for the code generator of a HInstanceOf
5378 * or `HCheckCast`.
5379 */
5380enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005381 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005382 kExactCheck, // Can do a single class compare.
5383 kClassHierarchyCheck, // Can just walk the super class chain.
5384 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5385 kInterfaceCheck, // No optimization yet when checking against an interface.
5386 kArrayObjectCheck, // Can just check if the array is not primitive.
5387 kArrayCheck // No optimization yet when checking against a generic array.
5388};
5389
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005390class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005391 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005392 HInstanceOf(HInstruction* object,
5393 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005394 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005395 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005396 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005397 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005398 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005399 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005400 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005401 SetRawInputAt(0, object);
5402 SetRawInputAt(1, constant);
5403 }
5404
5405 bool CanBeMoved() const OVERRIDE { return true; }
5406
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005407 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005408 return true;
5409 }
5410
5411 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005412 return false;
5413 }
5414
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005415 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5416
5417 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005418
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005419 // Used only in code generation.
5420 bool MustDoNullCheck() const { return must_do_null_check_; }
5421 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5422
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005423 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5424 return (check_kind == TypeCheckKind::kExactCheck)
5425 ? SideEffects::None()
5426 // Mips currently does runtime calls for any other checks.
5427 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005428 }
5429
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005430 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005431
5432 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005433 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005434 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005435
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005436 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5437};
5438
Calin Juravleb1498f62015-02-16 13:13:29 +00005439class HBoundType : public HExpression<1> {
5440 public:
David Brazdilf5552582015-12-27 13:36:12 +00005441 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005442 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005443 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5444 upper_can_be_null_(true),
5445 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005446 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005447 SetRawInputAt(0, input);
5448 }
5449
David Brazdilf5552582015-12-27 13:36:12 +00005450 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005451 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5452 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005453 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005454
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005455 void SetCanBeNull(bool can_be_null) {
5456 DCHECK(upper_can_be_null_ || !can_be_null);
5457 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005458 }
5459
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005460 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5461
Calin Juravleb1498f62015-02-16 13:13:29 +00005462 DECLARE_INSTRUCTION(BoundType);
5463
5464 private:
5465 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005466 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5467 // It is used to bound the type in cases like:
5468 // if (x instanceof ClassX) {
5469 // // uper_bound_ will be ClassX
5470 // }
David Brazdilf5552582015-12-27 13:36:12 +00005471 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005472 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5473 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005474 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005475 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005476
5477 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5478};
5479
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005480class HCheckCast : public HTemplateInstruction<2> {
5481 public:
5482 HCheckCast(HInstruction* object,
5483 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005484 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005485 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005486 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005487 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005488 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005489 SetRawInputAt(0, object);
5490 SetRawInputAt(1, constant);
5491 }
5492
5493 bool CanBeMoved() const OVERRIDE { return true; }
5494
5495 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5496 return true;
5497 }
5498
5499 bool NeedsEnvironment() const OVERRIDE {
5500 // Instruction may throw a CheckCastError.
5501 return true;
5502 }
5503
5504 bool CanThrow() const OVERRIDE { return true; }
5505
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005506 bool MustDoNullCheck() const { return must_do_null_check_; }
5507 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005508 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005509
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005510 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005511
5512 DECLARE_INSTRUCTION(CheckCast);
5513
5514 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005515 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005516 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005517
5518 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005519};
5520
Calin Juravle27df7582015-04-17 19:12:31 +01005521class HMemoryBarrier : public HTemplateInstruction<0> {
5522 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005523 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005524 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005525 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005526 barrier_kind_(barrier_kind) {}
5527
5528 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5529
5530 DECLARE_INSTRUCTION(MemoryBarrier);
5531
5532 private:
5533 const MemBarrierKind barrier_kind_;
5534
5535 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5536};
5537
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005538class HMonitorOperation : public HTemplateInstruction<1> {
5539 public:
5540 enum OperationKind {
5541 kEnter,
5542 kExit,
5543 };
5544
5545 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005546 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005547 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5548 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005549 SetRawInputAt(0, object);
5550 }
5551
5552 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005553 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5554
5555 bool CanThrow() const OVERRIDE {
5556 // Verifier guarantees that monitor-exit cannot throw.
5557 // This is important because it allows the HGraphBuilder to remove
5558 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5559 return IsEnter();
5560 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005561
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005562
5563 bool IsEnter() const { return kind_ == kEnter; }
5564
5565 DECLARE_INSTRUCTION(MonitorOperation);
5566
Calin Juravle52c48962014-12-16 17:02:57 +00005567 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005568 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005569
5570 private:
5571 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5572};
5573
Vladimir Markof9f64412015-09-02 14:05:49 +01005574class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005575 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005576 MoveOperands(Location source,
5577 Location destination,
5578 Primitive::Type type,
5579 HInstruction* instruction)
5580 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005581
5582 Location GetSource() const { return source_; }
5583 Location GetDestination() const { return destination_; }
5584
5585 void SetSource(Location value) { source_ = value; }
5586 void SetDestination(Location value) { destination_ = value; }
5587
5588 // The parallel move resolver marks moves as "in-progress" by clearing the
5589 // destination (but not the source).
5590 Location MarkPending() {
5591 DCHECK(!IsPending());
5592 Location dest = destination_;
5593 destination_ = Location::NoLocation();
5594 return dest;
5595 }
5596
5597 void ClearPending(Location dest) {
5598 DCHECK(IsPending());
5599 destination_ = dest;
5600 }
5601
5602 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005603 DCHECK(source_.IsValid() || destination_.IsInvalid());
5604 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005605 }
5606
5607 // True if this blocks a move from the given location.
5608 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005609 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005610 }
5611
5612 // A move is redundant if it's been eliminated, if its source and
5613 // destination are the same, or if its destination is unneeded.
5614 bool IsRedundant() const {
5615 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5616 }
5617
5618 // We clear both operands to indicate move that's been eliminated.
5619 void Eliminate() {
5620 source_ = destination_ = Location::NoLocation();
5621 }
5622
5623 bool IsEliminated() const {
5624 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5625 return source_.IsInvalid();
5626 }
5627
Alexey Frunze4dda3372015-06-01 18:31:49 -07005628 Primitive::Type GetType() const { return type_; }
5629
Nicolas Geoffray90218252015-04-15 11:56:51 +01005630 bool Is64BitMove() const {
5631 return Primitive::Is64BitType(type_);
5632 }
5633
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005634 HInstruction* GetInstruction() const { return instruction_; }
5635
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005636 private:
5637 Location source_;
5638 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005639 // The type this move is for.
5640 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005641 // The instruction this move is assocatied with. Null when this move is
5642 // for moving an input in the expected locations of user (including a phi user).
5643 // This is only used in debug mode, to ensure we do not connect interval siblings
5644 // in the same parallel move.
5645 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005646};
5647
Roland Levillainc9285912015-12-18 10:38:42 +00005648std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5649
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005650static constexpr size_t kDefaultNumberOfMoves = 4;
5651
5652class HParallelMove : public HTemplateInstruction<0> {
5653 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005654 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005655 : HTemplateInstruction(SideEffects::None(), dex_pc),
5656 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5657 moves_.reserve(kDefaultNumberOfMoves);
5658 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005659
Nicolas Geoffray90218252015-04-15 11:56:51 +01005660 void AddMove(Location source,
5661 Location destination,
5662 Primitive::Type type,
5663 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005664 DCHECK(source.IsValid());
5665 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005666 if (kIsDebugBuild) {
5667 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005668 for (const MoveOperands& move : moves_) {
5669 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005670 // Special case the situation where the move is for the spill slot
5671 // of the instruction.
5672 if ((GetPrevious() == instruction)
5673 || ((GetPrevious() == nullptr)
5674 && instruction->IsPhi()
5675 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005676 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005677 << "Doing parallel moves for the same instruction.";
5678 } else {
5679 DCHECK(false) << "Doing parallel moves for the same instruction.";
5680 }
5681 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005682 }
5683 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005684 for (const MoveOperands& move : moves_) {
5685 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005686 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005687 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005688 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005689 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005690 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005691 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005692 }
5693
Vladimir Marko225b6462015-09-28 12:17:40 +01005694 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005695 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005696 }
5697
Vladimir Marko225b6462015-09-28 12:17:40 +01005698 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005699
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005700 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005701
5702 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005703 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005704
5705 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5706};
5707
Mark Mendell0616ae02015-04-17 12:49:27 -04005708} // namespace art
5709
Vladimir Markob4536b72015-11-24 13:45:23 +00005710#ifdef ART_ENABLE_CODEGEN_arm
5711#include "nodes_arm.h"
5712#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005713#ifdef ART_ENABLE_CODEGEN_arm64
5714#include "nodes_arm64.h"
5715#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005716#ifdef ART_ENABLE_CODEGEN_x86
5717#include "nodes_x86.h"
5718#endif
5719
5720namespace art {
5721
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005722class HGraphVisitor : public ValueObject {
5723 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005724 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5725 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005726
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005727 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005728 virtual void VisitBasicBlock(HBasicBlock* block);
5729
Roland Levillain633021e2014-10-01 14:12:25 +01005730 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005731 void VisitInsertionOrder();
5732
Roland Levillain633021e2014-10-01 14:12:25 +01005733 // Visit the graph following dominator tree reverse post-order.
5734 void VisitReversePostOrder();
5735
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005736 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005737
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005738 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005739#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005740 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5741
5742 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5743
5744#undef DECLARE_VISIT_INSTRUCTION
5745
5746 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005747 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005748
5749 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5750};
5751
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005752class HGraphDelegateVisitor : public HGraphVisitor {
5753 public:
5754 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5755 virtual ~HGraphDelegateVisitor() {}
5756
5757 // Visit functions that delegate to to super class.
5758#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005759 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005760
5761 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5762
5763#undef DECLARE_VISIT_INSTRUCTION
5764
5765 private:
5766 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5767};
5768
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005769class HInsertionOrderIterator : public ValueObject {
5770 public:
5771 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5772
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005773 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005774 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005775 void Advance() { ++index_; }
5776
5777 private:
5778 const HGraph& graph_;
5779 size_t index_;
5780
5781 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5782};
5783
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005784class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005785 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005786 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5787 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005788 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005789 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005790
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005791 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5792 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005793 void Advance() { ++index_; }
5794
5795 private:
5796 const HGraph& graph_;
5797 size_t index_;
5798
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005799 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005800};
5801
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005802class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005803 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005804 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005805 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005806 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005807 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005808 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005809
5810 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005811 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005812 void Advance() { --index_; }
5813
5814 private:
5815 const HGraph& graph_;
5816 size_t index_;
5817
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005818 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005819};
5820
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005821class HLinearPostOrderIterator : public ValueObject {
5822 public:
5823 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005824 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005825
5826 bool Done() const { return index_ == 0; }
5827
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005828 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005829
5830 void Advance() {
5831 --index_;
5832 DCHECK_GE(index_, 0U);
5833 }
5834
5835 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005836 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005837 size_t index_;
5838
5839 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5840};
5841
5842class HLinearOrderIterator : public ValueObject {
5843 public:
5844 explicit HLinearOrderIterator(const HGraph& graph)
5845 : order_(graph.GetLinearOrder()), index_(0) {}
5846
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005847 bool Done() const { return index_ == order_.size(); }
5848 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005849 void Advance() { ++index_; }
5850
5851 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005852 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005853 size_t index_;
5854
5855 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5856};
5857
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005858// Iterator over the blocks that art part of the loop. Includes blocks part
5859// of an inner loop. The order in which the blocks are iterated is on their
5860// block id.
5861class HBlocksInLoopIterator : public ValueObject {
5862 public:
5863 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5864 : blocks_in_loop_(info.GetBlocks()),
5865 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5866 index_(0) {
5867 if (!blocks_in_loop_.IsBitSet(index_)) {
5868 Advance();
5869 }
5870 }
5871
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005872 bool Done() const { return index_ == blocks_.size(); }
5873 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005874 void Advance() {
5875 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005876 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005877 if (blocks_in_loop_.IsBitSet(index_)) {
5878 break;
5879 }
5880 }
5881 }
5882
5883 private:
5884 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005885 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005886 size_t index_;
5887
5888 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5889};
5890
Mingyao Yang3584bce2015-05-19 16:01:59 -07005891// Iterator over the blocks that art part of the loop. Includes blocks part
5892// of an inner loop. The order in which the blocks are iterated is reverse
5893// post order.
5894class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5895 public:
5896 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5897 : blocks_in_loop_(info.GetBlocks()),
5898 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5899 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005900 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005901 Advance();
5902 }
5903 }
5904
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005905 bool Done() const { return index_ == blocks_.size(); }
5906 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005907 void Advance() {
5908 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005909 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5910 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005911 break;
5912 }
5913 }
5914 }
5915
5916 private:
5917 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005918 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005919 size_t index_;
5920
5921 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5922};
5923
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005924inline int64_t Int64FromConstant(HConstant* constant) {
5925 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5926 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5927 : constant->AsLongConstant()->GetValue();
5928}
5929
Vladimir Marko58155012015-08-19 12:49:41 +00005930inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5931 // For the purposes of the compiler, the dex files must actually be the same object
5932 // if we want to safely treat them as the same. This is especially important for JIT
5933 // as custom class loaders can open the same underlying file (or memory) multiple
5934 // times and provide different class resolution but no two class loaders should ever
5935 // use the same DexFile object - doing so is an unsupported hack that can lead to
5936 // all sorts of weird failures.
5937 return &lhs == &rhs;
5938}
5939
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005940#define INSTRUCTION_TYPE_CHECK(type, super) \
5941 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5942 inline const H##type* HInstruction::As##type() const { \
5943 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5944 } \
5945 inline H##type* HInstruction::As##type() { \
5946 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5947 }
5948
5949 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
5950#undef INSTRUCTION_TYPE_CHECK
5951
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005952} // namespace art
5953
5954#endif // ART_COMPILER_OPTIMIZING_NODES_H_