blob: 35944ec1cead08108b178837bd598693ea28674d [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;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010047class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Calin Juravle9aec02f2014-11-18 23:06:35 +000075static constexpr uint32_t kMaxIntShiftValue = 0x1f;
76static constexpr uint64_t kMaxLongShiftValue = 0x3f;
77
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010078static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070079static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010081static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
82
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060083static constexpr uint32_t kNoDexPc = -1;
84
Dave Allison20dfc792014-06-16 20:44:29 -070085enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070086 // All types.
87 kCondEQ, // ==
88 kCondNE, // !=
89 // Signed integers and floating-point numbers.
90 kCondLT, // <
91 kCondLE, // <=
92 kCondGT, // >
93 kCondGE, // >=
94 // Unsigned integers.
95 kCondB, // <
96 kCondBE, // <=
97 kCondA, // >
98 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070099};
100
David Brazdil4833f5a2015-12-16 10:37:39 +0000101enum BuildSsaResult {
102 kBuildSsaFailNonNaturalLoop,
103 kBuildSsaFailThrowCatchLoop,
David Brazdil15693bf2015-12-16 10:30:45 +0000104 kBuildSsaFailAmbiguousArrayOp,
David Brazdil4833f5a2015-12-16 10:37:39 +0000105 kBuildSsaSuccess,
106};
107
Vladimir Markof9f64412015-09-02 14:05:49 +0100108class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 public:
110 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
111
112 void AddInstruction(HInstruction* instruction);
113 void RemoveInstruction(HInstruction* instruction);
114
David Brazdilc3d743f2015-04-22 13:40:50 +0100115 // Insert `instruction` before/after an existing instruction `cursor`.
116 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
117 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
118
Roland Levillain6b469232014-09-25 10:10:38 +0100119 // Return true if this list contains `instruction`.
120 bool Contains(HInstruction* instruction) const;
121
Roland Levillainccc07a92014-09-16 14:48:16 +0100122 // Return true if `instruction1` is found before `instruction2` in
123 // this instruction list and false otherwise. Abort if none
124 // of these instructions is found.
125 bool FoundBefore(const HInstruction* instruction1,
126 const HInstruction* instruction2) const;
127
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000128 bool IsEmpty() const { return first_instruction_ == nullptr; }
129 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
130
131 // Update the block of all instructions to be `block`.
132 void SetBlockOfInstructions(HBasicBlock* block) const;
133
134 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
135 void Add(const HInstructionList& instruction_list);
136
David Brazdil2d7352b2015-04-20 14:52:42 +0100137 // Return the number of instructions in the list. This is an expensive operation.
138 size_t CountSize() const;
139
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 private:
141 HInstruction* first_instruction_;
142 HInstruction* last_instruction_;
143
144 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000145 friend class HGraph;
146 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100148 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100149
150 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
151};
152
David Brazdil4833f5a2015-12-16 10:37:39 +0000153class ReferenceTypeInfo : ValueObject {
154 public:
155 typedef Handle<mirror::Class> TypeHandle;
156
157 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
158 // The constructor will check that the type_handle is valid.
159 return ReferenceTypeInfo(type_handle, is_exact);
160 }
161
162 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
163
164 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
165 return handle.GetReference() != nullptr;
166 }
167
168 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
169 return IsValidHandle(type_handle_);
170 }
171
172 bool IsExact() const { return is_exact_; }
173
174 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
175 DCHECK(IsValid());
176 return GetTypeHandle()->IsObjectClass();
177 }
178
179 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
180 DCHECK(IsValid());
181 return GetTypeHandle()->IsStringClass();
182 }
183
184 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
185 DCHECK(IsValid());
186 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
187 }
188
189 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
190 DCHECK(IsValid());
191 return GetTypeHandle()->IsInterface();
192 }
193
194 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
195 DCHECK(IsValid());
196 return GetTypeHandle()->IsArrayClass();
197 }
198
199 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
200 DCHECK(IsValid());
201 return GetTypeHandle()->IsPrimitiveArray();
202 }
203
204 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
205 DCHECK(IsValid());
206 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
207 }
208
209 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
210 DCHECK(IsValid());
211 if (!IsExact()) return false;
212 if (!IsArrayClass()) return false;
213 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
214 }
215
216 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
217 DCHECK(IsValid());
218 if (!IsExact()) return false;
219 if (!IsArrayClass()) return false;
220 if (!rti.IsArrayClass()) return false;
221 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
222 rti.GetTypeHandle()->GetComponentType());
223 }
224
225 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
226
227 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
228 DCHECK(IsValid());
229 DCHECK(rti.IsValid());
230 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
231 }
232
233 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
234 DCHECK(IsValid());
235 DCHECK(rti.IsValid());
236 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
237 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
238 }
239
240 // Returns true if the type information provide the same amount of details.
241 // Note that it does not mean that the instructions have the same actual type
242 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000243 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000244 if (!IsValid() && !rti.IsValid()) {
245 // Invalid types are equal.
246 return true;
247 }
248 if (!IsValid() || !rti.IsValid()) {
249 // One is valid, the other not.
250 return false;
251 }
252 return IsExact() == rti.IsExact()
253 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
254 }
255
256 private:
257 ReferenceTypeInfo();
258 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
259
260 // The class of the object.
261 TypeHandle type_handle_;
262 // Whether or not the type is exact or a superclass of the actual type.
263 // Whether or not we have any information about this type.
264 bool is_exact_;
265};
266
267std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
268
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000269// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100270class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100272 HGraph(ArenaAllocator* arena,
273 const DexFile& dex_file,
274 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100275 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100277 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100278 bool debuggable = false,
279 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100281 blocks_(arena->Adapter(kArenaAllocBlockList)),
282 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
283 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700284 entry_block_(nullptr),
285 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100286 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100287 number_of_vregs_(0),
288 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000289 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400290 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000291 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000292 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000293 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100294 dex_file_(dex_file),
295 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100296 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100297 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100298 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000300 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100301 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
302 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
303 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
304 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000305 cached_current_method_(nullptr),
306 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100307 blocks_.reserve(kDefaultNumberOfBlocks);
308 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000309
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000310 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100311 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
312
David Brazdil69ba7b72015-06-23 18:27:30 +0100313 bool IsInSsaForm() const { return in_ssa_form_; }
314
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000315 HBasicBlock* GetEntryBlock() const { return entry_block_; }
316 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100317 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000318
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000319 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
320 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000321
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000322 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100323
David Brazdil4833f5a2015-12-16 10:37:39 +0000324 // Try building the SSA form of this graph, with dominance computation and
325 // loop recognition. Returns a code specifying that it was successful or the
326 // reason for failure.
327 BuildSsaResult TryBuildingSsa(StackHandleScopeCollection* handles);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000328
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100329 void ComputeDominanceInformation();
330 void ClearDominanceInformation();
331
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000332 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100333 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100334 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335
David Brazdil4833f5a2015-12-16 10:37:39 +0000336 // Analyze all natural loops in this graph. Returns a code specifying that it
337 // was successful or the reason for failure. The method will fail if a loop
338 // is not natural, that is the header does not dominate a back edge, or if it
339 // is a throw-catch loop, i.e. the header is a catch block.
340 BuildSsaResult AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100341
David Brazdilffee3d32015-07-06 11:48:53 +0100342 // Iterate over blocks to compute try block membership. Needs reverse post
343 // order and loop information.
344 void ComputeTryBlockInformation();
345
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000346 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000347 // Returns the instruction used to replace the invoke expression or null if the
348 // invoke is for a void method.
349 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000350
Mingyao Yang3584bce2015-05-19 16:01:59 -0700351 // Need to add a couple of blocks to test if the loop body is entered and
352 // put deoptimization instructions, etc.
353 void TransformLoopHeaderForBCE(HBasicBlock* header);
354
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000355 // Removes `block` from the graph. Assumes `block` has been disconnected from
356 // other blocks and has no instructions or phis.
357 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000358
David Brazdilfc6a86a2015-06-26 10:33:45 +0000359 // Splits the edge between `block` and `successor` while preserving the
360 // indices in the predecessor/successor lists. If there are multiple edges
361 // between the blocks, the lowest indices are used.
362 // Returns the new block which is empty and has the same dex pc as `successor`.
363 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
364
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
366 void SimplifyLoop(HBasicBlock* header);
367
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000368 int32_t GetNextInstructionId() {
369 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000370 return current_instruction_id_++;
371 }
372
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000373 int32_t GetCurrentInstructionId() const {
374 return current_instruction_id_;
375 }
376
377 void SetCurrentInstructionId(int32_t id) {
378 current_instruction_id_ = id;
379 }
380
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100381 uint16_t GetMaximumNumberOfOutVRegs() const {
382 return maximum_number_of_out_vregs_;
383 }
384
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000385 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
386 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100387 }
388
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100389 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
390 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
391 }
392
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000393 void UpdateTemporariesVRegSlots(size_t slots) {
394 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100395 }
396
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000397 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100398 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000399 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100400 }
401
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100402 void SetNumberOfVRegs(uint16_t number_of_vregs) {
403 number_of_vregs_ = number_of_vregs;
404 }
405
406 uint16_t GetNumberOfVRegs() const {
407 return number_of_vregs_;
408 }
409
410 void SetNumberOfInVRegs(uint16_t value) {
411 number_of_in_vregs_ = value;
412 }
413
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100414 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100415 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100416 return number_of_vregs_ - number_of_in_vregs_;
417 }
418
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100419 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100421 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100422
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100423 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100424 return linear_order_;
425 }
426
Mark Mendell1152c922015-04-24 17:06:35 -0400427 bool HasBoundsChecks() const {
428 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800429 }
430
Mark Mendell1152c922015-04-24 17:06:35 -0400431 void SetHasBoundsChecks(bool value) {
432 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800433 }
434
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100435 bool ShouldGenerateConstructorBarrier() const {
436 return should_generate_constructor_barrier_;
437 }
438
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000439 bool IsDebuggable() const { return debuggable_; }
440
David Brazdil8d5b8b22015-03-24 10:51:52 +0000441 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000442 // already, it is created and inserted into the graph. This method is only for
443 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600444 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000445
446 // TODO: This is problematic for the consistency of reference type propagation
447 // because it can be created anytime after the pass and thus it will be left
448 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600449 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000450
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600451 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
452 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000453 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600454 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
455 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000456 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600457 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
458 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000459 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600460 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
461 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000462 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000463
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100464 HCurrentMethod* GetCurrentMethod();
465
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100466 const DexFile& GetDexFile() const {
467 return dex_file_;
468 }
469
470 uint32_t GetMethodIdx() const {
471 return method_idx_;
472 }
473
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100474 InvokeType GetInvokeType() const {
475 return invoke_type_;
476 }
477
Mark Mendellc4701932015-04-10 13:18:51 -0400478 InstructionSet GetInstructionSet() const {
479 return instruction_set_;
480 }
481
David Brazdil77a48ae2015-09-15 12:34:04 +0000482 bool HasTryCatch() const { return has_try_catch_; }
483 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100484
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100485 ArtMethod* GetArtMethod() const { return art_method_; }
486 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
487
Mark Mendellf6529172015-11-17 11:16:56 -0500488 // Returns an instruction with the opposite boolean value from 'cond'.
489 // The instruction has been inserted into the graph, either as a constant, or
490 // before cursor.
491 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
492
David Brazdil2d7352b2015-04-20 14:52:42 +0100493 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100494 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000495 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100496 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000498 template <class InstructionType, typename ValueType>
499 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600500 ArenaSafeMap<ValueType, InstructionType*>* cache,
501 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000502 // Try to find an existing constant of the given value.
503 InstructionType* constant = nullptr;
504 auto cached_constant = cache->find(value);
505 if (cached_constant != cache->end()) {
506 constant = cached_constant->second;
507 }
508
509 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100510 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000511 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600512 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000513 cache->Overwrite(value, constant);
514 InsertConstant(constant);
515 }
516 return constant;
517 }
518
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 void InsertConstant(HConstant* instruction);
520
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000521 // Cache a float constant into the graph. This method should only be
522 // called by the SsaBuilder when creating "equivalent" instructions.
523 void CacheFloatConstant(HFloatConstant* constant);
524
525 // See CacheFloatConstant comment.
526 void CacheDoubleConstant(HDoubleConstant* constant);
527
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000528 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000529
530 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100531 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000532
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100533 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100534 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000535
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100536 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100537 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100538
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000539 HBasicBlock* entry_block_;
540 HBasicBlock* exit_block_;
541
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100542 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100543 uint16_t maximum_number_of_out_vregs_;
544
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100545 // The number of virtual registers in this method. Contains the parameters.
546 uint16_t number_of_vregs_;
547
548 // The number of virtual registers used by parameters of this method.
549 uint16_t number_of_in_vregs_;
550
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000551 // Number of vreg size slots that the temporaries use (used in baseline compiler).
552 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100553
Mark Mendell1152c922015-04-24 17:06:35 -0400554 // Has bounds checks. We can totally skip BCE if it's false.
555 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800556
David Brazdil77a48ae2015-09-15 12:34:04 +0000557 // Flag whether there are any try/catch blocks in the graph. We will skip
558 // try/catch-related passes if false.
559 bool has_try_catch_;
560
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000561 // Indicates whether the graph should be compiled in a way that
562 // ensures full debuggability. If false, we can apply more
563 // aggressive optimizations that may limit the level of debugging.
564 const bool debuggable_;
565
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000566 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000567 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000568
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100569 // The dex file from which the method is from.
570 const DexFile& dex_file_;
571
572 // The method index in the dex file.
573 const uint32_t method_idx_;
574
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100575 // If inlined, this encodes how the callee is being invoked.
576 const InvokeType invoke_type_;
577
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100578 // Whether the graph has been transformed to SSA form. Only used
579 // in debug mode to ensure we are not using properties only valid
580 // for non-SSA form (like the number of temporaries).
581 bool in_ssa_form_;
582
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100583 const bool should_generate_constructor_barrier_;
584
Mathieu Chartiere401d142015-04-22 13:56:20 -0700585 const InstructionSet instruction_set_;
586
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000587 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000588 HNullConstant* cached_null_constant_;
589 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000590 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000591 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000592 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000593
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100594 HCurrentMethod* cached_current_method_;
595
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100596 // The ArtMethod this graph is for. Note that for AOT, it may be null,
597 // for example for methods whose declaring class could not be resolved
598 // (such as when the superclass could not be found).
599 ArtMethod* art_method_;
600
David Brazdil4833f5a2015-12-16 10:37:39 +0000601 // Keep the RTI of inexact Object to avoid having to pass stack handle
602 // collection pointer to passes which may create NullConstant.
603 ReferenceTypeInfo inexact_object_rti_;
604
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000605 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100606 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000607 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000608 DISALLOW_COPY_AND_ASSIGN(HGraph);
609};
610
Vladimir Markof9f64412015-09-02 14:05:49 +0100611class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000612 public:
613 HLoopInformation(HBasicBlock* header, HGraph* graph)
614 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100615 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100616 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100617 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100618 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
619 back_edges_.reserve(kDefaultNumberOfBackEdges);
620 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100621
622 HBasicBlock* GetHeader() const {
623 return header_;
624 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000625
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000626 void SetHeader(HBasicBlock* block) {
627 header_ = block;
628 }
629
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100630 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
631 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
632 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
633
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000634 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100635 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000636 }
637
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100638 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100639 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100640 }
641
David Brazdil46e2a392015-03-16 17:31:52 +0000642 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100643 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100644 }
645
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000646 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100647 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648 }
649
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100650 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100651
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100654 }
655
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100656 // Returns the lifetime position of the back edge that has the
657 // greatest lifetime position.
658 size_t GetLifetimeEnd() const;
659
660 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100661 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100662 }
663
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100664 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100665 // that is the header dominates the back edge.
666 bool Populate();
667
David Brazdila4b8c212015-05-07 09:59:30 +0100668 // Reanalyzes the loop by removing loop info from its blocks and re-running
669 // Populate(). If there are no back edges left, the loop info is completely
670 // removed as well as its SuspendCheck instruction. It must be run on nested
671 // inner loops first.
672 void Update();
673
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674 // Returns whether this loop information contains `block`.
675 // Note that this loop information *must* be populated before entering this function.
676 bool Contains(const HBasicBlock& block) const;
677
678 // Returns whether this loop information is an inner loop of `other`.
679 // Note that `other` *must* be populated before entering this function.
680 bool IsIn(const HLoopInformation& other) const;
681
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800682 // Returns true if instruction is not defined within this loop.
683 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700684
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 const ArenaBitVector& GetBlocks() const { return blocks_; }
686
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000687 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000688 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000690 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100691 // Internal recursive implementation of `Populate`.
692 void PopulateRecursive(HBasicBlock* block);
693
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100695 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100696 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000698
699 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
700};
701
David Brazdilec16f792015-08-19 15:04:01 +0100702// Stores try/catch information for basic blocks.
703// Note that HGraph is constructed so that catch blocks cannot simultaneously
704// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100705class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100706 public:
707 // Try block information constructor.
708 explicit TryCatchInformation(const HTryBoundary& try_entry)
709 : try_entry_(&try_entry),
710 catch_dex_file_(nullptr),
711 catch_type_index_(DexFile::kDexNoIndex16) {
712 DCHECK(try_entry_ != nullptr);
713 }
714
715 // Catch block information constructor.
716 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
717 : try_entry_(nullptr),
718 catch_dex_file_(&dex_file),
719 catch_type_index_(catch_type_index) {}
720
721 bool IsTryBlock() const { return try_entry_ != nullptr; }
722
723 const HTryBoundary& GetTryEntry() const {
724 DCHECK(IsTryBlock());
725 return *try_entry_;
726 }
727
728 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
729
730 bool IsCatchAllTypeIndex() const {
731 DCHECK(IsCatchBlock());
732 return catch_type_index_ == DexFile::kDexNoIndex16;
733 }
734
735 uint16_t GetCatchTypeIndex() const {
736 DCHECK(IsCatchBlock());
737 return catch_type_index_;
738 }
739
740 const DexFile& GetCatchDexFile() const {
741 DCHECK(IsCatchBlock());
742 return *catch_dex_file_;
743 }
744
745 private:
746 // One of possibly several TryBoundary instructions entering the block's try.
747 // Only set for try blocks.
748 const HTryBoundary* try_entry_;
749
750 // Exception type information. Only set for catch blocks.
751 const DexFile* catch_dex_file_;
752 const uint16_t catch_type_index_;
753};
754
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100755static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100756static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100757
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000758// A block in a method. Contains the list of instructions represented
759// as a double linked list. Each block knows its predecessors and
760// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100761
Vladimir Markof9f64412015-09-02 14:05:49 +0100762class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600764 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000765 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000766 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
767 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000768 loop_information_(nullptr),
769 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000770 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100771 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100772 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100773 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000774 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000775 try_catch_information_(nullptr) {
776 predecessors_.reserve(kDefaultNumberOfPredecessors);
777 successors_.reserve(kDefaultNumberOfSuccessors);
778 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
779 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780
Vladimir Marko60584552015-09-03 13:35:12 +0000781 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100782 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000783 }
784
Vladimir Marko60584552015-09-03 13:35:12 +0000785 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787 }
788
David Brazdild26a4112015-11-10 11:07:31 +0000789 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
790 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
791
Vladimir Marko60584552015-09-03 13:35:12 +0000792 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
793 return ContainsElement(successors_, block, start_from);
794 }
795
796 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100797 return dominated_blocks_;
798 }
799
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100800 bool IsEntryBlock() const {
801 return graph_->GetEntryBlock() == this;
802 }
803
804 bool IsExitBlock() const {
805 return graph_->GetExitBlock() == this;
806 }
807
David Brazdil46e2a392015-03-16 17:31:52 +0000808 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000809 bool IsSingleTryBoundary() const;
810
811 // Returns true if this block emits nothing but a jump.
812 bool IsSingleJump() const {
813 HLoopInformation* loop_info = GetLoopInformation();
814 return (IsSingleGoto() || IsSingleTryBoundary())
815 // Back edges generate a suspend check.
816 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
817 }
David Brazdil46e2a392015-03-16 17:31:52 +0000818
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000819 void AddBackEdge(HBasicBlock* back_edge) {
820 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000822 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100823 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000824 loop_information_->AddBackEdge(back_edge);
825 }
826
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000827 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000828 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100830 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600832 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000834 HBasicBlock* GetDominator() const { return dominator_; }
835 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000836 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
837
838 void RemoveDominatedBlock(HBasicBlock* block) {
839 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100840 }
Vladimir Marko60584552015-09-03 13:35:12 +0000841
842 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
843 ReplaceElement(dominated_blocks_, existing, new_block);
844 }
845
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100846 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000847
848 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100849 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000850 }
851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
853 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100854 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100855 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100856 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
857 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000858
859 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000860 successors_.push_back(block);
861 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862 }
863
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100864 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
865 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100866 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000867 new_block->predecessors_.push_back(this);
868 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000869 }
870
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000871 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
872 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000873 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000874 new_block->successors_.push_back(this);
875 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000876 }
877
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100878 // Insert `this` between `predecessor` and `successor. This method
879 // preserves the indicies, and will update the first edge found between
880 // `predecessor` and `successor`.
881 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
882 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100883 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000884 successor->predecessors_[predecessor_index] = this;
885 predecessor->successors_[successor_index] = this;
886 successors_.push_back(successor);
887 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100888 }
889
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100890 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000891 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 }
893
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000894 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000895 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000896 }
897
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000899 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100900 }
901
902 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000903 predecessors_.push_back(block);
904 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100905 }
906
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100907 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000908 DCHECK_EQ(predecessors_.size(), 2u);
909 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100910 }
911
David Brazdil769c9e52015-04-27 13:54:09 +0100912 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000913 DCHECK_EQ(successors_.size(), 2u);
914 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100915 }
916
David Brazdilfc6a86a2015-06-26 10:33:45 +0000917 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000918 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100919 }
920
David Brazdilfc6a86a2015-06-26 10:33:45 +0000921 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000922 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100923 }
924
David Brazdilfc6a86a2015-06-26 10:33:45 +0000925 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000926 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100927 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000928 }
929
930 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000931 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100932 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000933 }
934
935 // Returns whether the first occurrence of `predecessor` in the list of
936 // predecessors is at index `idx`.
937 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100938 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000939 return GetPredecessorIndexOf(predecessor) == idx;
940 }
941
David Brazdild7558da2015-09-22 13:04:14 +0100942 // Create a new block between this block and its predecessors. The new block
943 // is added to the graph, all predecessor edges are relinked to it and an edge
944 // is created to `this`. Returns the new empty block. Reverse post order or
945 // loop and try/catch information are not updated.
946 HBasicBlock* CreateImmediateDominator();
947
David Brazdilfc6a86a2015-06-26 10:33:45 +0000948 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100949 // created, latter block. Note that this method will add the block to the
950 // graph, create a Goto at the end of the former block and will create an edge
951 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100952 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000953 HBasicBlock* SplitBefore(HInstruction* cursor);
954
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000955 // Split the block into two blocks just after `cursor`. Returns the newly
956 // created block. Note that this method just updates raw block information,
957 // like predecessors, successors, dominators, and instruction list. It does not
958 // update the graph, reverse post order, loop information, nor make sure the
959 // blocks are consistent (for example ending with a control flow instruction).
960 HBasicBlock* SplitAfter(HInstruction* cursor);
961
David Brazdil9bc43612015-11-05 21:25:24 +0000962 // Split catch block into two blocks after the original move-exception bytecode
963 // instruction, or at the beginning if not present. Returns the newly created,
964 // latter block, or nullptr if such block could not be created (must be dead
965 // in that case). Note that this method just updates raw block information,
966 // like predecessors, successors, dominators, and instruction list. It does not
967 // update the graph, reverse post order, loop information, nor make sure the
968 // blocks are consistent (for example ending with a control flow instruction).
969 HBasicBlock* SplitCatchBlockAfterMoveException();
970
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000971 // Merge `other` at the end of `this`. Successors and dominated blocks of
972 // `other` are changed to be successors and dominated blocks of `this`. Note
973 // that this method does not update the graph, reverse post order, loop
974 // information, nor make sure the blocks are consistent (for example ending
975 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100976 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000977
978 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
979 // of `this` are moved to `other`.
980 // Note that this method does not update the graph, reverse post order, loop
981 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000982 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000983 void ReplaceWith(HBasicBlock* other);
984
David Brazdil2d7352b2015-04-20 14:52:42 +0100985 // Merge `other` at the end of `this`. This method updates loops, reverse post
986 // order, links to predecessors, successors, dominators and deletes the block
987 // from the graph. The two blocks must be successive, i.e. `this` the only
988 // predecessor of `other` and vice versa.
989 void MergeWith(HBasicBlock* other);
990
991 // Disconnects `this` from all its predecessors, successors and dominator,
992 // removes it from all loops it is included in and eventually from the graph.
993 // The block must not dominate any other block. Predecessors and successors
994 // are safely updated.
995 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000996
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000997 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100998 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100999 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001000 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001001 // Replace instruction `initial` with `replacement` within this block.
1002 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1003 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001004 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001005 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001006 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1007 // instruction list. With 'ensure_safety' set to true, it verifies that the
1008 // instruction is not in use and removes it from the use lists of its inputs.
1009 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1010 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001011 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001012
1013 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001014 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001015 }
1016
Roland Levillain6b879dd2014-09-22 17:13:44 +01001017 bool IsLoopPreHeaderFirstPredecessor() const {
1018 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001019 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001020 }
1021
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001022 HLoopInformation* GetLoopInformation() const {
1023 return loop_information_;
1024 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001025
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001026 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001027 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001028 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001029 void SetInLoop(HLoopInformation* info) {
1030 if (IsLoopHeader()) {
1031 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001032 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001033 loop_information_ = info;
1034 } else if (loop_information_->Contains(*info->GetHeader())) {
1035 // Block is currently part of an outer loop. Make it part of this inner loop.
1036 // Note that a non loop header having a loop information means this loop information
1037 // has already been populated
1038 loop_information_ = info;
1039 } else {
1040 // Block is part of an inner loop. Do not update the loop information.
1041 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1042 // at this point, because this method is being called while populating `info`.
1043 }
1044 }
1045
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001046 // Raw update of the loop information.
1047 void SetLoopInformation(HLoopInformation* info) {
1048 loop_information_ = info;
1049 }
1050
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001051 bool IsInLoop() const { return loop_information_ != nullptr; }
1052
David Brazdilec16f792015-08-19 15:04:01 +01001053 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1054
1055 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1056 try_catch_information_ = try_catch_information;
1057 }
1058
1059 bool IsTryBlock() const {
1060 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1061 }
1062
1063 bool IsCatchBlock() const {
1064 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1065 }
David Brazdilffee3d32015-07-06 11:48:53 +01001066
1067 // Returns the try entry that this block's successors should have. They will
1068 // be in the same try, unless the block ends in a try boundary. In that case,
1069 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001070 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001071
David Brazdild7558da2015-09-22 13:04:14 +01001072 bool HasThrowingInstructions() const;
1073
David Brazdila4b8c212015-05-07 09:59:30 +01001074 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001075 bool Dominates(HBasicBlock* block) const;
1076
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001077 size_t GetLifetimeStart() const { return lifetime_start_; }
1078 size_t GetLifetimeEnd() const { return lifetime_end_; }
1079
1080 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1081 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1082
David Brazdil8d5b8b22015-03-24 10:51:52 +00001083 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001084 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001085 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001086 bool HasSinglePhi() const;
1087
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001088 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001089 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001090 ArenaVector<HBasicBlock*> predecessors_;
1091 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001092 HInstructionList instructions_;
1093 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001094 HLoopInformation* loop_information_;
1095 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001096 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001097 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001098 // The dex program counter of the first instruction of this block.
1099 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001100 size_t lifetime_start_;
1101 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001102 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001103
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001104 friend class HGraph;
1105 friend class HInstruction;
1106
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001107 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1108};
1109
David Brazdilb2bd1c52015-03-25 11:17:37 +00001110// Iterates over the LoopInformation of all loops which contain 'block'
1111// from the innermost to the outermost.
1112class HLoopInformationOutwardIterator : public ValueObject {
1113 public:
1114 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1115 : current_(block.GetLoopInformation()) {}
1116
1117 bool Done() const { return current_ == nullptr; }
1118
1119 void Advance() {
1120 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001121 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001122 }
1123
1124 HLoopInformation* Current() const {
1125 DCHECK(!Done());
1126 return current_;
1127 }
1128
1129 private:
1130 HLoopInformation* current_;
1131
1132 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1133};
1134
Alexandre Ramesef20f712015-06-09 10:29:30 +01001135#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001136 M(Above, Condition) \
1137 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001138 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001139 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001140 M(ArrayGet, Instruction) \
1141 M(ArrayLength, Instruction) \
1142 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001143 M(Below, Condition) \
1144 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001145 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001146 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001147 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001148 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001149 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001150 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001151 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001152 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001153 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001154 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001155 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001156 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001157 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001158 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001159 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001160 M(FloatConstant, Constant) \
1161 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001162 M(GreaterThan, Condition) \
1163 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001164 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001165 M(InstanceFieldGet, Instruction) \
1166 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001167 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001168 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001169 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001170 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001171 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001172 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001173 M(LessThan, Condition) \
1174 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001175 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001176 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001177 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001178 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001179 M(Local, Instruction) \
1180 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001181 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001182 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001183 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001184 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001185 M(Neg, UnaryOperation) \
1186 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001187 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001188 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001189 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001190 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001191 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001192 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001193 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001194 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001195 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001196 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001197 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001198 M(Return, Instruction) \
1199 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001200 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001201 M(Shl, BinaryOperation) \
1202 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001203 M(StaticFieldGet, Instruction) \
1204 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001205 M(UnresolvedInstanceFieldGet, Instruction) \
1206 M(UnresolvedInstanceFieldSet, Instruction) \
1207 M(UnresolvedStaticFieldGet, Instruction) \
1208 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 M(StoreLocal, Instruction) \
1210 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001211 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001212 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001213 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001214 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001215 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001216 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001217 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001218
Vladimir Markob4536b72015-11-24 13:45:23 +00001219#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001220#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001221#else
1222#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1223 M(ArmDexCacheArraysBase, Instruction)
1224#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001225
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001226#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001227#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001228#else
1229#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001230 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001231 M(Arm64IntermediateAddress, Instruction) \
1232 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001233#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001234
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001235#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1236
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001237#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1238
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001239#ifndef ART_ENABLE_CODEGEN_x86
1240#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1241#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001242#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1243 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001244 M(X86LoadFromConstantTable, Instruction) \
1245 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001246#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001247
1248#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1249
1250#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1251 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1252 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1253 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001254 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001255 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001256 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1257 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1258
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001259#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1260 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001262 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001263 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001264 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001265
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001266#define FOR_EACH_INSTRUCTION(M) \
1267 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1268 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1269
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001270#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001271FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1272#undef FORWARD_DECLARATION
1273
Roland Levillainccc07a92014-09-16 14:48:16 +01001274#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001275 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001276 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001277 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001278 return other->Is##type(); \
1279 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001280 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001281
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001282#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1283 bool Is##type() const { return As##type() != nullptr; } \
1284 const H##type* As##type() const { return this; } \
1285 H##type* As##type() { return this; }
1286
David Brazdiled596192015-01-23 10:39:45 +00001287template <typename T> class HUseList;
1288
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001289template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001290class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001291 public:
David Brazdiled596192015-01-23 10:39:45 +00001292 HUseListNode* GetPrevious() const { return prev_; }
1293 HUseListNode* GetNext() const { return next_; }
1294 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001295 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001296 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001297
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001298 private:
David Brazdiled596192015-01-23 10:39:45 +00001299 HUseListNode(T user, size_t index)
1300 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1301
1302 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001303 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001304 HUseListNode<T>* prev_;
1305 HUseListNode<T>* next_;
1306
1307 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001308
1309 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1310};
1311
David Brazdiled596192015-01-23 10:39:45 +00001312template <typename T>
1313class HUseList : public ValueObject {
1314 public:
1315 HUseList() : first_(nullptr) {}
1316
1317 void Clear() {
1318 first_ = nullptr;
1319 }
1320
1321 // Adds a new entry at the beginning of the use list and returns
1322 // the newly created node.
1323 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001324 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001325 if (IsEmpty()) {
1326 first_ = new_node;
1327 } else {
1328 first_->prev_ = new_node;
1329 new_node->next_ = first_;
1330 first_ = new_node;
1331 }
1332 return new_node;
1333 }
1334
1335 HUseListNode<T>* GetFirst() const {
1336 return first_;
1337 }
1338
1339 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001340 DCHECK(node != nullptr);
1341 DCHECK(Contains(node));
1342
David Brazdiled596192015-01-23 10:39:45 +00001343 if (node->prev_ != nullptr) {
1344 node->prev_->next_ = node->next_;
1345 }
1346 if (node->next_ != nullptr) {
1347 node->next_->prev_ = node->prev_;
1348 }
1349 if (node == first_) {
1350 first_ = node->next_;
1351 }
1352 }
1353
David Brazdil1abb4192015-02-17 18:33:36 +00001354 bool Contains(const HUseListNode<T>* node) const {
1355 if (node == nullptr) {
1356 return false;
1357 }
1358 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1359 if (current == node) {
1360 return true;
1361 }
1362 }
1363 return false;
1364 }
1365
David Brazdiled596192015-01-23 10:39:45 +00001366 bool IsEmpty() const {
1367 return first_ == nullptr;
1368 }
1369
1370 bool HasOnlyOneUse() const {
1371 return first_ != nullptr && first_->next_ == nullptr;
1372 }
1373
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001374 size_t SizeSlow() const {
1375 size_t count = 0;
1376 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1377 ++count;
1378 }
1379 return count;
1380 }
1381
David Brazdiled596192015-01-23 10:39:45 +00001382 private:
1383 HUseListNode<T>* first_;
1384};
1385
1386template<typename T>
1387class HUseIterator : public ValueObject {
1388 public:
1389 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1390
1391 bool Done() const { return current_ == nullptr; }
1392
1393 void Advance() {
1394 DCHECK(!Done());
1395 current_ = current_->GetNext();
1396 }
1397
1398 HUseListNode<T>* Current() const {
1399 DCHECK(!Done());
1400 return current_;
1401 }
1402
1403 private:
1404 HUseListNode<T>* current_;
1405
1406 friend class HValue;
1407};
1408
David Brazdil1abb4192015-02-17 18:33:36 +00001409// This class is used by HEnvironment and HInstruction classes to record the
1410// instructions they use and pointers to the corresponding HUseListNodes kept
1411// by the used instructions.
1412template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001413class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001414 public:
1415 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1416 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1417
1418 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1419 : instruction_(old_record.instruction_), use_node_(use_node) {
1420 DCHECK(instruction_ != nullptr);
1421 DCHECK(use_node_ != nullptr);
1422 DCHECK(old_record.use_node_ == nullptr);
1423 }
1424
1425 HInstruction* GetInstruction() const { return instruction_; }
1426 HUseListNode<T>* GetUseNode() const { return use_node_; }
1427
1428 private:
1429 // Instruction used by the user.
1430 HInstruction* instruction_;
1431
1432 // Corresponding entry in the use list kept by 'instruction_'.
1433 HUseListNode<T>* use_node_;
1434};
1435
Aart Bik854a02b2015-07-14 16:07:00 -07001436/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001437 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001438 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001439 * For write/read dependences on fields/arrays, the dependence analysis uses
1440 * type disambiguation (e.g. a float field write cannot modify the value of an
1441 * integer field read) and the access type (e.g. a reference array write cannot
1442 * modify the value of a reference field read [although it may modify the
1443 * reference fetch prior to reading the field, which is represented by its own
1444 * write/read dependence]). The analysis makes conservative points-to
1445 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1446 * the same, and any reference read depends on any reference read without
1447 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001448 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001449 * The internal representation uses 38-bit and is described in the table below.
1450 * The first line indicates the side effect, and for field/array accesses the
1451 * second line indicates the type of the access (in the order of the
1452 * Primitive::Type enum).
1453 * The two numbered lines below indicate the bit position in the bitfield (read
1454 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001455 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001456 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1457 * +-------------+---------+---------+--------------+---------+---------+
1458 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1459 * | 3 |333333322|222222221| 1 |111111110|000000000|
1460 * | 7 |654321098|765432109| 8 |765432109|876543210|
1461 *
1462 * Note that, to ease the implementation, 'changes' bits are least significant
1463 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001464 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001465class SideEffects : public ValueObject {
1466 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001467 SideEffects() : flags_(0) {}
1468
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001469 static SideEffects None() {
1470 return SideEffects(0);
1471 }
1472
1473 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001474 return SideEffects(kAllChangeBits | kAllDependOnBits);
1475 }
1476
1477 static SideEffects AllChanges() {
1478 return SideEffects(kAllChangeBits);
1479 }
1480
1481 static SideEffects AllDependencies() {
1482 return SideEffects(kAllDependOnBits);
1483 }
1484
1485 static SideEffects AllExceptGCDependency() {
1486 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1487 }
1488
1489 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001490 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491 }
1492
Aart Bik34c3ba92015-07-20 14:08:59 -07001493 static SideEffects AllWrites() {
1494 return SideEffects(kAllWrites);
1495 }
1496
1497 static SideEffects AllReads() {
1498 return SideEffects(kAllReads);
1499 }
1500
1501 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1502 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001503 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001504 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505 }
1506
Aart Bik854a02b2015-07-14 16:07:00 -07001507 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1508 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001509 }
1510
Aart Bik34c3ba92015-07-20 14:08:59 -07001511 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1512 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001513 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001514 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001515 }
1516
1517 static SideEffects ArrayReadOfType(Primitive::Type type) {
1518 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1519 }
1520
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001521 static SideEffects CanTriggerGC() {
1522 return SideEffects(1ULL << kCanTriggerGCBit);
1523 }
1524
1525 static SideEffects DependsOnGC() {
1526 return SideEffects(1ULL << kDependsOnGCBit);
1527 }
1528
Aart Bik854a02b2015-07-14 16:07:00 -07001529 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001530 SideEffects Union(SideEffects other) const {
1531 return SideEffects(flags_ | other.flags_);
1532 }
1533
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001534 SideEffects Exclusion(SideEffects other) const {
1535 return SideEffects(flags_ & ~other.flags_);
1536 }
1537
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001538 void Add(SideEffects other) {
1539 flags_ |= other.flags_;
1540 }
1541
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001542 bool Includes(SideEffects other) const {
1543 return (other.flags_ & flags_) == other.flags_;
1544 }
1545
1546 bool HasSideEffects() const {
1547 return (flags_ & kAllChangeBits);
1548 }
1549
1550 bool HasDependencies() const {
1551 return (flags_ & kAllDependOnBits);
1552 }
1553
1554 // Returns true if there are no side effects or dependencies.
1555 bool DoesNothing() const {
1556 return flags_ == 0;
1557 }
1558
Aart Bik854a02b2015-07-14 16:07:00 -07001559 // Returns true if something is written.
1560 bool DoesAnyWrite() const {
1561 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001562 }
1563
Aart Bik854a02b2015-07-14 16:07:00 -07001564 // Returns true if something is read.
1565 bool DoesAnyRead() const {
1566 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001567 }
1568
Aart Bik854a02b2015-07-14 16:07:00 -07001569 // Returns true if potentially everything is written and read
1570 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 bool DoesAllReadWrite() const {
1572 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1573 }
1574
Aart Bik854a02b2015-07-14 16:07:00 -07001575 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001576 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001577 }
1578
Roland Levillain0d5a2812015-11-13 10:07:31 +00001579 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001580 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001581 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1582 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001583 }
1584
1585 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001587 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001588 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001589 for (int s = kLastBit; s >= 0; s--) {
1590 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1591 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1592 // This is a bit for the GC side effect.
1593 if (current_bit_is_set) {
1594 flags += "GC";
1595 }
Aart Bik854a02b2015-07-14 16:07:00 -07001596 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001597 } else {
1598 // This is a bit for the array/field analysis.
1599 // The underscore character stands for the 'can trigger GC' bit.
1600 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1601 if (current_bit_is_set) {
1602 flags += kDebug[s];
1603 }
1604 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1605 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1606 flags += "|";
1607 }
1608 }
Aart Bik854a02b2015-07-14 16:07:00 -07001609 }
1610 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001611 }
1612
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001614
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 private:
1616 static constexpr int kFieldArrayAnalysisBits = 9;
1617
1618 static constexpr int kFieldWriteOffset = 0;
1619 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1620 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1621 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1622
1623 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1624
1625 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1626 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1627 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1628 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1629
1630 static constexpr int kLastBit = kDependsOnGCBit;
1631 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1632
1633 // Aliases.
1634
1635 static_assert(kChangeBits == kDependOnBits,
1636 "the 'change' bits should match the 'depend on' bits.");
1637
1638 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1639 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1640 static constexpr uint64_t kAllWrites =
1641 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1642 static constexpr uint64_t kAllReads =
1643 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001644
Aart Bik854a02b2015-07-14 16:07:00 -07001645 // Work around the fact that HIR aliases I/F and J/D.
1646 // TODO: remove this interceptor once HIR types are clean
1647 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1648 switch (type) {
1649 case Primitive::kPrimInt:
1650 case Primitive::kPrimFloat:
1651 return TypeFlag(Primitive::kPrimInt, offset) |
1652 TypeFlag(Primitive::kPrimFloat, offset);
1653 case Primitive::kPrimLong:
1654 case Primitive::kPrimDouble:
1655 return TypeFlag(Primitive::kPrimLong, offset) |
1656 TypeFlag(Primitive::kPrimDouble, offset);
1657 default:
1658 return TypeFlag(type, offset);
1659 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001660 }
1661
Aart Bik854a02b2015-07-14 16:07:00 -07001662 // Translates type to bit flag.
1663 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1664 CHECK_NE(type, Primitive::kPrimVoid);
1665 const uint64_t one = 1;
1666 const int shift = type; // 0-based consecutive enum
1667 DCHECK_LE(kFieldWriteOffset, shift);
1668 DCHECK_LT(shift, kArrayWriteOffset);
1669 return one << (type + offset);
1670 }
1671
1672 // Private constructor on direct flags value.
1673 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1674
1675 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001676};
1677
David Brazdiled596192015-01-23 10:39:45 +00001678// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001679class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001680 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001681 HEnvironment(ArenaAllocator* arena,
1682 size_t number_of_vregs,
1683 const DexFile& dex_file,
1684 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001685 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001686 InvokeType invoke_type,
1687 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001688 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1689 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001690 parent_(nullptr),
1691 dex_file_(dex_file),
1692 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001693 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001694 invoke_type_(invoke_type),
1695 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001696 }
1697
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001698 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001699 : HEnvironment(arena,
1700 to_copy.Size(),
1701 to_copy.GetDexFile(),
1702 to_copy.GetMethodIdx(),
1703 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001704 to_copy.GetInvokeType(),
1705 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001706
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001707 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001708 if (parent_ != nullptr) {
1709 parent_->SetAndCopyParentChain(allocator, parent);
1710 } else {
1711 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1712 parent_->CopyFrom(parent);
1713 if (parent->GetParent() != nullptr) {
1714 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1715 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001716 }
David Brazdiled596192015-01-23 10:39:45 +00001717 }
1718
Vladimir Marko71bf8092015-09-15 15:33:14 +01001719 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001720 void CopyFrom(HEnvironment* environment);
1721
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001722 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1723 // input to the loop phi instead. This is for inserting instructions that
1724 // require an environment (like HDeoptimization) in the loop pre-header.
1725 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001726
1727 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001728 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001729 }
1730
1731 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001732 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001733 }
1734
David Brazdil1abb4192015-02-17 18:33:36 +00001735 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001736
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001737 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001738
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001739 HEnvironment* GetParent() const { return parent_; }
1740
1741 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001742 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001743 }
1744
1745 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001746 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001747 }
1748
1749 uint32_t GetDexPc() const {
1750 return dex_pc_;
1751 }
1752
1753 uint32_t GetMethodIdx() const {
1754 return method_idx_;
1755 }
1756
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001757 InvokeType GetInvokeType() const {
1758 return invoke_type_;
1759 }
1760
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001761 const DexFile& GetDexFile() const {
1762 return dex_file_;
1763 }
1764
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001765 HInstruction* GetHolder() const {
1766 return holder_;
1767 }
1768
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001769
1770 bool IsFromInlinedInvoke() const {
1771 return GetParent() != nullptr;
1772 }
1773
David Brazdiled596192015-01-23 10:39:45 +00001774 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001775 // Record instructions' use entries of this environment for constant-time removal.
1776 // It should only be called by HInstruction when a new environment use is added.
1777 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1778 DCHECK(env_use->GetUser() == this);
1779 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001780 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001781 }
David Brazdiled596192015-01-23 10:39:45 +00001782
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001783 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1784 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001785 HEnvironment* parent_;
1786 const DexFile& dex_file_;
1787 const uint32_t method_idx_;
1788 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001789 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001790
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001791 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001792 HInstruction* const holder_;
1793
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001794 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001795
1796 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1797};
1798
Vladimir Markof9f64412015-09-02 14:05:49 +01001799class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001800 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001801 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001802 : previous_(nullptr),
1803 next_(nullptr),
1804 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001805 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001806 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001807 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001808 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001809 locations_(nullptr),
1810 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001811 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001812 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001813 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001814
Dave Allison20dfc792014-06-16 20:44:29 -07001815 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001816
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001817#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001818 enum InstructionKind {
1819 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1820 };
1821#undef DECLARE_KIND
1822
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001823 HInstruction* GetNext() const { return next_; }
1824 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001825
Calin Juravle77520bc2015-01-12 18:45:46 +00001826 HInstruction* GetNextDisregardingMoves() const;
1827 HInstruction* GetPreviousDisregardingMoves() const;
1828
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001829 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001830 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001831 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001832 bool IsInBlock() const { return block_ != nullptr; }
1833 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001834 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001835
Roland Levillain6b879dd2014-09-22 17:13:44 +01001836 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001837 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001838
1839 virtual void Accept(HGraphVisitor* visitor) = 0;
1840 virtual const char* DebugName() const = 0;
1841
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001842 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001843 void SetRawInputAt(size_t index, HInstruction* input) {
1844 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1845 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001846
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001847 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001848
1849 uint32_t GetDexPc() const { return dex_pc_; }
1850
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001851 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001852
Roland Levillaine161a2a2014-10-03 12:45:18 +01001853 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001854 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001855
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001856 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001857 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858
Calin Juravle10e244f2015-01-26 18:54:32 +00001859 // Does not apply for all instructions, but having this at top level greatly
1860 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001861 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001862 virtual bool CanBeNull() const {
1863 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1864 return true;
1865 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001866
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001867 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001868 return false;
1869 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001870
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001871 virtual bool IsActualObject() const {
1872 return GetType() == Primitive::kPrimNot;
1873 }
1874
Calin Juravle2e768302015-07-28 14:41:11 +00001875 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001876
Calin Juravle61d544b2015-02-23 16:46:57 +00001877 ReferenceTypeInfo GetReferenceTypeInfo() const {
1878 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1879 return reference_type_info_;
1880 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001881
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001882 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001883 DCHECK(user != nullptr);
1884 HUseListNode<HInstruction*>* use =
1885 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1886 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001887 }
1888
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001890 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001891 HUseListNode<HEnvironment*>* env_use =
1892 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1893 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001894 }
1895
David Brazdil1abb4192015-02-17 18:33:36 +00001896 void RemoveAsUserOfInput(size_t input) {
1897 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1898 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1899 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001900
David Brazdil1abb4192015-02-17 18:33:36 +00001901 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1902 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001903
David Brazdiled596192015-01-23 10:39:45 +00001904 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1905 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001906 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001907 bool HasOnlyOneNonEnvironmentUse() const {
1908 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1909 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001910
Roland Levillain6c82d402014-10-13 16:10:27 +01001911 // Does this instruction strictly dominate `other_instruction`?
1912 // Returns false if this instruction and `other_instruction` are the same.
1913 // Aborts if this instruction and `other_instruction` are both phis.
1914 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001915
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001916 int GetId() const { return id_; }
1917 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001918
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001919 int GetSsaIndex() const { return ssa_index_; }
1920 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1921 bool HasSsaIndex() const { return ssa_index_ != -1; }
1922
1923 bool HasEnvironment() const { return environment_ != nullptr; }
1924 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001925 // Set the `environment_` field. Raw because this method does not
1926 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001927 void SetRawEnvironment(HEnvironment* environment) {
1928 DCHECK(environment_ == nullptr);
1929 DCHECK_EQ(environment->GetHolder(), this);
1930 environment_ = environment;
1931 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001932
1933 // Set the environment of this instruction, copying it from `environment`. While
1934 // copying, the uses lists are being updated.
1935 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001936 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001937 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001938 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001939 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001940 if (environment->GetParent() != nullptr) {
1941 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1942 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001943 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001944
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001945 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1946 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001947 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001948 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001949 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001950 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001951 if (environment->GetParent() != nullptr) {
1952 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1953 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001954 }
1955
Nicolas Geoffray39468442014-09-02 15:17:15 +01001956 // Returns the number of entries in the environment. Typically, that is the
1957 // number of dex registers in a method. It could be more in case of inlining.
1958 size_t EnvironmentSize() const;
1959
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001960 LocationSummary* GetLocations() const { return locations_; }
1961 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001962
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001963 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001964 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965
Alexandre Rames188d4312015-04-09 18:30:21 +01001966 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1967 // uses of this instruction by `other` are *not* updated.
1968 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1969 ReplaceWith(other);
1970 other->ReplaceInput(this, use_index);
1971 }
1972
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001973 // Move `this` instruction before `cursor`.
1974 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001975
Vladimir Markofb337ea2015-11-25 15:25:10 +00001976 // Move `this` before its first user and out of any loops. If there is no
1977 // out-of-loop user that dominates all other users, move the instruction
1978 // to the end of the out-of-loop common dominator of the user's blocks.
1979 //
1980 // This can be used only on non-throwing instructions with no side effects that
1981 // have at least one use but no environment uses.
1982 void MoveBeforeFirstUserAndOutOfLoops();
1983
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001984#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001985 bool Is##type() const; \
1986 const H##type* As##type() const; \
1987 H##type* As##type();
1988
1989 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1990#undef INSTRUCTION_TYPE_CHECK
1991
1992#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001993 bool Is##type() const { return (As##type() != nullptr); } \
1994 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001995 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001997#undef INSTRUCTION_TYPE_CHECK
1998
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001999 // Returns whether the instruction can be moved within the graph.
2000 virtual bool CanBeMoved() const { return false; }
2001
2002 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002003 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002004 return false;
2005 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002006
2007 // Returns whether any data encoded in the two instructions is equal.
2008 // This method does not look at the inputs. Both instructions must be
2009 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002010 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002011 return false;
2012 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002013
2014 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002015 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002016 // 2) Their inputs are identical.
2017 bool Equals(HInstruction* other) const;
2018
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2020 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2021 // the virtual function because the __attribute__((__pure__)) doesn't really
2022 // apply the strong requirement for virtual functions, preventing optimizations.
2023 InstructionKind GetKind() const PURE;
2024 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002025
2026 virtual size_t ComputeHashCode() const {
2027 size_t result = GetKind();
2028 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2029 result = (result * 31) + InputAt(i)->GetId();
2030 }
2031 return result;
2032 }
2033
2034 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002035 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002036
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002037 size_t GetLifetimePosition() const { return lifetime_position_; }
2038 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2039 LiveInterval* GetLiveInterval() const { return live_interval_; }
2040 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2041 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2042
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002043 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2044
2045 // Returns whether the code generation of the instruction will require to have access
2046 // to the current method. Such instructions are:
2047 // (1): Instructions that require an environment, as calling the runtime requires
2048 // to walk the stack and have the current method stored at a specific stack address.
2049 // (2): Object literals like classes and strings, that are loaded from the dex cache
2050 // fields of the current method.
2051 bool NeedsCurrentMethod() const {
2052 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2053 }
2054
Vladimir Markodc151b22015-10-15 18:02:30 +01002055 // Returns whether the code generation of the instruction will require to have access
2056 // to the dex cache of the current method's declaring class via the current method.
2057 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002058
Mark Mendellc4701932015-04-10 13:18:51 -04002059 // Does this instruction have any use in an environment before
2060 // control flow hits 'other'?
2061 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2062
2063 // Remove all references to environment uses of this instruction.
2064 // The caller must ensure that this is safe to do.
2065 void RemoveEnvironmentUsers();
2066
David Brazdil1abb4192015-02-17 18:33:36 +00002067 protected:
2068 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2069 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002070 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002071
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002072 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002073 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2074
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002075 HInstruction* previous_;
2076 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002077 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002078 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002079
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002080 // An instruction gets an id when it is added to the graph.
2081 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002082 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002083 int id_;
2084
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002085 // When doing liveness analysis, instructions that have uses get an SSA index.
2086 int ssa_index_;
2087
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002088 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002089 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002090
2091 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002092 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002093
Nicolas Geoffray39468442014-09-02 15:17:15 +01002094 // The environment associated with this instruction. Not null if the instruction
2095 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002096 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002097
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002098 // Set by the code generator.
2099 LocationSummary* locations_;
2100
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002101 // Set by the liveness analysis.
2102 LiveInterval* live_interval_;
2103
2104 // Set by the liveness analysis, this is the position in a linear
2105 // order of blocks where this instruction's live interval start.
2106 size_t lifetime_position_;
2107
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002108 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002109
Calin Juravleacf735c2015-02-12 15:25:22 +00002110 // TODO: for primitive types this should be marked as invalid.
2111 ReferenceTypeInfo reference_type_info_;
2112
David Brazdil1abb4192015-02-17 18:33:36 +00002113 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002115 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002116 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002117 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002118
2119 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2120};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002121std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002123class HInputIterator : public ValueObject {
2124 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002125 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002126
2127 bool Done() const { return index_ == instruction_->InputCount(); }
2128 HInstruction* Current() const { return instruction_->InputAt(index_); }
2129 void Advance() { index_++; }
2130
2131 private:
2132 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002133 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002134
2135 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2136};
2137
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002138class HInstructionIterator : public ValueObject {
2139 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002140 explicit HInstructionIterator(const HInstructionList& instructions)
2141 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002142 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002143 }
2144
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002145 bool Done() const { return instruction_ == nullptr; }
2146 HInstruction* Current() const { return instruction_; }
2147 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002148 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002149 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002150 }
2151
2152 private:
2153 HInstruction* instruction_;
2154 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002155
2156 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157};
2158
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002159class HBackwardInstructionIterator : public ValueObject {
2160 public:
2161 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2162 : instruction_(instructions.last_instruction_) {
2163 next_ = Done() ? nullptr : instruction_->GetPrevious();
2164 }
2165
2166 bool Done() const { return instruction_ == nullptr; }
2167 HInstruction* Current() const { return instruction_; }
2168 void Advance() {
2169 instruction_ = next_;
2170 next_ = Done() ? nullptr : instruction_->GetPrevious();
2171 }
2172
2173 private:
2174 HInstruction* instruction_;
2175 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002176
2177 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002178};
2179
Vladimir Markof9f64412015-09-02 14:05:49 +01002180template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181class HTemplateInstruction: public HInstruction {
2182 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002183 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002184 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002185 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002186
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002187 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002188
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002189 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002190 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2191 DCHECK_LT(i, N);
2192 return inputs_[i];
2193 }
David Brazdil1abb4192015-02-17 18:33:36 +00002194
2195 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002196 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002197 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002198 }
2199
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002200 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002201 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002202
2203 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002204};
2205
Vladimir Markof9f64412015-09-02 14:05:49 +01002206// HTemplateInstruction specialization for N=0.
2207template<>
2208class HTemplateInstruction<0>: public HInstruction {
2209 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002210 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002211 : HInstruction(side_effects, dex_pc) {}
2212
Vladimir Markof9f64412015-09-02 14:05:49 +01002213 virtual ~HTemplateInstruction() {}
2214
2215 size_t InputCount() const OVERRIDE { return 0; }
2216
2217 protected:
2218 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2219 LOG(FATAL) << "Unreachable";
2220 UNREACHABLE();
2221 }
2222
2223 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2224 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2225 LOG(FATAL) << "Unreachable";
2226 UNREACHABLE();
2227 }
2228
2229 private:
2230 friend class SsaBuilder;
2231};
2232
Dave Allison20dfc792014-06-16 20:44:29 -07002233template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002234class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002235 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002236 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002237 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002238 virtual ~HExpression() {}
2239
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002240 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002241
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002242 protected:
2243 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002244};
2245
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2247// instruction that branches to the exit block.
2248class HReturnVoid : public HTemplateInstruction<0> {
2249 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002250 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2251 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002252
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002253 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002254
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002255 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256
2257 private:
2258 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2259};
2260
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002261// Represents dex's RETURN opcodes. A HReturn is a control flow
2262// instruction that branches to the exit block.
2263class HReturn : public HTemplateInstruction<1> {
2264 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002265 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2266 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002267 SetRawInputAt(0, value);
2268 }
2269
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002270 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002271
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002272 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002273
2274 private:
2275 DISALLOW_COPY_AND_ASSIGN(HReturn);
2276};
2277
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002278// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002279// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002280// exit block.
2281class HExit : public HTemplateInstruction<0> {
2282 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002283 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002284
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002285 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002286
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002287 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002288
2289 private:
2290 DISALLOW_COPY_AND_ASSIGN(HExit);
2291};
2292
2293// Jumps from one block to another.
2294class HGoto : public HTemplateInstruction<0> {
2295 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002296 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002297
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002298 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002300 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002301 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002302 }
2303
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002304 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002305
2306 private:
2307 DISALLOW_COPY_AND_ASSIGN(HGoto);
2308};
2309
Roland Levillain9867bc72015-08-05 10:21:34 +01002310class HConstant : public HExpression<0> {
2311 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2313 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002314
2315 bool CanBeMoved() const OVERRIDE { return true; }
2316
2317 virtual bool IsMinusOne() const { return false; }
2318 virtual bool IsZero() const { return false; }
2319 virtual bool IsOne() const { return false; }
2320
David Brazdil77a48ae2015-09-15 12:34:04 +00002321 virtual uint64_t GetValueAsUint64() const = 0;
2322
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002323 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002324
2325 private:
2326 DISALLOW_COPY_AND_ASSIGN(HConstant);
2327};
2328
2329class HNullConstant : public HConstant {
2330 public:
2331 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2332 return true;
2333 }
2334
David Brazdil77a48ae2015-09-15 12:34:04 +00002335 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2336
Roland Levillain9867bc72015-08-05 10:21:34 +01002337 size_t ComputeHashCode() const OVERRIDE { return 0; }
2338
2339 DECLARE_INSTRUCTION(NullConstant);
2340
2341 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002342 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002343
2344 friend class HGraph;
2345 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2346};
2347
2348// Constants of the type int. Those can be from Dex instructions, or
2349// synthesized (for example with the if-eqz instruction).
2350class HIntConstant : public HConstant {
2351 public:
2352 int32_t GetValue() const { return value_; }
2353
David Brazdil9f389d42015-10-01 14:32:56 +01002354 uint64_t GetValueAsUint64() const OVERRIDE {
2355 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2356 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002357
Roland Levillain9867bc72015-08-05 10:21:34 +01002358 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2359 DCHECK(other->IsIntConstant());
2360 return other->AsIntConstant()->value_ == value_;
2361 }
2362
2363 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2364
2365 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2366 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2367 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2368
2369 DECLARE_INSTRUCTION(IntConstant);
2370
2371 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002372 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2373 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2374 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2375 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002376
2377 const int32_t value_;
2378
2379 friend class HGraph;
2380 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2381 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2382 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2383};
2384
2385class HLongConstant : public HConstant {
2386 public:
2387 int64_t GetValue() const { return value_; }
2388
David Brazdil77a48ae2015-09-15 12:34:04 +00002389 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2390
Roland Levillain9867bc72015-08-05 10:21:34 +01002391 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2392 DCHECK(other->IsLongConstant());
2393 return other->AsLongConstant()->value_ == value_;
2394 }
2395
2396 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2397
2398 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2399 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2400 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2401
2402 DECLARE_INSTRUCTION(LongConstant);
2403
2404 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002405 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2406 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002407
2408 const int64_t value_;
2409
2410 friend class HGraph;
2411 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2412};
Dave Allison20dfc792014-06-16 20:44:29 -07002413
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002414// Conditional branch. A block ending with an HIf instruction must have
2415// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002416class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002417 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002418 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2419 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002420 SetRawInputAt(0, input);
2421 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002422
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002423 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002424
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002425 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002426 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002427 }
2428
2429 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002430 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002431 }
2432
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002433 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002434
2435 private:
2436 DISALLOW_COPY_AND_ASSIGN(HIf);
2437};
2438
David Brazdilfc6a86a2015-06-26 10:33:45 +00002439
2440// Abstract instruction which marks the beginning and/or end of a try block and
2441// links it to the respective exception handlers. Behaves the same as a Goto in
2442// non-exceptional control flow.
2443// Normal-flow successor is stored at index zero, exception handlers under
2444// higher indices in no particular order.
2445class HTryBoundary : public HTemplateInstruction<0> {
2446 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002447 enum BoundaryKind {
2448 kEntry,
2449 kExit,
2450 };
2451
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002452 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2453 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002454
2455 bool IsControlFlow() const OVERRIDE { return true; }
2456
2457 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002458 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002459
David Brazdild26a4112015-11-10 11:07:31 +00002460 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2461 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2462 }
2463
David Brazdilfc6a86a2015-06-26 10:33:45 +00002464 // Returns whether `handler` is among its exception handlers (non-zero index
2465 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002466 bool HasExceptionHandler(const HBasicBlock& handler) const {
2467 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002468 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002469 }
2470
2471 // If not present already, adds `handler` to its block's list of exception
2472 // handlers.
2473 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002474 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002475 GetBlock()->AddSuccessor(handler);
2476 }
2477 }
2478
David Brazdil56e1acc2015-06-30 15:41:36 +01002479 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002480
David Brazdilffee3d32015-07-06 11:48:53 +01002481 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2482
David Brazdilfc6a86a2015-06-26 10:33:45 +00002483 DECLARE_INSTRUCTION(TryBoundary);
2484
2485 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002486 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002487
2488 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2489};
2490
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002491// Deoptimize to interpreter, upon checking a condition.
2492class HDeoptimize : public HTemplateInstruction<1> {
2493 public:
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002494 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002495 : HTemplateInstruction(SideEffects::None(), 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
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003253enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003254#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3255 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003256#include "intrinsics_list.h"
3257 kNone,
3258 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3259#undef INTRINSICS_LIST
3260#undef OPTIMIZING_INTRINSICS
3261};
3262std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3263
Agi Csaki05f20562015-08-19 14:58:14 -07003264enum IntrinsicNeedsEnvironmentOrCache {
3265 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3266 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003267};
3268
Aart Bik5d75afe2015-12-14 11:57:01 -08003269enum IntrinsicSideEffects {
3270 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3271 kReadSideEffects, // Intrinsic may read heap memory.
3272 kWriteSideEffects, // Intrinsic may write heap memory.
3273 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3274};
3275
3276enum IntrinsicExceptions {
3277 kNoThrow, // Intrinsic does not throw any exceptions.
3278 kCanThrow // Intrinsic may throw exceptions.
3279};
3280
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003281class HInvoke : public HInstruction {
3282 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003283 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003284
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003285 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003286
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003287 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003288 SetRawInputAt(index, argument);
3289 }
3290
Roland Levillain3e3d7332015-04-28 11:00:54 +01003291 // Return the number of arguments. This number can be lower than
3292 // the number of inputs returned by InputCount(), as some invoke
3293 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3294 // inputs at the end of their list of inputs.
3295 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3296
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003297 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003298
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003299 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003300 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003301
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003302 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3303
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003304 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003305 return intrinsic_;
3306 }
3307
Aart Bik5d75afe2015-12-14 11:57:01 -08003308 void SetIntrinsic(Intrinsics intrinsic,
3309 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3310 IntrinsicSideEffects side_effects,
3311 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003312
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003313 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003314 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003315 }
3316
Aart Bik5d75afe2015-12-14 11:57:01 -08003317 bool CanThrow() const OVERRIDE { return can_throw_; }
3318
3319 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3320
3321 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3322 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3323 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003324
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003325 uint32_t* GetIntrinsicOptimizations() {
3326 return &intrinsic_optimizations_;
3327 }
3328
3329 const uint32_t* GetIntrinsicOptimizations() const {
3330 return &intrinsic_optimizations_;
3331 }
3332
3333 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3334
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003335 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003336
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003337 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003338 HInvoke(ArenaAllocator* arena,
3339 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003340 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003341 Primitive::Type return_type,
3342 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003343 uint32_t dex_method_index,
3344 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003345 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003346 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003347 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003348 inputs_(number_of_arguments + number_of_other_inputs,
3349 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003350 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003351 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003352 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003353 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003354 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003355 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003356 }
3357
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003358 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003359 return inputs_[index];
3360 }
3361
David Brazdil1abb4192015-02-17 18:33:36 +00003362 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003363 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003364 }
3365
Aart Bik5d75afe2015-12-14 11:57:01 -08003366 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3367
Roland Levillain3e3d7332015-04-28 11:00:54 +01003368 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003369 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003370 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003371 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003372 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003373 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003374 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003375
3376 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3377 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003378
3379 private:
3380 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3381};
3382
Calin Juravle175dc732015-08-25 15:42:32 +01003383class HInvokeUnresolved : public HInvoke {
3384 public:
3385 HInvokeUnresolved(ArenaAllocator* arena,
3386 uint32_t number_of_arguments,
3387 Primitive::Type return_type,
3388 uint32_t dex_pc,
3389 uint32_t dex_method_index,
3390 InvokeType invoke_type)
3391 : HInvoke(arena,
3392 number_of_arguments,
3393 0u /* number_of_other_inputs */,
3394 return_type,
3395 dex_pc,
3396 dex_method_index,
3397 invoke_type) {
3398 }
3399
3400 DECLARE_INSTRUCTION(InvokeUnresolved);
3401
3402 private:
3403 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3404};
3405
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003406class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003407 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003408 // Requirements of this method call regarding the class
3409 // initialization (clinit) check of its declaring class.
3410 enum class ClinitCheckRequirement {
3411 kNone, // Class already initialized.
3412 kExplicit, // Static call having explicit clinit check as last input.
3413 kImplicit, // Static call implicitly requiring a clinit check.
3414 };
3415
Vladimir Marko58155012015-08-19 12:49:41 +00003416 // Determines how to load the target ArtMethod*.
3417 enum class MethodLoadKind {
3418 // Use a String init ArtMethod* loaded from Thread entrypoints.
3419 kStringInit,
3420
3421 // Use the method's own ArtMethod* loaded by the register allocator.
3422 kRecursive,
3423
3424 // Use ArtMethod* at a known address, embed the direct address in the code.
3425 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3426 kDirectAddress,
3427
3428 // Use ArtMethod* at an address that will be known at link time, embed the direct
3429 // address in the code. If the image is relocatable, emit .patch_oat entry.
3430 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3431 // the image relocatable or not.
3432 kDirectAddressWithFixup,
3433
3434 // Load from resoved methods array in the dex cache using a PC-relative load.
3435 // Used when we need to use the dex cache, for example for invoke-static that
3436 // may cause class initialization (the entry may point to a resolution method),
3437 // and we know that we can access the dex cache arrays using a PC-relative load.
3438 kDexCachePcRelative,
3439
3440 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3441 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3442 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3443 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3444 kDexCacheViaMethod,
3445 };
3446
3447 // Determines the location of the code pointer.
3448 enum class CodePtrLocation {
3449 // Recursive call, use local PC-relative call instruction.
3450 kCallSelf,
3451
3452 // Use PC-relative call instruction patched at link time.
3453 // Used for calls within an oat file, boot->boot or app->app.
3454 kCallPCRelative,
3455
3456 // Call to a known target address, embed the direct address in code.
3457 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3458 kCallDirect,
3459
3460 // Call to a target address that will be known at link time, embed the direct
3461 // address in code. If the image is relocatable, emit .patch_oat entry.
3462 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3463 // the image relocatable or not.
3464 kCallDirectWithFixup,
3465
3466 // Use code pointer from the ArtMethod*.
3467 // Used when we don't know the target code. This is also the last-resort-kind used when
3468 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3469 kCallArtMethod,
3470 };
3471
3472 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003473 MethodLoadKind method_load_kind;
3474 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003475 // The method load data holds
3476 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3477 // Note that there are multiple string init methods, each having its own offset.
3478 // - the method address for kDirectAddress
3479 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003480 uint64_t method_load_data;
3481 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003482 };
3483
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003484 HInvokeStaticOrDirect(ArenaAllocator* arena,
3485 uint32_t number_of_arguments,
3486 Primitive::Type return_type,
3487 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003488 uint32_t method_index,
3489 MethodReference target_method,
3490 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003491 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003492 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003493 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003494 : HInvoke(arena,
3495 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003496 // There is potentially one extra argument for the HCurrentMethod node, and
3497 // potentially one other if the clinit check is explicit, and potentially
3498 // one other if the method is a string factory.
3499 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3500 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3501 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003502 return_type,
3503 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003504 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003505 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003506 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003507 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003508 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003509 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003510
Vladimir Markodc151b22015-10-15 18:02:30 +01003511 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003512 bool had_current_method_input = HasCurrentMethodInput();
3513 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3514
3515 // Using the current method is the default and once we find a better
3516 // method load kind, we should not go back to using the current method.
3517 DCHECK(had_current_method_input || !needs_current_method_input);
3518
3519 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003520 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3521 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003522 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003523 dispatch_info_ = dispatch_info;
3524 }
3525
Vladimir Markoc53c0792015-11-19 15:48:33 +00003526 void AddSpecialInput(HInstruction* input) {
3527 // We allow only one special input.
3528 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3529 DCHECK(InputCount() == GetSpecialInputIndex() ||
3530 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3531 InsertInputAt(GetSpecialInputIndex(), input);
3532 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003533
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003534 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003535 // We access the method via the dex cache so we can't do an implicit null check.
3536 // TODO: for intrinsics we can generate implicit null checks.
3537 return false;
3538 }
3539
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003540 bool CanBeNull() const OVERRIDE {
3541 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3542 }
3543
Vladimir Markoc53c0792015-11-19 15:48:33 +00003544 // Get the index of the special input, if any.
3545 //
3546 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3547 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3548 // the "special input" is the current method pointer; otherwise there may be one
3549 // platform-specific special input, such as PC-relative addressing base.
3550 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3551
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003552 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3553 void SetOptimizedInvokeType(InvokeType invoke_type) {
3554 optimized_invoke_type_ = invoke_type;
3555 }
3556
Vladimir Marko58155012015-08-19 12:49:41 +00003557 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3558 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3559 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003560 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003561 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003562 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003563 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003564 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3565 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003566 bool HasCurrentMethodInput() const {
3567 // This function can be called only after the invoke has been fully initialized by the builder.
3568 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003569 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003570 return true;
3571 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003572 DCHECK(InputCount() == GetSpecialInputIndex() ||
3573 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003574 return false;
3575 }
3576 }
Vladimir Marko58155012015-08-19 12:49:41 +00003577 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3578 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003579 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003580
3581 int32_t GetStringInitOffset() const {
3582 DCHECK(IsStringInit());
3583 return dispatch_info_.method_load_data;
3584 }
3585
3586 uint64_t GetMethodAddress() const {
3587 DCHECK(HasMethodAddress());
3588 return dispatch_info_.method_load_data;
3589 }
3590
3591 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003592 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003593 return dispatch_info_.method_load_data;
3594 }
3595
3596 uint64_t GetDirectCodePtr() const {
3597 DCHECK(HasDirectCodePtr());
3598 return dispatch_info_.direct_code_ptr;
3599 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003600
Calin Juravle68ad6492015-08-18 17:08:12 +01003601 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3602
Roland Levillain4c0eb422015-04-24 16:43:49 +01003603 // Is this instruction a call to a static method?
3604 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003605 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003606 }
3607
Vladimir Markofbb184a2015-11-13 14:47:00 +00003608 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3609 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3610 // instruction; only relevant for static calls with explicit clinit check.
3611 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003612 DCHECK(IsStaticWithExplicitClinitCheck());
3613 size_t last_input_index = InputCount() - 1;
3614 HInstruction* last_input = InputAt(last_input_index);
3615 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003616 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003617 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003618 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003619 clinit_check_requirement_ = new_requirement;
3620 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003621 }
3622
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003623 bool IsStringFactoryFor(HFakeString* str) const {
3624 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003625 DCHECK(!HasCurrentMethodInput());
3626 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003627 return InputAt(InputCount() - 1)->AsFakeString() == str;
3628 }
3629
3630 void RemoveFakeStringArgumentAsLastInput() {
3631 DCHECK(IsStringInit());
3632 size_t last_input_index = InputCount() - 1;
3633 HInstruction* last_input = InputAt(last_input_index);
3634 DCHECK(last_input != nullptr);
3635 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3636 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003637 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003638 }
3639
Roland Levillain4c0eb422015-04-24 16:43:49 +01003640 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003641 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003642 bool IsStaticWithExplicitClinitCheck() const {
3643 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3644 }
3645
3646 // Is this a call to a static method whose declaring class has an
3647 // implicit intialization check requirement?
3648 bool IsStaticWithImplicitClinitCheck() const {
3649 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3650 }
3651
Vladimir Markob554b5a2015-11-06 12:57:55 +00003652 // Does this method load kind need the current method as an input?
3653 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3654 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3655 }
3656
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003657 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003658
Roland Levillain4c0eb422015-04-24 16:43:49 +01003659 protected:
3660 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3661 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3662 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3663 HInstruction* input = input_record.GetInstruction();
3664 // `input` is the last input of a static invoke marked as having
3665 // an explicit clinit check. It must either be:
3666 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3667 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3668 DCHECK(input != nullptr);
3669 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3670 }
3671 return input_record;
3672 }
3673
Vladimir Markoc53c0792015-11-19 15:48:33 +00003674 void InsertInputAt(size_t index, HInstruction* input);
3675 void RemoveInputAt(size_t index);
3676
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003677 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003678 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003679 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003680 // The target method may refer to different dex file or method index than the original
3681 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3682 // in derived class to increase visibility.
3683 MethodReference target_method_;
3684 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003685
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003686 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003687};
Vladimir Markof64242a2015-12-01 14:58:23 +00003688std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003689std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003690
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003691class HInvokeVirtual : public HInvoke {
3692 public:
3693 HInvokeVirtual(ArenaAllocator* arena,
3694 uint32_t number_of_arguments,
3695 Primitive::Type return_type,
3696 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003697 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003698 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003699 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003700 vtable_index_(vtable_index) {}
3701
Calin Juravle641547a2015-04-21 22:08:51 +01003702 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003703 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003704 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003705 }
3706
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003707 uint32_t GetVTableIndex() const { return vtable_index_; }
3708
3709 DECLARE_INSTRUCTION(InvokeVirtual);
3710
3711 private:
3712 const uint32_t vtable_index_;
3713
3714 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3715};
3716
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003717class HInvokeInterface : public HInvoke {
3718 public:
3719 HInvokeInterface(ArenaAllocator* arena,
3720 uint32_t number_of_arguments,
3721 Primitive::Type return_type,
3722 uint32_t dex_pc,
3723 uint32_t dex_method_index,
3724 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003725 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003726 imt_index_(imt_index) {}
3727
Calin Juravle641547a2015-04-21 22:08:51 +01003728 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003729 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003730 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003731 }
3732
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003733 uint32_t GetImtIndex() const { return imt_index_; }
3734 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3735
3736 DECLARE_INSTRUCTION(InvokeInterface);
3737
3738 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003739 const uint32_t imt_index_;
3740
3741 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3742};
3743
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003744class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003745 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003746 HNewInstance(HInstruction* cls,
3747 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003748 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003749 uint16_t type_index,
3750 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003751 bool can_throw,
3752 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003753 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003754 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003755 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003756 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003757 can_throw_(can_throw),
3758 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003759 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003760 SetRawInputAt(0, cls);
3761 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003762 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003763
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003764 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003765 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003766
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003767 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003768 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003769
3770 // It may throw when called on type that's not instantiable/accessible.
3771 // It can throw OOME.
3772 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3773 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3774
3775 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003776
Calin Juravle10e244f2015-01-26 18:54:32 +00003777 bool CanBeNull() const OVERRIDE { return false; }
3778
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003779 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3780
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003781 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3782 entrypoint_ = entrypoint;
3783 }
3784
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003785 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003786
3787 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003788 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003789 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003790 const bool can_throw_;
3791 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003792 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003793
3794 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3795};
3796
Roland Levillain88cb1752014-10-20 16:36:47 +01003797class HNeg : public HUnaryOperation {
3798 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003799 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3800 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003801
Roland Levillain9867bc72015-08-05 10:21:34 +01003802 template <typename T> T Compute(T x) const { return -x; }
3803
3804 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003805 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 }
3807 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003808 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003809 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003810
Roland Levillain88cb1752014-10-20 16:36:47 +01003811 DECLARE_INSTRUCTION(Neg);
3812
3813 private:
3814 DISALLOW_COPY_AND_ASSIGN(HNeg);
3815};
3816
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003817class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003818 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003819 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003820 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003821 uint32_t dex_pc,
3822 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003823 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003824 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003825 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003826 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003827 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003828 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003829 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003830 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003831 }
3832
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003833 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003834 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003835
3836 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003837 bool NeedsEnvironment() const OVERRIDE { return true; }
3838
Mingyao Yang0c365e62015-03-31 15:09:29 -07003839 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3840 bool CanThrow() const OVERRIDE { return true; }
3841
Calin Juravle10e244f2015-01-26 18:54:32 +00003842 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003843
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003844 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3845
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003846 DECLARE_INSTRUCTION(NewArray);
3847
3848 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003849 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003850 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003851 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003852
3853 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3854};
3855
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003856class HAdd : public HBinaryOperation {
3857 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003858 HAdd(Primitive::Type result_type,
3859 HInstruction* left,
3860 HInstruction* right,
3861 uint32_t dex_pc = kNoDexPc)
3862 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003863
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003864 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003865
Roland Levillain9867bc72015-08-05 10:21:34 +01003866 template <typename T> T Compute(T x, T y) const { return x + y; }
3867
3868 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003869 return GetBlock()->GetGraph()->GetIntConstant(
3870 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003871 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003872 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003873 return GetBlock()->GetGraph()->GetLongConstant(
3874 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003875 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003876
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003877 DECLARE_INSTRUCTION(Add);
3878
3879 private:
3880 DISALLOW_COPY_AND_ASSIGN(HAdd);
3881};
3882
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003883class HSub : public HBinaryOperation {
3884 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003885 HSub(Primitive::Type result_type,
3886 HInstruction* left,
3887 HInstruction* right,
3888 uint32_t dex_pc = kNoDexPc)
3889 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003890
Roland Levillain9867bc72015-08-05 10:21:34 +01003891 template <typename T> T Compute(T x, T y) const { return x - y; }
3892
3893 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003894 return GetBlock()->GetGraph()->GetIntConstant(
3895 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003896 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003897 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003898 return GetBlock()->GetGraph()->GetLongConstant(
3899 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003900 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003901
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003902 DECLARE_INSTRUCTION(Sub);
3903
3904 private:
3905 DISALLOW_COPY_AND_ASSIGN(HSub);
3906};
3907
Calin Juravle34bacdf2014-10-07 20:23:36 +01003908class HMul : public HBinaryOperation {
3909 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003910 HMul(Primitive::Type result_type,
3911 HInstruction* left,
3912 HInstruction* right,
3913 uint32_t dex_pc = kNoDexPc)
3914 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003915
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003916 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003917
Roland Levillain9867bc72015-08-05 10:21:34 +01003918 template <typename T> T Compute(T x, T y) const { return x * y; }
3919
3920 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003921 return GetBlock()->GetGraph()->GetIntConstant(
3922 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003923 }
3924 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003925 return GetBlock()->GetGraph()->GetLongConstant(
3926 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003927 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003928
3929 DECLARE_INSTRUCTION(Mul);
3930
3931 private:
3932 DISALLOW_COPY_AND_ASSIGN(HMul);
3933};
3934
Calin Juravle7c4954d2014-10-28 16:57:40 +00003935class HDiv : public HBinaryOperation {
3936 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003937 HDiv(Primitive::Type result_type,
3938 HInstruction* left,
3939 HInstruction* right,
3940 uint32_t dex_pc)
3941 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003942
Roland Levillain9867bc72015-08-05 10:21:34 +01003943 template <typename T>
3944 T Compute(T x, T y) const {
3945 // Our graph structure ensures we never have 0 for `y` during
3946 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003947 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003948 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003949 return (y == -1) ? -x : x / y;
3950 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003951
Roland Levillain9867bc72015-08-05 10:21:34 +01003952 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003953 return GetBlock()->GetGraph()->GetIntConstant(
3954 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003955 }
3956 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003957 return GetBlock()->GetGraph()->GetLongConstant(
3958 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003959 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003960
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003961 static SideEffects SideEffectsForArchRuntimeCalls() {
3962 // The generated code can use a runtime call.
3963 return SideEffects::CanTriggerGC();
3964 }
3965
Calin Juravle7c4954d2014-10-28 16:57:40 +00003966 DECLARE_INSTRUCTION(Div);
3967
3968 private:
3969 DISALLOW_COPY_AND_ASSIGN(HDiv);
3970};
3971
Calin Juravlebacfec32014-11-14 15:54:36 +00003972class HRem : public HBinaryOperation {
3973 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003974 HRem(Primitive::Type result_type,
3975 HInstruction* left,
3976 HInstruction* right,
3977 uint32_t dex_pc)
3978 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003979
Roland Levillain9867bc72015-08-05 10:21:34 +01003980 template <typename T>
3981 T Compute(T x, T y) const {
3982 // Our graph structure ensures we never have 0 for `y` during
3983 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003984 DCHECK_NE(y, 0);
3985 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3986 return (y == -1) ? 0 : x % y;
3987 }
3988
Roland Levillain9867bc72015-08-05 10:21:34 +01003989 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003990 return GetBlock()->GetGraph()->GetIntConstant(
3991 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 }
3993 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003994 return GetBlock()->GetGraph()->GetLongConstant(
3995 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003996 }
3997
Calin Juravlebacfec32014-11-14 15:54:36 +00003998
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003999 static SideEffects SideEffectsForArchRuntimeCalls() {
4000 return SideEffects::CanTriggerGC();
4001 }
4002
Calin Juravlebacfec32014-11-14 15:54:36 +00004003 DECLARE_INSTRUCTION(Rem);
4004
4005 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004006 DISALLOW_COPY_AND_ASSIGN(HRem);
4007};
4008
Calin Juravled0d48522014-11-04 16:40:20 +00004009class HDivZeroCheck : public HExpression<1> {
4010 public:
4011 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004012 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004013 SetRawInputAt(0, value);
4014 }
4015
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004016 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4017
Calin Juravled0d48522014-11-04 16:40:20 +00004018 bool CanBeMoved() const OVERRIDE { return true; }
4019
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004020 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004021 return true;
4022 }
4023
4024 bool NeedsEnvironment() const OVERRIDE { return true; }
4025 bool CanThrow() const OVERRIDE { return true; }
4026
Calin Juravled0d48522014-11-04 16:40:20 +00004027 DECLARE_INSTRUCTION(DivZeroCheck);
4028
4029 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004030 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4031};
4032
Calin Juravle9aec02f2014-11-18 23:06:35 +00004033class HShl : public HBinaryOperation {
4034 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004035 HShl(Primitive::Type result_type,
4036 HInstruction* left,
4037 HInstruction* right,
4038 uint32_t dex_pc = kNoDexPc)
4039 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004040
Roland Levillain9867bc72015-08-05 10:21:34 +01004041 template <typename T, typename U, typename V>
4042 T Compute(T x, U y, V max_shift_value) const {
4043 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4044 "V is not the unsigned integer type corresponding to T");
4045 return x << (y & max_shift_value);
4046 }
4047
4048 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4049 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004050 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004051 }
4052 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4053 // case is handled as `x << static_cast<int>(y)`.
4054 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4055 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004056 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004057 }
4058 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4059 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004060 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004061 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004062
4063 DECLARE_INSTRUCTION(Shl);
4064
4065 private:
4066 DISALLOW_COPY_AND_ASSIGN(HShl);
4067};
4068
4069class HShr : public HBinaryOperation {
4070 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004071 HShr(Primitive::Type result_type,
4072 HInstruction* left,
4073 HInstruction* right,
4074 uint32_t dex_pc = kNoDexPc)
4075 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004076
Roland Levillain9867bc72015-08-05 10:21:34 +01004077 template <typename T, typename U, typename V>
4078 T Compute(T x, U y, V max_shift_value) const {
4079 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4080 "V is not the unsigned integer type corresponding to T");
4081 return x >> (y & max_shift_value);
4082 }
4083
4084 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4085 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004086 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004087 }
4088 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4089 // case is handled as `x >> static_cast<int>(y)`.
4090 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4091 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004092 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004093 }
4094 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4095 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004096 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004097 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004098
4099 DECLARE_INSTRUCTION(Shr);
4100
4101 private:
4102 DISALLOW_COPY_AND_ASSIGN(HShr);
4103};
4104
4105class HUShr : public HBinaryOperation {
4106 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004107 HUShr(Primitive::Type result_type,
4108 HInstruction* left,
4109 HInstruction* right,
4110 uint32_t dex_pc = kNoDexPc)
4111 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004112
Roland Levillain9867bc72015-08-05 10:21:34 +01004113 template <typename T, typename U, typename V>
4114 T Compute(T x, U y, V max_shift_value) const {
4115 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4116 "V is not the unsigned integer type corresponding to T");
4117 V ux = static_cast<V>(x);
4118 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004119 }
4120
Roland Levillain9867bc72015-08-05 10:21:34 +01004121 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4122 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004123 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004124 }
4125 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4126 // case is handled as `x >>> static_cast<int>(y)`.
4127 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4128 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004129 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004130 }
4131 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4132 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004133 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004134 }
4135
4136 DECLARE_INSTRUCTION(UShr);
4137
4138 private:
4139 DISALLOW_COPY_AND_ASSIGN(HUShr);
4140};
4141
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004142class HAnd : public HBinaryOperation {
4143 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004144 HAnd(Primitive::Type result_type,
4145 HInstruction* left,
4146 HInstruction* right,
4147 uint32_t dex_pc = kNoDexPc)
4148 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004149
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004150 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004151
Roland Levillain9867bc72015-08-05 10:21:34 +01004152 template <typename T, typename U>
4153 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4154
4155 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004156 return GetBlock()->GetGraph()->GetIntConstant(
4157 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004158 }
4159 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004160 return GetBlock()->GetGraph()->GetLongConstant(
4161 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004162 }
4163 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004164 return GetBlock()->GetGraph()->GetLongConstant(
4165 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004166 }
4167 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004168 return GetBlock()->GetGraph()->GetLongConstant(
4169 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004170 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004171
4172 DECLARE_INSTRUCTION(And);
4173
4174 private:
4175 DISALLOW_COPY_AND_ASSIGN(HAnd);
4176};
4177
4178class HOr : public HBinaryOperation {
4179 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004180 HOr(Primitive::Type result_type,
4181 HInstruction* left,
4182 HInstruction* right,
4183 uint32_t dex_pc = kNoDexPc)
4184 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004185
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004186 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004187
Roland Levillain9867bc72015-08-05 10:21:34 +01004188 template <typename T, typename U>
4189 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4190
4191 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004192 return GetBlock()->GetGraph()->GetIntConstant(
4193 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004194 }
4195 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004196 return GetBlock()->GetGraph()->GetLongConstant(
4197 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004198 }
4199 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004200 return GetBlock()->GetGraph()->GetLongConstant(
4201 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004202 }
4203 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004204 return GetBlock()->GetGraph()->GetLongConstant(
4205 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004206 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004207
4208 DECLARE_INSTRUCTION(Or);
4209
4210 private:
4211 DISALLOW_COPY_AND_ASSIGN(HOr);
4212};
4213
4214class HXor : public HBinaryOperation {
4215 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004216 HXor(Primitive::Type result_type,
4217 HInstruction* left,
4218 HInstruction* right,
4219 uint32_t dex_pc = kNoDexPc)
4220 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004221
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004222 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004223
Roland Levillain9867bc72015-08-05 10:21:34 +01004224 template <typename T, typename U>
4225 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4226
4227 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004228 return GetBlock()->GetGraph()->GetIntConstant(
4229 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004230 }
4231 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004232 return GetBlock()->GetGraph()->GetLongConstant(
4233 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004234 }
4235 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004236 return GetBlock()->GetGraph()->GetLongConstant(
4237 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004238 }
4239 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004240 return GetBlock()->GetGraph()->GetLongConstant(
4241 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004242 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004243
4244 DECLARE_INSTRUCTION(Xor);
4245
4246 private:
4247 DISALLOW_COPY_AND_ASSIGN(HXor);
4248};
4249
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004250class HRor : public HBinaryOperation {
4251 public:
4252 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4253 : HBinaryOperation(result_type, value, distance) {}
4254
4255 template <typename T, typename U, typename V>
4256 T Compute(T x, U y, V max_shift_value) const {
4257 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4258 "V is not the unsigned integer type corresponding to T");
4259 V ux = static_cast<V>(x);
4260 if ((y & max_shift_value) == 0) {
4261 return static_cast<T>(ux);
4262 } else {
4263 const V reg_bits = sizeof(T) * 8;
4264 return static_cast<T>(ux >> (y & max_shift_value)) |
4265 (x << (reg_bits - (y & max_shift_value)));
4266 }
4267 }
4268
4269 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4270 return GetBlock()->GetGraph()->GetIntConstant(
4271 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4272 }
4273 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4274 return GetBlock()->GetGraph()->GetLongConstant(
4275 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4276 }
4277 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4278 return GetBlock()->GetGraph()->GetLongConstant(
4279 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4280 }
4281
4282 DECLARE_INSTRUCTION(Ror);
4283
4284 private:
4285 DISALLOW_COPY_AND_ASSIGN(HRor);
4286};
4287
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004288// The value of a parameter in this method. Its location depends on
4289// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004290class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004291 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004292 HParameterValue(const DexFile& dex_file,
4293 uint16_t type_index,
4294 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004295 Primitive::Type parameter_type,
4296 bool is_this = false)
4297 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004298 dex_file_(dex_file),
4299 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004300 index_(index),
4301 is_this_(is_this),
4302 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004303
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004304 const DexFile& GetDexFile() const { return dex_file_; }
4305 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004306 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004307 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004308
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004309 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4310 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004311
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004312 DECLARE_INSTRUCTION(ParameterValue);
4313
4314 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004315 const DexFile& dex_file_;
4316 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004317 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004318 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004319 const uint8_t index_;
4320
Calin Juravle10e244f2015-01-26 18:54:32 +00004321 // Whether or not the parameter value corresponds to 'this' argument.
4322 const bool is_this_;
4323
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004324 bool can_be_null_;
4325
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004326 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4327};
4328
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004329class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004330 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004331 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4332 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004334 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004335 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004336 return true;
4337 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004338
Roland Levillain9867bc72015-08-05 10:21:34 +01004339 template <typename T> T Compute(T x) const { return ~x; }
4340
4341 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004342 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004343 }
4344 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004345 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004346 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004347
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004348 DECLARE_INSTRUCTION(Not);
4349
4350 private:
4351 DISALLOW_COPY_AND_ASSIGN(HNot);
4352};
4353
David Brazdil66d126e2015-04-03 16:02:44 +01004354class HBooleanNot : public HUnaryOperation {
4355 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004356 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4357 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004358
4359 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004360 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004361 return true;
4362 }
4363
Roland Levillain9867bc72015-08-05 10:21:34 +01004364 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004365 DCHECK(IsUint<1>(x));
4366 return !x;
4367 }
4368
Roland Levillain9867bc72015-08-05 10:21:34 +01004369 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004370 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004371 }
4372 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4373 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004374 UNREACHABLE();
4375 }
4376
4377 DECLARE_INSTRUCTION(BooleanNot);
4378
4379 private:
4380 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4381};
4382
Roland Levillaindff1f282014-11-05 14:15:05 +00004383class HTypeConversion : public HExpression<1> {
4384 public:
4385 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004386 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004387 : HExpression(result_type,
4388 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4389 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004390 SetRawInputAt(0, input);
4391 DCHECK_NE(input->GetType(), result_type);
4392 }
4393
4394 HInstruction* GetInput() const { return InputAt(0); }
4395 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4396 Primitive::Type GetResultType() const { return GetType(); }
4397
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004398 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004399 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004400
Roland Levillaindff1f282014-11-05 14:15:05 +00004401 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004402 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004403
Mark Mendelle82549b2015-05-06 10:55:34 -04004404 // Try to statically evaluate the conversion and return a HConstant
4405 // containing the result. If the input cannot be converted, return nullptr.
4406 HConstant* TryStaticEvaluation() const;
4407
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004408 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4409 Primitive::Type result_type) {
4410 // Some architectures may not require the 'GC' side effects, but at this point
4411 // in the compilation process we do not know what architecture we will
4412 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004413 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4414 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004415 return SideEffects::CanTriggerGC();
4416 }
4417 return SideEffects::None();
4418 }
4419
Roland Levillaindff1f282014-11-05 14:15:05 +00004420 DECLARE_INSTRUCTION(TypeConversion);
4421
4422 private:
4423 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4424};
4425
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004426static constexpr uint32_t kNoRegNumber = -1;
4427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004428class HPhi : public HInstruction {
4429 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004430 HPhi(ArenaAllocator* arena,
4431 uint32_t reg_number,
4432 size_t number_of_inputs,
4433 Primitive::Type type,
4434 uint32_t dex_pc = kNoDexPc)
4435 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004436 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004437 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004438 type_(ToPhiType(type)),
4439 // Phis are constructed live and marked dead if conflicting or unused.
4440 // Individual steps of SsaBuilder should assume that if a phi has been
4441 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4442 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004443 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004444 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004445 }
4446
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004447 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4448 static Primitive::Type ToPhiType(Primitive::Type type) {
4449 switch (type) {
4450 case Primitive::kPrimBoolean:
4451 case Primitive::kPrimByte:
4452 case Primitive::kPrimShort:
4453 case Primitive::kPrimChar:
4454 return Primitive::kPrimInt;
4455 default:
4456 return type;
4457 }
4458 }
4459
David Brazdilffee3d32015-07-06 11:48:53 +01004460 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4461
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004462 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004463
4464 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004465 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004466
Calin Juravle10e244f2015-01-26 18:54:32 +00004467 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004468 void SetType(Primitive::Type new_type) {
4469 // Make sure that only valid type changes occur. The following are allowed:
4470 // (1) int -> float/ref (primitive type propagation),
4471 // (2) long -> double (primitive type propagation).
4472 DCHECK(type_ == new_type ||
4473 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4474 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4475 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4476 type_ = new_type;
4477 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004478
Calin Juravle10e244f2015-01-26 18:54:32 +00004479 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4480 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4481
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004482 uint32_t GetRegNumber() const { return reg_number_; }
4483
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004484 void SetDead() { is_live_ = false; }
4485 void SetLive() { is_live_ = true; }
4486 bool IsDead() const { return !is_live_; }
4487 bool IsLive() const { return is_live_; }
4488
David Brazdil77a48ae2015-09-15 12:34:04 +00004489 bool IsVRegEquivalentOf(HInstruction* other) const {
4490 return other != nullptr
4491 && other->IsPhi()
4492 && other->AsPhi()->GetBlock() == GetBlock()
4493 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4494 }
4495
Calin Juravlea4f88312015-04-16 12:57:19 +01004496 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4497 // An equivalent phi is a phi having the same dex register and type.
4498 // It assumes that phis with the same dex register are adjacent.
4499 HPhi* GetNextEquivalentPhiWithSameType() {
4500 HInstruction* next = GetNext();
4501 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4502 if (next->GetType() == GetType()) {
4503 return next->AsPhi();
4504 }
4505 next = next->GetNext();
4506 }
4507 return nullptr;
4508 }
4509
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004510 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004511
David Brazdil1abb4192015-02-17 18:33:36 +00004512 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004513 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004514 return inputs_[index];
4515 }
David Brazdil1abb4192015-02-17 18:33:36 +00004516
4517 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004518 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004519 }
4520
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004521 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004522 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004523 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004524 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004525 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004526 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004527
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004528 DISALLOW_COPY_AND_ASSIGN(HPhi);
4529};
4530
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004531class HNullCheck : public HExpression<1> {
4532 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004533 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4534 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004535 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004536 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004537 SetRawInputAt(0, value);
4538 }
4539
Calin Juravle10e244f2015-01-26 18:54:32 +00004540 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004541 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004542 return true;
4543 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004544
Calin Juravle10e244f2015-01-26 18:54:32 +00004545 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004546
Calin Juravle10e244f2015-01-26 18:54:32 +00004547 bool CanThrow() const OVERRIDE { return true; }
4548
4549 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004550
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551
4552 DECLARE_INSTRUCTION(NullCheck);
4553
4554 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4556};
4557
4558class FieldInfo : public ValueObject {
4559 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004560 FieldInfo(MemberOffset field_offset,
4561 Primitive::Type field_type,
4562 bool is_volatile,
4563 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004564 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004565 const DexFile& dex_file,
4566 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004567 : field_offset_(field_offset),
4568 field_type_(field_type),
4569 is_volatile_(is_volatile),
4570 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004571 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004572 dex_file_(dex_file),
4573 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004574
4575 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004576 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004577 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004578 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004579 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004580 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004581 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004582
4583 private:
4584 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004585 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004586 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004587 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004588 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004589 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004590 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004591};
4592
4593class HInstanceFieldGet : public HExpression<1> {
4594 public:
4595 HInstanceFieldGet(HInstruction* value,
4596 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004597 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004598 bool is_volatile,
4599 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004600 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004601 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004602 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004603 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004604 : HExpression(field_type,
4605 SideEffects::FieldReadOfType(field_type, is_volatile),
4606 dex_pc),
4607 field_info_(field_offset,
4608 field_type,
4609 is_volatile,
4610 field_idx,
4611 declaring_class_def_index,
4612 dex_file,
4613 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004614 SetRawInputAt(0, value);
4615 }
4616
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004617 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004618
4619 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4620 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4621 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004622 }
4623
Calin Juravle641547a2015-04-21 22:08:51 +01004624 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4625 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004626 }
4627
4628 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004629 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4630 }
4631
Calin Juravle52c48962014-12-16 17:02:57 +00004632 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004633 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004634 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004635 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004636
4637 DECLARE_INSTRUCTION(InstanceFieldGet);
4638
4639 private:
4640 const FieldInfo field_info_;
4641
4642 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4643};
4644
4645class HInstanceFieldSet : public HTemplateInstruction<2> {
4646 public:
4647 HInstanceFieldSet(HInstruction* object,
4648 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004649 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004650 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004651 bool is_volatile,
4652 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004653 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004654 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004655 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004656 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004657 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4658 dex_pc),
4659 field_info_(field_offset,
4660 field_type,
4661 is_volatile,
4662 field_idx,
4663 declaring_class_def_index,
4664 dex_file,
4665 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004666 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004667 SetRawInputAt(0, object);
4668 SetRawInputAt(1, value);
4669 }
4670
Calin Juravle641547a2015-04-21 22:08:51 +01004671 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4672 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004673 }
4674
Calin Juravle52c48962014-12-16 17:02:57 +00004675 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004676 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004677 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004678 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004679 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004680 bool GetValueCanBeNull() const { return value_can_be_null_; }
4681 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004682
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004683 DECLARE_INSTRUCTION(InstanceFieldSet);
4684
4685 private:
4686 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004687 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004688
4689 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4690};
4691
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004692class HArrayGet : public HExpression<2> {
4693 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004694 HArrayGet(HInstruction* array,
4695 HInstruction* index,
4696 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004697 uint32_t dex_pc,
4698 SideEffects additional_side_effects = SideEffects::None())
4699 : HExpression(type,
4700 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004701 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702 SetRawInputAt(0, array);
4703 SetRawInputAt(1, index);
4704 }
4705
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004706 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004707 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004708 return true;
4709 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004710 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004711 // TODO: We can be smarter here.
4712 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4713 // which generates the implicit null check. There are cases when these can be removed
4714 // to produce better code. If we ever add optimizations to do so we should allow an
4715 // implicit check here (as long as the address falls in the first page).
4716 return false;
4717 }
4718
David Brazdil4833f5a2015-12-16 10:37:39 +00004719 bool IsEquivalentOf(HArrayGet* other) const {
4720 bool result = (GetDexPc() == other->GetDexPc());
4721 if (kIsDebugBuild && result) {
4722 DCHECK_EQ(GetBlock(), other->GetBlock());
4723 DCHECK_EQ(GetArray(), other->GetArray());
4724 DCHECK_EQ(GetIndex(), other->GetIndex());
4725 if (Primitive::IsIntOrLongType(GetType())) {
4726 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4727 } else {
4728 DCHECK(Primitive::IsFloatingPointType(GetType()));
4729 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4730 }
4731 }
4732 return result;
4733 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004734
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004735 HInstruction* GetArray() const { return InputAt(0); }
4736 HInstruction* GetIndex() const { return InputAt(1); }
4737
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738 DECLARE_INSTRUCTION(ArrayGet);
4739
4740 private:
4741 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4742};
4743
4744class HArraySet : public HTemplateInstruction<3> {
4745 public:
4746 HArraySet(HInstruction* array,
4747 HInstruction* index,
4748 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004749 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004750 uint32_t dex_pc,
4751 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004752 : HTemplateInstruction(
4753 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004754 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4755 additional_side_effects),
4756 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004757 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004758 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 value_can_be_null_(true),
4760 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004761 SetRawInputAt(0, array);
4762 SetRawInputAt(1, index);
4763 SetRawInputAt(2, value);
4764 }
4765
Calin Juravle77520bc2015-01-12 18:45:46 +00004766 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004767 // We currently always call a runtime method to catch array store
4768 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004769 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004770 }
4771
Mingyao Yang81014cb2015-06-02 03:16:27 -07004772 // Can throw ArrayStoreException.
4773 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4774
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004775 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004776 // TODO: Same as for ArrayGet.
4777 return false;
4778 }
4779
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004780 void ClearNeedsTypeCheck() {
4781 needs_type_check_ = false;
4782 }
4783
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004784 void ClearValueCanBeNull() {
4785 value_can_be_null_ = false;
4786 }
4787
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004788 void SetStaticTypeOfArrayIsObjectArray() {
4789 static_type_of_array_is_object_array_ = true;
4790 }
4791
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004792 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004793 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004794 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004795
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004796 HInstruction* GetArray() const { return InputAt(0); }
4797 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004798 HInstruction* GetValue() const { return InputAt(2); }
4799
4800 Primitive::Type GetComponentType() const {
4801 // The Dex format does not type floating point index operations. Since the
4802 // `expected_component_type_` is set during building and can therefore not
4803 // be correct, we also check what is the value type. If it is a floating
4804 // point type, we must use that type.
4805 Primitive::Type value_type = GetValue()->GetType();
4806 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4807 ? value_type
4808 : expected_component_type_;
4809 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004810
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004811 Primitive::Type GetRawExpectedComponentType() const {
4812 return expected_component_type_;
4813 }
4814
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004815 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4816 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4817 }
4818
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004819 DECLARE_INSTRUCTION(ArraySet);
4820
4821 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004822 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004823 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004824 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004825 // Cached information for the reference_type_info_ so that codegen
4826 // does not need to inspect the static type.
4827 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004828
4829 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4830};
4831
4832class HArrayLength : public HExpression<1> {
4833 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004834 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004836 // Note that arrays do not change length, so the instruction does not
4837 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004838 SetRawInputAt(0, array);
4839 }
4840
Calin Juravle77520bc2015-01-12 18:45:46 +00004841 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004842 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004843 return true;
4844 }
Calin Juravle641547a2015-04-21 22:08:51 +01004845 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4846 return obj == InputAt(0);
4847 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004848
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004849 DECLARE_INSTRUCTION(ArrayLength);
4850
4851 private:
4852 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4853};
4854
4855class HBoundsCheck : public HExpression<2> {
4856 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004857 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4858 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004859 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004860 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004861 DCHECK(index->GetType() == Primitive::kPrimInt);
4862 SetRawInputAt(0, index);
4863 SetRawInputAt(1, length);
4864 }
4865
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004866 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004867 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004868 return true;
4869 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004870
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004871 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004872
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004873 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004874
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004875 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004876
4877 DECLARE_INSTRUCTION(BoundsCheck);
4878
4879 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4881};
4882
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004883/**
4884 * Some DEX instructions are folded into multiple HInstructions that need
4885 * to stay live until the last HInstruction. This class
4886 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004887 * HInstruction stays live. `index` represents the stack location index of the
4888 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004889 */
4890class HTemporary : public HTemplateInstruction<0> {
4891 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004892 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4893 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004894
4895 size_t GetIndex() const { return index_; }
4896
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004897 Primitive::Type GetType() const OVERRIDE {
4898 // The previous instruction is the one that will be stored in the temporary location.
4899 DCHECK(GetPrevious() != nullptr);
4900 return GetPrevious()->GetType();
4901 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004902
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004903 DECLARE_INSTRUCTION(Temporary);
4904
4905 private:
4906 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004907 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4908};
4909
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004910class HSuspendCheck : public HTemplateInstruction<0> {
4911 public:
4912 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004913 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004914
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004915 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004916 return true;
4917 }
4918
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004919 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4920 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004921
4922 DECLARE_INSTRUCTION(SuspendCheck);
4923
4924 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004925 // Only used for code generation, in order to share the same slow path between back edges
4926 // of a same loop.
4927 SlowPathCode* slow_path_;
4928
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004929 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4930};
4931
David Srbecky0cf44932015-12-09 14:09:59 +00004932// Pseudo-instruction which provides the native debugger with mapping information.
4933// It ensures that we can generate line number and local variables at this point.
4934class HNativeDebugInfo : public HTemplateInstruction<0> {
4935 public:
4936 explicit HNativeDebugInfo(uint32_t dex_pc)
4937 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4938
4939 bool NeedsEnvironment() const OVERRIDE {
4940 return true;
4941 }
4942
4943 DECLARE_INSTRUCTION(NativeDebugInfo);
4944
4945 private:
4946 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4947};
4948
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004949/**
4950 * Instruction to load a Class object.
4951 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004952class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004953 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004954 HLoadClass(HCurrentMethod* current_method,
4955 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004956 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004957 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004958 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004959 bool needs_access_check,
4960 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004961 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004962 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004963 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004964 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004965 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004966 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004967 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004968 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004969 // Referrers class should not need access check. We never inline unverified
4970 // methods so we can't possibly end up in this situation.
4971 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004972 SetRawInputAt(0, current_method);
4973 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004974
4975 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004976
4977 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004978 // Note that we don't need to test for generate_clinit_check_.
4979 // Whether or not we need to generate the clinit check is processed in
4980 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004981 return other->AsLoadClass()->type_index_ == type_index_ &&
4982 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004983 }
4984
4985 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4986
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004987 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004988 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004989 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004990
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004991 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004992 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004993 }
4994
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004995 bool MustGenerateClinitCheck() const {
4996 return generate_clinit_check_;
4997 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004998
Calin Juravle0ba218d2015-05-19 18:46:01 +01004999 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005000 // The entrypoint the code generator is going to call does not do
5001 // clinit of the class.
5002 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005003 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005004 }
5005
5006 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005007 return MustGenerateClinitCheck() ||
5008 (!is_referrers_class_ && !is_in_dex_cache_) ||
5009 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005010 }
5011
5012 bool NeedsAccessCheck() const {
5013 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005014 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005015
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005016 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005017 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005018 }
5019
Calin Juravleacf735c2015-02-12 15:25:22 +00005020 ReferenceTypeInfo GetLoadedClassRTI() {
5021 return loaded_class_rti_;
5022 }
5023
5024 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5025 // Make sure we only set exact types (the loaded class should never be merged).
5026 DCHECK(rti.IsExact());
5027 loaded_class_rti_ = rti;
5028 }
5029
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005030 const DexFile& GetDexFile() { return dex_file_; }
5031
Vladimir Markodc151b22015-10-15 18:02:30 +01005032 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005033
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005034 static SideEffects SideEffectsForArchRuntimeCalls() {
5035 return SideEffects::CanTriggerGC();
5036 }
5037
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005038 bool IsInDexCache() const { return is_in_dex_cache_; }
5039
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005040 DECLARE_INSTRUCTION(LoadClass);
5041
5042 private:
5043 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005044 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005045 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005046 // Whether this instruction must generate the initialization check.
5047 // Used for code generation.
5048 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005049 const bool needs_access_check_;
5050 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005051
Calin Juravleacf735c2015-02-12 15:25:22 +00005052 ReferenceTypeInfo loaded_class_rti_;
5053
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005054 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5055};
5056
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005057class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005058 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005059 HLoadString(HCurrentMethod* current_method,
5060 uint32_t string_index,
5061 uint32_t dex_pc,
5062 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005063 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005064 string_index_(string_index),
5065 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005066 SetRawInputAt(0, current_method);
5067 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005068
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005069 bool CanBeMoved() const OVERRIDE { return true; }
5070
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005071 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5072 return other->AsLoadString()->string_index_ == string_index_;
5073 }
5074
5075 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5076
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005077 uint32_t GetStringIndex() const { return string_index_; }
5078
5079 // TODO: Can we deopt or debug when we resolve a string?
5080 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005081 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005082 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005083 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005084
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005085 static SideEffects SideEffectsForArchRuntimeCalls() {
5086 return SideEffects::CanTriggerGC();
5087 }
5088
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005089 DECLARE_INSTRUCTION(LoadString);
5090
5091 private:
5092 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005093 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005094
5095 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5096};
5097
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005098/**
5099 * Performs an initialization check on its Class object input.
5100 */
5101class HClinitCheck : public HExpression<1> {
5102 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005103 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005104 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005105 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005106 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5107 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005108 SetRawInputAt(0, constant);
5109 }
5110
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005111 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005112 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005113 return true;
5114 }
5115
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005116 bool NeedsEnvironment() const OVERRIDE {
5117 // May call runtime to initialize the class.
5118 return true;
5119 }
5120
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005121 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005122
5123 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5124
5125 DECLARE_INSTRUCTION(ClinitCheck);
5126
5127 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005128 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5129};
5130
5131class HStaticFieldGet : public HExpression<1> {
5132 public:
5133 HStaticFieldGet(HInstruction* cls,
5134 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005135 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005136 bool is_volatile,
5137 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005138 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005139 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005140 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005141 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005142 : HExpression(field_type,
5143 SideEffects::FieldReadOfType(field_type, is_volatile),
5144 dex_pc),
5145 field_info_(field_offset,
5146 field_type,
5147 is_volatile,
5148 field_idx,
5149 declaring_class_def_index,
5150 dex_file,
5151 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005152 SetRawInputAt(0, cls);
5153 }
5154
Calin Juravle52c48962014-12-16 17:02:57 +00005155
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005156 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005157
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005158 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005159 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5160 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005161 }
5162
5163 size_t ComputeHashCode() const OVERRIDE {
5164 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5165 }
5166
Calin Juravle52c48962014-12-16 17:02:57 +00005167 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005168 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5169 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005170 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005171
5172 DECLARE_INSTRUCTION(StaticFieldGet);
5173
5174 private:
5175 const FieldInfo field_info_;
5176
5177 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5178};
5179
5180class HStaticFieldSet : public HTemplateInstruction<2> {
5181 public:
5182 HStaticFieldSet(HInstruction* cls,
5183 HInstruction* value,
5184 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005185 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005186 bool is_volatile,
5187 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005188 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005189 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005190 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005191 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005192 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5193 dex_pc),
5194 field_info_(field_offset,
5195 field_type,
5196 is_volatile,
5197 field_idx,
5198 declaring_class_def_index,
5199 dex_file,
5200 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005201 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005202 SetRawInputAt(0, cls);
5203 SetRawInputAt(1, value);
5204 }
5205
Calin Juravle52c48962014-12-16 17:02:57 +00005206 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005207 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5208 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005209 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005210
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005211 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005212 bool GetValueCanBeNull() const { return value_can_be_null_; }
5213 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005214
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005215 DECLARE_INSTRUCTION(StaticFieldSet);
5216
5217 private:
5218 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005219 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005220
5221 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5222};
5223
Calin Juravlee460d1d2015-09-29 04:52:17 +01005224class HUnresolvedInstanceFieldGet : public HExpression<1> {
5225 public:
5226 HUnresolvedInstanceFieldGet(HInstruction* obj,
5227 Primitive::Type field_type,
5228 uint32_t field_index,
5229 uint32_t dex_pc)
5230 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5231 field_index_(field_index) {
5232 SetRawInputAt(0, obj);
5233 }
5234
5235 bool NeedsEnvironment() const OVERRIDE { return true; }
5236 bool CanThrow() const OVERRIDE { return true; }
5237
5238 Primitive::Type GetFieldType() const { return GetType(); }
5239 uint32_t GetFieldIndex() const { return field_index_; }
5240
5241 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5242
5243 private:
5244 const uint32_t field_index_;
5245
5246 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5247};
5248
5249class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5250 public:
5251 HUnresolvedInstanceFieldSet(HInstruction* obj,
5252 HInstruction* value,
5253 Primitive::Type field_type,
5254 uint32_t field_index,
5255 uint32_t dex_pc)
5256 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5257 field_type_(field_type),
5258 field_index_(field_index) {
5259 DCHECK_EQ(field_type, value->GetType());
5260 SetRawInputAt(0, obj);
5261 SetRawInputAt(1, value);
5262 }
5263
5264 bool NeedsEnvironment() const OVERRIDE { return true; }
5265 bool CanThrow() const OVERRIDE { return true; }
5266
5267 Primitive::Type GetFieldType() const { return field_type_; }
5268 uint32_t GetFieldIndex() const { return field_index_; }
5269
5270 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5271
5272 private:
5273 const Primitive::Type field_type_;
5274 const uint32_t field_index_;
5275
5276 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5277};
5278
5279class HUnresolvedStaticFieldGet : public HExpression<0> {
5280 public:
5281 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5282 uint32_t field_index,
5283 uint32_t dex_pc)
5284 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5285 field_index_(field_index) {
5286 }
5287
5288 bool NeedsEnvironment() const OVERRIDE { return true; }
5289 bool CanThrow() const OVERRIDE { return true; }
5290
5291 Primitive::Type GetFieldType() const { return GetType(); }
5292 uint32_t GetFieldIndex() const { return field_index_; }
5293
5294 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5295
5296 private:
5297 const uint32_t field_index_;
5298
5299 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5300};
5301
5302class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5303 public:
5304 HUnresolvedStaticFieldSet(HInstruction* value,
5305 Primitive::Type field_type,
5306 uint32_t field_index,
5307 uint32_t dex_pc)
5308 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5309 field_type_(field_type),
5310 field_index_(field_index) {
5311 DCHECK_EQ(field_type, value->GetType());
5312 SetRawInputAt(0, value);
5313 }
5314
5315 bool NeedsEnvironment() const OVERRIDE { return true; }
5316 bool CanThrow() const OVERRIDE { return true; }
5317
5318 Primitive::Type GetFieldType() const { return field_type_; }
5319 uint32_t GetFieldIndex() const { return field_index_; }
5320
5321 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5322
5323 private:
5324 const Primitive::Type field_type_;
5325 const uint32_t field_index_;
5326
5327 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5328};
5329
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005330// Implement the move-exception DEX instruction.
5331class HLoadException : public HExpression<0> {
5332 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005333 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5334 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005335
David Brazdilbbd733e2015-08-18 17:48:17 +01005336 bool CanBeNull() const OVERRIDE { return false; }
5337
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005338 DECLARE_INSTRUCTION(LoadException);
5339
5340 private:
5341 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5342};
5343
David Brazdilcb1c0552015-08-04 16:22:25 +01005344// Implicit part of move-exception which clears thread-local exception storage.
5345// Must not be removed because the runtime expects the TLS to get cleared.
5346class HClearException : public HTemplateInstruction<0> {
5347 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005348 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5349 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005350
5351 DECLARE_INSTRUCTION(ClearException);
5352
5353 private:
5354 DISALLOW_COPY_AND_ASSIGN(HClearException);
5355};
5356
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005357class HThrow : public HTemplateInstruction<1> {
5358 public:
5359 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005360 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005361 SetRawInputAt(0, exception);
5362 }
5363
5364 bool IsControlFlow() const OVERRIDE { return true; }
5365
5366 bool NeedsEnvironment() const OVERRIDE { return true; }
5367
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005368 bool CanThrow() const OVERRIDE { return true; }
5369
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005370
5371 DECLARE_INSTRUCTION(Throw);
5372
5373 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005374 DISALLOW_COPY_AND_ASSIGN(HThrow);
5375};
5376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005377/**
5378 * Implementation strategies for the code generator of a HInstanceOf
5379 * or `HCheckCast`.
5380 */
5381enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005382 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005383 kExactCheck, // Can do a single class compare.
5384 kClassHierarchyCheck, // Can just walk the super class chain.
5385 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5386 kInterfaceCheck, // No optimization yet when checking against an interface.
5387 kArrayObjectCheck, // Can just check if the array is not primitive.
5388 kArrayCheck // No optimization yet when checking against a generic array.
5389};
5390
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005391class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005392 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005393 HInstanceOf(HInstruction* object,
5394 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005395 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005396 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005397 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005398 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005399 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005400 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005401 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005402 SetRawInputAt(0, object);
5403 SetRawInputAt(1, constant);
5404 }
5405
5406 bool CanBeMoved() const OVERRIDE { return true; }
5407
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005408 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005409 return true;
5410 }
5411
5412 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005413 return false;
5414 }
5415
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005416 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5417
5418 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005419
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005420 // Used only in code generation.
5421 bool MustDoNullCheck() const { return must_do_null_check_; }
5422 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5423
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005424 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5425 return (check_kind == TypeCheckKind::kExactCheck)
5426 ? SideEffects::None()
5427 // Mips currently does runtime calls for any other checks.
5428 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005429 }
5430
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005431 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005432
5433 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005434 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005435 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005436
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005437 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5438};
5439
Calin Juravleb1498f62015-02-16 13:13:29 +00005440class HBoundType : public HExpression<1> {
5441 public:
David Brazdilf5552582015-12-27 13:36:12 +00005442 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005443 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005444 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5445 upper_can_be_null_(true),
5446 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005447 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005448 SetRawInputAt(0, input);
5449 }
5450
David Brazdilf5552582015-12-27 13:36:12 +00005451 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005452 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5453 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005454 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005455
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005456 void SetCanBeNull(bool can_be_null) {
5457 DCHECK(upper_can_be_null_ || !can_be_null);
5458 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005459 }
5460
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005461 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5462
Calin Juravleb1498f62015-02-16 13:13:29 +00005463 DECLARE_INSTRUCTION(BoundType);
5464
5465 private:
5466 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005467 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5468 // It is used to bound the type in cases like:
5469 // if (x instanceof ClassX) {
5470 // // uper_bound_ will be ClassX
5471 // }
David Brazdilf5552582015-12-27 13:36:12 +00005472 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005473 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5474 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005475 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005476 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005477
5478 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5479};
5480
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005481class HCheckCast : public HTemplateInstruction<2> {
5482 public:
5483 HCheckCast(HInstruction* object,
5484 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005485 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005486 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005487 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005488 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005489 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005490 SetRawInputAt(0, object);
5491 SetRawInputAt(1, constant);
5492 }
5493
5494 bool CanBeMoved() const OVERRIDE { return true; }
5495
5496 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5497 return true;
5498 }
5499
5500 bool NeedsEnvironment() const OVERRIDE {
5501 // Instruction may throw a CheckCastError.
5502 return true;
5503 }
5504
5505 bool CanThrow() const OVERRIDE { return true; }
5506
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005507 bool MustDoNullCheck() const { return must_do_null_check_; }
5508 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005509 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005510
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005511 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005512
5513 DECLARE_INSTRUCTION(CheckCast);
5514
5515 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005516 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005517 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005518
5519 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005520};
5521
Calin Juravle27df7582015-04-17 19:12:31 +01005522class HMemoryBarrier : public HTemplateInstruction<0> {
5523 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005524 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005525 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005526 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005527 barrier_kind_(barrier_kind) {}
5528
5529 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5530
5531 DECLARE_INSTRUCTION(MemoryBarrier);
5532
5533 private:
5534 const MemBarrierKind barrier_kind_;
5535
5536 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5537};
5538
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005539class HMonitorOperation : public HTemplateInstruction<1> {
5540 public:
5541 enum OperationKind {
5542 kEnter,
5543 kExit,
5544 };
5545
5546 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005547 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005548 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5549 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005550 SetRawInputAt(0, object);
5551 }
5552
5553 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005554 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5555
5556 bool CanThrow() const OVERRIDE {
5557 // Verifier guarantees that monitor-exit cannot throw.
5558 // This is important because it allows the HGraphBuilder to remove
5559 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5560 return IsEnter();
5561 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005562
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005563
5564 bool IsEnter() const { return kind_ == kEnter; }
5565
5566 DECLARE_INSTRUCTION(MonitorOperation);
5567
Calin Juravle52c48962014-12-16 17:02:57 +00005568 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005569 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005570
5571 private:
5572 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5573};
5574
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005575/**
5576 * A HInstruction used as a marker for the replacement of new + <init>
5577 * of a String to a call to a StringFactory. Only baseline will see
5578 * the node at code generation, where it will be be treated as null.
5579 * When compiling non-baseline, `HFakeString` instructions are being removed
5580 * in the instruction simplifier.
5581 */
5582class HFakeString : public HTemplateInstruction<0> {
5583 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005584 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5585 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005586
5587 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5588
5589 DECLARE_INSTRUCTION(FakeString);
5590
5591 private:
5592 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5593};
5594
Vladimir Markof9f64412015-09-02 14:05:49 +01005595class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005596 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005597 MoveOperands(Location source,
5598 Location destination,
5599 Primitive::Type type,
5600 HInstruction* instruction)
5601 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005602
5603 Location GetSource() const { return source_; }
5604 Location GetDestination() const { return destination_; }
5605
5606 void SetSource(Location value) { source_ = value; }
5607 void SetDestination(Location value) { destination_ = value; }
5608
5609 // The parallel move resolver marks moves as "in-progress" by clearing the
5610 // destination (but not the source).
5611 Location MarkPending() {
5612 DCHECK(!IsPending());
5613 Location dest = destination_;
5614 destination_ = Location::NoLocation();
5615 return dest;
5616 }
5617
5618 void ClearPending(Location dest) {
5619 DCHECK(IsPending());
5620 destination_ = dest;
5621 }
5622
5623 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005624 DCHECK(source_.IsValid() || destination_.IsInvalid());
5625 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005626 }
5627
5628 // True if this blocks a move from the given location.
5629 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005630 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005631 }
5632
5633 // A move is redundant if it's been eliminated, if its source and
5634 // destination are the same, or if its destination is unneeded.
5635 bool IsRedundant() const {
5636 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5637 }
5638
5639 // We clear both operands to indicate move that's been eliminated.
5640 void Eliminate() {
5641 source_ = destination_ = Location::NoLocation();
5642 }
5643
5644 bool IsEliminated() const {
5645 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5646 return source_.IsInvalid();
5647 }
5648
Alexey Frunze4dda3372015-06-01 18:31:49 -07005649 Primitive::Type GetType() const { return type_; }
5650
Nicolas Geoffray90218252015-04-15 11:56:51 +01005651 bool Is64BitMove() const {
5652 return Primitive::Is64BitType(type_);
5653 }
5654
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005655 HInstruction* GetInstruction() const { return instruction_; }
5656
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005657 private:
5658 Location source_;
5659 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005660 // The type this move is for.
5661 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005662 // The instruction this move is assocatied with. Null when this move is
5663 // for moving an input in the expected locations of user (including a phi user).
5664 // This is only used in debug mode, to ensure we do not connect interval siblings
5665 // in the same parallel move.
5666 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005667};
5668
Roland Levillainc9285912015-12-18 10:38:42 +00005669std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5670
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005671static constexpr size_t kDefaultNumberOfMoves = 4;
5672
5673class HParallelMove : public HTemplateInstruction<0> {
5674 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005675 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005676 : HTemplateInstruction(SideEffects::None(), dex_pc),
5677 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5678 moves_.reserve(kDefaultNumberOfMoves);
5679 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005680
Nicolas Geoffray90218252015-04-15 11:56:51 +01005681 void AddMove(Location source,
5682 Location destination,
5683 Primitive::Type type,
5684 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005685 DCHECK(source.IsValid());
5686 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005687 if (kIsDebugBuild) {
5688 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005689 for (const MoveOperands& move : moves_) {
5690 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005691 // Special case the situation where the move is for the spill slot
5692 // of the instruction.
5693 if ((GetPrevious() == instruction)
5694 || ((GetPrevious() == nullptr)
5695 && instruction->IsPhi()
5696 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005697 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005698 << "Doing parallel moves for the same instruction.";
5699 } else {
5700 DCHECK(false) << "Doing parallel moves for the same instruction.";
5701 }
5702 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005703 }
5704 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005705 for (const MoveOperands& move : moves_) {
5706 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005707 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005708 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005709 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005710 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005711 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005712 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005713 }
5714
Vladimir Marko225b6462015-09-28 12:17:40 +01005715 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005716 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005717 }
5718
Vladimir Marko225b6462015-09-28 12:17:40 +01005719 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005720
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005721 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005722
5723 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005724 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005725
5726 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5727};
5728
Mark Mendell0616ae02015-04-17 12:49:27 -04005729} // namespace art
5730
Vladimir Markob4536b72015-11-24 13:45:23 +00005731#ifdef ART_ENABLE_CODEGEN_arm
5732#include "nodes_arm.h"
5733#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005734#ifdef ART_ENABLE_CODEGEN_arm64
5735#include "nodes_arm64.h"
5736#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005737#ifdef ART_ENABLE_CODEGEN_x86
5738#include "nodes_x86.h"
5739#endif
5740
5741namespace art {
5742
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005743class HGraphVisitor : public ValueObject {
5744 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005745 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5746 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005747
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005748 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005749 virtual void VisitBasicBlock(HBasicBlock* block);
5750
Roland Levillain633021e2014-10-01 14:12:25 +01005751 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005752 void VisitInsertionOrder();
5753
Roland Levillain633021e2014-10-01 14:12:25 +01005754 // Visit the graph following dominator tree reverse post-order.
5755 void VisitReversePostOrder();
5756
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005757 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005758
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005759 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005760#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005761 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5762
5763 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5764
5765#undef DECLARE_VISIT_INSTRUCTION
5766
5767 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005768 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005769
5770 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5771};
5772
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005773class HGraphDelegateVisitor : public HGraphVisitor {
5774 public:
5775 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5776 virtual ~HGraphDelegateVisitor() {}
5777
5778 // Visit functions that delegate to to super class.
5779#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005780 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005781
5782 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5783
5784#undef DECLARE_VISIT_INSTRUCTION
5785
5786 private:
5787 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5788};
5789
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005790class HInsertionOrderIterator : public ValueObject {
5791 public:
5792 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5793
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005794 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005795 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005796 void Advance() { ++index_; }
5797
5798 private:
5799 const HGraph& graph_;
5800 size_t index_;
5801
5802 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5803};
5804
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005805class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005806 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005807 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5808 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005809 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005810 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005811
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005812 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5813 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005814 void Advance() { ++index_; }
5815
5816 private:
5817 const HGraph& graph_;
5818 size_t index_;
5819
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005820 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005821};
5822
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005823class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005824 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005825 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005826 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005827 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005828 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005829 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005830
5831 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005832 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005833 void Advance() { --index_; }
5834
5835 private:
5836 const HGraph& graph_;
5837 size_t index_;
5838
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005839 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005840};
5841
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005842class HLinearPostOrderIterator : public ValueObject {
5843 public:
5844 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005845 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005846
5847 bool Done() const { return index_ == 0; }
5848
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005849 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005850
5851 void Advance() {
5852 --index_;
5853 DCHECK_GE(index_, 0U);
5854 }
5855
5856 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005857 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005858 size_t index_;
5859
5860 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5861};
5862
5863class HLinearOrderIterator : public ValueObject {
5864 public:
5865 explicit HLinearOrderIterator(const HGraph& graph)
5866 : order_(graph.GetLinearOrder()), index_(0) {}
5867
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005868 bool Done() const { return index_ == order_.size(); }
5869 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005870 void Advance() { ++index_; }
5871
5872 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005873 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005874 size_t index_;
5875
5876 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5877};
5878
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005879// Iterator over the blocks that art part of the loop. Includes blocks part
5880// of an inner loop. The order in which the blocks are iterated is on their
5881// block id.
5882class HBlocksInLoopIterator : public ValueObject {
5883 public:
5884 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5885 : blocks_in_loop_(info.GetBlocks()),
5886 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5887 index_(0) {
5888 if (!blocks_in_loop_.IsBitSet(index_)) {
5889 Advance();
5890 }
5891 }
5892
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005893 bool Done() const { return index_ == blocks_.size(); }
5894 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005895 void Advance() {
5896 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005897 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005898 if (blocks_in_loop_.IsBitSet(index_)) {
5899 break;
5900 }
5901 }
5902 }
5903
5904 private:
5905 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005906 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005907 size_t index_;
5908
5909 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5910};
5911
Mingyao Yang3584bce2015-05-19 16:01:59 -07005912// Iterator over the blocks that art part of the loop. Includes blocks part
5913// of an inner loop. The order in which the blocks are iterated is reverse
5914// post order.
5915class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5916 public:
5917 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5918 : blocks_in_loop_(info.GetBlocks()),
5919 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5920 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005921 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005922 Advance();
5923 }
5924 }
5925
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005926 bool Done() const { return index_ == blocks_.size(); }
5927 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005928 void Advance() {
5929 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005930 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5931 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005932 break;
5933 }
5934 }
5935 }
5936
5937 private:
5938 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005939 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005940 size_t index_;
5941
5942 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5943};
5944
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005945inline int64_t Int64FromConstant(HConstant* constant) {
5946 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5947 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5948 : constant->AsLongConstant()->GetValue();
5949}
5950
Vladimir Marko58155012015-08-19 12:49:41 +00005951inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5952 // For the purposes of the compiler, the dex files must actually be the same object
5953 // if we want to safely treat them as the same. This is especially important for JIT
5954 // as custom class loaders can open the same underlying file (or memory) multiple
5955 // times and provide different class resolution but no two class loaders should ever
5956 // use the same DexFile object - doing so is an unsupported hack that can lead to
5957 // all sorts of weird failures.
5958 return &lhs == &rhs;
5959}
5960
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005961#define INSTRUCTION_TYPE_CHECK(type, super) \
5962 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5963 inline const H##type* HInstruction::As##type() const { \
5964 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5965 } \
5966 inline H##type* HInstruction::As##type() { \
5967 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5968 }
5969
5970 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
5971#undef INSTRUCTION_TYPE_CHECK
5972
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005973} // namespace art
5974
5975#endif // ART_COMPILER_OPTIMIZING_NODES_H_