blob: 01870c36faec540358eb33a1e94a1267de5d1876 [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,
2659 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002660 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2661 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002662 type_index_(type_index),
2663 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002664 SetRawInputAt(0, length);
2665 }
2666
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002667 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002668 uint16_t GetTypeIndex() const { return type_index_; }
2669
2670 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002671 bool NeedsEnvironment() const OVERRIDE { return true; }
2672
Mingyao Yang0c365e62015-03-31 15:09:29 -07002673 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2674 bool CanThrow() const OVERRIDE { return true; }
2675
Calin Juravle10e244f2015-01-26 18:54:32 +00002676 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002677
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002678 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2679
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002680 DECLARE_INSTRUCTION(NewArray);
2681
2682 private:
2683 const uint32_t dex_pc_;
2684 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002685 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002686
2687 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2688};
2689
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002690class HAdd : public HBinaryOperation {
2691 public:
2692 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2693 : HBinaryOperation(result_type, left, right) {}
2694
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002695 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002696
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002697 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002698 return x + y;
2699 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002700 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002701 return x + y;
2702 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002703
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002704 DECLARE_INSTRUCTION(Add);
2705
2706 private:
2707 DISALLOW_COPY_AND_ASSIGN(HAdd);
2708};
2709
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002710class HSub : public HBinaryOperation {
2711 public:
2712 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2713 : HBinaryOperation(result_type, left, right) {}
2714
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002715 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002716 return x - y;
2717 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002718 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002719 return x - y;
2720 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002721
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002722 DECLARE_INSTRUCTION(Sub);
2723
2724 private:
2725 DISALLOW_COPY_AND_ASSIGN(HSub);
2726};
2727
Calin Juravle34bacdf2014-10-07 20:23:36 +01002728class HMul : public HBinaryOperation {
2729 public:
2730 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2731 : HBinaryOperation(result_type, left, right) {}
2732
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002733 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002734
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002735 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2736 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002737
2738 DECLARE_INSTRUCTION(Mul);
2739
2740 private:
2741 DISALLOW_COPY_AND_ASSIGN(HMul);
2742};
2743
Calin Juravle7c4954d2014-10-28 16:57:40 +00002744class HDiv : public HBinaryOperation {
2745 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002746 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2747 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002748
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002749 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002750 // Our graph structure ensures we never have 0 for `y` during constant folding.
2751 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002752 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002753 return (y == -1) ? -x : x / y;
2754 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002755
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002756 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002757 DCHECK_NE(y, 0);
2758 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2759 return (y == -1) ? -x : x / y;
2760 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002761
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002762 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002763
Calin Juravle7c4954d2014-10-28 16:57:40 +00002764 DECLARE_INSTRUCTION(Div);
2765
2766 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002767 const uint32_t dex_pc_;
2768
Calin Juravle7c4954d2014-10-28 16:57:40 +00002769 DISALLOW_COPY_AND_ASSIGN(HDiv);
2770};
2771
Calin Juravlebacfec32014-11-14 15:54:36 +00002772class HRem : public HBinaryOperation {
2773 public:
2774 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2775 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2776
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002777 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002778 DCHECK_NE(y, 0);
2779 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2780 return (y == -1) ? 0 : x % y;
2781 }
2782
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002783 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002784 DCHECK_NE(y, 0);
2785 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2786 return (y == -1) ? 0 : x % y;
2787 }
2788
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002789 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002790
2791 DECLARE_INSTRUCTION(Rem);
2792
2793 private:
2794 const uint32_t dex_pc_;
2795
2796 DISALLOW_COPY_AND_ASSIGN(HRem);
2797};
2798
Calin Juravled0d48522014-11-04 16:40:20 +00002799class HDivZeroCheck : public HExpression<1> {
2800 public:
2801 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2802 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2803 SetRawInputAt(0, value);
2804 }
2805
2806 bool CanBeMoved() const OVERRIDE { return true; }
2807
2808 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2809 UNUSED(other);
2810 return true;
2811 }
2812
2813 bool NeedsEnvironment() const OVERRIDE { return true; }
2814 bool CanThrow() const OVERRIDE { return true; }
2815
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002816 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002817
2818 DECLARE_INSTRUCTION(DivZeroCheck);
2819
2820 private:
2821 const uint32_t dex_pc_;
2822
2823 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2824};
2825
Calin Juravle9aec02f2014-11-18 23:06:35 +00002826class HShl : public HBinaryOperation {
2827 public:
2828 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2829 : HBinaryOperation(result_type, left, right) {}
2830
2831 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2832 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2833
2834 DECLARE_INSTRUCTION(Shl);
2835
2836 private:
2837 DISALLOW_COPY_AND_ASSIGN(HShl);
2838};
2839
2840class HShr : public HBinaryOperation {
2841 public:
2842 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2843 : HBinaryOperation(result_type, left, right) {}
2844
2845 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2846 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2847
2848 DECLARE_INSTRUCTION(Shr);
2849
2850 private:
2851 DISALLOW_COPY_AND_ASSIGN(HShr);
2852};
2853
2854class HUShr : public HBinaryOperation {
2855 public:
2856 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2857 : HBinaryOperation(result_type, left, right) {}
2858
2859 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2860 uint32_t ux = static_cast<uint32_t>(x);
2861 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2862 return static_cast<int32_t>(ux >> uy);
2863 }
2864
2865 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2866 uint64_t ux = static_cast<uint64_t>(x);
2867 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2868 return static_cast<int64_t>(ux >> uy);
2869 }
2870
2871 DECLARE_INSTRUCTION(UShr);
2872
2873 private:
2874 DISALLOW_COPY_AND_ASSIGN(HUShr);
2875};
2876
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002877class HAnd : public HBinaryOperation {
2878 public:
2879 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2880 : HBinaryOperation(result_type, left, right) {}
2881
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002882 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002883
2884 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2885 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2886
2887 DECLARE_INSTRUCTION(And);
2888
2889 private:
2890 DISALLOW_COPY_AND_ASSIGN(HAnd);
2891};
2892
2893class HOr : public HBinaryOperation {
2894 public:
2895 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2896 : HBinaryOperation(result_type, left, right) {}
2897
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002898 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002899
2900 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2901 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2902
2903 DECLARE_INSTRUCTION(Or);
2904
2905 private:
2906 DISALLOW_COPY_AND_ASSIGN(HOr);
2907};
2908
2909class HXor : public HBinaryOperation {
2910 public:
2911 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2912 : HBinaryOperation(result_type, left, right) {}
2913
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002914 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002915
2916 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2917 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2918
2919 DECLARE_INSTRUCTION(Xor);
2920
2921 private:
2922 DISALLOW_COPY_AND_ASSIGN(HXor);
2923};
2924
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002925// The value of a parameter in this method. Its location depends on
2926// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002927class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002928 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002929 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2930 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002931
2932 uint8_t GetIndex() const { return index_; }
2933
Calin Juravle10e244f2015-01-26 18:54:32 +00002934 bool CanBeNull() const OVERRIDE { return !is_this_; }
2935
Calin Juravle3cd4fc82015-05-14 15:15:42 +01002936 bool IsThis() const { return is_this_; }
2937
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002938 DECLARE_INSTRUCTION(ParameterValue);
2939
2940 private:
2941 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002942 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002943 const uint8_t index_;
2944
Calin Juravle10e244f2015-01-26 18:54:32 +00002945 // Whether or not the parameter value corresponds to 'this' argument.
2946 const bool is_this_;
2947
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002948 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2949};
2950
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002951class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002952 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002953 explicit HNot(Primitive::Type result_type, HInstruction* input)
2954 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002955
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002956 bool CanBeMoved() const OVERRIDE { return true; }
2957 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002958 UNUSED(other);
2959 return true;
2960 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002961
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002962 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2963 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002964
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002965 DECLARE_INSTRUCTION(Not);
2966
2967 private:
2968 DISALLOW_COPY_AND_ASSIGN(HNot);
2969};
2970
David Brazdil66d126e2015-04-03 16:02:44 +01002971class HBooleanNot : public HUnaryOperation {
2972 public:
2973 explicit HBooleanNot(HInstruction* input)
2974 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2975
2976 bool CanBeMoved() const OVERRIDE { return true; }
2977 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2978 UNUSED(other);
2979 return true;
2980 }
2981
2982 int32_t Evaluate(int32_t x) const OVERRIDE {
2983 DCHECK(IsUint<1>(x));
2984 return !x;
2985 }
2986
2987 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2988 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2989 UNREACHABLE();
2990 }
2991
2992 DECLARE_INSTRUCTION(BooleanNot);
2993
2994 private:
2995 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2996};
2997
Roland Levillaindff1f282014-11-05 14:15:05 +00002998class HTypeConversion : public HExpression<1> {
2999 public:
3000 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003001 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3002 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003003 SetRawInputAt(0, input);
3004 DCHECK_NE(input->GetType(), result_type);
3005 }
3006
3007 HInstruction* GetInput() const { return InputAt(0); }
3008 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3009 Primitive::Type GetResultType() const { return GetType(); }
3010
Roland Levillain624279f2014-12-04 11:54:28 +00003011 // Required by the x86 and ARM code generators when producing calls
3012 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003013 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003014
Roland Levillaindff1f282014-11-05 14:15:05 +00003015 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003016 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003017
Mark Mendelle82549b2015-05-06 10:55:34 -04003018 // Try to statically evaluate the conversion and return a HConstant
3019 // containing the result. If the input cannot be converted, return nullptr.
3020 HConstant* TryStaticEvaluation() const;
3021
Roland Levillaindff1f282014-11-05 14:15:05 +00003022 DECLARE_INSTRUCTION(TypeConversion);
3023
3024 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003025 const uint32_t dex_pc_;
3026
Roland Levillaindff1f282014-11-05 14:15:05 +00003027 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3028};
3029
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003030static constexpr uint32_t kNoRegNumber = -1;
3031
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003032class HPhi : public HInstruction {
3033 public:
3034 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003035 : HInstruction(SideEffects::None()),
3036 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003037 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003038 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003039 is_live_(false),
3040 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003041 inputs_.SetSize(number_of_inputs);
3042 }
3043
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003044 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3045 static Primitive::Type ToPhiType(Primitive::Type type) {
3046 switch (type) {
3047 case Primitive::kPrimBoolean:
3048 case Primitive::kPrimByte:
3049 case Primitive::kPrimShort:
3050 case Primitive::kPrimChar:
3051 return Primitive::kPrimInt;
3052 default:
3053 return type;
3054 }
3055 }
3056
Calin Juravle10e244f2015-01-26 18:54:32 +00003057 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003058
3059 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003060 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003061
Calin Juravle10e244f2015-01-26 18:54:32 +00003062 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003063 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003064
Calin Juravle10e244f2015-01-26 18:54:32 +00003065 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3066 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3067
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003068 uint32_t GetRegNumber() const { return reg_number_; }
3069
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003070 void SetDead() { is_live_ = false; }
3071 void SetLive() { is_live_ = true; }
3072 bool IsDead() const { return !is_live_; }
3073 bool IsLive() const { return is_live_; }
3074
Calin Juravlea4f88312015-04-16 12:57:19 +01003075 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3076 // An equivalent phi is a phi having the same dex register and type.
3077 // It assumes that phis with the same dex register are adjacent.
3078 HPhi* GetNextEquivalentPhiWithSameType() {
3079 HInstruction* next = GetNext();
3080 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3081 if (next->GetType() == GetType()) {
3082 return next->AsPhi();
3083 }
3084 next = next->GetNext();
3085 }
3086 return nullptr;
3087 }
3088
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003089 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003090
David Brazdil1abb4192015-02-17 18:33:36 +00003091 protected:
3092 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3093
3094 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3095 inputs_.Put(index, input);
3096 }
3097
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003098 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003099 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003100 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003101 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003102 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003103 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003104
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003105 DISALLOW_COPY_AND_ASSIGN(HPhi);
3106};
3107
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003108class HNullCheck : public HExpression<1> {
3109 public:
3110 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003111 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003112 SetRawInputAt(0, value);
3113 }
3114
Calin Juravle10e244f2015-01-26 18:54:32 +00003115 bool CanBeMoved() const OVERRIDE { return true; }
3116 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003117 UNUSED(other);
3118 return true;
3119 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003120
Calin Juravle10e244f2015-01-26 18:54:32 +00003121 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003122
Calin Juravle10e244f2015-01-26 18:54:32 +00003123 bool CanThrow() const OVERRIDE { return true; }
3124
3125 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003126
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003127 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003128
3129 DECLARE_INSTRUCTION(NullCheck);
3130
3131 private:
3132 const uint32_t dex_pc_;
3133
3134 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3135};
3136
3137class FieldInfo : public ValueObject {
3138 public:
Calin Juravle52c48962014-12-16 17:02:57 +00003139 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3140 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003141
3142 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003143 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003144 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003145
3146 private:
3147 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003148 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003149 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003150};
3151
3152class HInstanceFieldGet : public HExpression<1> {
3153 public:
3154 HInstanceFieldGet(HInstruction* value,
3155 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003156 MemberOffset field_offset,
3157 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003158 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003159 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003160 SetRawInputAt(0, value);
3161 }
3162
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003163 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003164
3165 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3166 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3167 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003168 }
3169
Calin Juravle641547a2015-04-21 22:08:51 +01003170 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3171 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003172 }
3173
3174 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003175 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3176 }
3177
Calin Juravle52c48962014-12-16 17:02:57 +00003178 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003179 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003180 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003181 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003182
3183 DECLARE_INSTRUCTION(InstanceFieldGet);
3184
3185 private:
3186 const FieldInfo field_info_;
3187
3188 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3189};
3190
3191class HInstanceFieldSet : public HTemplateInstruction<2> {
3192 public:
3193 HInstanceFieldSet(HInstruction* object,
3194 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003195 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003196 MemberOffset field_offset,
3197 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003198 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003199 field_info_(field_offset, field_type, is_volatile),
3200 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003201 SetRawInputAt(0, object);
3202 SetRawInputAt(1, value);
3203 }
3204
Calin Juravle641547a2015-04-21 22:08:51 +01003205 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3206 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003207 }
3208
Calin Juravle52c48962014-12-16 17:02:57 +00003209 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003210 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003211 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003212 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003213 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003214 bool GetValueCanBeNull() const { return value_can_be_null_; }
3215 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003216
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003217 DECLARE_INSTRUCTION(InstanceFieldSet);
3218
3219 private:
3220 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003221 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003222
3223 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3224};
3225
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003226class HArrayGet : public HExpression<2> {
3227 public:
3228 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003229 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003230 SetRawInputAt(0, array);
3231 SetRawInputAt(1, index);
3232 }
3233
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003234 bool CanBeMoved() const OVERRIDE { return true; }
3235 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003236 UNUSED(other);
3237 return true;
3238 }
Calin Juravle641547a2015-04-21 22:08:51 +01003239 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3240 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003241 // TODO: We can be smarter here.
3242 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3243 // which generates the implicit null check. There are cases when these can be removed
3244 // to produce better code. If we ever add optimizations to do so we should allow an
3245 // implicit check here (as long as the address falls in the first page).
3246 return false;
3247 }
3248
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003249 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003250
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003251 HInstruction* GetArray() const { return InputAt(0); }
3252 HInstruction* GetIndex() const { return InputAt(1); }
3253
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003254 DECLARE_INSTRUCTION(ArrayGet);
3255
3256 private:
3257 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3258};
3259
3260class HArraySet : public HTemplateInstruction<3> {
3261 public:
3262 HArraySet(HInstruction* array,
3263 HInstruction* index,
3264 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003265 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003266 uint32_t dex_pc)
3267 : HTemplateInstruction(SideEffects::ChangesSomething()),
3268 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003269 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003270 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3271 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003272 SetRawInputAt(0, array);
3273 SetRawInputAt(1, index);
3274 SetRawInputAt(2, value);
3275 }
3276
Calin Juravle77520bc2015-01-12 18:45:46 +00003277 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278 // We currently always call a runtime method to catch array store
3279 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003280 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003281 }
3282
Calin Juravle641547a2015-04-21 22:08:51 +01003283 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3284 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003285 // TODO: Same as for ArrayGet.
3286 return false;
3287 }
3288
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003289 void ClearNeedsTypeCheck() {
3290 needs_type_check_ = false;
3291 }
3292
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003293 void ClearValueCanBeNull() {
3294 value_can_be_null_ = false;
3295 }
3296
3297 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003298 bool NeedsTypeCheck() const { return needs_type_check_; }
3299
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003300 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003301
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003302 HInstruction* GetArray() const { return InputAt(0); }
3303 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003304 HInstruction* GetValue() const { return InputAt(2); }
3305
3306 Primitive::Type GetComponentType() const {
3307 // The Dex format does not type floating point index operations. Since the
3308 // `expected_component_type_` is set during building and can therefore not
3309 // be correct, we also check what is the value type. If it is a floating
3310 // point type, we must use that type.
3311 Primitive::Type value_type = GetValue()->GetType();
3312 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3313 ? value_type
3314 : expected_component_type_;
3315 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003316
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003317 DECLARE_INSTRUCTION(ArraySet);
3318
3319 private:
3320 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003321 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003322 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003323 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003324
3325 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3326};
3327
3328class HArrayLength : public HExpression<1> {
3329 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003330 explicit HArrayLength(HInstruction* array)
3331 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3332 // Note that arrays do not change length, so the instruction does not
3333 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003334 SetRawInputAt(0, array);
3335 }
3336
Calin Juravle77520bc2015-01-12 18:45:46 +00003337 bool CanBeMoved() const OVERRIDE { return true; }
3338 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003339 UNUSED(other);
3340 return true;
3341 }
Calin Juravle641547a2015-04-21 22:08:51 +01003342 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3343 return obj == InputAt(0);
3344 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003345
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003346 DECLARE_INSTRUCTION(ArrayLength);
3347
3348 private:
3349 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3350};
3351
3352class HBoundsCheck : public HExpression<2> {
3353 public:
3354 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003355 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003356 DCHECK(index->GetType() == Primitive::kPrimInt);
3357 SetRawInputAt(0, index);
3358 SetRawInputAt(1, length);
3359 }
3360
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003361 bool CanBeMoved() const OVERRIDE { return true; }
3362 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003363 UNUSED(other);
3364 return true;
3365 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003367 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003368
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003369 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003370
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003371 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003372
3373 DECLARE_INSTRUCTION(BoundsCheck);
3374
3375 private:
3376 const uint32_t dex_pc_;
3377
3378 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3379};
3380
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003381/**
3382 * Some DEX instructions are folded into multiple HInstructions that need
3383 * to stay live until the last HInstruction. This class
3384 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003385 * HInstruction stays live. `index` represents the stack location index of the
3386 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003387 */
3388class HTemporary : public HTemplateInstruction<0> {
3389 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003390 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003391
3392 size_t GetIndex() const { return index_; }
3393
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003394 Primitive::Type GetType() const OVERRIDE {
3395 // The previous instruction is the one that will be stored in the temporary location.
3396 DCHECK(GetPrevious() != nullptr);
3397 return GetPrevious()->GetType();
3398 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003399
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003400 DECLARE_INSTRUCTION(Temporary);
3401
3402 private:
3403 const size_t index_;
3404
3405 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3406};
3407
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003408class HSuspendCheck : public HTemplateInstruction<0> {
3409 public:
3410 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003411 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003412
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003413 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003414 return true;
3415 }
3416
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003417 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003418 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3419 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003420
3421 DECLARE_INSTRUCTION(SuspendCheck);
3422
3423 private:
3424 const uint32_t dex_pc_;
3425
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003426 // Only used for code generation, in order to share the same slow path between back edges
3427 // of a same loop.
3428 SlowPathCode* slow_path_;
3429
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003430 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3431};
3432
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003433/**
3434 * Instruction to load a Class object.
3435 */
3436class HLoadClass : public HExpression<0> {
3437 public:
3438 HLoadClass(uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003439 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003440 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003441 uint32_t dex_pc)
3442 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3443 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003444 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003445 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003446 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003447 generate_clinit_check_(false),
3448 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003449
3450 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003451
3452 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3453 return other->AsLoadClass()->type_index_ == type_index_;
3454 }
3455
3456 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3457
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003458 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003459 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003460 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003461
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003462 bool NeedsEnvironment() const OVERRIDE {
3463 // Will call runtime and load the class if the class is not loaded yet.
3464 // TODO: finer grain decision.
3465 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003466 }
3467
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003468 bool MustGenerateClinitCheck() const {
3469 return generate_clinit_check_;
3470 }
3471
Calin Juravle0ba218d2015-05-19 18:46:01 +01003472 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3473 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003474 }
3475
3476 bool CanCallRuntime() const {
3477 return MustGenerateClinitCheck() || !is_referrers_class_;
3478 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003479
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003480 bool CanThrow() const OVERRIDE {
3481 // May call runtime and and therefore can throw.
3482 // TODO: finer grain decision.
3483 return !is_referrers_class_;
3484 }
3485
Calin Juravleacf735c2015-02-12 15:25:22 +00003486 ReferenceTypeInfo GetLoadedClassRTI() {
3487 return loaded_class_rti_;
3488 }
3489
3490 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3491 // Make sure we only set exact types (the loaded class should never be merged).
3492 DCHECK(rti.IsExact());
3493 loaded_class_rti_ = rti;
3494 }
3495
3496 bool IsResolved() {
3497 return loaded_class_rti_.IsExact();
3498 }
3499
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003500 const DexFile& GetDexFile() { return dex_file_; }
3501
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003502 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3503
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003504 DECLARE_INSTRUCTION(LoadClass);
3505
3506 private:
3507 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003508 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003509 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003510 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003511 // Whether this instruction must generate the initialization check.
3512 // Used for code generation.
3513 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003514
Calin Juravleacf735c2015-02-12 15:25:22 +00003515 ReferenceTypeInfo loaded_class_rti_;
3516
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003517 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3518};
3519
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003520class HLoadString : public HExpression<0> {
3521 public:
3522 HLoadString(uint32_t string_index, uint32_t dex_pc)
3523 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3524 string_index_(string_index),
3525 dex_pc_(dex_pc) {}
3526
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003527 bool CanBeMoved() const OVERRIDE { return true; }
3528
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003529 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3530 return other->AsLoadString()->string_index_ == string_index_;
3531 }
3532
3533 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3534
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003535 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003536 uint32_t GetStringIndex() const { return string_index_; }
3537
3538 // TODO: Can we deopt or debug when we resolve a string?
3539 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003540 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003541
3542 DECLARE_INSTRUCTION(LoadString);
3543
3544 private:
3545 const uint32_t string_index_;
3546 const uint32_t dex_pc_;
3547
3548 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3549};
3550
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003551/**
3552 * Performs an initialization check on its Class object input.
3553 */
3554class HClinitCheck : public HExpression<1> {
3555 public:
3556 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003557 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003558 dex_pc_(dex_pc) {
3559 SetRawInputAt(0, constant);
3560 }
3561
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003562 bool CanBeMoved() const OVERRIDE { return true; }
3563 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3564 UNUSED(other);
3565 return true;
3566 }
3567
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003568 bool NeedsEnvironment() const OVERRIDE {
3569 // May call runtime to initialize the class.
3570 return true;
3571 }
3572
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003573 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003574
3575 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3576
3577 DECLARE_INSTRUCTION(ClinitCheck);
3578
3579 private:
3580 const uint32_t dex_pc_;
3581
3582 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3583};
3584
3585class HStaticFieldGet : public HExpression<1> {
3586 public:
3587 HStaticFieldGet(HInstruction* cls,
3588 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003589 MemberOffset field_offset,
3590 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003591 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003592 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003593 SetRawInputAt(0, cls);
3594 }
3595
Calin Juravle52c48962014-12-16 17:02:57 +00003596
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003597 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003598
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003599 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003600 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3601 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003602 }
3603
3604 size_t ComputeHashCode() const OVERRIDE {
3605 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3606 }
3607
Calin Juravle52c48962014-12-16 17:02:57 +00003608 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003609 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3610 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003611 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003612
3613 DECLARE_INSTRUCTION(StaticFieldGet);
3614
3615 private:
3616 const FieldInfo field_info_;
3617
3618 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3619};
3620
3621class HStaticFieldSet : public HTemplateInstruction<2> {
3622 public:
3623 HStaticFieldSet(HInstruction* cls,
3624 HInstruction* value,
3625 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003626 MemberOffset field_offset,
3627 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003628 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003629 field_info_(field_offset, field_type, is_volatile),
3630 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003631 SetRawInputAt(0, cls);
3632 SetRawInputAt(1, value);
3633 }
3634
Calin Juravle52c48962014-12-16 17:02:57 +00003635 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003636 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3637 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003638 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003639
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003640 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003641 bool GetValueCanBeNull() const { return value_can_be_null_; }
3642 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003643
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003644 DECLARE_INSTRUCTION(StaticFieldSet);
3645
3646 private:
3647 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003648 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003649
3650 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3651};
3652
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003653// Implement the move-exception DEX instruction.
3654class HLoadException : public HExpression<0> {
3655 public:
3656 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3657
3658 DECLARE_INSTRUCTION(LoadException);
3659
3660 private:
3661 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3662};
3663
3664class HThrow : public HTemplateInstruction<1> {
3665 public:
3666 HThrow(HInstruction* exception, uint32_t dex_pc)
3667 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3668 SetRawInputAt(0, exception);
3669 }
3670
3671 bool IsControlFlow() const OVERRIDE { return true; }
3672
3673 bool NeedsEnvironment() const OVERRIDE { return true; }
3674
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003675 bool CanThrow() const OVERRIDE { return true; }
3676
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003677 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003678
3679 DECLARE_INSTRUCTION(Throw);
3680
3681 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003682 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003683
3684 DISALLOW_COPY_AND_ASSIGN(HThrow);
3685};
3686
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003687class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003688 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003689 HInstanceOf(HInstruction* object,
3690 HLoadClass* constant,
3691 bool class_is_final,
3692 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003693 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3694 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003695 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003696 dex_pc_(dex_pc) {
3697 SetRawInputAt(0, object);
3698 SetRawInputAt(1, constant);
3699 }
3700
3701 bool CanBeMoved() const OVERRIDE { return true; }
3702
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003703 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003704 return true;
3705 }
3706
3707 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003708 return false;
3709 }
3710
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003711 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003712
3713 bool IsClassFinal() const { return class_is_final_; }
3714
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003715 // Used only in code generation.
3716 bool MustDoNullCheck() const { return must_do_null_check_; }
3717 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3718
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003719 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003720
3721 private:
3722 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003723 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003724 const uint32_t dex_pc_;
3725
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003726 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3727};
3728
Calin Juravleb1498f62015-02-16 13:13:29 +00003729class HBoundType : public HExpression<1> {
3730 public:
3731 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3732 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3733 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003734 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003735 SetRawInputAt(0, input);
3736 }
3737
3738 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3739
3740 bool CanBeNull() const OVERRIDE {
3741 // `null instanceof ClassX` always return false so we can't be null.
3742 return false;
3743 }
3744
3745 DECLARE_INSTRUCTION(BoundType);
3746
3747 private:
3748 // Encodes the most upper class that this instruction can have. In other words
3749 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3750 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3751 const ReferenceTypeInfo bound_type_;
3752
3753 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3754};
3755
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003756class HCheckCast : public HTemplateInstruction<2> {
3757 public:
3758 HCheckCast(HInstruction* object,
3759 HLoadClass* constant,
3760 bool class_is_final,
3761 uint32_t dex_pc)
3762 : HTemplateInstruction(SideEffects::None()),
3763 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003764 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003765 dex_pc_(dex_pc) {
3766 SetRawInputAt(0, object);
3767 SetRawInputAt(1, constant);
3768 }
3769
3770 bool CanBeMoved() const OVERRIDE { return true; }
3771
3772 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3773 return true;
3774 }
3775
3776 bool NeedsEnvironment() const OVERRIDE {
3777 // Instruction may throw a CheckCastError.
3778 return true;
3779 }
3780
3781 bool CanThrow() const OVERRIDE { return true; }
3782
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003783 bool MustDoNullCheck() const { return must_do_null_check_; }
3784 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3785
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003786 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003787
3788 bool IsClassFinal() const { return class_is_final_; }
3789
3790 DECLARE_INSTRUCTION(CheckCast);
3791
3792 private:
3793 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003794 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003795 const uint32_t dex_pc_;
3796
3797 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003798};
3799
Calin Juravle27df7582015-04-17 19:12:31 +01003800class HMemoryBarrier : public HTemplateInstruction<0> {
3801 public:
3802 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3803 : HTemplateInstruction(SideEffects::None()),
3804 barrier_kind_(barrier_kind) {}
3805
3806 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3807
3808 DECLARE_INSTRUCTION(MemoryBarrier);
3809
3810 private:
3811 const MemBarrierKind barrier_kind_;
3812
3813 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3814};
3815
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003816class HMonitorOperation : public HTemplateInstruction<1> {
3817 public:
3818 enum OperationKind {
3819 kEnter,
3820 kExit,
3821 };
3822
3823 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3824 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3825 SetRawInputAt(0, object);
3826 }
3827
3828 // Instruction may throw a Java exception, so we need an environment.
3829 bool NeedsEnvironment() const OVERRIDE { return true; }
3830 bool CanThrow() const OVERRIDE { return true; }
3831
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003832 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003833
3834 bool IsEnter() const { return kind_ == kEnter; }
3835
3836 DECLARE_INSTRUCTION(MonitorOperation);
3837
Calin Juravle52c48962014-12-16 17:02:57 +00003838 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003839 const OperationKind kind_;
3840 const uint32_t dex_pc_;
3841
3842 private:
3843 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3844};
3845
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003846class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003847 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003848 MoveOperands(Location source,
3849 Location destination,
3850 Primitive::Type type,
3851 HInstruction* instruction)
3852 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003853
3854 Location GetSource() const { return source_; }
3855 Location GetDestination() const { return destination_; }
3856
3857 void SetSource(Location value) { source_ = value; }
3858 void SetDestination(Location value) { destination_ = value; }
3859
3860 // The parallel move resolver marks moves as "in-progress" by clearing the
3861 // destination (but not the source).
3862 Location MarkPending() {
3863 DCHECK(!IsPending());
3864 Location dest = destination_;
3865 destination_ = Location::NoLocation();
3866 return dest;
3867 }
3868
3869 void ClearPending(Location dest) {
3870 DCHECK(IsPending());
3871 destination_ = dest;
3872 }
3873
3874 bool IsPending() const {
3875 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3876 return destination_.IsInvalid() && !source_.IsInvalid();
3877 }
3878
3879 // True if this blocks a move from the given location.
3880 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003881 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003882 }
3883
3884 // A move is redundant if it's been eliminated, if its source and
3885 // destination are the same, or if its destination is unneeded.
3886 bool IsRedundant() const {
3887 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3888 }
3889
3890 // We clear both operands to indicate move that's been eliminated.
3891 void Eliminate() {
3892 source_ = destination_ = Location::NoLocation();
3893 }
3894
3895 bool IsEliminated() const {
3896 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3897 return source_.IsInvalid();
3898 }
3899
Nicolas Geoffray90218252015-04-15 11:56:51 +01003900 bool Is64BitMove() const {
3901 return Primitive::Is64BitType(type_);
3902 }
3903
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003904 HInstruction* GetInstruction() const { return instruction_; }
3905
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003906 private:
3907 Location source_;
3908 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003909 // The type this move is for.
3910 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003911 // The instruction this move is assocatied with. Null when this move is
3912 // for moving an input in the expected locations of user (including a phi user).
3913 // This is only used in debug mode, to ensure we do not connect interval siblings
3914 // in the same parallel move.
3915 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003916};
3917
3918static constexpr size_t kDefaultNumberOfMoves = 4;
3919
3920class HParallelMove : public HTemplateInstruction<0> {
3921 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003922 explicit HParallelMove(ArenaAllocator* arena)
3923 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003924
Nicolas Geoffray90218252015-04-15 11:56:51 +01003925 void AddMove(Location source,
3926 Location destination,
3927 Primitive::Type type,
3928 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003929 DCHECK(source.IsValid());
3930 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003931 if (kIsDebugBuild) {
3932 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003933 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003934 if (moves_.Get(i).GetInstruction() == instruction) {
3935 // Special case the situation where the move is for the spill slot
3936 // of the instruction.
3937 if ((GetPrevious() == instruction)
3938 || ((GetPrevious() == nullptr)
3939 && instruction->IsPhi()
3940 && instruction->GetBlock() == GetBlock())) {
3941 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3942 << "Doing parallel moves for the same instruction.";
3943 } else {
3944 DCHECK(false) << "Doing parallel moves for the same instruction.";
3945 }
3946 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003947 }
3948 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003949 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003950 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01003951 << "Overlapped destination for two moves in a parallel move: "
3952 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
3953 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003954 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003955 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003956 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003957 }
3958
3959 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003960 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003961 }
3962
3963 size_t NumMoves() const { return moves_.Size(); }
3964
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003965 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003966
3967 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003968 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003969
3970 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3971};
3972
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003973class HGraphVisitor : public ValueObject {
3974 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003975 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3976 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003977
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003978 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003979 virtual void VisitBasicBlock(HBasicBlock* block);
3980
Roland Levillain633021e2014-10-01 14:12:25 +01003981 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003982 void VisitInsertionOrder();
3983
Roland Levillain633021e2014-10-01 14:12:25 +01003984 // Visit the graph following dominator tree reverse post-order.
3985 void VisitReversePostOrder();
3986
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003987 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003988
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003989 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003990#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003991 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3992
3993 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3994
3995#undef DECLARE_VISIT_INSTRUCTION
3996
3997 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003998 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003999
4000 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4001};
4002
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004003class HGraphDelegateVisitor : public HGraphVisitor {
4004 public:
4005 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4006 virtual ~HGraphDelegateVisitor() {}
4007
4008 // Visit functions that delegate to to super class.
4009#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004010 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004011
4012 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4013
4014#undef DECLARE_VISIT_INSTRUCTION
4015
4016 private:
4017 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4018};
4019
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004020class HInsertionOrderIterator : public ValueObject {
4021 public:
4022 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4023
4024 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4025 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4026 void Advance() { ++index_; }
4027
4028 private:
4029 const HGraph& graph_;
4030 size_t index_;
4031
4032 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4033};
4034
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004035class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004036 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004037 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4038 // Check that reverse post order of the graph has been built.
4039 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4040 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004041
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004042 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4043 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004044 void Advance() { ++index_; }
4045
4046 private:
4047 const HGraph& graph_;
4048 size_t index_;
4049
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004050 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004051};
4052
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004053class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004054 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004055 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004056 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4057 // Check that reverse post order of the graph has been built.
4058 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4059 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004060
4061 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004062 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004063 void Advance() { --index_; }
4064
4065 private:
4066 const HGraph& graph_;
4067 size_t index_;
4068
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004069 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004070};
4071
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004072class HLinearPostOrderIterator : public ValueObject {
4073 public:
4074 explicit HLinearPostOrderIterator(const HGraph& graph)
4075 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4076
4077 bool Done() const { return index_ == 0; }
4078
4079 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4080
4081 void Advance() {
4082 --index_;
4083 DCHECK_GE(index_, 0U);
4084 }
4085
4086 private:
4087 const GrowableArray<HBasicBlock*>& order_;
4088 size_t index_;
4089
4090 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4091};
4092
4093class HLinearOrderIterator : public ValueObject {
4094 public:
4095 explicit HLinearOrderIterator(const HGraph& graph)
4096 : order_(graph.GetLinearOrder()), index_(0) {}
4097
4098 bool Done() const { return index_ == order_.Size(); }
4099 HBasicBlock* Current() const { return order_.Get(index_); }
4100 void Advance() { ++index_; }
4101
4102 private:
4103 const GrowableArray<HBasicBlock*>& order_;
4104 size_t index_;
4105
4106 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4107};
4108
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004109// Iterator over the blocks that art part of the loop. Includes blocks part
4110// of an inner loop. The order in which the blocks are iterated is on their
4111// block id.
4112class HBlocksInLoopIterator : public ValueObject {
4113 public:
4114 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4115 : blocks_in_loop_(info.GetBlocks()),
4116 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4117 index_(0) {
4118 if (!blocks_in_loop_.IsBitSet(index_)) {
4119 Advance();
4120 }
4121 }
4122
4123 bool Done() const { return index_ == blocks_.Size(); }
4124 HBasicBlock* Current() const { return blocks_.Get(index_); }
4125 void Advance() {
4126 ++index_;
4127 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4128 if (blocks_in_loop_.IsBitSet(index_)) {
4129 break;
4130 }
4131 }
4132 }
4133
4134 private:
4135 const BitVector& blocks_in_loop_;
4136 const GrowableArray<HBasicBlock*>& blocks_;
4137 size_t index_;
4138
4139 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4140};
4141
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004142inline int64_t Int64FromConstant(HConstant* constant) {
4143 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4144 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4145 : constant->AsLongConstant()->GetValue();
4146}
4147
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004148} // namespace art
4149
4150#endif // ART_COMPILER_OPTIMIZING_NODES_H_