blob: a44d745c11ba7d5dde72f2b7109cc73529a39caf [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
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000038class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000040class HFloatConstant;
41class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000046class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010048class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010051class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053
54static const int kDefaultNumberOfBlocks = 8;
55static const int kDefaultNumberOfSuccessors = 2;
56static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010057static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000058static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000059
Calin Juravle9aec02f2014-11-18 23:06:35 +000060static constexpr uint32_t kMaxIntShiftValue = 0x1f;
61static constexpr uint64_t kMaxLongShiftValue = 0x3f;
62
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010063static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
64
Dave Allison20dfc792014-06-16 20:44:29 -070065enum IfCondition {
66 kCondEQ,
67 kCondNE,
68 kCondLT,
69 kCondLE,
70 kCondGT,
71 kCondGE,
72};
73
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010074class HInstructionList {
75 public:
76 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
77
78 void AddInstruction(HInstruction* instruction);
79 void RemoveInstruction(HInstruction* instruction);
80
David Brazdilc3d743f2015-04-22 13:40:50 +010081 // Insert `instruction` before/after an existing instruction `cursor`.
82 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
83 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
84
Roland Levillain6b469232014-09-25 10:10:38 +010085 // Return true if this list contains `instruction`.
86 bool Contains(HInstruction* instruction) const;
87
Roland Levillainccc07a92014-09-16 14:48:16 +010088 // Return true if `instruction1` is found before `instruction2` in
89 // this instruction list and false otherwise. Abort if none
90 // of these instructions is found.
91 bool FoundBefore(const HInstruction* instruction1,
92 const HInstruction* instruction2) const;
93
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000094 bool IsEmpty() const { return first_instruction_ == nullptr; }
95 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
96
97 // Update the block of all instructions to be `block`.
98 void SetBlockOfInstructions(HBasicBlock* block) const;
99
100 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
101 void Add(const HInstructionList& instruction_list);
102
David Brazdil2d7352b2015-04-20 14:52:42 +0100103 // Return the number of instructions in the list. This is an expensive operation.
104 size_t CountSize() const;
105
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100106 private:
107 HInstruction* first_instruction_;
108 HInstruction* last_instruction_;
109
110 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000111 friend class HGraph;
112 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100113 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100114 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100115
116 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
117};
118
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700120class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000121 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100122 HGraph(ArenaAllocator* arena,
123 const DexFile& dex_file,
124 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100125 bool should_generate_constructor_barrier,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100126 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100127 bool debuggable = false,
128 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000129 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000130 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100131 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100132 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700133 entry_block_(nullptr),
134 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100135 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100136 number_of_vregs_(0),
137 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000138 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400139 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000140 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000141 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100142 dex_file_(dex_file),
143 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100144 invoke_type_(invoke_type),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100145 should_generate_constructor_barrier_(should_generate_constructor_barrier),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000146 cached_null_constant_(nullptr),
147 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000148 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
149 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
150 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000151
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000152 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100153 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100154 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000155
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000156 HBasicBlock* GetEntryBlock() const { return entry_block_; }
157 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100158 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000159
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000160 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
161 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000162
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000163 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100164
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 // Try building the SSA form of this graph, with dominance computation and loop
166 // recognition. Returns whether it was successful in doing all these steps.
167 bool TryBuildingSsa() {
168 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000169 // The SSA builder requires loops to all be natural. Specifically, the dead phi
170 // elimination phase checks the consistency of the graph when doing a post-order
171 // visit for eliminating dead phis: a dead phi can only have loop header phi
172 // users remaining when being visited.
173 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000175 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 }
177
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000178 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000179 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100180 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000181
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000182 // Analyze all natural loops in this graph. Returns false if one
183 // loop is not natural, that is the header does not dominate the
184 // back edge.
185 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100186
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
188 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
189
David Brazdil2d7352b2015-04-20 14:52:42 +0100190 // Removes `block` from the graph.
191 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000192
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100193 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
194 void SimplifyLoop(HBasicBlock* header);
195
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000196 int32_t GetNextInstructionId() {
197 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000198 return current_instruction_id_++;
199 }
200
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000201 int32_t GetCurrentInstructionId() const {
202 return current_instruction_id_;
203 }
204
205 void SetCurrentInstructionId(int32_t id) {
206 current_instruction_id_ = id;
207 }
208
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100209 uint16_t GetMaximumNumberOfOutVRegs() const {
210 return maximum_number_of_out_vregs_;
211 }
212
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
214 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100215 }
216
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000217 void UpdateTemporariesVRegSlots(size_t slots) {
218 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100219 }
220
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000221 size_t GetTemporariesVRegSlots() const {
222 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100223 }
224
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100225 void SetNumberOfVRegs(uint16_t number_of_vregs) {
226 number_of_vregs_ = number_of_vregs;
227 }
228
229 uint16_t GetNumberOfVRegs() const {
230 return number_of_vregs_;
231 }
232
233 void SetNumberOfInVRegs(uint16_t value) {
234 number_of_in_vregs_ = value;
235 }
236
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100237 uint16_t GetNumberOfLocalVRegs() const {
238 return number_of_vregs_ - number_of_in_vregs_;
239 }
240
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100241 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
242 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100243 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100244
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100245 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
246 return linear_order_;
247 }
248
Mark Mendell1152c922015-04-24 17:06:35 -0400249 bool HasBoundsChecks() const {
250 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800251 }
252
Mark Mendell1152c922015-04-24 17:06:35 -0400253 void SetHasBoundsChecks(bool value) {
254 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800255 }
256
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100257 bool ShouldGenerateConstructorBarrier() const {
258 return should_generate_constructor_barrier_;
259 }
260
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000261 bool IsDebuggable() const { return debuggable_; }
262
David Brazdil8d5b8b22015-03-24 10:51:52 +0000263 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000264 // already, it is created and inserted into the graph. This method is only for
265 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000266 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000267 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000268 HIntConstant* GetIntConstant(int32_t value) {
269 return CreateConstant(value, &cached_int_constants_);
270 }
271 HLongConstant* GetLongConstant(int64_t value) {
272 return CreateConstant(value, &cached_long_constants_);
273 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000274 HFloatConstant* GetFloatConstant(float value) {
275 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
276 }
277 HDoubleConstant* GetDoubleConstant(double value) {
278 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
279 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000280
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000281 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100282
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100283 const DexFile& GetDexFile() const {
284 return dex_file_;
285 }
286
287 uint32_t GetMethodIdx() const {
288 return method_idx_;
289 }
290
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100291 InvokeType GetInvokeType() const {
292 return invoke_type_;
293 }
294
David Brazdil2d7352b2015-04-20 14:52:42 +0100295 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000296 void VisitBlockForDominatorTree(HBasicBlock* block,
297 HBasicBlock* predecessor,
298 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100299 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000300 void VisitBlockForBackEdges(HBasicBlock* block,
301 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100302 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000303 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100304 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000305
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000306 template <class InstructionType, typename ValueType>
307 InstructionType* CreateConstant(ValueType value,
308 ArenaSafeMap<ValueType, InstructionType*>* cache) {
309 // Try to find an existing constant of the given value.
310 InstructionType* constant = nullptr;
311 auto cached_constant = cache->find(value);
312 if (cached_constant != cache->end()) {
313 constant = cached_constant->second;
314 }
315
316 // If not found or previously deleted, create and cache a new instruction.
317 if (constant == nullptr || constant->GetBlock() == nullptr) {
318 constant = new (arena_) InstructionType(value);
319 cache->Overwrite(value, constant);
320 InsertConstant(constant);
321 }
322 return constant;
323 }
324
David Brazdil8d5b8b22015-03-24 10:51:52 +0000325 void InsertConstant(HConstant* instruction);
326
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000327 // Cache a float constant into the graph. This method should only be
328 // called by the SsaBuilder when creating "equivalent" instructions.
329 void CacheFloatConstant(HFloatConstant* constant);
330
331 // See CacheFloatConstant comment.
332 void CacheDoubleConstant(HDoubleConstant* constant);
333
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000335
336 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337 GrowableArray<HBasicBlock*> blocks_;
338
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100339 // List of blocks to perform a reverse post order tree traversal.
340 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000341
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100342 // List of blocks to perform a linear order tree traversal.
343 GrowableArray<HBasicBlock*> linear_order_;
344
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000345 HBasicBlock* entry_block_;
346 HBasicBlock* exit_block_;
347
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100348 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100349 uint16_t maximum_number_of_out_vregs_;
350
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100351 // The number of virtual registers in this method. Contains the parameters.
352 uint16_t number_of_vregs_;
353
354 // The number of virtual registers used by parameters of this method.
355 uint16_t number_of_in_vregs_;
356
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000357 // Number of vreg size slots that the temporaries use (used in baseline compiler).
358 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100359
Mark Mendell1152c922015-04-24 17:06:35 -0400360 // Has bounds checks. We can totally skip BCE if it's false.
361 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800362
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000363 // Indicates whether the graph should be compiled in a way that
364 // ensures full debuggability. If false, we can apply more
365 // aggressive optimizations that may limit the level of debugging.
366 const bool debuggable_;
367
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000368 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000369 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000370
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100371 // The dex file from which the method is from.
372 const DexFile& dex_file_;
373
374 // The method index in the dex file.
375 const uint32_t method_idx_;
376
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100377 // If inlined, this encodes how the callee is being invoked.
378 const InvokeType invoke_type_;
379
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100380 const bool should_generate_constructor_barrier_;
381
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000382 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000383 HNullConstant* cached_null_constant_;
384 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000385 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000386 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000387 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000388
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000389 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100390 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000392 DISALLOW_COPY_AND_ASSIGN(HGraph);
393};
394
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700395class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000396 public:
397 HLoopInformation(HBasicBlock* header, HGraph* graph)
398 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100399 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100400 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100401 // Make bit vector growable, as the number of blocks may change.
402 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100403
404 HBasicBlock* GetHeader() const {
405 return header_;
406 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000407
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000408 void SetHeader(HBasicBlock* block) {
409 header_ = block;
410 }
411
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100412 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
413 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
414 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
415
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000416 void AddBackEdge(HBasicBlock* back_edge) {
417 back_edges_.Add(back_edge);
418 }
419
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 void RemoveBackEdge(HBasicBlock* back_edge) {
421 back_edges_.Delete(back_edge);
422 }
423
David Brazdil46e2a392015-03-16 17:31:52 +0000424 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100425 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000426 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100427 }
428 return false;
429 }
430
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000431 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000432 return back_edges_.Size();
433 }
434
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100435 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100436
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100437 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
438 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100439 }
440
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100441 // Returns the lifetime position of the back edge that has the
442 // greatest lifetime position.
443 size_t GetLifetimeEnd() const;
444
445 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
446 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
447 if (back_edges_.Get(i) == existing) {
448 back_edges_.Put(i, new_back_edge);
449 return;
450 }
451 }
452 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100453 }
454
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100455 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100456 // that is the header dominates the back edge.
457 bool Populate();
458
David Brazdila4b8c212015-05-07 09:59:30 +0100459 // Reanalyzes the loop by removing loop info from its blocks and re-running
460 // Populate(). If there are no back edges left, the loop info is completely
461 // removed as well as its SuspendCheck instruction. It must be run on nested
462 // inner loops first.
463 void Update();
464
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100465 // Returns whether this loop information contains `block`.
466 // Note that this loop information *must* be populated before entering this function.
467 bool Contains(const HBasicBlock& block) const;
468
469 // Returns whether this loop information is an inner loop of `other`.
470 // Note that `other` *must* be populated before entering this function.
471 bool IsIn(const HLoopInformation& other) const;
472
473 const ArenaBitVector& GetBlocks() const { return blocks_; }
474
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000475 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000476 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000477
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000478 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100479 // Internal recursive implementation of `Populate`.
480 void PopulateRecursive(HBasicBlock* block);
481
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000482 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100483 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000486
487 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
488};
489
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100490static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100491static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100492
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000493// A block in a method. Contains the list of instructions represented
494// as a double linked list. Each block knows its predecessors and
495// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100496
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700497class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000498 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100499 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000500 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000501 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
502 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000503 loop_information_(nullptr),
504 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100505 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100506 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100507 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100508 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000509 lifetime_end_(kNoLifetime),
510 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000511
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100512 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
513 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000514 }
515
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100516 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
517 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000518 }
519
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100520 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
521 return dominated_blocks_;
522 }
523
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100524 bool IsEntryBlock() const {
525 return graph_->GetEntryBlock() == this;
526 }
527
528 bool IsExitBlock() const {
529 return graph_->GetExitBlock() == this;
530 }
531
David Brazdil46e2a392015-03-16 17:31:52 +0000532 bool IsSingleGoto() const;
533
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000534 void AddBackEdge(HBasicBlock* back_edge) {
535 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000536 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000537 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000539 loop_information_->AddBackEdge(back_edge);
540 }
541
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000542 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000543 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000544
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000545 int GetBlockId() const { return block_id_; }
546 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000547
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000548 HBasicBlock* GetDominator() const { return dominator_; }
549 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100550 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100551 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000552 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
553 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
554 if (dominated_blocks_.Get(i) == existing) {
555 dominated_blocks_.Put(i, new_block);
556 return;
557 }
558 }
559 LOG(FATAL) << "Unreachable";
560 UNREACHABLE();
561 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000562
563 int NumberOfBackEdges() const {
564 return loop_information_ == nullptr
565 ? 0
566 : loop_information_->NumberOfBackEdges();
567 }
568
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100569 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
570 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100571 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100572 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100573 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
574 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000575
576 void AddSuccessor(HBasicBlock* block) {
577 successors_.Add(block);
578 block->predecessors_.Add(this);
579 }
580
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100581 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
582 size_t successor_index = GetSuccessorIndexOf(existing);
583 DCHECK_NE(successor_index, static_cast<size_t>(-1));
584 existing->RemovePredecessor(this);
585 new_block->predecessors_.Add(this);
586 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000587 }
588
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000589 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
590 size_t predecessor_index = GetPredecessorIndexOf(existing);
591 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
592 existing->RemoveSuccessor(this);
593 new_block->successors_.Add(this);
594 predecessors_.Put(predecessor_index, new_block);
595 }
596
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100597 void RemovePredecessor(HBasicBlock* block) {
598 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100599 }
600
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000601 void RemoveSuccessor(HBasicBlock* block) {
602 successors_.Delete(block);
603 }
604
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100605 void ClearAllPredecessors() {
606 predecessors_.Reset();
607 }
608
609 void AddPredecessor(HBasicBlock* block) {
610 predecessors_.Add(block);
611 block->successors_.Add(this);
612 }
613
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100614 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100615 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100616 HBasicBlock* temp = predecessors_.Get(0);
617 predecessors_.Put(0, predecessors_.Get(1));
618 predecessors_.Put(1, temp);
619 }
620
David Brazdil769c9e52015-04-27 13:54:09 +0100621 void SwapSuccessors() {
622 DCHECK_EQ(successors_.Size(), 2u);
623 HBasicBlock* temp = successors_.Get(0);
624 successors_.Put(0, successors_.Get(1));
625 successors_.Put(1, temp);
626 }
627
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100628 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
629 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
630 if (predecessors_.Get(i) == predecessor) {
631 return i;
632 }
633 }
634 return -1;
635 }
636
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100637 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
638 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
639 if (successors_.Get(i) == successor) {
640 return i;
641 }
642 }
643 return -1;
644 }
645
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000646 // Split the block into two blocks just after `cursor`. Returns the newly
647 // created block. Note that this method just updates raw block information,
648 // like predecessors, successors, dominators, and instruction list. It does not
649 // update the graph, reverse post order, loop information, nor make sure the
650 // blocks are consistent (for example ending with a control flow instruction).
651 HBasicBlock* SplitAfter(HInstruction* cursor);
652
653 // Merge `other` at the end of `this`. Successors and dominated blocks of
654 // `other` are changed to be successors and dominated blocks of `this`. Note
655 // that this method does not update the graph, reverse post order, loop
656 // information, nor make sure the blocks are consistent (for example ending
657 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100658 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000659
660 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
661 // of `this` are moved to `other`.
662 // Note that this method does not update the graph, reverse post order, loop
663 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000664 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000665 void ReplaceWith(HBasicBlock* other);
666
David Brazdil2d7352b2015-04-20 14:52:42 +0100667 // Merge `other` at the end of `this`. This method updates loops, reverse post
668 // order, links to predecessors, successors, dominators and deletes the block
669 // from the graph. The two blocks must be successive, i.e. `this` the only
670 // predecessor of `other` and vice versa.
671 void MergeWith(HBasicBlock* other);
672
673 // Disconnects `this` from all its predecessors, successors and dominator,
674 // removes it from all loops it is included in and eventually from the graph.
675 // The block must not dominate any other block. Predecessors and successors
676 // are safely updated.
677 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000678
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000679 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100680 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100681 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100682 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100683 // Replace instruction `initial` with `replacement` within this block.
684 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
685 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100686 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100687 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000688 // RemoveInstruction and RemovePhi delete a given instruction from the respective
689 // instruction list. With 'ensure_safety' set to true, it verifies that the
690 // instruction is not in use and removes it from the use lists of its inputs.
691 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
692 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100693 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694
695 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100696 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100697 }
698
Roland Levillain6b879dd2014-09-22 17:13:44 +0100699 bool IsLoopPreHeaderFirstPredecessor() const {
700 DCHECK(IsLoopHeader());
701 DCHECK(!GetPredecessors().IsEmpty());
702 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
703 }
704
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100705 HLoopInformation* GetLoopInformation() const {
706 return loop_information_;
707 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000708
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000709 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100710 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000711 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100712 void SetInLoop(HLoopInformation* info) {
713 if (IsLoopHeader()) {
714 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100715 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100716 loop_information_ = info;
717 } else if (loop_information_->Contains(*info->GetHeader())) {
718 // Block is currently part of an outer loop. Make it part of this inner loop.
719 // Note that a non loop header having a loop information means this loop information
720 // has already been populated
721 loop_information_ = info;
722 } else {
723 // Block is part of an inner loop. Do not update the loop information.
724 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
725 // at this point, because this method is being called while populating `info`.
726 }
727 }
728
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000729 // Raw update of the loop information.
730 void SetLoopInformation(HLoopInformation* info) {
731 loop_information_ = info;
732 }
733
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100734 bool IsInLoop() const { return loop_information_ != nullptr; }
735
David Brazdila4b8c212015-05-07 09:59:30 +0100736 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100737 bool Dominates(HBasicBlock* block) const;
738
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100739 size_t GetLifetimeStart() const { return lifetime_start_; }
740 size_t GetLifetimeEnd() const { return lifetime_end_; }
741
742 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
743 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
744
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100745 uint32_t GetDexPc() const { return dex_pc_; }
746
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000747 bool IsCatchBlock() const { return is_catch_block_; }
748 void SetIsCatchBlock() { is_catch_block_ = true; }
749
David Brazdil8d5b8b22015-03-24 10:51:52 +0000750 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000751 bool EndsWithIf() const;
752 bool HasSinglePhi() const;
753
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000754 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000755 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000756 GrowableArray<HBasicBlock*> predecessors_;
757 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100758 HInstructionList instructions_;
759 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000760 HLoopInformation* loop_information_;
761 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100762 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000763 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100764 // The dex program counter of the first instruction of this block.
765 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100766 size_t lifetime_start_;
767 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000768 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000769
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000770 friend class HGraph;
771 friend class HInstruction;
772
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000773 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
774};
775
David Brazdilb2bd1c52015-03-25 11:17:37 +0000776// Iterates over the LoopInformation of all loops which contain 'block'
777// from the innermost to the outermost.
778class HLoopInformationOutwardIterator : public ValueObject {
779 public:
780 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
781 : current_(block.GetLoopInformation()) {}
782
783 bool Done() const { return current_ == nullptr; }
784
785 void Advance() {
786 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100787 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000788 }
789
790 HLoopInformation* Current() const {
791 DCHECK(!Done());
792 return current_;
793 }
794
795 private:
796 HLoopInformation* current_;
797
798 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
799};
800
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100801#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
802 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000803 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000804 M(ArrayGet, Instruction) \
805 M(ArrayLength, Instruction) \
806 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100807 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000808 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000809 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000810 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100811 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000812 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100813 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700814 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000815 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000816 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000817 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100818 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000819 M(Exit, Instruction) \
820 M(FloatConstant, Constant) \
821 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100822 M(GreaterThan, Condition) \
823 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100824 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000825 M(InstanceFieldGet, Instruction) \
826 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000827 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100828 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000829 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000830 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100831 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000832 M(LessThan, Condition) \
833 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000834 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000835 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100836 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000837 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100838 M(Local, Instruction) \
839 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100840 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000841 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000842 M(Mul, BinaryOperation) \
843 M(Neg, UnaryOperation) \
844 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100845 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100846 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000847 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000848 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000849 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000850 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100851 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000852 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100853 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000854 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100855 M(Return, Instruction) \
856 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000857 M(Shl, BinaryOperation) \
858 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100859 M(StaticFieldGet, Instruction) \
860 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100861 M(StoreLocal, Instruction) \
862 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100863 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000864 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000865 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000866 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000867 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000868 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100870#define FOR_EACH_INSTRUCTION(M) \
871 FOR_EACH_CONCRETE_INSTRUCTION(M) \
872 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100873 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100874 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100875 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700876
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100877#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000878FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
879#undef FORWARD_DECLARATION
880
Roland Levillainccc07a92014-09-16 14:48:16 +0100881#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000882 InstructionKind GetKind() const OVERRIDE { return k##type; } \
883 const char* DebugName() const OVERRIDE { return #type; } \
884 const H##type* As##type() const OVERRIDE { return this; } \
885 H##type* As##type() OVERRIDE { return this; } \
886 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100887 return other->Is##type(); \
888 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000889 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000890
David Brazdiled596192015-01-23 10:39:45 +0000891template <typename T> class HUseList;
892
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100893template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700894class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000895 public:
David Brazdiled596192015-01-23 10:39:45 +0000896 HUseListNode* GetPrevious() const { return prev_; }
897 HUseListNode* GetNext() const { return next_; }
898 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100899 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100900 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100901
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000902 private:
David Brazdiled596192015-01-23 10:39:45 +0000903 HUseListNode(T user, size_t index)
904 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
905
906 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100907 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000908 HUseListNode<T>* prev_;
909 HUseListNode<T>* next_;
910
911 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000912
913 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
914};
915
David Brazdiled596192015-01-23 10:39:45 +0000916template <typename T>
917class HUseList : public ValueObject {
918 public:
919 HUseList() : first_(nullptr) {}
920
921 void Clear() {
922 first_ = nullptr;
923 }
924
925 // Adds a new entry at the beginning of the use list and returns
926 // the newly created node.
927 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000928 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000929 if (IsEmpty()) {
930 first_ = new_node;
931 } else {
932 first_->prev_ = new_node;
933 new_node->next_ = first_;
934 first_ = new_node;
935 }
936 return new_node;
937 }
938
939 HUseListNode<T>* GetFirst() const {
940 return first_;
941 }
942
943 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000944 DCHECK(node != nullptr);
945 DCHECK(Contains(node));
946
David Brazdiled596192015-01-23 10:39:45 +0000947 if (node->prev_ != nullptr) {
948 node->prev_->next_ = node->next_;
949 }
950 if (node->next_ != nullptr) {
951 node->next_->prev_ = node->prev_;
952 }
953 if (node == first_) {
954 first_ = node->next_;
955 }
956 }
957
David Brazdil1abb4192015-02-17 18:33:36 +0000958 bool Contains(const HUseListNode<T>* node) const {
959 if (node == nullptr) {
960 return false;
961 }
962 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
963 if (current == node) {
964 return true;
965 }
966 }
967 return false;
968 }
969
David Brazdiled596192015-01-23 10:39:45 +0000970 bool IsEmpty() const {
971 return first_ == nullptr;
972 }
973
974 bool HasOnlyOneUse() const {
975 return first_ != nullptr && first_->next_ == nullptr;
976 }
977
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100978 size_t SizeSlow() const {
979 size_t count = 0;
980 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
981 ++count;
982 }
983 return count;
984 }
985
David Brazdiled596192015-01-23 10:39:45 +0000986 private:
987 HUseListNode<T>* first_;
988};
989
990template<typename T>
991class HUseIterator : public ValueObject {
992 public:
993 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
994
995 bool Done() const { return current_ == nullptr; }
996
997 void Advance() {
998 DCHECK(!Done());
999 current_ = current_->GetNext();
1000 }
1001
1002 HUseListNode<T>* Current() const {
1003 DCHECK(!Done());
1004 return current_;
1005 }
1006
1007 private:
1008 HUseListNode<T>* current_;
1009
1010 friend class HValue;
1011};
1012
David Brazdil1abb4192015-02-17 18:33:36 +00001013// This class is used by HEnvironment and HInstruction classes to record the
1014// instructions they use and pointers to the corresponding HUseListNodes kept
1015// by the used instructions.
1016template <typename T>
1017class HUserRecord : public ValueObject {
1018 public:
1019 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1020 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1021
1022 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1023 : instruction_(old_record.instruction_), use_node_(use_node) {
1024 DCHECK(instruction_ != nullptr);
1025 DCHECK(use_node_ != nullptr);
1026 DCHECK(old_record.use_node_ == nullptr);
1027 }
1028
1029 HInstruction* GetInstruction() const { return instruction_; }
1030 HUseListNode<T>* GetUseNode() const { return use_node_; }
1031
1032 private:
1033 // Instruction used by the user.
1034 HInstruction* instruction_;
1035
1036 // Corresponding entry in the use list kept by 'instruction_'.
1037 HUseListNode<T>* use_node_;
1038};
1039
Calin Juravle27df7582015-04-17 19:12:31 +01001040// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1041// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1042// flag is consider.
1043// - DependsOn suggests that there is a real dependency between side effects but it only
1044// checks DependendsOnSomething flag.
1045//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001046// Represents the side effects an instruction may have.
1047class SideEffects : public ValueObject {
1048 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001049 SideEffects() : flags_(0) {}
1050
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001051 static SideEffects None() {
1052 return SideEffects(0);
1053 }
1054
1055 static SideEffects All() {
1056 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1057 }
1058
1059 static SideEffects ChangesSomething() {
1060 return SideEffects((1 << kFlagChangesCount) - 1);
1061 }
1062
1063 static SideEffects DependsOnSomething() {
1064 int count = kFlagDependsOnCount - kFlagChangesCount;
1065 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1066 }
1067
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001068 SideEffects Union(SideEffects other) const {
1069 return SideEffects(flags_ | other.flags_);
1070 }
1071
Roland Levillain72bceff2014-09-15 18:29:00 +01001072 bool HasSideEffects() const {
1073 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1074 return (flags_ & all_bits_set) != 0;
1075 }
1076
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001077 bool HasAllSideEffects() const {
1078 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1079 return all_bits_set == (flags_ & all_bits_set);
1080 }
1081
1082 bool DependsOn(SideEffects other) const {
1083 size_t depends_flags = other.ComputeDependsFlags();
1084 return (flags_ & depends_flags) != 0;
1085 }
1086
1087 bool HasDependencies() const {
1088 int count = kFlagDependsOnCount - kFlagChangesCount;
1089 size_t all_bits_set = (1 << count) - 1;
1090 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1091 }
1092
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001093 private:
1094 static constexpr int kFlagChangesSomething = 0;
1095 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1096
1097 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1098 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1099
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001100 explicit SideEffects(size_t flags) : flags_(flags) {}
1101
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001102 size_t ComputeDependsFlags() const {
1103 return flags_ << kFlagChangesCount;
1104 }
1105
1106 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001107};
1108
David Brazdiled596192015-01-23 10:39:45 +00001109// A HEnvironment object contains the values of virtual registers at a given location.
1110class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1111 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001112 HEnvironment(ArenaAllocator* arena,
1113 size_t number_of_vregs,
1114 const DexFile& dex_file,
1115 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001116 uint32_t dex_pc,
1117 InvokeType invoke_type)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001118 : vregs_(arena, number_of_vregs),
1119 locations_(arena, number_of_vregs),
1120 parent_(nullptr),
1121 dex_file_(dex_file),
1122 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001123 dex_pc_(dex_pc),
1124 invoke_type_(invoke_type) {
David Brazdiled596192015-01-23 10:39:45 +00001125 vregs_.SetSize(number_of_vregs);
1126 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001127 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001128 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001129
1130 locations_.SetSize(number_of_vregs);
1131 for (size_t i = 0; i < number_of_vregs; ++i) {
1132 locations_.Put(i, Location());
1133 }
1134 }
1135
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001136 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy)
1137 : HEnvironment(arena,
1138 to_copy.Size(),
1139 to_copy.GetDexFile(),
1140 to_copy.GetMethodIdx(),
1141 to_copy.GetDexPc(),
1142 to_copy.GetInvokeType()) {}
1143
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001144 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001145 parent_ = new (allocator) HEnvironment(allocator, *parent);
1146 parent_->CopyFrom(parent);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001147 if (parent->GetParent() != nullptr) {
1148 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1149 }
David Brazdiled596192015-01-23 10:39:45 +00001150 }
1151
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001152 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1153 void CopyFrom(HEnvironment* environment);
1154
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001155 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1156 // input to the loop phi instead. This is for inserting instructions that
1157 // require an environment (like HDeoptimization) in the loop pre-header.
1158 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001159
1160 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001161 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001162 }
1163
1164 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001165 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001166 }
1167
David Brazdil1abb4192015-02-17 18:33:36 +00001168 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001169
1170 size_t Size() const { return vregs_.Size(); }
1171
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001172 HEnvironment* GetParent() const { return parent_; }
1173
1174 void SetLocationAt(size_t index, Location location) {
1175 locations_.Put(index, location);
1176 }
1177
1178 Location GetLocationAt(size_t index) const {
1179 return locations_.Get(index);
1180 }
1181
1182 uint32_t GetDexPc() const {
1183 return dex_pc_;
1184 }
1185
1186 uint32_t GetMethodIdx() const {
1187 return method_idx_;
1188 }
1189
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001190 InvokeType GetInvokeType() const {
1191 return invoke_type_;
1192 }
1193
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001194 const DexFile& GetDexFile() const {
1195 return dex_file_;
1196 }
1197
David Brazdiled596192015-01-23 10:39:45 +00001198 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001199 // Record instructions' use entries of this environment for constant-time removal.
1200 // It should only be called by HInstruction when a new environment use is added.
1201 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1202 DCHECK(env_use->GetUser() == this);
1203 size_t index = env_use->GetIndex();
1204 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1205 }
David Brazdiled596192015-01-23 10:39:45 +00001206
David Brazdil1abb4192015-02-17 18:33:36 +00001207 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001208 GrowableArray<Location> locations_;
1209 HEnvironment* parent_;
1210 const DexFile& dex_file_;
1211 const uint32_t method_idx_;
1212 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001213 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001214
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001215 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001216
1217 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1218};
1219
Calin Juravleacf735c2015-02-12 15:25:22 +00001220class ReferenceTypeInfo : ValueObject {
1221 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001222 typedef Handle<mirror::Class> TypeHandle;
1223
1224 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1226 if (type_handle->IsObjectClass()) {
1227 // Override the type handle to be consistent with the case when we get to
1228 // Top but don't have the Object class available. It avoids having to guess
1229 // what value the type_handle has when it's Top.
1230 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1231 } else {
1232 return ReferenceTypeInfo(type_handle, is_exact, false);
1233 }
1234 }
1235
1236 static ReferenceTypeInfo CreateTop(bool is_exact) {
1237 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001238 }
1239
1240 bool IsExact() const { return is_exact_; }
1241 bool IsTop() const { return is_top_; }
1242
1243 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1244
Calin Juravleb1498f62015-02-16 13:13:29 +00001245 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001246 if (IsTop()) {
1247 // Top (equivalent for java.lang.Object) is supertype of anything.
1248 return true;
1249 }
1250 if (rti.IsTop()) {
1251 // If we get here `this` is not Top() so it can't be a supertype.
1252 return false;
1253 }
1254 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1255 }
1256
1257 // Returns true if the type information provide the same amount of details.
1258 // Note that it does not mean that the instructions have the same actual type
1259 // (e.g. tops are equal but they can be the result of a merge).
1260 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1261 if (IsExact() != rti.IsExact()) {
1262 return false;
1263 }
1264 if (IsTop() && rti.IsTop()) {
1265 // `Top` means java.lang.Object, so the types are equivalent.
1266 return true;
1267 }
1268 if (IsTop() || rti.IsTop()) {
1269 // If only one is top or object than they are not equivalent.
1270 // NB: We need this extra check because the type_handle of `Top` is invalid
1271 // and we cannot inspect its reference.
1272 return false;
1273 }
1274
1275 // Finally check the types.
1276 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1277 }
1278
1279 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001280 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1281 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1282 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1283
Calin Juravleacf735c2015-02-12 15:25:22 +00001284 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001285 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001286 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001287 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001288 bool is_exact_;
1289 // A true value here means that the object type should be java.lang.Object.
1290 // We don't have access to the corresponding mirror object every time so this
1291 // flag acts as a substitute. When true, the TypeHandle refers to a null
1292 // pointer and should not be used.
1293 bool is_top_;
1294};
1295
1296std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1297
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001298class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001299 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001300 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001301 : previous_(nullptr),
1302 next_(nullptr),
1303 block_(nullptr),
1304 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001305 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001306 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001307 locations_(nullptr),
1308 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001309 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001310 side_effects_(side_effects),
1311 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001312
Dave Allison20dfc792014-06-16 20:44:29 -07001313 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001314
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001315#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001316 enum InstructionKind {
1317 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1318 };
1319#undef DECLARE_KIND
1320
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001321 HInstruction* GetNext() const { return next_; }
1322 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001323
Calin Juravle77520bc2015-01-12 18:45:46 +00001324 HInstruction* GetNextDisregardingMoves() const;
1325 HInstruction* GetPreviousDisregardingMoves() const;
1326
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001327 HBasicBlock* GetBlock() const { return block_; }
1328 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001329 bool IsInBlock() const { return block_ != nullptr; }
1330 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001331 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001332
Roland Levillain6b879dd2014-09-22 17:13:44 +01001333 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001334 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001335
1336 virtual void Accept(HGraphVisitor* visitor) = 0;
1337 virtual const char* DebugName() const = 0;
1338
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001339 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001340 void SetRawInputAt(size_t index, HInstruction* input) {
1341 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1342 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001343
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001344 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001345 virtual uint32_t GetDexPc() const {
1346 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1347 " does not need an environment";
1348 UNREACHABLE();
1349 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001350 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001351 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001352 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353
Calin Juravle10e244f2015-01-26 18:54:32 +00001354 // Does not apply for all instructions, but having this at top level greatly
1355 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001356 virtual bool CanBeNull() const {
1357 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1358 return true;
1359 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001360
Calin Juravle641547a2015-04-21 22:08:51 +01001361 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1362 UNUSED(obj);
1363 return false;
1364 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001365
Calin Juravleacf735c2015-02-12 15:25:22 +00001366 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001367 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001368 reference_type_info_ = reference_type_info;
1369 }
1370
Calin Juravle61d544b2015-02-23 16:46:57 +00001371 ReferenceTypeInfo GetReferenceTypeInfo() const {
1372 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1373 return reference_type_info_;
1374 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001375
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001376 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001377 DCHECK(user != nullptr);
1378 HUseListNode<HInstruction*>* use =
1379 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1380 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001381 }
1382
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001383 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001384 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001385 HUseListNode<HEnvironment*>* env_use =
1386 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1387 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001388 }
1389
David Brazdil1abb4192015-02-17 18:33:36 +00001390 void RemoveAsUserOfInput(size_t input) {
1391 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1392 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1393 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001394
David Brazdil1abb4192015-02-17 18:33:36 +00001395 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1396 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001397
David Brazdiled596192015-01-23 10:39:45 +00001398 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1399 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001400 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001401 bool HasOnlyOneNonEnvironmentUse() const {
1402 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1403 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001404
Roland Levillain6c82d402014-10-13 16:10:27 +01001405 // Does this instruction strictly dominate `other_instruction`?
1406 // Returns false if this instruction and `other_instruction` are the same.
1407 // Aborts if this instruction and `other_instruction` are both phis.
1408 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001409
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001410 int GetId() const { return id_; }
1411 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001412
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001413 int GetSsaIndex() const { return ssa_index_; }
1414 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1415 bool HasSsaIndex() const { return ssa_index_ != -1; }
1416
1417 bool HasEnvironment() const { return environment_ != nullptr; }
1418 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001419 // Set the `environment_` field. Raw because this method does not
1420 // update the uses lists.
1421 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1422
1423 // Set the environment of this instruction, copying it from `environment`. While
1424 // copying, the uses lists are being updated.
1425 void CopyEnvironmentFrom(HEnvironment* environment) {
1426 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001427 environment_ = new (allocator) HEnvironment(allocator, *environment);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001428 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001429 if (environment->GetParent() != nullptr) {
1430 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1431 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001432 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001433
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001434 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1435 HBasicBlock* block) {
1436 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001437 environment_ = new (allocator) HEnvironment(allocator, *environment);
1438 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001439 if (environment->GetParent() != nullptr) {
1440 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1441 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001442 }
1443
Nicolas Geoffray39468442014-09-02 15:17:15 +01001444 // Returns the number of entries in the environment. Typically, that is the
1445 // number of dex registers in a method. It could be more in case of inlining.
1446 size_t EnvironmentSize() const;
1447
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001448 LocationSummary* GetLocations() const { return locations_; }
1449 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001450
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001451 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001452 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001453
Alexandre Rames188d4312015-04-09 18:30:21 +01001454 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1455 // uses of this instruction by `other` are *not* updated.
1456 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1457 ReplaceWith(other);
1458 other->ReplaceInput(this, use_index);
1459 }
1460
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001461 // Move `this` instruction before `cursor`.
1462 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001463
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001464#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001465 bool Is##type() const { return (As##type() != nullptr); } \
1466 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001467 virtual H##type* As##type() { return nullptr; }
1468
1469 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1470#undef INSTRUCTION_TYPE_CHECK
1471
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001472 // Returns whether the instruction can be moved within the graph.
1473 virtual bool CanBeMoved() const { return false; }
1474
1475 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001476 virtual bool InstructionTypeEquals(HInstruction* other) const {
1477 UNUSED(other);
1478 return false;
1479 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001480
1481 // Returns whether any data encoded in the two instructions is equal.
1482 // This method does not look at the inputs. Both instructions must be
1483 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001484 virtual bool InstructionDataEquals(HInstruction* other) const {
1485 UNUSED(other);
1486 return false;
1487 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001488
1489 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001490 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491 // 2) Their inputs are identical.
1492 bool Equals(HInstruction* other) const;
1493
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001494 virtual InstructionKind GetKind() const = 0;
1495
1496 virtual size_t ComputeHashCode() const {
1497 size_t result = GetKind();
1498 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1499 result = (result * 31) + InputAt(i)->GetId();
1500 }
1501 return result;
1502 }
1503
1504 SideEffects GetSideEffects() const { return side_effects_; }
1505
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001506 size_t GetLifetimePosition() const { return lifetime_position_; }
1507 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1508 LiveInterval* GetLiveInterval() const { return live_interval_; }
1509 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1510 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1511
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001512 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1513
1514 // Returns whether the code generation of the instruction will require to have access
1515 // to the current method. Such instructions are:
1516 // (1): Instructions that require an environment, as calling the runtime requires
1517 // to walk the stack and have the current method stored at a specific stack address.
1518 // (2): Object literals like classes and strings, that are loaded from the dex cache
1519 // fields of the current method.
1520 bool NeedsCurrentMethod() const {
1521 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1522 }
1523
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001524 virtual bool NeedsDexCache() const { return false; }
1525
David Brazdil1abb4192015-02-17 18:33:36 +00001526 protected:
1527 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1528 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1529
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001530 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001531 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1532
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001533 HInstruction* previous_;
1534 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001535 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001536
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001537 // An instruction gets an id when it is added to the graph.
1538 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001539 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001540 int id_;
1541
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001542 // When doing liveness analysis, instructions that have uses get an SSA index.
1543 int ssa_index_;
1544
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001545 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001546 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001547
1548 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001549 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550
Nicolas Geoffray39468442014-09-02 15:17:15 +01001551 // The environment associated with this instruction. Not null if the instruction
1552 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001553 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001554
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001555 // Set by the code generator.
1556 LocationSummary* locations_;
1557
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001558 // Set by the liveness analysis.
1559 LiveInterval* live_interval_;
1560
1561 // Set by the liveness analysis, this is the position in a linear
1562 // order of blocks where this instruction's live interval start.
1563 size_t lifetime_position_;
1564
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001565 const SideEffects side_effects_;
1566
Calin Juravleacf735c2015-02-12 15:25:22 +00001567 // TODO: for primitive types this should be marked as invalid.
1568 ReferenceTypeInfo reference_type_info_;
1569
David Brazdil1abb4192015-02-17 18:33:36 +00001570 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001571 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001572 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001573 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001574 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001575
1576 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1577};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001578std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001579
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001580class HInputIterator : public ValueObject {
1581 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001582 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001583
1584 bool Done() const { return index_ == instruction_->InputCount(); }
1585 HInstruction* Current() const { return instruction_->InputAt(index_); }
1586 void Advance() { index_++; }
1587
1588 private:
1589 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001590 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001591
1592 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1593};
1594
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001595class HInstructionIterator : public ValueObject {
1596 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001597 explicit HInstructionIterator(const HInstructionList& instructions)
1598 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001599 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001600 }
1601
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001602 bool Done() const { return instruction_ == nullptr; }
1603 HInstruction* Current() const { return instruction_; }
1604 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001605 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001606 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001607 }
1608
1609 private:
1610 HInstruction* instruction_;
1611 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001612
1613 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001614};
1615
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001616class HBackwardInstructionIterator : public ValueObject {
1617 public:
1618 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1619 : instruction_(instructions.last_instruction_) {
1620 next_ = Done() ? nullptr : instruction_->GetPrevious();
1621 }
1622
1623 bool Done() const { return instruction_ == nullptr; }
1624 HInstruction* Current() const { return instruction_; }
1625 void Advance() {
1626 instruction_ = next_;
1627 next_ = Done() ? nullptr : instruction_->GetPrevious();
1628 }
1629
1630 private:
1631 HInstruction* instruction_;
1632 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001633
1634 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001635};
1636
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001637// An embedded container with N elements of type T. Used (with partial
1638// specialization for N=0) because embedded arrays cannot have size 0.
1639template<typename T, intptr_t N>
1640class EmbeddedArray {
1641 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001642 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001643
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001644 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001645
1646 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001647 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001648 return elements_[i];
1649 }
1650
1651 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001652 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001653 return elements_[i];
1654 }
1655
1656 const T& At(intptr_t i) const {
1657 return (*this)[i];
1658 }
1659
1660 void SetAt(intptr_t i, const T& val) {
1661 (*this)[i] = val;
1662 }
1663
1664 private:
1665 T elements_[N];
1666};
1667
1668template<typename T>
1669class EmbeddedArray<T, 0> {
1670 public:
1671 intptr_t length() const { return 0; }
1672 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001673 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001674 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001675 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001676 }
1677 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001678 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001679 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001680 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001681 }
1682};
1683
1684template<intptr_t N>
1685class HTemplateInstruction: public HInstruction {
1686 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001687 HTemplateInstruction<N>(SideEffects side_effects)
1688 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001689 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001690
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001691 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001692
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001693 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001694 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1695
1696 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1697 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001698 }
1699
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001700 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001701 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001702
1703 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001704};
1705
Dave Allison20dfc792014-06-16 20:44:29 -07001706template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001707class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001708 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001709 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1710 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001711 virtual ~HExpression() {}
1712
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001713 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001714
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001715 protected:
1716 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001717};
1718
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001719// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1720// instruction that branches to the exit block.
1721class HReturnVoid : public HTemplateInstruction<0> {
1722 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001723 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001724
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001725 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001726
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001727 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001728
1729 private:
1730 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1731};
1732
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001733// Represents dex's RETURN opcodes. A HReturn is a control flow
1734// instruction that branches to the exit block.
1735class HReturn : public HTemplateInstruction<1> {
1736 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001737 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001738 SetRawInputAt(0, value);
1739 }
1740
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001741 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001742
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001743 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001744
1745 private:
1746 DISALLOW_COPY_AND_ASSIGN(HReturn);
1747};
1748
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001749// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001750// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001751// exit block.
1752class HExit : public HTemplateInstruction<0> {
1753 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001754 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001755
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001756 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001757
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001758 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001759
1760 private:
1761 DISALLOW_COPY_AND_ASSIGN(HExit);
1762};
1763
1764// Jumps from one block to another.
1765class HGoto : public HTemplateInstruction<0> {
1766 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001767 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1768
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001769 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001770
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001771 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001772 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001773 }
1774
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001775 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001776
1777 private:
1778 DISALLOW_COPY_AND_ASSIGN(HGoto);
1779};
1780
Dave Allison20dfc792014-06-16 20:44:29 -07001781
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001782// Conditional branch. A block ending with an HIf instruction must have
1783// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001784class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001785 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001786 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001787 SetRawInputAt(0, input);
1788 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001789
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001790 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001791
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001792 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001793 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001794 }
1795
1796 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001797 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001798 }
1799
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001800 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001801
1802 private:
1803 DISALLOW_COPY_AND_ASSIGN(HIf);
1804};
1805
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001806// Deoptimize to interpreter, upon checking a condition.
1807class HDeoptimize : public HTemplateInstruction<1> {
1808 public:
1809 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1810 : HTemplateInstruction(SideEffects::None()),
1811 dex_pc_(dex_pc) {
1812 SetRawInputAt(0, cond);
1813 }
1814
1815 bool NeedsEnvironment() const OVERRIDE { return true; }
1816 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001817 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001818
1819 DECLARE_INSTRUCTION(Deoptimize);
1820
1821 private:
1822 uint32_t dex_pc_;
1823
1824 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1825};
1826
Roland Levillain88cb1752014-10-20 16:36:47 +01001827class HUnaryOperation : public HExpression<1> {
1828 public:
1829 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1830 : HExpression(result_type, SideEffects::None()) {
1831 SetRawInputAt(0, input);
1832 }
1833
1834 HInstruction* GetInput() const { return InputAt(0); }
1835 Primitive::Type GetResultType() const { return GetType(); }
1836
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001837 bool CanBeMoved() const OVERRIDE { return true; }
1838 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001839 UNUSED(other);
1840 return true;
1841 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001842
Roland Levillain9240d6a2014-10-20 16:47:04 +01001843 // Try to statically evaluate `operation` and return a HConstant
1844 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001845 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001846 HConstant* TryStaticEvaluation() const;
1847
1848 // Apply this operation to `x`.
1849 virtual int32_t Evaluate(int32_t x) const = 0;
1850 virtual int64_t Evaluate(int64_t x) const = 0;
1851
Roland Levillain88cb1752014-10-20 16:36:47 +01001852 DECLARE_INSTRUCTION(UnaryOperation);
1853
1854 private:
1855 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1856};
1857
Dave Allison20dfc792014-06-16 20:44:29 -07001858class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001859 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001860 HBinaryOperation(Primitive::Type result_type,
1861 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001862 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001863 SetRawInputAt(0, left);
1864 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001865 }
1866
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001867 HInstruction* GetLeft() const { return InputAt(0); }
1868 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001869 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001870
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001871 virtual bool IsCommutative() const { return false; }
1872
1873 // Put constant on the right.
1874 // Returns whether order is changed.
1875 bool OrderInputsWithConstantOnTheRight() {
1876 HInstruction* left = InputAt(0);
1877 HInstruction* right = InputAt(1);
1878 if (left->IsConstant() && !right->IsConstant()) {
1879 ReplaceInput(right, 0);
1880 ReplaceInput(left, 1);
1881 return true;
1882 }
1883 return false;
1884 }
1885
1886 // Order inputs by instruction id, but favor constant on the right side.
1887 // This helps GVN for commutative ops.
1888 void OrderInputs() {
1889 DCHECK(IsCommutative());
1890 HInstruction* left = InputAt(0);
1891 HInstruction* right = InputAt(1);
1892 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1893 return;
1894 }
1895 if (OrderInputsWithConstantOnTheRight()) {
1896 return;
1897 }
1898 // Order according to instruction id.
1899 if (left->GetId() > right->GetId()) {
1900 ReplaceInput(right, 0);
1901 ReplaceInput(left, 1);
1902 }
1903 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001904
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001905 bool CanBeMoved() const OVERRIDE { return true; }
1906 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001907 UNUSED(other);
1908 return true;
1909 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001910
Roland Levillain9240d6a2014-10-20 16:47:04 +01001911 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001912 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001913 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001914 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001915
1916 // Apply this operation to `x` and `y`.
1917 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1918 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1919
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001920 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001921 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001922 HConstant* GetConstantRight() const;
1923
1924 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001925 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001926 HInstruction* GetLeastConstantLeft() const;
1927
Roland Levillainccc07a92014-09-16 14:48:16 +01001928 DECLARE_INSTRUCTION(BinaryOperation);
1929
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001930 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001931 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1932};
1933
Dave Allison20dfc792014-06-16 20:44:29 -07001934class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001935 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001936 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001937 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1938 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001939
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001940 bool NeedsMaterialization() const { return needs_materialization_; }
1941 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001942
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001943 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001944 // `instruction`, and disregard moves in between.
1945 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001946
Dave Allison20dfc792014-06-16 20:44:29 -07001947 DECLARE_INSTRUCTION(Condition);
1948
1949 virtual IfCondition GetCondition() const = 0;
1950
1951 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001952 // For register allocation purposes, returns whether this instruction needs to be
1953 // materialized (that is, not just be in the processor flags).
1954 bool needs_materialization_;
1955
Dave Allison20dfc792014-06-16 20:44:29 -07001956 DISALLOW_COPY_AND_ASSIGN(HCondition);
1957};
1958
1959// Instruction to check if two inputs are equal to each other.
1960class HEqual : public HCondition {
1961 public:
1962 HEqual(HInstruction* first, HInstruction* second)
1963 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001964
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001965 bool IsCommutative() const OVERRIDE { return true; }
1966
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001967 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001968 return x == y ? 1 : 0;
1969 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001970 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001971 return x == y ? 1 : 0;
1972 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001973
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001974 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001975
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001976 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001977 return kCondEQ;
1978 }
1979
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001980 private:
1981 DISALLOW_COPY_AND_ASSIGN(HEqual);
1982};
1983
Dave Allison20dfc792014-06-16 20:44:29 -07001984class HNotEqual : public HCondition {
1985 public:
1986 HNotEqual(HInstruction* first, HInstruction* second)
1987 : HCondition(first, second) {}
1988
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001989 bool IsCommutative() const OVERRIDE { return true; }
1990
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001991 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001992 return x != y ? 1 : 0;
1993 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001994 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001995 return x != y ? 1 : 0;
1996 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001997
Dave Allison20dfc792014-06-16 20:44:29 -07001998 DECLARE_INSTRUCTION(NotEqual);
1999
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002000 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002001 return kCondNE;
2002 }
2003
2004 private:
2005 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2006};
2007
2008class HLessThan : public HCondition {
2009 public:
2010 HLessThan(HInstruction* first, HInstruction* second)
2011 : HCondition(first, second) {}
2012
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002013 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002014 return x < y ? 1 : 0;
2015 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002016 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002017 return x < y ? 1 : 0;
2018 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002019
Dave Allison20dfc792014-06-16 20:44:29 -07002020 DECLARE_INSTRUCTION(LessThan);
2021
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002022 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002023 return kCondLT;
2024 }
2025
2026 private:
2027 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2028};
2029
2030class HLessThanOrEqual : public HCondition {
2031 public:
2032 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2033 : HCondition(first, second) {}
2034
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002035 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002036 return x <= y ? 1 : 0;
2037 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002038 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002039 return x <= y ? 1 : 0;
2040 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002041
Dave Allison20dfc792014-06-16 20:44:29 -07002042 DECLARE_INSTRUCTION(LessThanOrEqual);
2043
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002044 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002045 return kCondLE;
2046 }
2047
2048 private:
2049 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2050};
2051
2052class HGreaterThan : public HCondition {
2053 public:
2054 HGreaterThan(HInstruction* first, HInstruction* second)
2055 : HCondition(first, second) {}
2056
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002057 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002058 return x > y ? 1 : 0;
2059 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002060 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002061 return x > y ? 1 : 0;
2062 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002063
Dave Allison20dfc792014-06-16 20:44:29 -07002064 DECLARE_INSTRUCTION(GreaterThan);
2065
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002066 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002067 return kCondGT;
2068 }
2069
2070 private:
2071 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2072};
2073
2074class HGreaterThanOrEqual : public HCondition {
2075 public:
2076 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2077 : HCondition(first, second) {}
2078
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002079 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002080 return x >= y ? 1 : 0;
2081 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002082 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002083 return x >= y ? 1 : 0;
2084 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002085
Dave Allison20dfc792014-06-16 20:44:29 -07002086 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2087
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002088 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002089 return kCondGE;
2090 }
2091
2092 private:
2093 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2094};
2095
2096
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002097// Instruction to check how two inputs compare to each other.
2098// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2099class HCompare : public HBinaryOperation {
2100 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002101 // The bias applies for floating point operations and indicates how NaN
2102 // comparisons are treated:
2103 enum Bias {
2104 kNoBias, // bias is not applicable (i.e. for long operation)
2105 kGtBias, // return 1 for NaN comparisons
2106 kLtBias, // return -1 for NaN comparisons
2107 };
2108
2109 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
2110 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002111 DCHECK_EQ(type, first->GetType());
2112 DCHECK_EQ(type, second->GetType());
2113 }
2114
Calin Juravleddb7df22014-11-25 20:56:51 +00002115 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002116 return
2117 x == y ? 0 :
2118 x > y ? 1 :
2119 -1;
2120 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002121
2122 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002123 return
2124 x == y ? 0 :
2125 x > y ? 1 :
2126 -1;
2127 }
2128
Calin Juravleddb7df22014-11-25 20:56:51 +00002129 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2130 return bias_ == other->AsCompare()->bias_;
2131 }
2132
2133 bool IsGtBias() { return bias_ == kGtBias; }
2134
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002135 DECLARE_INSTRUCTION(Compare);
2136
2137 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002138 const Bias bias_;
2139
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002140 DISALLOW_COPY_AND_ASSIGN(HCompare);
2141};
2142
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002143// A local in the graph. Corresponds to a Dex register.
2144class HLocal : public HTemplateInstruction<0> {
2145 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002146 explicit HLocal(uint16_t reg_number)
2147 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002148
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002149 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002150
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002151 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002152
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002153 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002154 // The Dex register number.
2155 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002156
2157 DISALLOW_COPY_AND_ASSIGN(HLocal);
2158};
2159
2160// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002161class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002162 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002163 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002164 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002165 SetRawInputAt(0, local);
2166 }
2167
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002168 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2169
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002170 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002171
2172 private:
2173 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2174};
2175
2176// Store a value in a given local. This instruction has two inputs: the value
2177// and the local.
2178class HStoreLocal : public HTemplateInstruction<2> {
2179 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002180 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002181 SetRawInputAt(0, local);
2182 SetRawInputAt(1, value);
2183 }
2184
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002185 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2186
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002187 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002188
2189 private:
2190 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2191};
2192
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002193class HConstant : public HExpression<0> {
2194 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002195 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002197 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002198
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002199 virtual bool IsMinusOne() const { return false; }
2200 virtual bool IsZero() const { return false; }
2201 virtual bool IsOne() const { return false; }
2202
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002203 DECLARE_INSTRUCTION(Constant);
2204
2205 private:
2206 DISALLOW_COPY_AND_ASSIGN(HConstant);
2207};
2208
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002209class HFloatConstant : public HConstant {
2210 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002211 float GetValue() const { return value_; }
2212
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002213 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002214 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2215 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002216 }
2217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002218 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002219
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002220 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002221 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002222 }
2223 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002224 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002225 }
2226 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002227 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2228 }
2229 bool IsNaN() const {
2230 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002231 }
2232
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002233 DECLARE_INSTRUCTION(FloatConstant);
2234
2235 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002236 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002237 explicit HFloatConstant(int32_t value)
2238 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002239
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002240 const float value_;
2241
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002242 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002243 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002244 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002245 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2246};
2247
2248class HDoubleConstant : public HConstant {
2249 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002250 double GetValue() const { return value_; }
2251
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002252 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002253 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2254 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002255 }
2256
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002257 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002258
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002259 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002260 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002261 }
2262 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002263 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002264 }
2265 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002266 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2267 }
2268 bool IsNaN() const {
2269 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002270 }
2271
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002272 DECLARE_INSTRUCTION(DoubleConstant);
2273
2274 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002275 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002276 explicit HDoubleConstant(int64_t value)
2277 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002278
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002279 const double value_;
2280
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002281 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002282 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002283 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002284 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2285};
2286
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002287class HNullConstant : public HConstant {
2288 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002289 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2290 return true;
2291 }
2292
2293 size_t ComputeHashCode() const OVERRIDE { return 0; }
2294
2295 DECLARE_INSTRUCTION(NullConstant);
2296
2297 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002298 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2299
2300 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002301 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2302};
2303
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002304// Constants of the type int. Those can be from Dex instructions, or
2305// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002306class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002307 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002308 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002309
Calin Juravle61d544b2015-02-23 16:46:57 +00002310 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002311 return other->AsIntConstant()->value_ == value_;
2312 }
2313
Calin Juravle61d544b2015-02-23 16:46:57 +00002314 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2315
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002316 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2317 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2318 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2319
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002320 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002321
2322 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002323 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2324
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002325 const int32_t value_;
2326
David Brazdil8d5b8b22015-03-24 10:51:52 +00002327 friend class HGraph;
2328 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002329 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002330 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2331};
2332
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002333class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002334 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002335 int64_t GetValue() const { return value_; }
2336
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002337 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002338 return other->AsLongConstant()->value_ == value_;
2339 }
2340
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002341 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002342
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002343 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2344 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2345 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002347 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002348
2349 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002350 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2351
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002352 const int64_t value_;
2353
David Brazdil8d5b8b22015-03-24 10:51:52 +00002354 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002355 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2356};
2357
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002358enum class Intrinsics {
2359#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2360#include "intrinsics_list.h"
2361 kNone,
2362 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2363#undef INTRINSICS_LIST
2364#undef OPTIMIZING_INTRINSICS
2365};
2366std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2367
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002368class HInvoke : public HInstruction {
2369 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002370 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002371
2372 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2373 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002374 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002375
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002376 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002377 SetRawInputAt(index, argument);
2378 }
2379
Roland Levillain3e3d7332015-04-28 11:00:54 +01002380 // Return the number of arguments. This number can be lower than
2381 // the number of inputs returned by InputCount(), as some invoke
2382 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2383 // inputs at the end of their list of inputs.
2384 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2385
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002386 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002387
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002388 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002389
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002390 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002391 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002392
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002393 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2394
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002395 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002396 return intrinsic_;
2397 }
2398
2399 void SetIntrinsic(Intrinsics intrinsic) {
2400 intrinsic_ = intrinsic;
2401 }
2402
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002403 DECLARE_INSTRUCTION(Invoke);
2404
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002405 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002406 HInvoke(ArenaAllocator* arena,
2407 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002408 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002409 Primitive::Type return_type,
2410 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002411 uint32_t dex_method_index,
2412 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002413 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002414 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002415 inputs_(arena, number_of_arguments),
2416 return_type_(return_type),
2417 dex_pc_(dex_pc),
2418 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002419 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002420 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002421 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2422 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002423 }
2424
David Brazdil1abb4192015-02-17 18:33:36 +00002425 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2426 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2427 inputs_.Put(index, input);
2428 }
2429
Roland Levillain3e3d7332015-04-28 11:00:54 +01002430 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002431 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002432 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002433 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002434 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002435 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002436 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002437
2438 private:
2439 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2440};
2441
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002442class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002443 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002444 // Requirements of this method call regarding the class
2445 // initialization (clinit) check of its declaring class.
2446 enum class ClinitCheckRequirement {
2447 kNone, // Class already initialized.
2448 kExplicit, // Static call having explicit clinit check as last input.
2449 kImplicit, // Static call implicitly requiring a clinit check.
2450 };
2451
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002452 HInvokeStaticOrDirect(ArenaAllocator* arena,
2453 uint32_t number_of_arguments,
2454 Primitive::Type return_type,
2455 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002456 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002457 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002458 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002459 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002460 InvokeType invoke_type,
2461 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002462 : HInvoke(arena,
2463 number_of_arguments,
2464 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2465 return_type,
2466 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002467 dex_method_index,
2468 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002469 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002470 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002471 clinit_check_requirement_(clinit_check_requirement),
2472 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002473
Calin Juravle641547a2015-04-21 22:08:51 +01002474 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2475 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002476 // We access the method via the dex cache so we can't do an implicit null check.
2477 // TODO: for intrinsics we can generate implicit null checks.
2478 return false;
2479 }
2480
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002481 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002482 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002483 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002484 bool IsStringInit() const { return string_init_offset_ != 0; }
2485 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002486
Roland Levillain4c0eb422015-04-24 16:43:49 +01002487 // Is this instruction a call to a static method?
2488 bool IsStatic() const {
2489 return GetInvokeType() == kStatic;
2490 }
2491
Roland Levillain3e3d7332015-04-28 11:00:54 +01002492 // Remove the art::HLoadClass instruction set as last input by
2493 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2494 // the initial art::HClinitCheck instruction (only relevant for
2495 // static calls with explicit clinit check).
2496 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002497 DCHECK(IsStaticWithExplicitClinitCheck());
2498 size_t last_input_index = InputCount() - 1;
2499 HInstruction* last_input = InputAt(last_input_index);
2500 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002501 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002502 RemoveAsUserOfInput(last_input_index);
2503 inputs_.DeleteAt(last_input_index);
2504 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2505 DCHECK(IsStaticWithImplicitClinitCheck());
2506 }
2507
2508 // Is this a call to a static method whose declaring class has an
2509 // explicit intialization check in the graph?
2510 bool IsStaticWithExplicitClinitCheck() const {
2511 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2512 }
2513
2514 // Is this a call to a static method whose declaring class has an
2515 // implicit intialization check requirement?
2516 bool IsStaticWithImplicitClinitCheck() const {
2517 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2518 }
2519
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002520 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002521
Roland Levillain4c0eb422015-04-24 16:43:49 +01002522 protected:
2523 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2524 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2525 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2526 HInstruction* input = input_record.GetInstruction();
2527 // `input` is the last input of a static invoke marked as having
2528 // an explicit clinit check. It must either be:
2529 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2530 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2531 DCHECK(input != nullptr);
2532 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2533 }
2534 return input_record;
2535 }
2536
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002537 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002538 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002539 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002540 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002541 // Thread entrypoint offset for string init method if this is a string init invoke.
2542 // Note that there are multiple string init methods, each having its own offset.
2543 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002544
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002545 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002546};
2547
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002548class HInvokeVirtual : public HInvoke {
2549 public:
2550 HInvokeVirtual(ArenaAllocator* arena,
2551 uint32_t number_of_arguments,
2552 Primitive::Type return_type,
2553 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002554 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002555 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002556 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002557 vtable_index_(vtable_index) {}
2558
Calin Juravle641547a2015-04-21 22:08:51 +01002559 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002560 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002561 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002562 }
2563
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002564 uint32_t GetVTableIndex() const { return vtable_index_; }
2565
2566 DECLARE_INSTRUCTION(InvokeVirtual);
2567
2568 private:
2569 const uint32_t vtable_index_;
2570
2571 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2572};
2573
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002574class HInvokeInterface : public HInvoke {
2575 public:
2576 HInvokeInterface(ArenaAllocator* arena,
2577 uint32_t number_of_arguments,
2578 Primitive::Type return_type,
2579 uint32_t dex_pc,
2580 uint32_t dex_method_index,
2581 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002582 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002583 imt_index_(imt_index) {}
2584
Calin Juravle641547a2015-04-21 22:08:51 +01002585 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002586 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002587 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002588 }
2589
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002590 uint32_t GetImtIndex() const { return imt_index_; }
2591 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2592
2593 DECLARE_INSTRUCTION(InvokeInterface);
2594
2595 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002596 const uint32_t imt_index_;
2597
2598 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2599};
2600
Dave Allison20dfc792014-06-16 20:44:29 -07002601class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002602 public:
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002603 HNewInstance(uint32_t dex_pc,
2604 uint16_t type_index,
2605 const DexFile& dex_file,
2606 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002607 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2608 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002609 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002610 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002611 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002612
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002613 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002614 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002615 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002616
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002617 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002618 bool NeedsEnvironment() const OVERRIDE { return true; }
2619 // It may throw when called on:
2620 // - interfaces
2621 // - abstract/innaccessible/unknown classes
2622 // TODO: optimize when possible.
2623 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002624
Calin Juravle10e244f2015-01-26 18:54:32 +00002625 bool CanBeNull() const OVERRIDE { return false; }
2626
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002627 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2628
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002629 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002630
2631 private:
2632 const uint32_t dex_pc_;
2633 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002634 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002635 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002636
2637 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2638};
2639
Roland Levillain88cb1752014-10-20 16:36:47 +01002640class HNeg : public HUnaryOperation {
2641 public:
2642 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2643 : HUnaryOperation(result_type, input) {}
2644
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002645 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2646 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002647
Roland Levillain88cb1752014-10-20 16:36:47 +01002648 DECLARE_INSTRUCTION(Neg);
2649
2650 private:
2651 DISALLOW_COPY_AND_ASSIGN(HNeg);
2652};
2653
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002654class HNewArray : public HExpression<1> {
2655 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002656 HNewArray(HInstruction* length,
2657 uint32_t dex_pc,
2658 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002659 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002660 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002661 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2662 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002663 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002664 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002665 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002666 SetRawInputAt(0, length);
2667 }
2668
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002669 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002670 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002671 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002672
2673 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002674 bool NeedsEnvironment() const OVERRIDE { return true; }
2675
Mingyao Yang0c365e62015-03-31 15:09:29 -07002676 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2677 bool CanThrow() const OVERRIDE { return true; }
2678
Calin Juravle10e244f2015-01-26 18:54:32 +00002679 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002680
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002681 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2682
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002683 DECLARE_INSTRUCTION(NewArray);
2684
2685 private:
2686 const uint32_t dex_pc_;
2687 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002688 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002689 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002690
2691 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2692};
2693
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002694class HAdd : public HBinaryOperation {
2695 public:
2696 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2697 : HBinaryOperation(result_type, left, right) {}
2698
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002699 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002700
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002701 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002702 return x + y;
2703 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002704 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002705 return x + y;
2706 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002707
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002708 DECLARE_INSTRUCTION(Add);
2709
2710 private:
2711 DISALLOW_COPY_AND_ASSIGN(HAdd);
2712};
2713
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002714class HSub : public HBinaryOperation {
2715 public:
2716 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2717 : HBinaryOperation(result_type, left, right) {}
2718
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002719 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002720 return x - y;
2721 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002722 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002723 return x - y;
2724 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002725
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002726 DECLARE_INSTRUCTION(Sub);
2727
2728 private:
2729 DISALLOW_COPY_AND_ASSIGN(HSub);
2730};
2731
Calin Juravle34bacdf2014-10-07 20:23:36 +01002732class HMul : public HBinaryOperation {
2733 public:
2734 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2735 : HBinaryOperation(result_type, left, right) {}
2736
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002737 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002739 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2740 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002741
2742 DECLARE_INSTRUCTION(Mul);
2743
2744 private:
2745 DISALLOW_COPY_AND_ASSIGN(HMul);
2746};
2747
Calin Juravle7c4954d2014-10-28 16:57:40 +00002748class HDiv : public HBinaryOperation {
2749 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002750 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2751 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002752
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002753 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002754 // Our graph structure ensures we never have 0 for `y` during constant folding.
2755 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002756 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002757 return (y == -1) ? -x : x / y;
2758 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002759
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002760 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002761 DCHECK_NE(y, 0);
2762 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2763 return (y == -1) ? -x : x / y;
2764 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002765
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002766 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002767
Calin Juravle7c4954d2014-10-28 16:57:40 +00002768 DECLARE_INSTRUCTION(Div);
2769
2770 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002771 const uint32_t dex_pc_;
2772
Calin Juravle7c4954d2014-10-28 16:57:40 +00002773 DISALLOW_COPY_AND_ASSIGN(HDiv);
2774};
2775
Calin Juravlebacfec32014-11-14 15:54:36 +00002776class HRem : public HBinaryOperation {
2777 public:
2778 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2779 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2780
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002781 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002782 DCHECK_NE(y, 0);
2783 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2784 return (y == -1) ? 0 : x % y;
2785 }
2786
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002787 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002788 DCHECK_NE(y, 0);
2789 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2790 return (y == -1) ? 0 : x % y;
2791 }
2792
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002793 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002794
2795 DECLARE_INSTRUCTION(Rem);
2796
2797 private:
2798 const uint32_t dex_pc_;
2799
2800 DISALLOW_COPY_AND_ASSIGN(HRem);
2801};
2802
Calin Juravled0d48522014-11-04 16:40:20 +00002803class HDivZeroCheck : public HExpression<1> {
2804 public:
2805 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2806 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2807 SetRawInputAt(0, value);
2808 }
2809
2810 bool CanBeMoved() const OVERRIDE { return true; }
2811
2812 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2813 UNUSED(other);
2814 return true;
2815 }
2816
2817 bool NeedsEnvironment() const OVERRIDE { return true; }
2818 bool CanThrow() const OVERRIDE { return true; }
2819
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002820 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002821
2822 DECLARE_INSTRUCTION(DivZeroCheck);
2823
2824 private:
2825 const uint32_t dex_pc_;
2826
2827 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2828};
2829
Calin Juravle9aec02f2014-11-18 23:06:35 +00002830class HShl : public HBinaryOperation {
2831 public:
2832 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2833 : HBinaryOperation(result_type, left, right) {}
2834
2835 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2836 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2837
2838 DECLARE_INSTRUCTION(Shl);
2839
2840 private:
2841 DISALLOW_COPY_AND_ASSIGN(HShl);
2842};
2843
2844class HShr : public HBinaryOperation {
2845 public:
2846 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2847 : HBinaryOperation(result_type, left, right) {}
2848
2849 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2850 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2851
2852 DECLARE_INSTRUCTION(Shr);
2853
2854 private:
2855 DISALLOW_COPY_AND_ASSIGN(HShr);
2856};
2857
2858class HUShr : public HBinaryOperation {
2859 public:
2860 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2861 : HBinaryOperation(result_type, left, right) {}
2862
2863 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2864 uint32_t ux = static_cast<uint32_t>(x);
2865 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2866 return static_cast<int32_t>(ux >> uy);
2867 }
2868
2869 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2870 uint64_t ux = static_cast<uint64_t>(x);
2871 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2872 return static_cast<int64_t>(ux >> uy);
2873 }
2874
2875 DECLARE_INSTRUCTION(UShr);
2876
2877 private:
2878 DISALLOW_COPY_AND_ASSIGN(HUShr);
2879};
2880
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002881class HAnd : public HBinaryOperation {
2882 public:
2883 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2884 : HBinaryOperation(result_type, left, right) {}
2885
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002886 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002887
2888 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2889 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2890
2891 DECLARE_INSTRUCTION(And);
2892
2893 private:
2894 DISALLOW_COPY_AND_ASSIGN(HAnd);
2895};
2896
2897class HOr : public HBinaryOperation {
2898 public:
2899 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2900 : HBinaryOperation(result_type, left, right) {}
2901
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002902 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002903
2904 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2905 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2906
2907 DECLARE_INSTRUCTION(Or);
2908
2909 private:
2910 DISALLOW_COPY_AND_ASSIGN(HOr);
2911};
2912
2913class HXor : public HBinaryOperation {
2914 public:
2915 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2916 : HBinaryOperation(result_type, left, right) {}
2917
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002918 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002919
2920 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2921 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2922
2923 DECLARE_INSTRUCTION(Xor);
2924
2925 private:
2926 DISALLOW_COPY_AND_ASSIGN(HXor);
2927};
2928
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002929// The value of a parameter in this method. Its location depends on
2930// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002931class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002932 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002933 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2934 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002935
2936 uint8_t GetIndex() const { return index_; }
2937
Calin Juravle10e244f2015-01-26 18:54:32 +00002938 bool CanBeNull() const OVERRIDE { return !is_this_; }
2939
Calin Juravle3cd4fc82015-05-14 15:15:42 +01002940 bool IsThis() const { return is_this_; }
2941
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002942 DECLARE_INSTRUCTION(ParameterValue);
2943
2944 private:
2945 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002946 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002947 const uint8_t index_;
2948
Calin Juravle10e244f2015-01-26 18:54:32 +00002949 // Whether or not the parameter value corresponds to 'this' argument.
2950 const bool is_this_;
2951
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002952 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2953};
2954
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002955class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002956 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002957 explicit HNot(Primitive::Type result_type, HInstruction* input)
2958 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002959
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002960 bool CanBeMoved() const OVERRIDE { return true; }
2961 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002962 UNUSED(other);
2963 return true;
2964 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002965
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002966 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2967 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002968
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002969 DECLARE_INSTRUCTION(Not);
2970
2971 private:
2972 DISALLOW_COPY_AND_ASSIGN(HNot);
2973};
2974
David Brazdil66d126e2015-04-03 16:02:44 +01002975class HBooleanNot : public HUnaryOperation {
2976 public:
2977 explicit HBooleanNot(HInstruction* input)
2978 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2979
2980 bool CanBeMoved() const OVERRIDE { return true; }
2981 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2982 UNUSED(other);
2983 return true;
2984 }
2985
2986 int32_t Evaluate(int32_t x) const OVERRIDE {
2987 DCHECK(IsUint<1>(x));
2988 return !x;
2989 }
2990
2991 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2992 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2993 UNREACHABLE();
2994 }
2995
2996 DECLARE_INSTRUCTION(BooleanNot);
2997
2998 private:
2999 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3000};
3001
Roland Levillaindff1f282014-11-05 14:15:05 +00003002class HTypeConversion : public HExpression<1> {
3003 public:
3004 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003005 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3006 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003007 SetRawInputAt(0, input);
3008 DCHECK_NE(input->GetType(), result_type);
3009 }
3010
3011 HInstruction* GetInput() const { return InputAt(0); }
3012 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3013 Primitive::Type GetResultType() const { return GetType(); }
3014
Roland Levillain624279f2014-12-04 11:54:28 +00003015 // Required by the x86 and ARM code generators when producing calls
3016 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003017 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003018
Roland Levillaindff1f282014-11-05 14:15:05 +00003019 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003020 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003021
Mark Mendelle82549b2015-05-06 10:55:34 -04003022 // Try to statically evaluate the conversion and return a HConstant
3023 // containing the result. If the input cannot be converted, return nullptr.
3024 HConstant* TryStaticEvaluation() const;
3025
Roland Levillaindff1f282014-11-05 14:15:05 +00003026 DECLARE_INSTRUCTION(TypeConversion);
3027
3028 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003029 const uint32_t dex_pc_;
3030
Roland Levillaindff1f282014-11-05 14:15:05 +00003031 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3032};
3033
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003034static constexpr uint32_t kNoRegNumber = -1;
3035
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003036class HPhi : public HInstruction {
3037 public:
3038 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003039 : HInstruction(SideEffects::None()),
3040 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003041 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003042 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003043 is_live_(false),
3044 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003045 inputs_.SetSize(number_of_inputs);
3046 }
3047
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003048 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3049 static Primitive::Type ToPhiType(Primitive::Type type) {
3050 switch (type) {
3051 case Primitive::kPrimBoolean:
3052 case Primitive::kPrimByte:
3053 case Primitive::kPrimShort:
3054 case Primitive::kPrimChar:
3055 return Primitive::kPrimInt;
3056 default:
3057 return type;
3058 }
3059 }
3060
Calin Juravle10e244f2015-01-26 18:54:32 +00003061 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003062
3063 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003064 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003065
Calin Juravle10e244f2015-01-26 18:54:32 +00003066 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003067 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003068
Calin Juravle10e244f2015-01-26 18:54:32 +00003069 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3070 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3071
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003072 uint32_t GetRegNumber() const { return reg_number_; }
3073
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003074 void SetDead() { is_live_ = false; }
3075 void SetLive() { is_live_ = true; }
3076 bool IsDead() const { return !is_live_; }
3077 bool IsLive() const { return is_live_; }
3078
Calin Juravlea4f88312015-04-16 12:57:19 +01003079 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3080 // An equivalent phi is a phi having the same dex register and type.
3081 // It assumes that phis with the same dex register are adjacent.
3082 HPhi* GetNextEquivalentPhiWithSameType() {
3083 HInstruction* next = GetNext();
3084 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3085 if (next->GetType() == GetType()) {
3086 return next->AsPhi();
3087 }
3088 next = next->GetNext();
3089 }
3090 return nullptr;
3091 }
3092
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003093 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003094
David Brazdil1abb4192015-02-17 18:33:36 +00003095 protected:
3096 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3097
3098 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3099 inputs_.Put(index, input);
3100 }
3101
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003102 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003103 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003104 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003105 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003106 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003107 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003108
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003109 DISALLOW_COPY_AND_ASSIGN(HPhi);
3110};
3111
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003112class HNullCheck : public HExpression<1> {
3113 public:
3114 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003115 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003116 SetRawInputAt(0, value);
3117 }
3118
Calin Juravle10e244f2015-01-26 18:54:32 +00003119 bool CanBeMoved() const OVERRIDE { return true; }
3120 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003121 UNUSED(other);
3122 return true;
3123 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003124
Calin Juravle10e244f2015-01-26 18:54:32 +00003125 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003126
Calin Juravle10e244f2015-01-26 18:54:32 +00003127 bool CanThrow() const OVERRIDE { return true; }
3128
3129 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003130
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003131 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003132
3133 DECLARE_INSTRUCTION(NullCheck);
3134
3135 private:
3136 const uint32_t dex_pc_;
3137
3138 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3139};
3140
3141class FieldInfo : public ValueObject {
3142 public:
Calin Juravle52c48962014-12-16 17:02:57 +00003143 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3144 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003145
3146 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003147 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003148 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003149
3150 private:
3151 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003152 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003153 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003154};
3155
3156class HInstanceFieldGet : public HExpression<1> {
3157 public:
3158 HInstanceFieldGet(HInstruction* value,
3159 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003160 MemberOffset field_offset,
3161 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003162 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003163 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003164 SetRawInputAt(0, value);
3165 }
3166
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003167 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003168
3169 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3170 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3171 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003172 }
3173
Calin Juravle641547a2015-04-21 22:08:51 +01003174 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3175 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003176 }
3177
3178 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003179 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3180 }
3181
Calin Juravle52c48962014-12-16 17:02:57 +00003182 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003183 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003184 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003185 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003186
3187 DECLARE_INSTRUCTION(InstanceFieldGet);
3188
3189 private:
3190 const FieldInfo field_info_;
3191
3192 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3193};
3194
3195class HInstanceFieldSet : public HTemplateInstruction<2> {
3196 public:
3197 HInstanceFieldSet(HInstruction* object,
3198 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003199 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003200 MemberOffset field_offset,
3201 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003202 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003203 field_info_(field_offset, field_type, is_volatile),
3204 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003205 SetRawInputAt(0, object);
3206 SetRawInputAt(1, value);
3207 }
3208
Calin Juravle641547a2015-04-21 22:08:51 +01003209 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3210 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003211 }
3212
Calin Juravle52c48962014-12-16 17:02:57 +00003213 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003214 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003215 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003216 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003217 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003218 bool GetValueCanBeNull() const { return value_can_be_null_; }
3219 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003220
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003221 DECLARE_INSTRUCTION(InstanceFieldSet);
3222
3223 private:
3224 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003225 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003226
3227 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3228};
3229
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003230class HArrayGet : public HExpression<2> {
3231 public:
3232 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003233 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003234 SetRawInputAt(0, array);
3235 SetRawInputAt(1, index);
3236 }
3237
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003238 bool CanBeMoved() const OVERRIDE { return true; }
3239 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003240 UNUSED(other);
3241 return true;
3242 }
Calin Juravle641547a2015-04-21 22:08:51 +01003243 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3244 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003245 // TODO: We can be smarter here.
3246 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3247 // which generates the implicit null check. There are cases when these can be removed
3248 // to produce better code. If we ever add optimizations to do so we should allow an
3249 // implicit check here (as long as the address falls in the first page).
3250 return false;
3251 }
3252
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003253 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003254
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003255 HInstruction* GetArray() const { return InputAt(0); }
3256 HInstruction* GetIndex() const { return InputAt(1); }
3257
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003258 DECLARE_INSTRUCTION(ArrayGet);
3259
3260 private:
3261 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3262};
3263
3264class HArraySet : public HTemplateInstruction<3> {
3265 public:
3266 HArraySet(HInstruction* array,
3267 HInstruction* index,
3268 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003269 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003270 uint32_t dex_pc)
3271 : HTemplateInstruction(SideEffects::ChangesSomething()),
3272 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003273 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003274 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3275 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003276 SetRawInputAt(0, array);
3277 SetRawInputAt(1, index);
3278 SetRawInputAt(2, value);
3279 }
3280
Calin Juravle77520bc2015-01-12 18:45:46 +00003281 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003282 // We currently always call a runtime method to catch array store
3283 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003284 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003285 }
3286
Calin Juravle641547a2015-04-21 22:08:51 +01003287 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3288 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003289 // TODO: Same as for ArrayGet.
3290 return false;
3291 }
3292
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003293 void ClearNeedsTypeCheck() {
3294 needs_type_check_ = false;
3295 }
3296
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003297 void ClearValueCanBeNull() {
3298 value_can_be_null_ = false;
3299 }
3300
3301 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003302 bool NeedsTypeCheck() const { return needs_type_check_; }
3303
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003304 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003305
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003306 HInstruction* GetArray() const { return InputAt(0); }
3307 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003308 HInstruction* GetValue() const { return InputAt(2); }
3309
3310 Primitive::Type GetComponentType() const {
3311 // The Dex format does not type floating point index operations. Since the
3312 // `expected_component_type_` is set during building and can therefore not
3313 // be correct, we also check what is the value type. If it is a floating
3314 // point type, we must use that type.
3315 Primitive::Type value_type = GetValue()->GetType();
3316 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3317 ? value_type
3318 : expected_component_type_;
3319 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003320
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003321 DECLARE_INSTRUCTION(ArraySet);
3322
3323 private:
3324 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003325 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003326 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003327 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003328
3329 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3330};
3331
3332class HArrayLength : public HExpression<1> {
3333 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003334 explicit HArrayLength(HInstruction* array)
3335 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3336 // Note that arrays do not change length, so the instruction does not
3337 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003338 SetRawInputAt(0, array);
3339 }
3340
Calin Juravle77520bc2015-01-12 18:45:46 +00003341 bool CanBeMoved() const OVERRIDE { return true; }
3342 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003343 UNUSED(other);
3344 return true;
3345 }
Calin Juravle641547a2015-04-21 22:08:51 +01003346 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3347 return obj == InputAt(0);
3348 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003349
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003350 DECLARE_INSTRUCTION(ArrayLength);
3351
3352 private:
3353 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3354};
3355
3356class HBoundsCheck : public HExpression<2> {
3357 public:
3358 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003359 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003360 DCHECK(index->GetType() == Primitive::kPrimInt);
3361 SetRawInputAt(0, index);
3362 SetRawInputAt(1, length);
3363 }
3364
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003365 bool CanBeMoved() const OVERRIDE { return true; }
3366 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003367 UNUSED(other);
3368 return true;
3369 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003370
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003371 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003372
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003373 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003374
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003375 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003376
3377 DECLARE_INSTRUCTION(BoundsCheck);
3378
3379 private:
3380 const uint32_t dex_pc_;
3381
3382 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3383};
3384
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003385/**
3386 * Some DEX instructions are folded into multiple HInstructions that need
3387 * to stay live until the last HInstruction. This class
3388 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003389 * HInstruction stays live. `index` represents the stack location index of the
3390 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003391 */
3392class HTemporary : public HTemplateInstruction<0> {
3393 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003394 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003395
3396 size_t GetIndex() const { return index_; }
3397
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003398 Primitive::Type GetType() const OVERRIDE {
3399 // The previous instruction is the one that will be stored in the temporary location.
3400 DCHECK(GetPrevious() != nullptr);
3401 return GetPrevious()->GetType();
3402 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003403
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003404 DECLARE_INSTRUCTION(Temporary);
3405
3406 private:
3407 const size_t index_;
3408
3409 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3410};
3411
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003412class HSuspendCheck : public HTemplateInstruction<0> {
3413 public:
3414 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003415 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003416
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003417 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003418 return true;
3419 }
3420
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003421 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003422 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3423 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003424
3425 DECLARE_INSTRUCTION(SuspendCheck);
3426
3427 private:
3428 const uint32_t dex_pc_;
3429
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003430 // Only used for code generation, in order to share the same slow path between back edges
3431 // of a same loop.
3432 SlowPathCode* slow_path_;
3433
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003434 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3435};
3436
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003437/**
3438 * Instruction to load a Class object.
3439 */
3440class HLoadClass : public HExpression<0> {
3441 public:
3442 HLoadClass(uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003443 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003444 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003445 uint32_t dex_pc)
3446 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3447 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003448 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003449 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003450 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003451 generate_clinit_check_(false),
3452 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003453
3454 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003455
3456 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3457 return other->AsLoadClass()->type_index_ == type_index_;
3458 }
3459
3460 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3461
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003462 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003463 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003464 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003465
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003466 bool NeedsEnvironment() const OVERRIDE {
3467 // Will call runtime and load the class if the class is not loaded yet.
3468 // TODO: finer grain decision.
3469 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003470 }
3471
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003472 bool MustGenerateClinitCheck() const {
3473 return generate_clinit_check_;
3474 }
3475
Calin Juravle0ba218d2015-05-19 18:46:01 +01003476 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3477 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003478 }
3479
3480 bool CanCallRuntime() const {
3481 return MustGenerateClinitCheck() || !is_referrers_class_;
3482 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003483
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003484 bool CanThrow() const OVERRIDE {
3485 // May call runtime and and therefore can throw.
3486 // TODO: finer grain decision.
3487 return !is_referrers_class_;
3488 }
3489
Calin Juravleacf735c2015-02-12 15:25:22 +00003490 ReferenceTypeInfo GetLoadedClassRTI() {
3491 return loaded_class_rti_;
3492 }
3493
3494 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3495 // Make sure we only set exact types (the loaded class should never be merged).
3496 DCHECK(rti.IsExact());
3497 loaded_class_rti_ = rti;
3498 }
3499
3500 bool IsResolved() {
3501 return loaded_class_rti_.IsExact();
3502 }
3503
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003504 const DexFile& GetDexFile() { return dex_file_; }
3505
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003506 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3507
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003508 DECLARE_INSTRUCTION(LoadClass);
3509
3510 private:
3511 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003512 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003513 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003514 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003515 // Whether this instruction must generate the initialization check.
3516 // Used for code generation.
3517 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003518
Calin Juravleacf735c2015-02-12 15:25:22 +00003519 ReferenceTypeInfo loaded_class_rti_;
3520
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003521 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3522};
3523
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003524class HLoadString : public HExpression<0> {
3525 public:
3526 HLoadString(uint32_t string_index, uint32_t dex_pc)
3527 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3528 string_index_(string_index),
3529 dex_pc_(dex_pc) {}
3530
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003531 bool CanBeMoved() const OVERRIDE { return true; }
3532
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003533 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3534 return other->AsLoadString()->string_index_ == string_index_;
3535 }
3536
3537 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3538
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003539 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003540 uint32_t GetStringIndex() const { return string_index_; }
3541
3542 // TODO: Can we deopt or debug when we resolve a string?
3543 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003544 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003545
3546 DECLARE_INSTRUCTION(LoadString);
3547
3548 private:
3549 const uint32_t string_index_;
3550 const uint32_t dex_pc_;
3551
3552 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3553};
3554
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003555/**
3556 * Performs an initialization check on its Class object input.
3557 */
3558class HClinitCheck : public HExpression<1> {
3559 public:
3560 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003561 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003562 dex_pc_(dex_pc) {
3563 SetRawInputAt(0, constant);
3564 }
3565
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003566 bool CanBeMoved() const OVERRIDE { return true; }
3567 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3568 UNUSED(other);
3569 return true;
3570 }
3571
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003572 bool NeedsEnvironment() const OVERRIDE {
3573 // May call runtime to initialize the class.
3574 return true;
3575 }
3576
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003577 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003578
3579 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3580
3581 DECLARE_INSTRUCTION(ClinitCheck);
3582
3583 private:
3584 const uint32_t dex_pc_;
3585
3586 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3587};
3588
3589class HStaticFieldGet : public HExpression<1> {
3590 public:
3591 HStaticFieldGet(HInstruction* cls,
3592 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003593 MemberOffset field_offset,
3594 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003595 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003596 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003597 SetRawInputAt(0, cls);
3598 }
3599
Calin Juravle52c48962014-12-16 17:02:57 +00003600
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003601 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003602
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003603 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003604 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3605 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003606 }
3607
3608 size_t ComputeHashCode() const OVERRIDE {
3609 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3610 }
3611
Calin Juravle52c48962014-12-16 17:02:57 +00003612 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003613 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3614 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003615 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003616
3617 DECLARE_INSTRUCTION(StaticFieldGet);
3618
3619 private:
3620 const FieldInfo field_info_;
3621
3622 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3623};
3624
3625class HStaticFieldSet : public HTemplateInstruction<2> {
3626 public:
3627 HStaticFieldSet(HInstruction* cls,
3628 HInstruction* value,
3629 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003630 MemberOffset field_offset,
3631 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003632 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003633 field_info_(field_offset, field_type, is_volatile),
3634 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003635 SetRawInputAt(0, cls);
3636 SetRawInputAt(1, value);
3637 }
3638
Calin Juravle52c48962014-12-16 17:02:57 +00003639 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003640 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3641 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003642 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003643
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003644 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003645 bool GetValueCanBeNull() const { return value_can_be_null_; }
3646 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003647
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003648 DECLARE_INSTRUCTION(StaticFieldSet);
3649
3650 private:
3651 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003652 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003653
3654 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3655};
3656
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003657// Implement the move-exception DEX instruction.
3658class HLoadException : public HExpression<0> {
3659 public:
3660 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3661
3662 DECLARE_INSTRUCTION(LoadException);
3663
3664 private:
3665 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3666};
3667
3668class HThrow : public HTemplateInstruction<1> {
3669 public:
3670 HThrow(HInstruction* exception, uint32_t dex_pc)
3671 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3672 SetRawInputAt(0, exception);
3673 }
3674
3675 bool IsControlFlow() const OVERRIDE { return true; }
3676
3677 bool NeedsEnvironment() const OVERRIDE { return true; }
3678
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003679 bool CanThrow() const OVERRIDE { return true; }
3680
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003681 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003682
3683 DECLARE_INSTRUCTION(Throw);
3684
3685 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003686 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003687
3688 DISALLOW_COPY_AND_ASSIGN(HThrow);
3689};
3690
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003691class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003692 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003693 HInstanceOf(HInstruction* object,
3694 HLoadClass* constant,
3695 bool class_is_final,
3696 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003697 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3698 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003699 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003700 dex_pc_(dex_pc) {
3701 SetRawInputAt(0, object);
3702 SetRawInputAt(1, constant);
3703 }
3704
3705 bool CanBeMoved() const OVERRIDE { return true; }
3706
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003707 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003708 return true;
3709 }
3710
3711 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003712 return false;
3713 }
3714
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003715 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003716
3717 bool IsClassFinal() const { return class_is_final_; }
3718
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003719 // Used only in code generation.
3720 bool MustDoNullCheck() const { return must_do_null_check_; }
3721 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3722
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003723 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003724
3725 private:
3726 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003727 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003728 const uint32_t dex_pc_;
3729
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003730 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3731};
3732
Calin Juravleb1498f62015-02-16 13:13:29 +00003733class HBoundType : public HExpression<1> {
3734 public:
3735 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3736 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3737 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003738 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003739 SetRawInputAt(0, input);
3740 }
3741
3742 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3743
3744 bool CanBeNull() const OVERRIDE {
3745 // `null instanceof ClassX` always return false so we can't be null.
3746 return false;
3747 }
3748
3749 DECLARE_INSTRUCTION(BoundType);
3750
3751 private:
3752 // Encodes the most upper class that this instruction can have. In other words
3753 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3754 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3755 const ReferenceTypeInfo bound_type_;
3756
3757 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3758};
3759
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003760class HCheckCast : public HTemplateInstruction<2> {
3761 public:
3762 HCheckCast(HInstruction* object,
3763 HLoadClass* constant,
3764 bool class_is_final,
3765 uint32_t dex_pc)
3766 : HTemplateInstruction(SideEffects::None()),
3767 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003768 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003769 dex_pc_(dex_pc) {
3770 SetRawInputAt(0, object);
3771 SetRawInputAt(1, constant);
3772 }
3773
3774 bool CanBeMoved() const OVERRIDE { return true; }
3775
3776 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3777 return true;
3778 }
3779
3780 bool NeedsEnvironment() const OVERRIDE {
3781 // Instruction may throw a CheckCastError.
3782 return true;
3783 }
3784
3785 bool CanThrow() const OVERRIDE { return true; }
3786
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003787 bool MustDoNullCheck() const { return must_do_null_check_; }
3788 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3789
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003790 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003791
3792 bool IsClassFinal() const { return class_is_final_; }
3793
3794 DECLARE_INSTRUCTION(CheckCast);
3795
3796 private:
3797 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003798 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003799 const uint32_t dex_pc_;
3800
3801 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003802};
3803
Calin Juravle27df7582015-04-17 19:12:31 +01003804class HMemoryBarrier : public HTemplateInstruction<0> {
3805 public:
3806 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3807 : HTemplateInstruction(SideEffects::None()),
3808 barrier_kind_(barrier_kind) {}
3809
3810 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3811
3812 DECLARE_INSTRUCTION(MemoryBarrier);
3813
3814 private:
3815 const MemBarrierKind barrier_kind_;
3816
3817 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3818};
3819
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003820class HMonitorOperation : public HTemplateInstruction<1> {
3821 public:
3822 enum OperationKind {
3823 kEnter,
3824 kExit,
3825 };
3826
3827 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3828 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3829 SetRawInputAt(0, object);
3830 }
3831
3832 // Instruction may throw a Java exception, so we need an environment.
3833 bool NeedsEnvironment() const OVERRIDE { return true; }
3834 bool CanThrow() const OVERRIDE { return true; }
3835
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003836 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003837
3838 bool IsEnter() const { return kind_ == kEnter; }
3839
3840 DECLARE_INSTRUCTION(MonitorOperation);
3841
Calin Juravle52c48962014-12-16 17:02:57 +00003842 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003843 const OperationKind kind_;
3844 const uint32_t dex_pc_;
3845
3846 private:
3847 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3848};
3849
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003850class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003851 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003852 MoveOperands(Location source,
3853 Location destination,
3854 Primitive::Type type,
3855 HInstruction* instruction)
3856 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003857
3858 Location GetSource() const { return source_; }
3859 Location GetDestination() const { return destination_; }
3860
3861 void SetSource(Location value) { source_ = value; }
3862 void SetDestination(Location value) { destination_ = value; }
3863
3864 // The parallel move resolver marks moves as "in-progress" by clearing the
3865 // destination (but not the source).
3866 Location MarkPending() {
3867 DCHECK(!IsPending());
3868 Location dest = destination_;
3869 destination_ = Location::NoLocation();
3870 return dest;
3871 }
3872
3873 void ClearPending(Location dest) {
3874 DCHECK(IsPending());
3875 destination_ = dest;
3876 }
3877
3878 bool IsPending() const {
3879 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3880 return destination_.IsInvalid() && !source_.IsInvalid();
3881 }
3882
3883 // True if this blocks a move from the given location.
3884 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003885 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003886 }
3887
3888 // A move is redundant if it's been eliminated, if its source and
3889 // destination are the same, or if its destination is unneeded.
3890 bool IsRedundant() const {
3891 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3892 }
3893
3894 // We clear both operands to indicate move that's been eliminated.
3895 void Eliminate() {
3896 source_ = destination_ = Location::NoLocation();
3897 }
3898
3899 bool IsEliminated() const {
3900 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3901 return source_.IsInvalid();
3902 }
3903
Nicolas Geoffray90218252015-04-15 11:56:51 +01003904 bool Is64BitMove() const {
3905 return Primitive::Is64BitType(type_);
3906 }
3907
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003908 HInstruction* GetInstruction() const { return instruction_; }
3909
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003910 private:
3911 Location source_;
3912 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003913 // The type this move is for.
3914 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003915 // The instruction this move is assocatied with. Null when this move is
3916 // for moving an input in the expected locations of user (including a phi user).
3917 // This is only used in debug mode, to ensure we do not connect interval siblings
3918 // in the same parallel move.
3919 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003920};
3921
3922static constexpr size_t kDefaultNumberOfMoves = 4;
3923
3924class HParallelMove : public HTemplateInstruction<0> {
3925 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003926 explicit HParallelMove(ArenaAllocator* arena)
3927 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003928
Nicolas Geoffray90218252015-04-15 11:56:51 +01003929 void AddMove(Location source,
3930 Location destination,
3931 Primitive::Type type,
3932 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003933 DCHECK(source.IsValid());
3934 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003935 if (kIsDebugBuild) {
3936 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003937 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003938 if (moves_.Get(i).GetInstruction() == instruction) {
3939 // Special case the situation where the move is for the spill slot
3940 // of the instruction.
3941 if ((GetPrevious() == instruction)
3942 || ((GetPrevious() == nullptr)
3943 && instruction->IsPhi()
3944 && instruction->GetBlock() == GetBlock())) {
3945 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3946 << "Doing parallel moves for the same instruction.";
3947 } else {
3948 DCHECK(false) << "Doing parallel moves for the same instruction.";
3949 }
3950 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003951 }
3952 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003953 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003954 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01003955 << "Overlapped destination for two moves in a parallel move: "
3956 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
3957 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003958 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003959 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003960 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003961 }
3962
3963 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003964 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003965 }
3966
3967 size_t NumMoves() const { return moves_.Size(); }
3968
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003969 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003970
3971 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003972 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003973
3974 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3975};
3976
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003977class HGraphVisitor : public ValueObject {
3978 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003979 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3980 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003981
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003982 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003983 virtual void VisitBasicBlock(HBasicBlock* block);
3984
Roland Levillain633021e2014-10-01 14:12:25 +01003985 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003986 void VisitInsertionOrder();
3987
Roland Levillain633021e2014-10-01 14:12:25 +01003988 // Visit the graph following dominator tree reverse post-order.
3989 void VisitReversePostOrder();
3990
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003991 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003992
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003993 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003994#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003995 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3996
3997 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3998
3999#undef DECLARE_VISIT_INSTRUCTION
4000
4001 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004002 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004003
4004 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4005};
4006
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004007class HGraphDelegateVisitor : public HGraphVisitor {
4008 public:
4009 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4010 virtual ~HGraphDelegateVisitor() {}
4011
4012 // Visit functions that delegate to to super class.
4013#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004014 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004015
4016 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4017
4018#undef DECLARE_VISIT_INSTRUCTION
4019
4020 private:
4021 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4022};
4023
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004024class HInsertionOrderIterator : public ValueObject {
4025 public:
4026 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4027
4028 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4029 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4030 void Advance() { ++index_; }
4031
4032 private:
4033 const HGraph& graph_;
4034 size_t index_;
4035
4036 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4037};
4038
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004039class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004040 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004041 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4042 // Check that reverse post order of the graph has been built.
4043 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4044 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004045
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004046 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4047 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004048 void Advance() { ++index_; }
4049
4050 private:
4051 const HGraph& graph_;
4052 size_t index_;
4053
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004054 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004055};
4056
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004057class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004058 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004059 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004060 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4061 // Check that reverse post order of the graph has been built.
4062 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4063 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004064
4065 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004066 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004067 void Advance() { --index_; }
4068
4069 private:
4070 const HGraph& graph_;
4071 size_t index_;
4072
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004073 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004074};
4075
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004076class HLinearPostOrderIterator : public ValueObject {
4077 public:
4078 explicit HLinearPostOrderIterator(const HGraph& graph)
4079 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4080
4081 bool Done() const { return index_ == 0; }
4082
4083 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4084
4085 void Advance() {
4086 --index_;
4087 DCHECK_GE(index_, 0U);
4088 }
4089
4090 private:
4091 const GrowableArray<HBasicBlock*>& order_;
4092 size_t index_;
4093
4094 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4095};
4096
4097class HLinearOrderIterator : public ValueObject {
4098 public:
4099 explicit HLinearOrderIterator(const HGraph& graph)
4100 : order_(graph.GetLinearOrder()), index_(0) {}
4101
4102 bool Done() const { return index_ == order_.Size(); }
4103 HBasicBlock* Current() const { return order_.Get(index_); }
4104 void Advance() { ++index_; }
4105
4106 private:
4107 const GrowableArray<HBasicBlock*>& order_;
4108 size_t index_;
4109
4110 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4111};
4112
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004113// Iterator over the blocks that art part of the loop. Includes blocks part
4114// of an inner loop. The order in which the blocks are iterated is on their
4115// block id.
4116class HBlocksInLoopIterator : public ValueObject {
4117 public:
4118 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4119 : blocks_in_loop_(info.GetBlocks()),
4120 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4121 index_(0) {
4122 if (!blocks_in_loop_.IsBitSet(index_)) {
4123 Advance();
4124 }
4125 }
4126
4127 bool Done() const { return index_ == blocks_.Size(); }
4128 HBasicBlock* Current() const { return blocks_.Get(index_); }
4129 void Advance() {
4130 ++index_;
4131 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4132 if (blocks_in_loop_.IsBitSet(index_)) {
4133 break;
4134 }
4135 }
4136 }
4137
4138 private:
4139 const BitVector& blocks_in_loop_;
4140 const GrowableArray<HBasicBlock*>& blocks_;
4141 size_t index_;
4142
4143 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4144};
4145
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004146inline int64_t Int64FromConstant(HConstant* constant) {
4147 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4148 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4149 : constant->AsLongConstant()->GetValue();
4150}
4151
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004152} // namespace art
4153
4154#endif // ART_COMPILER_OPTIMIZING_NODES_H_