blob: d542e596210e1f73f8c4963950a38f0c44003032 [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;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010038class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000039class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010040class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000041class HFloatConstant;
42class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000045class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000047class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010048class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010049class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010050class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000051class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010052class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000054
55static const int kDefaultNumberOfBlocks = 8;
56static const int kDefaultNumberOfSuccessors = 2;
57static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010058static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000059static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060
Calin Juravle9aec02f2014-11-18 23:06:35 +000061static constexpr uint32_t kMaxIntShiftValue = 0x1f;
62static constexpr uint64_t kMaxLongShiftValue = 0x3f;
63
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010064static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
65
Dave Allison20dfc792014-06-16 20:44:29 -070066enum IfCondition {
67 kCondEQ,
68 kCondNE,
69 kCondLT,
70 kCondLE,
71 kCondGT,
72 kCondGE,
73};
74
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010075class HInstructionList {
76 public:
77 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
78
79 void AddInstruction(HInstruction* instruction);
80 void RemoveInstruction(HInstruction* instruction);
81
David Brazdilc3d743f2015-04-22 13:40:50 +010082 // Insert `instruction` before/after an existing instruction `cursor`.
83 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
84 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
85
Roland Levillain6b469232014-09-25 10:10:38 +010086 // Return true if this list contains `instruction`.
87 bool Contains(HInstruction* instruction) const;
88
Roland Levillainccc07a92014-09-16 14:48:16 +010089 // Return true if `instruction1` is found before `instruction2` in
90 // this instruction list and false otherwise. Abort if none
91 // of these instructions is found.
92 bool FoundBefore(const HInstruction* instruction1,
93 const HInstruction* instruction2) const;
94
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000095 bool IsEmpty() const { return first_instruction_ == nullptr; }
96 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
97
98 // Update the block of all instructions to be `block`.
99 void SetBlockOfInstructions(HBasicBlock* block) const;
100
101 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
102 void Add(const HInstructionList& instruction_list);
103
David Brazdil2d7352b2015-04-20 14:52:42 +0100104 // Return the number of instructions in the list. This is an expensive operation.
105 size_t CountSize() const;
106
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 private:
108 HInstruction* first_instruction_;
109 HInstruction* last_instruction_;
110
111 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000112 friend class HGraph;
113 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100114 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100115 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100116
117 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
118};
119
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000120// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700121class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000122 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100123 HGraph(ArenaAllocator* arena,
124 const DexFile& dex_file,
125 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100126 bool should_generate_constructor_barrier,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100127 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100128 bool debuggable = false,
129 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000130 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000131 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100132 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100133 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700134 entry_block_(nullptr),
135 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100136 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100137 number_of_vregs_(0),
138 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000139 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400140 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000141 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000142 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100143 dex_file_(dex_file),
144 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100145 invoke_type_(invoke_type),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100146 should_generate_constructor_barrier_(should_generate_constructor_barrier),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000147 cached_null_constant_(nullptr),
148 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000149 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
150 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100151 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
152 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000153
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000154 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100155 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100156 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000157
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000158 HBasicBlock* GetEntryBlock() const { return entry_block_; }
159 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100160 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000161
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000162 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
163 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000164
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000165 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000167 // Try building the SSA form of this graph, with dominance computation and loop
168 // recognition. Returns whether it was successful in doing all these steps.
169 bool TryBuildingSsa() {
170 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000171 // The SSA builder requires loops to all be natural. Specifically, the dead phi
172 // elimination phase checks the consistency of the graph when doing a post-order
173 // visit for eliminating dead phis: a dead phi can only have loop header phi
174 // users remaining when being visited.
175 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000177 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000178 }
179
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000180 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100182 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000183
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000184 // Analyze all natural loops in this graph. Returns false if one
185 // loop is not natural, that is the header does not dominate the
186 // back edge.
187 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100188
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000189 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
190 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
191
David Brazdil2d7352b2015-04-20 14:52:42 +0100192 // Removes `block` from the graph.
193 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000194
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100195 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
196 void SimplifyLoop(HBasicBlock* header);
197
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000198 int32_t GetNextInstructionId() {
199 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000200 return current_instruction_id_++;
201 }
202
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000203 int32_t GetCurrentInstructionId() const {
204 return current_instruction_id_;
205 }
206
207 void SetCurrentInstructionId(int32_t id) {
208 current_instruction_id_ = id;
209 }
210
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100211 uint16_t GetMaximumNumberOfOutVRegs() const {
212 return maximum_number_of_out_vregs_;
213 }
214
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
216 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100217 }
218
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000219 void UpdateTemporariesVRegSlots(size_t slots) {
220 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100221 }
222
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000223 size_t GetTemporariesVRegSlots() const {
224 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100225 }
226
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100227 void SetNumberOfVRegs(uint16_t number_of_vregs) {
228 number_of_vregs_ = number_of_vregs;
229 }
230
231 uint16_t GetNumberOfVRegs() const {
232 return number_of_vregs_;
233 }
234
235 void SetNumberOfInVRegs(uint16_t value) {
236 number_of_in_vregs_ = value;
237 }
238
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100239 uint16_t GetNumberOfLocalVRegs() const {
240 return number_of_vregs_ - number_of_in_vregs_;
241 }
242
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100243 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
244 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100245 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100246
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100247 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
248 return linear_order_;
249 }
250
Mark Mendell1152c922015-04-24 17:06:35 -0400251 bool HasBoundsChecks() const {
252 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800253 }
254
Mark Mendell1152c922015-04-24 17:06:35 -0400255 void SetHasBoundsChecks(bool value) {
256 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800257 }
258
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100259 bool ShouldGenerateConstructorBarrier() const {
260 return should_generate_constructor_barrier_;
261 }
262
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000263 bool IsDebuggable() const { return debuggable_; }
264
David Brazdil8d5b8b22015-03-24 10:51:52 +0000265 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000266 // already, it is created and inserted into the graph. This method is only for
267 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000268 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000269 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000270 HIntConstant* GetIntConstant(int32_t value) {
271 return CreateConstant(value, &cached_int_constants_);
272 }
273 HLongConstant* GetLongConstant(int64_t value) {
274 return CreateConstant(value, &cached_long_constants_);
275 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000276 HFloatConstant* GetFloatConstant(float value) {
277 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
278 }
279 HDoubleConstant* GetDoubleConstant(double value) {
280 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
281 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000282
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100283 HCurrentMethod* GetCurrentMethod();
284
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000285 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100286
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100287 const DexFile& GetDexFile() const {
288 return dex_file_;
289 }
290
291 uint32_t GetMethodIdx() const {
292 return method_idx_;
293 }
294
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100295 InvokeType GetInvokeType() const {
296 return invoke_type_;
297 }
298
David Brazdil2d7352b2015-04-20 14:52:42 +0100299 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000300 void VisitBlockForDominatorTree(HBasicBlock* block,
301 HBasicBlock* predecessor,
302 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100303 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000304 void VisitBlockForBackEdges(HBasicBlock* block,
305 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100306 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000307 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100308 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000309
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000310 template <class InstructionType, typename ValueType>
311 InstructionType* CreateConstant(ValueType value,
312 ArenaSafeMap<ValueType, InstructionType*>* cache) {
313 // Try to find an existing constant of the given value.
314 InstructionType* constant = nullptr;
315 auto cached_constant = cache->find(value);
316 if (cached_constant != cache->end()) {
317 constant = cached_constant->second;
318 }
319
320 // If not found or previously deleted, create and cache a new instruction.
321 if (constant == nullptr || constant->GetBlock() == nullptr) {
322 constant = new (arena_) InstructionType(value);
323 cache->Overwrite(value, constant);
324 InsertConstant(constant);
325 }
326 return constant;
327 }
328
David Brazdil8d5b8b22015-03-24 10:51:52 +0000329 void InsertConstant(HConstant* instruction);
330
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000331 // Cache a float constant into the graph. This method should only be
332 // called by the SsaBuilder when creating "equivalent" instructions.
333 void CacheFloatConstant(HFloatConstant* constant);
334
335 // See CacheFloatConstant comment.
336 void CacheDoubleConstant(HDoubleConstant* constant);
337
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000338 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000339
340 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000341 GrowableArray<HBasicBlock*> blocks_;
342
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100343 // List of blocks to perform a reverse post order tree traversal.
344 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000345
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100346 // List of blocks to perform a linear order tree traversal.
347 GrowableArray<HBasicBlock*> linear_order_;
348
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000349 HBasicBlock* entry_block_;
350 HBasicBlock* exit_block_;
351
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100352 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100353 uint16_t maximum_number_of_out_vregs_;
354
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100355 // The number of virtual registers in this method. Contains the parameters.
356 uint16_t number_of_vregs_;
357
358 // The number of virtual registers used by parameters of this method.
359 uint16_t number_of_in_vregs_;
360
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000361 // Number of vreg size slots that the temporaries use (used in baseline compiler).
362 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100363
Mark Mendell1152c922015-04-24 17:06:35 -0400364 // Has bounds checks. We can totally skip BCE if it's false.
365 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800366
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000367 // Indicates whether the graph should be compiled in a way that
368 // ensures full debuggability. If false, we can apply more
369 // aggressive optimizations that may limit the level of debugging.
370 const bool debuggable_;
371
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000372 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000373 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000374
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100375 // The dex file from which the method is from.
376 const DexFile& dex_file_;
377
378 // The method index in the dex file.
379 const uint32_t method_idx_;
380
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100381 // If inlined, this encodes how the callee is being invoked.
382 const InvokeType invoke_type_;
383
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100384 const bool should_generate_constructor_barrier_;
385
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000386 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000387 HNullConstant* cached_null_constant_;
388 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000389 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000390 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000391 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000392
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100393 HCurrentMethod* cached_current_method_;
394
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000395 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100396 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000397 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000398 DISALLOW_COPY_AND_ASSIGN(HGraph);
399};
400
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700401class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000402 public:
403 HLoopInformation(HBasicBlock* header, HGraph* graph)
404 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100405 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100406 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100407 // Make bit vector growable, as the number of blocks may change.
408 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100409
410 HBasicBlock* GetHeader() const {
411 return header_;
412 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000413
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000414 void SetHeader(HBasicBlock* block) {
415 header_ = block;
416 }
417
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100418 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
419 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
420 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
421
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000422 void AddBackEdge(HBasicBlock* back_edge) {
423 back_edges_.Add(back_edge);
424 }
425
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 void RemoveBackEdge(HBasicBlock* back_edge) {
427 back_edges_.Delete(back_edge);
428 }
429
David Brazdil46e2a392015-03-16 17:31:52 +0000430 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100431 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000432 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100433 }
434 return false;
435 }
436
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000437 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000438 return back_edges_.Size();
439 }
440
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100441 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100442
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100443 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
444 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100445 }
446
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100447 // Returns the lifetime position of the back edge that has the
448 // greatest lifetime position.
449 size_t GetLifetimeEnd() const;
450
451 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
452 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
453 if (back_edges_.Get(i) == existing) {
454 back_edges_.Put(i, new_back_edge);
455 return;
456 }
457 }
458 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100459 }
460
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100461 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100462 // that is the header dominates the back edge.
463 bool Populate();
464
David Brazdila4b8c212015-05-07 09:59:30 +0100465 // Reanalyzes the loop by removing loop info from its blocks and re-running
466 // Populate(). If there are no back edges left, the loop info is completely
467 // removed as well as its SuspendCheck instruction. It must be run on nested
468 // inner loops first.
469 void Update();
470
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100471 // Returns whether this loop information contains `block`.
472 // Note that this loop information *must* be populated before entering this function.
473 bool Contains(const HBasicBlock& block) const;
474
475 // Returns whether this loop information is an inner loop of `other`.
476 // Note that `other` *must* be populated before entering this function.
477 bool IsIn(const HLoopInformation& other) const;
478
479 const ArenaBitVector& GetBlocks() const { return blocks_; }
480
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000481 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000482 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000483
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485 // Internal recursive implementation of `Populate`.
486 void PopulateRecursive(HBasicBlock* block);
487
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000488 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100489 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000490 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100491 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000492
493 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
494};
495
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100496static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100497static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100498
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000499// A block in a method. Contains the list of instructions represented
500// as a double linked list. Each block knows its predecessors and
501// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100502
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700503class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000504 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100505 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000506 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000507 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
508 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000509 loop_information_(nullptr),
510 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100511 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100512 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100513 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100514 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000515 lifetime_end_(kNoLifetime),
516 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000517
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100518 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
519 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000520 }
521
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
523 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000524 }
525
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100526 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
527 return dominated_blocks_;
528 }
529
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100530 bool IsEntryBlock() const {
531 return graph_->GetEntryBlock() == this;
532 }
533
534 bool IsExitBlock() const {
535 return graph_->GetExitBlock() == this;
536 }
537
David Brazdil46e2a392015-03-16 17:31:52 +0000538 bool IsSingleGoto() const;
539
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000540 void AddBackEdge(HBasicBlock* back_edge) {
541 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000542 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000543 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100544 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000545 loop_information_->AddBackEdge(back_edge);
546 }
547
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000548 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000549 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000550
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000551 int GetBlockId() const { return block_id_; }
552 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000553
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000554 HBasicBlock* GetDominator() const { return dominator_; }
555 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100556 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100557 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000558 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
559 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
560 if (dominated_blocks_.Get(i) == existing) {
561 dominated_blocks_.Put(i, new_block);
562 return;
563 }
564 }
565 LOG(FATAL) << "Unreachable";
566 UNREACHABLE();
567 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568
569 int NumberOfBackEdges() const {
570 return loop_information_ == nullptr
571 ? 0
572 : loop_information_->NumberOfBackEdges();
573 }
574
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100575 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
576 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100577 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100578 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100579 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
580 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000581
582 void AddSuccessor(HBasicBlock* block) {
583 successors_.Add(block);
584 block->predecessors_.Add(this);
585 }
586
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100587 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
588 size_t successor_index = GetSuccessorIndexOf(existing);
589 DCHECK_NE(successor_index, static_cast<size_t>(-1));
590 existing->RemovePredecessor(this);
591 new_block->predecessors_.Add(this);
592 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000593 }
594
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000595 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
596 size_t predecessor_index = GetPredecessorIndexOf(existing);
597 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
598 existing->RemoveSuccessor(this);
599 new_block->successors_.Add(this);
600 predecessors_.Put(predecessor_index, new_block);
601 }
602
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100603 void RemovePredecessor(HBasicBlock* block) {
604 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100605 }
606
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000607 void RemoveSuccessor(HBasicBlock* block) {
608 successors_.Delete(block);
609 }
610
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100611 void ClearAllPredecessors() {
612 predecessors_.Reset();
613 }
614
615 void AddPredecessor(HBasicBlock* block) {
616 predecessors_.Add(block);
617 block->successors_.Add(this);
618 }
619
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100620 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100621 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100622 HBasicBlock* temp = predecessors_.Get(0);
623 predecessors_.Put(0, predecessors_.Get(1));
624 predecessors_.Put(1, temp);
625 }
626
David Brazdil769c9e52015-04-27 13:54:09 +0100627 void SwapSuccessors() {
628 DCHECK_EQ(successors_.Size(), 2u);
629 HBasicBlock* temp = successors_.Get(0);
630 successors_.Put(0, successors_.Get(1));
631 successors_.Put(1, temp);
632 }
633
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100634 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
635 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
636 if (predecessors_.Get(i) == predecessor) {
637 return i;
638 }
639 }
640 return -1;
641 }
642
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100643 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
644 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
645 if (successors_.Get(i) == successor) {
646 return i;
647 }
648 }
649 return -1;
650 }
651
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000652 // Split the block into two blocks just after `cursor`. Returns the newly
653 // created block. Note that this method just updates raw block information,
654 // like predecessors, successors, dominators, and instruction list. It does not
655 // update the graph, reverse post order, loop information, nor make sure the
656 // blocks are consistent (for example ending with a control flow instruction).
657 HBasicBlock* SplitAfter(HInstruction* cursor);
658
659 // Merge `other` at the end of `this`. Successors and dominated blocks of
660 // `other` are changed to be successors and dominated blocks of `this`. Note
661 // that this method does not update the graph, reverse post order, loop
662 // information, nor make sure the blocks are consistent (for example ending
663 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100664 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000665
666 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
667 // of `this` are moved to `other`.
668 // Note that this method does not update the graph, reverse post order, loop
669 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000670 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000671 void ReplaceWith(HBasicBlock* other);
672
David Brazdil2d7352b2015-04-20 14:52:42 +0100673 // Merge `other` at the end of `this`. This method updates loops, reverse post
674 // order, links to predecessors, successors, dominators and deletes the block
675 // from the graph. The two blocks must be successive, i.e. `this` the only
676 // predecessor of `other` and vice versa.
677 void MergeWith(HBasicBlock* other);
678
679 // Disconnects `this` from all its predecessors, successors and dominator,
680 // removes it from all loops it is included in and eventually from the graph.
681 // The block must not dominate any other block. Predecessors and successors
682 // are safely updated.
683 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000684
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000685 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100686 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100687 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100688 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100689 // Replace instruction `initial` with `replacement` within this block.
690 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
691 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100692 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100693 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000694 // RemoveInstruction and RemovePhi delete a given instruction from the respective
695 // instruction list. With 'ensure_safety' set to true, it verifies that the
696 // instruction is not in use and removes it from the use lists of its inputs.
697 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
698 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100699 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100700
701 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100702 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100703 }
704
Roland Levillain6b879dd2014-09-22 17:13:44 +0100705 bool IsLoopPreHeaderFirstPredecessor() const {
706 DCHECK(IsLoopHeader());
707 DCHECK(!GetPredecessors().IsEmpty());
708 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
709 }
710
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100711 HLoopInformation* GetLoopInformation() const {
712 return loop_information_;
713 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000714
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000715 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100716 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000717 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100718 void SetInLoop(HLoopInformation* info) {
719 if (IsLoopHeader()) {
720 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100721 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100722 loop_information_ = info;
723 } else if (loop_information_->Contains(*info->GetHeader())) {
724 // Block is currently part of an outer loop. Make it part of this inner loop.
725 // Note that a non loop header having a loop information means this loop information
726 // has already been populated
727 loop_information_ = info;
728 } else {
729 // Block is part of an inner loop. Do not update the loop information.
730 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
731 // at this point, because this method is being called while populating `info`.
732 }
733 }
734
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000735 // Raw update of the loop information.
736 void SetLoopInformation(HLoopInformation* info) {
737 loop_information_ = info;
738 }
739
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100740 bool IsInLoop() const { return loop_information_ != nullptr; }
741
David Brazdila4b8c212015-05-07 09:59:30 +0100742 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100743 bool Dominates(HBasicBlock* block) const;
744
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100745 size_t GetLifetimeStart() const { return lifetime_start_; }
746 size_t GetLifetimeEnd() const { return lifetime_end_; }
747
748 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
749 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
750
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100751 uint32_t GetDexPc() const { return dex_pc_; }
752
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000753 bool IsCatchBlock() const { return is_catch_block_; }
754 void SetIsCatchBlock() { is_catch_block_ = true; }
755
David Brazdil8d5b8b22015-03-24 10:51:52 +0000756 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000757 bool EndsWithIf() const;
758 bool HasSinglePhi() const;
759
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000761 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762 GrowableArray<HBasicBlock*> predecessors_;
763 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100764 HInstructionList instructions_;
765 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000766 HLoopInformation* loop_information_;
767 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100768 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000769 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100770 // The dex program counter of the first instruction of this block.
771 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100772 size_t lifetime_start_;
773 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000774 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000775
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000776 friend class HGraph;
777 friend class HInstruction;
778
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000779 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
780};
781
David Brazdilb2bd1c52015-03-25 11:17:37 +0000782// Iterates over the LoopInformation of all loops which contain 'block'
783// from the innermost to the outermost.
784class HLoopInformationOutwardIterator : public ValueObject {
785 public:
786 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
787 : current_(block.GetLoopInformation()) {}
788
789 bool Done() const { return current_ == nullptr; }
790
791 void Advance() {
792 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100793 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000794 }
795
796 HLoopInformation* Current() const {
797 DCHECK(!Done());
798 return current_;
799 }
800
801 private:
802 HLoopInformation* current_;
803
804 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
805};
806
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100807#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
808 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000809 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000810 M(ArrayGet, Instruction) \
811 M(ArrayLength, Instruction) \
812 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100813 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000814 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000815 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000816 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100817 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000818 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100819 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100820 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700821 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000822 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000823 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000824 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100825 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000826 M(Exit, Instruction) \
827 M(FloatConstant, Constant) \
828 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100829 M(GreaterThan, Condition) \
830 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100831 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000832 M(InstanceFieldGet, Instruction) \
833 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000834 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100835 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000836 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000837 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100838 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000839 M(LessThan, Condition) \
840 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000841 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000842 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100843 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000844 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100845 M(Local, Instruction) \
846 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100847 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000848 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000849 M(Mul, BinaryOperation) \
850 M(Neg, UnaryOperation) \
851 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100852 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100853 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000854 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000855 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000856 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000857 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100858 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000859 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100860 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000861 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100862 M(Return, Instruction) \
863 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000864 M(Shl, BinaryOperation) \
865 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100866 M(StaticFieldGet, Instruction) \
867 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100868 M(StoreLocal, Instruction) \
869 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100870 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000871 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000872 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000873 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000874 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000875 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000876
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100877#define FOR_EACH_INSTRUCTION(M) \
878 FOR_EACH_CONCRETE_INSTRUCTION(M) \
879 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100880 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100881 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100882 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700883
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100884#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000885FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
886#undef FORWARD_DECLARATION
887
Roland Levillainccc07a92014-09-16 14:48:16 +0100888#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000889 InstructionKind GetKind() const OVERRIDE { return k##type; } \
890 const char* DebugName() const OVERRIDE { return #type; } \
891 const H##type* As##type() const OVERRIDE { return this; } \
892 H##type* As##type() OVERRIDE { return this; } \
893 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100894 return other->Is##type(); \
895 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000896 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000897
David Brazdiled596192015-01-23 10:39:45 +0000898template <typename T> class HUseList;
899
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100900template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700901class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000902 public:
David Brazdiled596192015-01-23 10:39:45 +0000903 HUseListNode* GetPrevious() const { return prev_; }
904 HUseListNode* GetNext() const { return next_; }
905 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100906 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100907 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100908
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000909 private:
David Brazdiled596192015-01-23 10:39:45 +0000910 HUseListNode(T user, size_t index)
911 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
912
913 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100914 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000915 HUseListNode<T>* prev_;
916 HUseListNode<T>* next_;
917
918 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000919
920 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
921};
922
David Brazdiled596192015-01-23 10:39:45 +0000923template <typename T>
924class HUseList : public ValueObject {
925 public:
926 HUseList() : first_(nullptr) {}
927
928 void Clear() {
929 first_ = nullptr;
930 }
931
932 // Adds a new entry at the beginning of the use list and returns
933 // the newly created node.
934 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000935 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000936 if (IsEmpty()) {
937 first_ = new_node;
938 } else {
939 first_->prev_ = new_node;
940 new_node->next_ = first_;
941 first_ = new_node;
942 }
943 return new_node;
944 }
945
946 HUseListNode<T>* GetFirst() const {
947 return first_;
948 }
949
950 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000951 DCHECK(node != nullptr);
952 DCHECK(Contains(node));
953
David Brazdiled596192015-01-23 10:39:45 +0000954 if (node->prev_ != nullptr) {
955 node->prev_->next_ = node->next_;
956 }
957 if (node->next_ != nullptr) {
958 node->next_->prev_ = node->prev_;
959 }
960 if (node == first_) {
961 first_ = node->next_;
962 }
963 }
964
David Brazdil1abb4192015-02-17 18:33:36 +0000965 bool Contains(const HUseListNode<T>* node) const {
966 if (node == nullptr) {
967 return false;
968 }
969 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
970 if (current == node) {
971 return true;
972 }
973 }
974 return false;
975 }
976
David Brazdiled596192015-01-23 10:39:45 +0000977 bool IsEmpty() const {
978 return first_ == nullptr;
979 }
980
981 bool HasOnlyOneUse() const {
982 return first_ != nullptr && first_->next_ == nullptr;
983 }
984
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100985 size_t SizeSlow() const {
986 size_t count = 0;
987 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
988 ++count;
989 }
990 return count;
991 }
992
David Brazdiled596192015-01-23 10:39:45 +0000993 private:
994 HUseListNode<T>* first_;
995};
996
997template<typename T>
998class HUseIterator : public ValueObject {
999 public:
1000 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1001
1002 bool Done() const { return current_ == nullptr; }
1003
1004 void Advance() {
1005 DCHECK(!Done());
1006 current_ = current_->GetNext();
1007 }
1008
1009 HUseListNode<T>* Current() const {
1010 DCHECK(!Done());
1011 return current_;
1012 }
1013
1014 private:
1015 HUseListNode<T>* current_;
1016
1017 friend class HValue;
1018};
1019
David Brazdil1abb4192015-02-17 18:33:36 +00001020// This class is used by HEnvironment and HInstruction classes to record the
1021// instructions they use and pointers to the corresponding HUseListNodes kept
1022// by the used instructions.
1023template <typename T>
1024class HUserRecord : public ValueObject {
1025 public:
1026 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1027 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1028
1029 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1030 : instruction_(old_record.instruction_), use_node_(use_node) {
1031 DCHECK(instruction_ != nullptr);
1032 DCHECK(use_node_ != nullptr);
1033 DCHECK(old_record.use_node_ == nullptr);
1034 }
1035
1036 HInstruction* GetInstruction() const { return instruction_; }
1037 HUseListNode<T>* GetUseNode() const { return use_node_; }
1038
1039 private:
1040 // Instruction used by the user.
1041 HInstruction* instruction_;
1042
1043 // Corresponding entry in the use list kept by 'instruction_'.
1044 HUseListNode<T>* use_node_;
1045};
1046
Calin Juravle27df7582015-04-17 19:12:31 +01001047// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1048// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1049// flag is consider.
1050// - DependsOn suggests that there is a real dependency between side effects but it only
1051// checks DependendsOnSomething flag.
1052//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001053// Represents the side effects an instruction may have.
1054class SideEffects : public ValueObject {
1055 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001056 SideEffects() : flags_(0) {}
1057
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001058 static SideEffects None() {
1059 return SideEffects(0);
1060 }
1061
1062 static SideEffects All() {
1063 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1064 }
1065
1066 static SideEffects ChangesSomething() {
1067 return SideEffects((1 << kFlagChangesCount) - 1);
1068 }
1069
1070 static SideEffects DependsOnSomething() {
1071 int count = kFlagDependsOnCount - kFlagChangesCount;
1072 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1073 }
1074
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001075 SideEffects Union(SideEffects other) const {
1076 return SideEffects(flags_ | other.flags_);
1077 }
1078
Roland Levillain72bceff2014-09-15 18:29:00 +01001079 bool HasSideEffects() const {
1080 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1081 return (flags_ & all_bits_set) != 0;
1082 }
1083
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001084 bool HasAllSideEffects() const {
1085 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1086 return all_bits_set == (flags_ & all_bits_set);
1087 }
1088
1089 bool DependsOn(SideEffects other) const {
1090 size_t depends_flags = other.ComputeDependsFlags();
1091 return (flags_ & depends_flags) != 0;
1092 }
1093
1094 bool HasDependencies() const {
1095 int count = kFlagDependsOnCount - kFlagChangesCount;
1096 size_t all_bits_set = (1 << count) - 1;
1097 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1098 }
1099
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001100 private:
1101 static constexpr int kFlagChangesSomething = 0;
1102 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1103
1104 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1105 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1106
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001107 explicit SideEffects(size_t flags) : flags_(flags) {}
1108
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001109 size_t ComputeDependsFlags() const {
1110 return flags_ << kFlagChangesCount;
1111 }
1112
1113 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001114};
1115
David Brazdiled596192015-01-23 10:39:45 +00001116// A HEnvironment object contains the values of virtual registers at a given location.
1117class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1118 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001119 HEnvironment(ArenaAllocator* arena,
1120 size_t number_of_vregs,
1121 const DexFile& dex_file,
1122 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001123 uint32_t dex_pc,
1124 InvokeType invoke_type)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001125 : vregs_(arena, number_of_vregs),
1126 locations_(arena, number_of_vregs),
1127 parent_(nullptr),
1128 dex_file_(dex_file),
1129 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001130 dex_pc_(dex_pc),
1131 invoke_type_(invoke_type) {
David Brazdiled596192015-01-23 10:39:45 +00001132 vregs_.SetSize(number_of_vregs);
1133 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001134 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001135 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001136
1137 locations_.SetSize(number_of_vregs);
1138 for (size_t i = 0; i < number_of_vregs; ++i) {
1139 locations_.Put(i, Location());
1140 }
1141 }
1142
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001143 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy)
1144 : HEnvironment(arena,
1145 to_copy.Size(),
1146 to_copy.GetDexFile(),
1147 to_copy.GetMethodIdx(),
1148 to_copy.GetDexPc(),
1149 to_copy.GetInvokeType()) {}
1150
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001151 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001152 parent_ = new (allocator) HEnvironment(allocator, *parent);
1153 parent_->CopyFrom(parent);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001154 if (parent->GetParent() != nullptr) {
1155 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1156 }
David Brazdiled596192015-01-23 10:39:45 +00001157 }
1158
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001159 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1160 void CopyFrom(HEnvironment* environment);
1161
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001162 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1163 // input to the loop phi instead. This is for inserting instructions that
1164 // require an environment (like HDeoptimization) in the loop pre-header.
1165 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001166
1167 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001168 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001169 }
1170
1171 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001172 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001173 }
1174
David Brazdil1abb4192015-02-17 18:33:36 +00001175 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001176
1177 size_t Size() const { return vregs_.Size(); }
1178
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001179 HEnvironment* GetParent() const { return parent_; }
1180
1181 void SetLocationAt(size_t index, Location location) {
1182 locations_.Put(index, location);
1183 }
1184
1185 Location GetLocationAt(size_t index) const {
1186 return locations_.Get(index);
1187 }
1188
1189 uint32_t GetDexPc() const {
1190 return dex_pc_;
1191 }
1192
1193 uint32_t GetMethodIdx() const {
1194 return method_idx_;
1195 }
1196
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001197 InvokeType GetInvokeType() const {
1198 return invoke_type_;
1199 }
1200
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001201 const DexFile& GetDexFile() const {
1202 return dex_file_;
1203 }
1204
David Brazdiled596192015-01-23 10:39:45 +00001205 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001206 // Record instructions' use entries of this environment for constant-time removal.
1207 // It should only be called by HInstruction when a new environment use is added.
1208 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1209 DCHECK(env_use->GetUser() == this);
1210 size_t index = env_use->GetIndex();
1211 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1212 }
David Brazdiled596192015-01-23 10:39:45 +00001213
David Brazdil1abb4192015-02-17 18:33:36 +00001214 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001215 GrowableArray<Location> locations_;
1216 HEnvironment* parent_;
1217 const DexFile& dex_file_;
1218 const uint32_t method_idx_;
1219 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001220 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001221
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001222 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001223
1224 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1225};
1226
Calin Juravleacf735c2015-02-12 15:25:22 +00001227class ReferenceTypeInfo : ValueObject {
1228 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001229 typedef Handle<mirror::Class> TypeHandle;
1230
1231 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1233 if (type_handle->IsObjectClass()) {
1234 // Override the type handle to be consistent with the case when we get to
1235 // Top but don't have the Object class available. It avoids having to guess
1236 // what value the type_handle has when it's Top.
1237 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1238 } else {
1239 return ReferenceTypeInfo(type_handle, is_exact, false);
1240 }
1241 }
1242
1243 static ReferenceTypeInfo CreateTop(bool is_exact) {
1244 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001245 }
1246
1247 bool IsExact() const { return is_exact_; }
1248 bool IsTop() const { return is_top_; }
1249
1250 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1251
Calin Juravleb1498f62015-02-16 13:13:29 +00001252 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001253 if (IsTop()) {
1254 // Top (equivalent for java.lang.Object) is supertype of anything.
1255 return true;
1256 }
1257 if (rti.IsTop()) {
1258 // If we get here `this` is not Top() so it can't be a supertype.
1259 return false;
1260 }
1261 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1262 }
1263
1264 // Returns true if the type information provide the same amount of details.
1265 // Note that it does not mean that the instructions have the same actual type
1266 // (e.g. tops are equal but they can be the result of a merge).
1267 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1268 if (IsExact() != rti.IsExact()) {
1269 return false;
1270 }
1271 if (IsTop() && rti.IsTop()) {
1272 // `Top` means java.lang.Object, so the types are equivalent.
1273 return true;
1274 }
1275 if (IsTop() || rti.IsTop()) {
1276 // If only one is top or object than they are not equivalent.
1277 // NB: We need this extra check because the type_handle of `Top` is invalid
1278 // and we cannot inspect its reference.
1279 return false;
1280 }
1281
1282 // Finally check the types.
1283 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1284 }
1285
1286 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001287 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1288 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1289 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1290
Calin Juravleacf735c2015-02-12 15:25:22 +00001291 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001292 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001293 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001294 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001295 bool is_exact_;
1296 // A true value here means that the object type should be java.lang.Object.
1297 // We don't have access to the corresponding mirror object every time so this
1298 // flag acts as a substitute. When true, the TypeHandle refers to a null
1299 // pointer and should not be used.
1300 bool is_top_;
1301};
1302
1303std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1304
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001305class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001306 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001307 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001308 : previous_(nullptr),
1309 next_(nullptr),
1310 block_(nullptr),
1311 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001312 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001313 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001314 locations_(nullptr),
1315 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001316 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001317 side_effects_(side_effects),
1318 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001319
Dave Allison20dfc792014-06-16 20:44:29 -07001320 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001321
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001323 enum InstructionKind {
1324 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1325 };
1326#undef DECLARE_KIND
1327
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001328 HInstruction* GetNext() const { return next_; }
1329 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001330
Calin Juravle77520bc2015-01-12 18:45:46 +00001331 HInstruction* GetNextDisregardingMoves() const;
1332 HInstruction* GetPreviousDisregardingMoves() const;
1333
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001334 HBasicBlock* GetBlock() const { return block_; }
1335 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001336 bool IsInBlock() const { return block_ != nullptr; }
1337 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001338 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001339
Roland Levillain6b879dd2014-09-22 17:13:44 +01001340 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001341 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001342
1343 virtual void Accept(HGraphVisitor* visitor) = 0;
1344 virtual const char* DebugName() const = 0;
1345
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001346 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001347 void SetRawInputAt(size_t index, HInstruction* input) {
1348 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1349 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001350
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001351 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001352 virtual uint32_t GetDexPc() const {
1353 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1354 " does not need an environment";
1355 UNREACHABLE();
1356 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001357 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001358 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001359 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001360
Calin Juravle10e244f2015-01-26 18:54:32 +00001361 // Does not apply for all instructions, but having this at top level greatly
1362 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001363 virtual bool CanBeNull() const {
1364 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1365 return true;
1366 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001367
Calin Juravle641547a2015-04-21 22:08:51 +01001368 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1369 UNUSED(obj);
1370 return false;
1371 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001372
Calin Juravleacf735c2015-02-12 15:25:22 +00001373 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001374 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001375 reference_type_info_ = reference_type_info;
1376 }
1377
Calin Juravle61d544b2015-02-23 16:46:57 +00001378 ReferenceTypeInfo GetReferenceTypeInfo() const {
1379 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1380 return reference_type_info_;
1381 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001382
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001383 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001384 DCHECK(user != nullptr);
1385 HUseListNode<HInstruction*>* use =
1386 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1387 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001388 }
1389
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001390 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001391 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001392 HUseListNode<HEnvironment*>* env_use =
1393 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1394 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001395 }
1396
David Brazdil1abb4192015-02-17 18:33:36 +00001397 void RemoveAsUserOfInput(size_t input) {
1398 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1399 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1400 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001401
David Brazdil1abb4192015-02-17 18:33:36 +00001402 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1403 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001404
David Brazdiled596192015-01-23 10:39:45 +00001405 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1406 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001407 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001408 bool HasOnlyOneNonEnvironmentUse() const {
1409 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1410 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001411
Roland Levillain6c82d402014-10-13 16:10:27 +01001412 // Does this instruction strictly dominate `other_instruction`?
1413 // Returns false if this instruction and `other_instruction` are the same.
1414 // Aborts if this instruction and `other_instruction` are both phis.
1415 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001416
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001417 int GetId() const { return id_; }
1418 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001419
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001420 int GetSsaIndex() const { return ssa_index_; }
1421 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1422 bool HasSsaIndex() const { return ssa_index_ != -1; }
1423
1424 bool HasEnvironment() const { return environment_ != nullptr; }
1425 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001426 // Set the `environment_` field. Raw because this method does not
1427 // update the uses lists.
1428 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1429
1430 // Set the environment of this instruction, copying it from `environment`. While
1431 // copying, the uses lists are being updated.
1432 void CopyEnvironmentFrom(HEnvironment* environment) {
1433 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001434 environment_ = new (allocator) HEnvironment(allocator, *environment);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001435 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001436 if (environment->GetParent() != nullptr) {
1437 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1438 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001439 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001440
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001441 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1442 HBasicBlock* block) {
1443 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001444 environment_ = new (allocator) HEnvironment(allocator, *environment);
1445 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001446 if (environment->GetParent() != nullptr) {
1447 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1448 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001449 }
1450
Nicolas Geoffray39468442014-09-02 15:17:15 +01001451 // Returns the number of entries in the environment. Typically, that is the
1452 // number of dex registers in a method. It could be more in case of inlining.
1453 size_t EnvironmentSize() const;
1454
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001455 LocationSummary* GetLocations() const { return locations_; }
1456 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001457
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001458 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001459 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001460
Alexandre Rames188d4312015-04-09 18:30:21 +01001461 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1462 // uses of this instruction by `other` are *not* updated.
1463 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1464 ReplaceWith(other);
1465 other->ReplaceInput(this, use_index);
1466 }
1467
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001468 // Move `this` instruction before `cursor`.
1469 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001470
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001471#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001472 bool Is##type() const { return (As##type() != nullptr); } \
1473 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001474 virtual H##type* As##type() { return nullptr; }
1475
1476 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1477#undef INSTRUCTION_TYPE_CHECK
1478
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479 // Returns whether the instruction can be moved within the graph.
1480 virtual bool CanBeMoved() const { return false; }
1481
1482 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001483 virtual bool InstructionTypeEquals(HInstruction* other) const {
1484 UNUSED(other);
1485 return false;
1486 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001487
1488 // Returns whether any data encoded in the two instructions is equal.
1489 // This method does not look at the inputs. Both instructions must be
1490 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001491 virtual bool InstructionDataEquals(HInstruction* other) const {
1492 UNUSED(other);
1493 return false;
1494 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001495
1496 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001497 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001498 // 2) Their inputs are identical.
1499 bool Equals(HInstruction* other) const;
1500
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001501 virtual InstructionKind GetKind() const = 0;
1502
1503 virtual size_t ComputeHashCode() const {
1504 size_t result = GetKind();
1505 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1506 result = (result * 31) + InputAt(i)->GetId();
1507 }
1508 return result;
1509 }
1510
1511 SideEffects GetSideEffects() const { return side_effects_; }
1512
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001513 size_t GetLifetimePosition() const { return lifetime_position_; }
1514 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1515 LiveInterval* GetLiveInterval() const { return live_interval_; }
1516 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1517 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1518
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001519 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1520
1521 // Returns whether the code generation of the instruction will require to have access
1522 // to the current method. Such instructions are:
1523 // (1): Instructions that require an environment, as calling the runtime requires
1524 // to walk the stack and have the current method stored at a specific stack address.
1525 // (2): Object literals like classes and strings, that are loaded from the dex cache
1526 // fields of the current method.
1527 bool NeedsCurrentMethod() const {
1528 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1529 }
1530
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001531 virtual bool NeedsDexCache() const { return false; }
1532
David Brazdil1abb4192015-02-17 18:33:36 +00001533 protected:
1534 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1535 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1536
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001537 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001538 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1539
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001540 HInstruction* previous_;
1541 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001542 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001543
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001544 // An instruction gets an id when it is added to the graph.
1545 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001546 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001547 int id_;
1548
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001549 // When doing liveness analysis, instructions that have uses get an SSA index.
1550 int ssa_index_;
1551
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001553 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001554
1555 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001556 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001557
Nicolas Geoffray39468442014-09-02 15:17:15 +01001558 // The environment associated with this instruction. Not null if the instruction
1559 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001560 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001561
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001562 // Set by the code generator.
1563 LocationSummary* locations_;
1564
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001565 // Set by the liveness analysis.
1566 LiveInterval* live_interval_;
1567
1568 // Set by the liveness analysis, this is the position in a linear
1569 // order of blocks where this instruction's live interval start.
1570 size_t lifetime_position_;
1571
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001572 const SideEffects side_effects_;
1573
Calin Juravleacf735c2015-02-12 15:25:22 +00001574 // TODO: for primitive types this should be marked as invalid.
1575 ReferenceTypeInfo reference_type_info_;
1576
David Brazdil1abb4192015-02-17 18:33:36 +00001577 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001578 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001579 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001580 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001581 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001582
1583 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1584};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001585std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001586
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001587class HInputIterator : public ValueObject {
1588 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001589 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001590
1591 bool Done() const { return index_ == instruction_->InputCount(); }
1592 HInstruction* Current() const { return instruction_->InputAt(index_); }
1593 void Advance() { index_++; }
1594
1595 private:
1596 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001597 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001598
1599 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1600};
1601
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001602class HInstructionIterator : public ValueObject {
1603 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001604 explicit HInstructionIterator(const HInstructionList& instructions)
1605 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001606 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001607 }
1608
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001609 bool Done() const { return instruction_ == nullptr; }
1610 HInstruction* Current() const { return instruction_; }
1611 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001612 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001613 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001614 }
1615
1616 private:
1617 HInstruction* instruction_;
1618 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001619
1620 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001621};
1622
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001623class HBackwardInstructionIterator : public ValueObject {
1624 public:
1625 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1626 : instruction_(instructions.last_instruction_) {
1627 next_ = Done() ? nullptr : instruction_->GetPrevious();
1628 }
1629
1630 bool Done() const { return instruction_ == nullptr; }
1631 HInstruction* Current() const { return instruction_; }
1632 void Advance() {
1633 instruction_ = next_;
1634 next_ = Done() ? nullptr : instruction_->GetPrevious();
1635 }
1636
1637 private:
1638 HInstruction* instruction_;
1639 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001640
1641 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001642};
1643
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001644// An embedded container with N elements of type T. Used (with partial
1645// specialization for N=0) because embedded arrays cannot have size 0.
1646template<typename T, intptr_t N>
1647class EmbeddedArray {
1648 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001649 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001650
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001651 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001652
1653 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001654 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001655 return elements_[i];
1656 }
1657
1658 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001659 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001660 return elements_[i];
1661 }
1662
1663 const T& At(intptr_t i) const {
1664 return (*this)[i];
1665 }
1666
1667 void SetAt(intptr_t i, const T& val) {
1668 (*this)[i] = val;
1669 }
1670
1671 private:
1672 T elements_[N];
1673};
1674
1675template<typename T>
1676class EmbeddedArray<T, 0> {
1677 public:
1678 intptr_t length() const { return 0; }
1679 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001680 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001681 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001682 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001683 }
1684 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001685 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001686 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001687 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001688 }
1689};
1690
1691template<intptr_t N>
1692class HTemplateInstruction: public HInstruction {
1693 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001694 HTemplateInstruction<N>(SideEffects side_effects)
1695 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001696 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001697
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001698 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001699
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001700 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001701 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1702
1703 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1704 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001705 }
1706
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001707 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001708 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001709
1710 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001711};
1712
Dave Allison20dfc792014-06-16 20:44:29 -07001713template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001714class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001715 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001716 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1717 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001718 virtual ~HExpression() {}
1719
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001720 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001721
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001722 protected:
1723 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001724};
1725
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001726// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1727// instruction that branches to the exit block.
1728class HReturnVoid : public HTemplateInstruction<0> {
1729 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001731
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001732 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001733
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001734 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001735
1736 private:
1737 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1738};
1739
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001740// Represents dex's RETURN opcodes. A HReturn is a control flow
1741// instruction that branches to the exit block.
1742class HReturn : public HTemplateInstruction<1> {
1743 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001744 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001745 SetRawInputAt(0, value);
1746 }
1747
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001748 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001749
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001750 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001751
1752 private:
1753 DISALLOW_COPY_AND_ASSIGN(HReturn);
1754};
1755
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001756// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001757// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001758// exit block.
1759class HExit : public HTemplateInstruction<0> {
1760 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001761 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001762
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001763 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001764
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001765 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001766
1767 private:
1768 DISALLOW_COPY_AND_ASSIGN(HExit);
1769};
1770
1771// Jumps from one block to another.
1772class HGoto : public HTemplateInstruction<0> {
1773 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001774 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1775
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001776 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001778 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001779 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001780 }
1781
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001782 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001783
1784 private:
1785 DISALLOW_COPY_AND_ASSIGN(HGoto);
1786};
1787
Dave Allison20dfc792014-06-16 20:44:29 -07001788
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001789// Conditional branch. A block ending with an HIf instruction must have
1790// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001791class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001792 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001793 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001794 SetRawInputAt(0, input);
1795 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001796
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001797 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001799 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001800 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801 }
1802
1803 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001804 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001805 }
1806
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001807 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001808
1809 private:
1810 DISALLOW_COPY_AND_ASSIGN(HIf);
1811};
1812
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001813// Deoptimize to interpreter, upon checking a condition.
1814class HDeoptimize : public HTemplateInstruction<1> {
1815 public:
1816 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1817 : HTemplateInstruction(SideEffects::None()),
1818 dex_pc_(dex_pc) {
1819 SetRawInputAt(0, cond);
1820 }
1821
1822 bool NeedsEnvironment() const OVERRIDE { return true; }
1823 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001824 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001825
1826 DECLARE_INSTRUCTION(Deoptimize);
1827
1828 private:
1829 uint32_t dex_pc_;
1830
1831 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1832};
1833
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001834// Represents the ArtMethod that was passed as a first argument to
1835// the method. It is used by instructions that depend on it, like
1836// instructions that work with the dex cache.
1837class HCurrentMethod : public HExpression<0> {
1838 public:
1839 HCurrentMethod() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
1840
1841 DECLARE_INSTRUCTION(CurrentMethod);
1842
1843 private:
1844 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
1845};
1846
Roland Levillain88cb1752014-10-20 16:36:47 +01001847class HUnaryOperation : public HExpression<1> {
1848 public:
1849 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1850 : HExpression(result_type, SideEffects::None()) {
1851 SetRawInputAt(0, input);
1852 }
1853
1854 HInstruction* GetInput() const { return InputAt(0); }
1855 Primitive::Type GetResultType() const { return GetType(); }
1856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001857 bool CanBeMoved() const OVERRIDE { return true; }
1858 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001859 UNUSED(other);
1860 return true;
1861 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001862
Roland Levillain9240d6a2014-10-20 16:47:04 +01001863 // Try to statically evaluate `operation` and return a HConstant
1864 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001865 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001866 HConstant* TryStaticEvaluation() const;
1867
1868 // Apply this operation to `x`.
1869 virtual int32_t Evaluate(int32_t x) const = 0;
1870 virtual int64_t Evaluate(int64_t x) const = 0;
1871
Roland Levillain88cb1752014-10-20 16:36:47 +01001872 DECLARE_INSTRUCTION(UnaryOperation);
1873
1874 private:
1875 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1876};
1877
Dave Allison20dfc792014-06-16 20:44:29 -07001878class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001879 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001880 HBinaryOperation(Primitive::Type result_type,
1881 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001882 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001883 SetRawInputAt(0, left);
1884 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001885 }
1886
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001887 HInstruction* GetLeft() const { return InputAt(0); }
1888 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001889 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001890
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001891 virtual bool IsCommutative() const { return false; }
1892
1893 // Put constant on the right.
1894 // Returns whether order is changed.
1895 bool OrderInputsWithConstantOnTheRight() {
1896 HInstruction* left = InputAt(0);
1897 HInstruction* right = InputAt(1);
1898 if (left->IsConstant() && !right->IsConstant()) {
1899 ReplaceInput(right, 0);
1900 ReplaceInput(left, 1);
1901 return true;
1902 }
1903 return false;
1904 }
1905
1906 // Order inputs by instruction id, but favor constant on the right side.
1907 // This helps GVN for commutative ops.
1908 void OrderInputs() {
1909 DCHECK(IsCommutative());
1910 HInstruction* left = InputAt(0);
1911 HInstruction* right = InputAt(1);
1912 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1913 return;
1914 }
1915 if (OrderInputsWithConstantOnTheRight()) {
1916 return;
1917 }
1918 // Order according to instruction id.
1919 if (left->GetId() > right->GetId()) {
1920 ReplaceInput(right, 0);
1921 ReplaceInput(left, 1);
1922 }
1923 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001924
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001925 bool CanBeMoved() const OVERRIDE { return true; }
1926 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001927 UNUSED(other);
1928 return true;
1929 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001930
Roland Levillain9240d6a2014-10-20 16:47:04 +01001931 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001932 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001933 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001934 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001935
1936 // Apply this operation to `x` and `y`.
1937 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1938 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1939
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001940 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001941 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001942 HConstant* GetConstantRight() const;
1943
1944 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001945 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001946 HInstruction* GetLeastConstantLeft() const;
1947
Roland Levillainccc07a92014-09-16 14:48:16 +01001948 DECLARE_INSTRUCTION(BinaryOperation);
1949
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001950 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001951 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1952};
1953
Dave Allison20dfc792014-06-16 20:44:29 -07001954class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001955 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001956 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001957 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1958 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001959
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001960 bool NeedsMaterialization() const { return needs_materialization_; }
1961 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001962
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001963 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001964 // `instruction`, and disregard moves in between.
1965 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001966
Dave Allison20dfc792014-06-16 20:44:29 -07001967 DECLARE_INSTRUCTION(Condition);
1968
1969 virtual IfCondition GetCondition() const = 0;
1970
1971 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001972 // For register allocation purposes, returns whether this instruction needs to be
1973 // materialized (that is, not just be in the processor flags).
1974 bool needs_materialization_;
1975
Dave Allison20dfc792014-06-16 20:44:29 -07001976 DISALLOW_COPY_AND_ASSIGN(HCondition);
1977};
1978
1979// Instruction to check if two inputs are equal to each other.
1980class HEqual : public HCondition {
1981 public:
1982 HEqual(HInstruction* first, HInstruction* second)
1983 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001984
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001985 bool IsCommutative() const OVERRIDE { return true; }
1986
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001987 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001988 return x == y ? 1 : 0;
1989 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001990 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001991 return x == y ? 1 : 0;
1992 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001993
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001994 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001995
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001996 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001997 return kCondEQ;
1998 }
1999
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002000 private:
2001 DISALLOW_COPY_AND_ASSIGN(HEqual);
2002};
2003
Dave Allison20dfc792014-06-16 20:44:29 -07002004class HNotEqual : public HCondition {
2005 public:
2006 HNotEqual(HInstruction* first, HInstruction* second)
2007 : HCondition(first, second) {}
2008
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002009 bool IsCommutative() const OVERRIDE { return true; }
2010
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002011 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002012 return x != y ? 1 : 0;
2013 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002014 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002015 return x != y ? 1 : 0;
2016 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002017
Dave Allison20dfc792014-06-16 20:44:29 -07002018 DECLARE_INSTRUCTION(NotEqual);
2019
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002020 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002021 return kCondNE;
2022 }
2023
2024 private:
2025 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2026};
2027
2028class HLessThan : public HCondition {
2029 public:
2030 HLessThan(HInstruction* first, HInstruction* second)
2031 : HCondition(first, second) {}
2032
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002033 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002034 return x < y ? 1 : 0;
2035 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002036 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002037 return x < y ? 1 : 0;
2038 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002039
Dave Allison20dfc792014-06-16 20:44:29 -07002040 DECLARE_INSTRUCTION(LessThan);
2041
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002042 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002043 return kCondLT;
2044 }
2045
2046 private:
2047 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2048};
2049
2050class HLessThanOrEqual : public HCondition {
2051 public:
2052 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2053 : HCondition(first, second) {}
2054
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002055 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002056 return x <= y ? 1 : 0;
2057 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002058 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002059 return x <= y ? 1 : 0;
2060 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002061
Dave Allison20dfc792014-06-16 20:44:29 -07002062 DECLARE_INSTRUCTION(LessThanOrEqual);
2063
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002064 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002065 return kCondLE;
2066 }
2067
2068 private:
2069 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2070};
2071
2072class HGreaterThan : public HCondition {
2073 public:
2074 HGreaterThan(HInstruction* first, HInstruction* second)
2075 : HCondition(first, second) {}
2076
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002077 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002078 return x > y ? 1 : 0;
2079 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002080 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002081 return x > y ? 1 : 0;
2082 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002083
Dave Allison20dfc792014-06-16 20:44:29 -07002084 DECLARE_INSTRUCTION(GreaterThan);
2085
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002086 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002087 return kCondGT;
2088 }
2089
2090 private:
2091 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2092};
2093
2094class HGreaterThanOrEqual : public HCondition {
2095 public:
2096 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2097 : HCondition(first, second) {}
2098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002099 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002100 return x >= y ? 1 : 0;
2101 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002102 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002103 return x >= y ? 1 : 0;
2104 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002105
Dave Allison20dfc792014-06-16 20:44:29 -07002106 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2107
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002108 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002109 return kCondGE;
2110 }
2111
2112 private:
2113 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2114};
2115
2116
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002117// Instruction to check how two inputs compare to each other.
2118// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2119class HCompare : public HBinaryOperation {
2120 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002121 // The bias applies for floating point operations and indicates how NaN
2122 // comparisons are treated:
2123 enum Bias {
2124 kNoBias, // bias is not applicable (i.e. for long operation)
2125 kGtBias, // return 1 for NaN comparisons
2126 kLtBias, // return -1 for NaN comparisons
2127 };
2128
2129 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
2130 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002131 DCHECK_EQ(type, first->GetType());
2132 DCHECK_EQ(type, second->GetType());
2133 }
2134
Calin Juravleddb7df22014-11-25 20:56:51 +00002135 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002136 return
2137 x == y ? 0 :
2138 x > y ? 1 :
2139 -1;
2140 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002141
2142 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002143 return
2144 x == y ? 0 :
2145 x > y ? 1 :
2146 -1;
2147 }
2148
Calin Juravleddb7df22014-11-25 20:56:51 +00002149 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2150 return bias_ == other->AsCompare()->bias_;
2151 }
2152
2153 bool IsGtBias() { return bias_ == kGtBias; }
2154
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002155 DECLARE_INSTRUCTION(Compare);
2156
2157 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002158 const Bias bias_;
2159
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002160 DISALLOW_COPY_AND_ASSIGN(HCompare);
2161};
2162
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002163// A local in the graph. Corresponds to a Dex register.
2164class HLocal : public HTemplateInstruction<0> {
2165 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166 explicit HLocal(uint16_t reg_number)
2167 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002168
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002169 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002170
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002171 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002172
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002173 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002174 // The Dex register number.
2175 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176
2177 DISALLOW_COPY_AND_ASSIGN(HLocal);
2178};
2179
2180// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002181class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002182 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002183 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002184 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002185 SetRawInputAt(0, local);
2186 }
2187
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002188 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2189
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002190 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002191
2192 private:
2193 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2194};
2195
2196// Store a value in a given local. This instruction has two inputs: the value
2197// and the local.
2198class HStoreLocal : public HTemplateInstruction<2> {
2199 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002200 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002201 SetRawInputAt(0, local);
2202 SetRawInputAt(1, value);
2203 }
2204
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002205 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2206
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002207 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002208
2209 private:
2210 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2211};
2212
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002213class HConstant : public HExpression<0> {
2214 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002215 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002217 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002218
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002219 virtual bool IsMinusOne() const { return false; }
2220 virtual bool IsZero() const { return false; }
2221 virtual bool IsOne() const { return false; }
2222
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002223 DECLARE_INSTRUCTION(Constant);
2224
2225 private:
2226 DISALLOW_COPY_AND_ASSIGN(HConstant);
2227};
2228
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002229class HFloatConstant : public HConstant {
2230 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002231 float GetValue() const { return value_; }
2232
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002233 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002234 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2235 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002236 }
2237
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002238 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002239
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002240 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002241 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002242 }
2243 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002244 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002245 }
2246 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002247 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2248 }
2249 bool IsNaN() const {
2250 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002251 }
2252
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002253 DECLARE_INSTRUCTION(FloatConstant);
2254
2255 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002256 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002257 explicit HFloatConstant(int32_t value)
2258 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002259
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002260 const float value_;
2261
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002262 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002263 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002264 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002265 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2266};
2267
2268class HDoubleConstant : public HConstant {
2269 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002270 double GetValue() const { return value_; }
2271
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002272 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002273 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2274 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002275 }
2276
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002277 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002278
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002279 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002280 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002281 }
2282 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002283 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002284 }
2285 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002286 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2287 }
2288 bool IsNaN() const {
2289 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002290 }
2291
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002292 DECLARE_INSTRUCTION(DoubleConstant);
2293
2294 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002295 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002296 explicit HDoubleConstant(int64_t value)
2297 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002298
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002299 const double value_;
2300
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002301 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002302 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002303 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002304 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2305};
2306
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002307class HNullConstant : public HConstant {
2308 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002309 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2310 return true;
2311 }
2312
2313 size_t ComputeHashCode() const OVERRIDE { return 0; }
2314
2315 DECLARE_INSTRUCTION(NullConstant);
2316
2317 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002318 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2319
2320 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002321 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2322};
2323
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002324// Constants of the type int. Those can be from Dex instructions, or
2325// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002326class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002327 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002328 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002329
Calin Juravle61d544b2015-02-23 16:46:57 +00002330 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002331 return other->AsIntConstant()->value_ == value_;
2332 }
2333
Calin Juravle61d544b2015-02-23 16:46:57 +00002334 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2335
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002336 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2337 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2338 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2339
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002340 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002341
2342 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002343 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2344
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002345 const int32_t value_;
2346
David Brazdil8d5b8b22015-03-24 10:51:52 +00002347 friend class HGraph;
2348 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002349 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002350 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2351};
2352
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002353class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002354 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002355 int64_t GetValue() const { return value_; }
2356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002357 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002358 return other->AsLongConstant()->value_ == value_;
2359 }
2360
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002361 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002362
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002363 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2364 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2365 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2366
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002367 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002368
2369 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002370 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2371
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002372 const int64_t value_;
2373
David Brazdil8d5b8b22015-03-24 10:51:52 +00002374 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002375 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2376};
2377
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002378enum class Intrinsics {
2379#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2380#include "intrinsics_list.h"
2381 kNone,
2382 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2383#undef INTRINSICS_LIST
2384#undef OPTIMIZING_INTRINSICS
2385};
2386std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2387
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002388class HInvoke : public HInstruction {
2389 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002390 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002391
2392 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2393 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002394 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002395
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002396 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002397 SetRawInputAt(index, argument);
2398 }
2399
Roland Levillain3e3d7332015-04-28 11:00:54 +01002400 // Return the number of arguments. This number can be lower than
2401 // the number of inputs returned by InputCount(), as some invoke
2402 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2403 // inputs at the end of their list of inputs.
2404 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2405
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002406 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002407
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002408 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002409
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002410 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002411 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002412
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002413 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2414
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002415 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002416 return intrinsic_;
2417 }
2418
2419 void SetIntrinsic(Intrinsics intrinsic) {
2420 intrinsic_ = intrinsic;
2421 }
2422
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002423 DECLARE_INSTRUCTION(Invoke);
2424
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002425 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002426 HInvoke(ArenaAllocator* arena,
2427 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002428 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002429 Primitive::Type return_type,
2430 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002431 uint32_t dex_method_index,
2432 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002433 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002434 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002435 inputs_(arena, number_of_arguments),
2436 return_type_(return_type),
2437 dex_pc_(dex_pc),
2438 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002439 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002440 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002441 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2442 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002443 }
2444
David Brazdil1abb4192015-02-17 18:33:36 +00002445 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2446 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2447 inputs_.Put(index, input);
2448 }
2449
Roland Levillain3e3d7332015-04-28 11:00:54 +01002450 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002451 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002452 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002453 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002454 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002455 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002456 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002457
2458 private:
2459 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2460};
2461
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002462class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002463 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002464 // Requirements of this method call regarding the class
2465 // initialization (clinit) check of its declaring class.
2466 enum class ClinitCheckRequirement {
2467 kNone, // Class already initialized.
2468 kExplicit, // Static call having explicit clinit check as last input.
2469 kImplicit, // Static call implicitly requiring a clinit check.
2470 };
2471
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002472 HInvokeStaticOrDirect(ArenaAllocator* arena,
2473 uint32_t number_of_arguments,
2474 Primitive::Type return_type,
2475 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002476 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002477 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002478 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002479 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002480 InvokeType invoke_type,
2481 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002482 : HInvoke(arena,
2483 number_of_arguments,
2484 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2485 return_type,
2486 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002487 dex_method_index,
2488 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002489 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002490 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002491 clinit_check_requirement_(clinit_check_requirement),
2492 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002493
Calin Juravle641547a2015-04-21 22:08:51 +01002494 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2495 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002496 // We access the method via the dex cache so we can't do an implicit null check.
2497 // TODO: for intrinsics we can generate implicit null checks.
2498 return false;
2499 }
2500
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002501 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002502 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002503 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002504 bool IsStringInit() const { return string_init_offset_ != 0; }
2505 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002506
Roland Levillain4c0eb422015-04-24 16:43:49 +01002507 // Is this instruction a call to a static method?
2508 bool IsStatic() const {
2509 return GetInvokeType() == kStatic;
2510 }
2511
Roland Levillain3e3d7332015-04-28 11:00:54 +01002512 // Remove the art::HLoadClass instruction set as last input by
2513 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2514 // the initial art::HClinitCheck instruction (only relevant for
2515 // static calls with explicit clinit check).
2516 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002517 DCHECK(IsStaticWithExplicitClinitCheck());
2518 size_t last_input_index = InputCount() - 1;
2519 HInstruction* last_input = InputAt(last_input_index);
2520 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002521 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002522 RemoveAsUserOfInput(last_input_index);
2523 inputs_.DeleteAt(last_input_index);
2524 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2525 DCHECK(IsStaticWithImplicitClinitCheck());
2526 }
2527
2528 // Is this a call to a static method whose declaring class has an
2529 // explicit intialization check in the graph?
2530 bool IsStaticWithExplicitClinitCheck() const {
2531 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2532 }
2533
2534 // Is this a call to a static method whose declaring class has an
2535 // implicit intialization check requirement?
2536 bool IsStaticWithImplicitClinitCheck() const {
2537 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2538 }
2539
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002540 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002541
Roland Levillain4c0eb422015-04-24 16:43:49 +01002542 protected:
2543 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2544 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2545 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2546 HInstruction* input = input_record.GetInstruction();
2547 // `input` is the last input of a static invoke marked as having
2548 // an explicit clinit check. It must either be:
2549 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2550 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2551 DCHECK(input != nullptr);
2552 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2553 }
2554 return input_record;
2555 }
2556
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002557 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002558 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002559 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002560 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002561 // Thread entrypoint offset for string init method if this is a string init invoke.
2562 // Note that there are multiple string init methods, each having its own offset.
2563 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002564
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002565 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002566};
2567
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002568class HInvokeVirtual : public HInvoke {
2569 public:
2570 HInvokeVirtual(ArenaAllocator* arena,
2571 uint32_t number_of_arguments,
2572 Primitive::Type return_type,
2573 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002574 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002575 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002576 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002577 vtable_index_(vtable_index) {}
2578
Calin Juravle641547a2015-04-21 22:08:51 +01002579 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002580 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002581 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002582 }
2583
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002584 uint32_t GetVTableIndex() const { return vtable_index_; }
2585
2586 DECLARE_INSTRUCTION(InvokeVirtual);
2587
2588 private:
2589 const uint32_t vtable_index_;
2590
2591 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2592};
2593
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002594class HInvokeInterface : public HInvoke {
2595 public:
2596 HInvokeInterface(ArenaAllocator* arena,
2597 uint32_t number_of_arguments,
2598 Primitive::Type return_type,
2599 uint32_t dex_pc,
2600 uint32_t dex_method_index,
2601 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002602 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002603 imt_index_(imt_index) {}
2604
Calin Juravle641547a2015-04-21 22:08:51 +01002605 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002606 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002607 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002608 }
2609
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002610 uint32_t GetImtIndex() const { return imt_index_; }
2611 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2612
2613 DECLARE_INSTRUCTION(InvokeInterface);
2614
2615 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002616 const uint32_t imt_index_;
2617
2618 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2619};
2620
Dave Allison20dfc792014-06-16 20:44:29 -07002621class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002622 public:
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002623 HNewInstance(uint32_t dex_pc,
2624 uint16_t type_index,
2625 const DexFile& dex_file,
2626 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002627 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2628 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002629 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002630 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002631 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002632
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002633 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002634 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002635 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002636
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002637 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002638 bool NeedsEnvironment() const OVERRIDE { return true; }
2639 // It may throw when called on:
2640 // - interfaces
2641 // - abstract/innaccessible/unknown classes
2642 // TODO: optimize when possible.
2643 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002644
Calin Juravle10e244f2015-01-26 18:54:32 +00002645 bool CanBeNull() const OVERRIDE { return false; }
2646
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002647 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2648
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002649 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002650
2651 private:
2652 const uint32_t dex_pc_;
2653 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002654 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002655 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002656
2657 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2658};
2659
Roland Levillain88cb1752014-10-20 16:36:47 +01002660class HNeg : public HUnaryOperation {
2661 public:
2662 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2663 : HUnaryOperation(result_type, input) {}
2664
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002665 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2666 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002667
Roland Levillain88cb1752014-10-20 16:36:47 +01002668 DECLARE_INSTRUCTION(Neg);
2669
2670 private:
2671 DISALLOW_COPY_AND_ASSIGN(HNeg);
2672};
2673
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002674class HNewArray : public HExpression<1> {
2675 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002676 HNewArray(HInstruction* length,
2677 uint32_t dex_pc,
2678 uint16_t type_index,
2679 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002680 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2681 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002682 type_index_(type_index),
2683 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002684 SetRawInputAt(0, length);
2685 }
2686
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002687 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002688 uint16_t GetTypeIndex() const { return type_index_; }
2689
2690 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002691 bool NeedsEnvironment() const OVERRIDE { return true; }
2692
Mingyao Yang0c365e62015-03-31 15:09:29 -07002693 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2694 bool CanThrow() const OVERRIDE { return true; }
2695
Calin Juravle10e244f2015-01-26 18:54:32 +00002696 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002697
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002698 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2699
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002700 DECLARE_INSTRUCTION(NewArray);
2701
2702 private:
2703 const uint32_t dex_pc_;
2704 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002705 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002706
2707 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2708};
2709
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002710class HAdd : public HBinaryOperation {
2711 public:
2712 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2713 : HBinaryOperation(result_type, left, right) {}
2714
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002715 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002716
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002717 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002718 return x + y;
2719 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002720 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002721 return x + y;
2722 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002723
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002724 DECLARE_INSTRUCTION(Add);
2725
2726 private:
2727 DISALLOW_COPY_AND_ASSIGN(HAdd);
2728};
2729
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002730class HSub : public HBinaryOperation {
2731 public:
2732 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2733 : HBinaryOperation(result_type, left, right) {}
2734
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002735 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002736 return x - y;
2737 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002738 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002739 return x - y;
2740 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002741
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002742 DECLARE_INSTRUCTION(Sub);
2743
2744 private:
2745 DISALLOW_COPY_AND_ASSIGN(HSub);
2746};
2747
Calin Juravle34bacdf2014-10-07 20:23:36 +01002748class HMul : public HBinaryOperation {
2749 public:
2750 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2751 : HBinaryOperation(result_type, left, right) {}
2752
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002753 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002754
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002755 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2756 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002757
2758 DECLARE_INSTRUCTION(Mul);
2759
2760 private:
2761 DISALLOW_COPY_AND_ASSIGN(HMul);
2762};
2763
Calin Juravle7c4954d2014-10-28 16:57:40 +00002764class HDiv : public HBinaryOperation {
2765 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002766 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2767 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002768
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002769 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002770 // Our graph structure ensures we never have 0 for `y` during constant folding.
2771 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002772 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002773 return (y == -1) ? -x : x / y;
2774 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002775
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002776 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002777 DCHECK_NE(y, 0);
2778 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2779 return (y == -1) ? -x : x / y;
2780 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002781
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002782 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002783
Calin Juravle7c4954d2014-10-28 16:57:40 +00002784 DECLARE_INSTRUCTION(Div);
2785
2786 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002787 const uint32_t dex_pc_;
2788
Calin Juravle7c4954d2014-10-28 16:57:40 +00002789 DISALLOW_COPY_AND_ASSIGN(HDiv);
2790};
2791
Calin Juravlebacfec32014-11-14 15:54:36 +00002792class HRem : public HBinaryOperation {
2793 public:
2794 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2795 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2796
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002797 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002798 DCHECK_NE(y, 0);
2799 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2800 return (y == -1) ? 0 : x % y;
2801 }
2802
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002803 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002804 DCHECK_NE(y, 0);
2805 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2806 return (y == -1) ? 0 : x % y;
2807 }
2808
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002809 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002810
2811 DECLARE_INSTRUCTION(Rem);
2812
2813 private:
2814 const uint32_t dex_pc_;
2815
2816 DISALLOW_COPY_AND_ASSIGN(HRem);
2817};
2818
Calin Juravled0d48522014-11-04 16:40:20 +00002819class HDivZeroCheck : public HExpression<1> {
2820 public:
2821 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2822 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2823 SetRawInputAt(0, value);
2824 }
2825
2826 bool CanBeMoved() const OVERRIDE { return true; }
2827
2828 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2829 UNUSED(other);
2830 return true;
2831 }
2832
2833 bool NeedsEnvironment() const OVERRIDE { return true; }
2834 bool CanThrow() const OVERRIDE { return true; }
2835
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002836 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002837
2838 DECLARE_INSTRUCTION(DivZeroCheck);
2839
2840 private:
2841 const uint32_t dex_pc_;
2842
2843 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2844};
2845
Calin Juravle9aec02f2014-11-18 23:06:35 +00002846class HShl : public HBinaryOperation {
2847 public:
2848 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2849 : HBinaryOperation(result_type, left, right) {}
2850
2851 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2852 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2853
2854 DECLARE_INSTRUCTION(Shl);
2855
2856 private:
2857 DISALLOW_COPY_AND_ASSIGN(HShl);
2858};
2859
2860class HShr : public HBinaryOperation {
2861 public:
2862 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2863 : HBinaryOperation(result_type, left, right) {}
2864
2865 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2866 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2867
2868 DECLARE_INSTRUCTION(Shr);
2869
2870 private:
2871 DISALLOW_COPY_AND_ASSIGN(HShr);
2872};
2873
2874class HUShr : public HBinaryOperation {
2875 public:
2876 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2877 : HBinaryOperation(result_type, left, right) {}
2878
2879 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2880 uint32_t ux = static_cast<uint32_t>(x);
2881 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2882 return static_cast<int32_t>(ux >> uy);
2883 }
2884
2885 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2886 uint64_t ux = static_cast<uint64_t>(x);
2887 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2888 return static_cast<int64_t>(ux >> uy);
2889 }
2890
2891 DECLARE_INSTRUCTION(UShr);
2892
2893 private:
2894 DISALLOW_COPY_AND_ASSIGN(HUShr);
2895};
2896
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002897class HAnd : public HBinaryOperation {
2898 public:
2899 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2900 : HBinaryOperation(result_type, left, right) {}
2901
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002902 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002903
2904 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2905 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2906
2907 DECLARE_INSTRUCTION(And);
2908
2909 private:
2910 DISALLOW_COPY_AND_ASSIGN(HAnd);
2911};
2912
2913class HOr : public HBinaryOperation {
2914 public:
2915 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2916 : HBinaryOperation(result_type, left, right) {}
2917
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002918 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002919
2920 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2921 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2922
2923 DECLARE_INSTRUCTION(Or);
2924
2925 private:
2926 DISALLOW_COPY_AND_ASSIGN(HOr);
2927};
2928
2929class HXor : public HBinaryOperation {
2930 public:
2931 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2932 : HBinaryOperation(result_type, left, right) {}
2933
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002934 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002935
2936 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2937 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2938
2939 DECLARE_INSTRUCTION(Xor);
2940
2941 private:
2942 DISALLOW_COPY_AND_ASSIGN(HXor);
2943};
2944
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002945// The value of a parameter in this method. Its location depends on
2946// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002947class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002948 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002949 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2950 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002951
2952 uint8_t GetIndex() const { return index_; }
2953
Calin Juravle10e244f2015-01-26 18:54:32 +00002954 bool CanBeNull() const OVERRIDE { return !is_this_; }
2955
Calin Juravle3cd4fc82015-05-14 15:15:42 +01002956 bool IsThis() const { return is_this_; }
2957
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002958 DECLARE_INSTRUCTION(ParameterValue);
2959
2960 private:
2961 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002962 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002963 const uint8_t index_;
2964
Calin Juravle10e244f2015-01-26 18:54:32 +00002965 // Whether or not the parameter value corresponds to 'this' argument.
2966 const bool is_this_;
2967
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002968 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2969};
2970
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002971class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002972 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002973 explicit HNot(Primitive::Type result_type, HInstruction* input)
2974 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002975
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002976 bool CanBeMoved() const OVERRIDE { return true; }
2977 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002978 UNUSED(other);
2979 return true;
2980 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002981
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002982 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2983 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002984
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002985 DECLARE_INSTRUCTION(Not);
2986
2987 private:
2988 DISALLOW_COPY_AND_ASSIGN(HNot);
2989};
2990
David Brazdil66d126e2015-04-03 16:02:44 +01002991class HBooleanNot : public HUnaryOperation {
2992 public:
2993 explicit HBooleanNot(HInstruction* input)
2994 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2995
2996 bool CanBeMoved() const OVERRIDE { return true; }
2997 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2998 UNUSED(other);
2999 return true;
3000 }
3001
3002 int32_t Evaluate(int32_t x) const OVERRIDE {
3003 DCHECK(IsUint<1>(x));
3004 return !x;
3005 }
3006
3007 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3008 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3009 UNREACHABLE();
3010 }
3011
3012 DECLARE_INSTRUCTION(BooleanNot);
3013
3014 private:
3015 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3016};
3017
Roland Levillaindff1f282014-11-05 14:15:05 +00003018class HTypeConversion : public HExpression<1> {
3019 public:
3020 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003021 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3022 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003023 SetRawInputAt(0, input);
3024 DCHECK_NE(input->GetType(), result_type);
3025 }
3026
3027 HInstruction* GetInput() const { return InputAt(0); }
3028 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3029 Primitive::Type GetResultType() const { return GetType(); }
3030
Roland Levillain624279f2014-12-04 11:54:28 +00003031 // Required by the x86 and ARM code generators when producing calls
3032 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003033 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003034
Roland Levillaindff1f282014-11-05 14:15:05 +00003035 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003036 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003037
Mark Mendelle82549b2015-05-06 10:55:34 -04003038 // Try to statically evaluate the conversion and return a HConstant
3039 // containing the result. If the input cannot be converted, return nullptr.
3040 HConstant* TryStaticEvaluation() const;
3041
Roland Levillaindff1f282014-11-05 14:15:05 +00003042 DECLARE_INSTRUCTION(TypeConversion);
3043
3044 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003045 const uint32_t dex_pc_;
3046
Roland Levillaindff1f282014-11-05 14:15:05 +00003047 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3048};
3049
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003050static constexpr uint32_t kNoRegNumber = -1;
3051
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003052class HPhi : public HInstruction {
3053 public:
3054 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003055 : HInstruction(SideEffects::None()),
3056 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003057 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003058 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003059 is_live_(false),
3060 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003061 inputs_.SetSize(number_of_inputs);
3062 }
3063
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003064 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3065 static Primitive::Type ToPhiType(Primitive::Type type) {
3066 switch (type) {
3067 case Primitive::kPrimBoolean:
3068 case Primitive::kPrimByte:
3069 case Primitive::kPrimShort:
3070 case Primitive::kPrimChar:
3071 return Primitive::kPrimInt;
3072 default:
3073 return type;
3074 }
3075 }
3076
Calin Juravle10e244f2015-01-26 18:54:32 +00003077 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003078
3079 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003080 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003081
Calin Juravle10e244f2015-01-26 18:54:32 +00003082 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003083 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003084
Calin Juravle10e244f2015-01-26 18:54:32 +00003085 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3086 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3087
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003088 uint32_t GetRegNumber() const { return reg_number_; }
3089
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003090 void SetDead() { is_live_ = false; }
3091 void SetLive() { is_live_ = true; }
3092 bool IsDead() const { return !is_live_; }
3093 bool IsLive() const { return is_live_; }
3094
Calin Juravlea4f88312015-04-16 12:57:19 +01003095 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3096 // An equivalent phi is a phi having the same dex register and type.
3097 // It assumes that phis with the same dex register are adjacent.
3098 HPhi* GetNextEquivalentPhiWithSameType() {
3099 HInstruction* next = GetNext();
3100 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3101 if (next->GetType() == GetType()) {
3102 return next->AsPhi();
3103 }
3104 next = next->GetNext();
3105 }
3106 return nullptr;
3107 }
3108
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003109 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003110
David Brazdil1abb4192015-02-17 18:33:36 +00003111 protected:
3112 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3113
3114 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3115 inputs_.Put(index, input);
3116 }
3117
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003118 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003119 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003120 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003121 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003122 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003123 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003124
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003125 DISALLOW_COPY_AND_ASSIGN(HPhi);
3126};
3127
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003128class HNullCheck : public HExpression<1> {
3129 public:
3130 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003131 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003132 SetRawInputAt(0, value);
3133 }
3134
Calin Juravle10e244f2015-01-26 18:54:32 +00003135 bool CanBeMoved() const OVERRIDE { return true; }
3136 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003137 UNUSED(other);
3138 return true;
3139 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003140
Calin Juravle10e244f2015-01-26 18:54:32 +00003141 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003142
Calin Juravle10e244f2015-01-26 18:54:32 +00003143 bool CanThrow() const OVERRIDE { return true; }
3144
3145 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003146
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003147 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003148
3149 DECLARE_INSTRUCTION(NullCheck);
3150
3151 private:
3152 const uint32_t dex_pc_;
3153
3154 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3155};
3156
3157class FieldInfo : public ValueObject {
3158 public:
Calin Juravle52c48962014-12-16 17:02:57 +00003159 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3160 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003161
3162 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003163 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003164 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003165
3166 private:
3167 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003168 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003169 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003170};
3171
3172class HInstanceFieldGet : public HExpression<1> {
3173 public:
3174 HInstanceFieldGet(HInstruction* value,
3175 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003176 MemberOffset field_offset,
3177 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003178 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003179 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003180 SetRawInputAt(0, value);
3181 }
3182
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003183 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003184
3185 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3186 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3187 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003188 }
3189
Calin Juravle641547a2015-04-21 22:08:51 +01003190 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3191 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003192 }
3193
3194 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003195 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3196 }
3197
Calin Juravle52c48962014-12-16 17:02:57 +00003198 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003199 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003200 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003201 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003202
3203 DECLARE_INSTRUCTION(InstanceFieldGet);
3204
3205 private:
3206 const FieldInfo field_info_;
3207
3208 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3209};
3210
3211class HInstanceFieldSet : public HTemplateInstruction<2> {
3212 public:
3213 HInstanceFieldSet(HInstruction* object,
3214 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003215 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003216 MemberOffset field_offset,
3217 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003218 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003219 field_info_(field_offset, field_type, is_volatile),
3220 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003221 SetRawInputAt(0, object);
3222 SetRawInputAt(1, value);
3223 }
3224
Calin Juravle641547a2015-04-21 22:08:51 +01003225 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3226 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003227 }
3228
Calin Juravle52c48962014-12-16 17:02:57 +00003229 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003230 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003231 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003232 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003233 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003234 bool GetValueCanBeNull() const { return value_can_be_null_; }
3235 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003236
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003237 DECLARE_INSTRUCTION(InstanceFieldSet);
3238
3239 private:
3240 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003241 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003242
3243 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3244};
3245
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003246class HArrayGet : public HExpression<2> {
3247 public:
3248 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003249 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003250 SetRawInputAt(0, array);
3251 SetRawInputAt(1, index);
3252 }
3253
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003254 bool CanBeMoved() const OVERRIDE { return true; }
3255 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003256 UNUSED(other);
3257 return true;
3258 }
Calin Juravle641547a2015-04-21 22:08:51 +01003259 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3260 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003261 // TODO: We can be smarter here.
3262 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3263 // which generates the implicit null check. There are cases when these can be removed
3264 // to produce better code. If we ever add optimizations to do so we should allow an
3265 // implicit check here (as long as the address falls in the first page).
3266 return false;
3267 }
3268
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003269 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003270
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003271 HInstruction* GetArray() const { return InputAt(0); }
3272 HInstruction* GetIndex() const { return InputAt(1); }
3273
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003274 DECLARE_INSTRUCTION(ArrayGet);
3275
3276 private:
3277 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3278};
3279
3280class HArraySet : public HTemplateInstruction<3> {
3281 public:
3282 HArraySet(HInstruction* array,
3283 HInstruction* index,
3284 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003285 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003286 uint32_t dex_pc)
3287 : HTemplateInstruction(SideEffects::ChangesSomething()),
3288 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003289 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003290 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3291 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003292 SetRawInputAt(0, array);
3293 SetRawInputAt(1, index);
3294 SetRawInputAt(2, value);
3295 }
3296
Calin Juravle77520bc2015-01-12 18:45:46 +00003297 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003298 // We currently always call a runtime method to catch array store
3299 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003300 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003301 }
3302
Calin Juravle641547a2015-04-21 22:08:51 +01003303 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3304 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003305 // TODO: Same as for ArrayGet.
3306 return false;
3307 }
3308
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003309 void ClearNeedsTypeCheck() {
3310 needs_type_check_ = false;
3311 }
3312
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003313 void ClearValueCanBeNull() {
3314 value_can_be_null_ = false;
3315 }
3316
3317 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003318 bool NeedsTypeCheck() const { return needs_type_check_; }
3319
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003320 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003321
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003322 HInstruction* GetArray() const { return InputAt(0); }
3323 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003324 HInstruction* GetValue() const { return InputAt(2); }
3325
3326 Primitive::Type GetComponentType() const {
3327 // The Dex format does not type floating point index operations. Since the
3328 // `expected_component_type_` is set during building and can therefore not
3329 // be correct, we also check what is the value type. If it is a floating
3330 // point type, we must use that type.
3331 Primitive::Type value_type = GetValue()->GetType();
3332 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3333 ? value_type
3334 : expected_component_type_;
3335 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003336
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003337 DECLARE_INSTRUCTION(ArraySet);
3338
3339 private:
3340 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003341 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003342 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003343 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003344
3345 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3346};
3347
3348class HArrayLength : public HExpression<1> {
3349 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003350 explicit HArrayLength(HInstruction* array)
3351 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3352 // Note that arrays do not change length, so the instruction does not
3353 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003354 SetRawInputAt(0, array);
3355 }
3356
Calin Juravle77520bc2015-01-12 18:45:46 +00003357 bool CanBeMoved() const OVERRIDE { return true; }
3358 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003359 UNUSED(other);
3360 return true;
3361 }
Calin Juravle641547a2015-04-21 22:08:51 +01003362 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3363 return obj == InputAt(0);
3364 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003365
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003366 DECLARE_INSTRUCTION(ArrayLength);
3367
3368 private:
3369 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3370};
3371
3372class HBoundsCheck : public HExpression<2> {
3373 public:
3374 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003375 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003376 DCHECK(index->GetType() == Primitive::kPrimInt);
3377 SetRawInputAt(0, index);
3378 SetRawInputAt(1, length);
3379 }
3380
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003381 bool CanBeMoved() const OVERRIDE { return true; }
3382 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003383 UNUSED(other);
3384 return true;
3385 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003386
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003387 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003388
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003389 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003390
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003391 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003392
3393 DECLARE_INSTRUCTION(BoundsCheck);
3394
3395 private:
3396 const uint32_t dex_pc_;
3397
3398 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3399};
3400
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003401/**
3402 * Some DEX instructions are folded into multiple HInstructions that need
3403 * to stay live until the last HInstruction. This class
3404 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003405 * HInstruction stays live. `index` represents the stack location index of the
3406 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003407 */
3408class HTemporary : public HTemplateInstruction<0> {
3409 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003410 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003411
3412 size_t GetIndex() const { return index_; }
3413
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003414 Primitive::Type GetType() const OVERRIDE {
3415 // The previous instruction is the one that will be stored in the temporary location.
3416 DCHECK(GetPrevious() != nullptr);
3417 return GetPrevious()->GetType();
3418 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003419
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003420 DECLARE_INSTRUCTION(Temporary);
3421
3422 private:
3423 const size_t index_;
3424
3425 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3426};
3427
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003428class HSuspendCheck : public HTemplateInstruction<0> {
3429 public:
3430 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003431 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003432
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003433 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003434 return true;
3435 }
3436
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003437 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003438 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3439 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003440
3441 DECLARE_INSTRUCTION(SuspendCheck);
3442
3443 private:
3444 const uint32_t dex_pc_;
3445
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003446 // Only used for code generation, in order to share the same slow path between back edges
3447 // of a same loop.
3448 SlowPathCode* slow_path_;
3449
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003450 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3451};
3452
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003453/**
3454 * Instruction to load a Class object.
3455 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003456class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003457 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003458 HLoadClass(HCurrentMethod* current_method,
3459 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003460 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003461 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003462 uint32_t dex_pc)
3463 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3464 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003465 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003466 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003467 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003468 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003469 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3470 SetRawInputAt(0, current_method);
3471 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003472
3473 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003474
3475 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3476 return other->AsLoadClass()->type_index_ == type_index_;
3477 }
3478
3479 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3480
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003481 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003482 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003483 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003484
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003485 bool NeedsEnvironment() const OVERRIDE {
3486 // Will call runtime and load the class if the class is not loaded yet.
3487 // TODO: finer grain decision.
3488 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003489 }
3490
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003491 bool MustGenerateClinitCheck() const {
3492 return generate_clinit_check_;
3493 }
3494
Calin Juravle0ba218d2015-05-19 18:46:01 +01003495 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3496 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003497 }
3498
3499 bool CanCallRuntime() const {
3500 return MustGenerateClinitCheck() || !is_referrers_class_;
3501 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003502
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003503 bool CanThrow() const OVERRIDE {
3504 // May call runtime and and therefore can throw.
3505 // TODO: finer grain decision.
3506 return !is_referrers_class_;
3507 }
3508
Calin Juravleacf735c2015-02-12 15:25:22 +00003509 ReferenceTypeInfo GetLoadedClassRTI() {
3510 return loaded_class_rti_;
3511 }
3512
3513 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3514 // Make sure we only set exact types (the loaded class should never be merged).
3515 DCHECK(rti.IsExact());
3516 loaded_class_rti_ = rti;
3517 }
3518
3519 bool IsResolved() {
3520 return loaded_class_rti_.IsExact();
3521 }
3522
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003523 const DexFile& GetDexFile() { return dex_file_; }
3524
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003525 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3526
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003527 DECLARE_INSTRUCTION(LoadClass);
3528
3529 private:
3530 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003531 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003532 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003533 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003534 // Whether this instruction must generate the initialization check.
3535 // Used for code generation.
3536 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003537
Calin Juravleacf735c2015-02-12 15:25:22 +00003538 ReferenceTypeInfo loaded_class_rti_;
3539
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003540 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3541};
3542
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003543class HLoadString : public HExpression<0> {
3544 public:
3545 HLoadString(uint32_t string_index, uint32_t dex_pc)
3546 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3547 string_index_(string_index),
3548 dex_pc_(dex_pc) {}
3549
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003550 bool CanBeMoved() const OVERRIDE { return true; }
3551
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003552 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3553 return other->AsLoadString()->string_index_ == string_index_;
3554 }
3555
3556 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3557
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003558 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003559 uint32_t GetStringIndex() const { return string_index_; }
3560
3561 // TODO: Can we deopt or debug when we resolve a string?
3562 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003563 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003564
3565 DECLARE_INSTRUCTION(LoadString);
3566
3567 private:
3568 const uint32_t string_index_;
3569 const uint32_t dex_pc_;
3570
3571 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3572};
3573
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003574/**
3575 * Performs an initialization check on its Class object input.
3576 */
3577class HClinitCheck : public HExpression<1> {
3578 public:
3579 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003580 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003581 dex_pc_(dex_pc) {
3582 SetRawInputAt(0, constant);
3583 }
3584
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003585 bool CanBeMoved() const OVERRIDE { return true; }
3586 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3587 UNUSED(other);
3588 return true;
3589 }
3590
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003591 bool NeedsEnvironment() const OVERRIDE {
3592 // May call runtime to initialize the class.
3593 return true;
3594 }
3595
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003596 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003597
3598 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3599
3600 DECLARE_INSTRUCTION(ClinitCheck);
3601
3602 private:
3603 const uint32_t dex_pc_;
3604
3605 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3606};
3607
3608class HStaticFieldGet : public HExpression<1> {
3609 public:
3610 HStaticFieldGet(HInstruction* cls,
3611 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003612 MemberOffset field_offset,
3613 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003614 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003615 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003616 SetRawInputAt(0, cls);
3617 }
3618
Calin Juravle52c48962014-12-16 17:02:57 +00003619
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003620 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003621
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003622 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003623 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3624 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003625 }
3626
3627 size_t ComputeHashCode() const OVERRIDE {
3628 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3629 }
3630
Calin Juravle52c48962014-12-16 17:02:57 +00003631 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003632 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3633 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003634 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003635
3636 DECLARE_INSTRUCTION(StaticFieldGet);
3637
3638 private:
3639 const FieldInfo field_info_;
3640
3641 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3642};
3643
3644class HStaticFieldSet : public HTemplateInstruction<2> {
3645 public:
3646 HStaticFieldSet(HInstruction* cls,
3647 HInstruction* value,
3648 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003649 MemberOffset field_offset,
3650 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003651 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003652 field_info_(field_offset, field_type, is_volatile),
3653 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003654 SetRawInputAt(0, cls);
3655 SetRawInputAt(1, value);
3656 }
3657
Calin Juravle52c48962014-12-16 17:02:57 +00003658 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003659 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3660 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003661 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003662
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003663 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003664 bool GetValueCanBeNull() const { return value_can_be_null_; }
3665 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003666
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003667 DECLARE_INSTRUCTION(StaticFieldSet);
3668
3669 private:
3670 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003671 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003672
3673 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3674};
3675
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003676// Implement the move-exception DEX instruction.
3677class HLoadException : public HExpression<0> {
3678 public:
3679 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3680
3681 DECLARE_INSTRUCTION(LoadException);
3682
3683 private:
3684 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3685};
3686
3687class HThrow : public HTemplateInstruction<1> {
3688 public:
3689 HThrow(HInstruction* exception, uint32_t dex_pc)
3690 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3691 SetRawInputAt(0, exception);
3692 }
3693
3694 bool IsControlFlow() const OVERRIDE { return true; }
3695
3696 bool NeedsEnvironment() const OVERRIDE { return true; }
3697
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003698 bool CanThrow() const OVERRIDE { return true; }
3699
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003700 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003701
3702 DECLARE_INSTRUCTION(Throw);
3703
3704 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003705 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003706
3707 DISALLOW_COPY_AND_ASSIGN(HThrow);
3708};
3709
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003710class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003711 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003712 HInstanceOf(HInstruction* object,
3713 HLoadClass* constant,
3714 bool class_is_final,
3715 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003716 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3717 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003718 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003719 dex_pc_(dex_pc) {
3720 SetRawInputAt(0, object);
3721 SetRawInputAt(1, constant);
3722 }
3723
3724 bool CanBeMoved() const OVERRIDE { return true; }
3725
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003726 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003727 return true;
3728 }
3729
3730 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003731 return false;
3732 }
3733
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003734 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003735
3736 bool IsClassFinal() const { return class_is_final_; }
3737
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003738 // Used only in code generation.
3739 bool MustDoNullCheck() const { return must_do_null_check_; }
3740 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3741
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003742 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003743
3744 private:
3745 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003746 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003747 const uint32_t dex_pc_;
3748
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003749 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3750};
3751
Calin Juravleb1498f62015-02-16 13:13:29 +00003752class HBoundType : public HExpression<1> {
3753 public:
3754 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3755 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3756 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003757 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003758 SetRawInputAt(0, input);
3759 }
3760
3761 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3762
3763 bool CanBeNull() const OVERRIDE {
3764 // `null instanceof ClassX` always return false so we can't be null.
3765 return false;
3766 }
3767
3768 DECLARE_INSTRUCTION(BoundType);
3769
3770 private:
3771 // Encodes the most upper class that this instruction can have. In other words
3772 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3773 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3774 const ReferenceTypeInfo bound_type_;
3775
3776 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3777};
3778
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003779class HCheckCast : public HTemplateInstruction<2> {
3780 public:
3781 HCheckCast(HInstruction* object,
3782 HLoadClass* constant,
3783 bool class_is_final,
3784 uint32_t dex_pc)
3785 : HTemplateInstruction(SideEffects::None()),
3786 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003787 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003788 dex_pc_(dex_pc) {
3789 SetRawInputAt(0, object);
3790 SetRawInputAt(1, constant);
3791 }
3792
3793 bool CanBeMoved() const OVERRIDE { return true; }
3794
3795 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3796 return true;
3797 }
3798
3799 bool NeedsEnvironment() const OVERRIDE {
3800 // Instruction may throw a CheckCastError.
3801 return true;
3802 }
3803
3804 bool CanThrow() const OVERRIDE { return true; }
3805
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003806 bool MustDoNullCheck() const { return must_do_null_check_; }
3807 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3808
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003809 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003810
3811 bool IsClassFinal() const { return class_is_final_; }
3812
3813 DECLARE_INSTRUCTION(CheckCast);
3814
3815 private:
3816 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003817 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003818 const uint32_t dex_pc_;
3819
3820 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003821};
3822
Calin Juravle27df7582015-04-17 19:12:31 +01003823class HMemoryBarrier : public HTemplateInstruction<0> {
3824 public:
3825 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3826 : HTemplateInstruction(SideEffects::None()),
3827 barrier_kind_(barrier_kind) {}
3828
3829 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3830
3831 DECLARE_INSTRUCTION(MemoryBarrier);
3832
3833 private:
3834 const MemBarrierKind barrier_kind_;
3835
3836 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3837};
3838
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003839class HMonitorOperation : public HTemplateInstruction<1> {
3840 public:
3841 enum OperationKind {
3842 kEnter,
3843 kExit,
3844 };
3845
3846 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3847 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3848 SetRawInputAt(0, object);
3849 }
3850
3851 // Instruction may throw a Java exception, so we need an environment.
3852 bool NeedsEnvironment() const OVERRIDE { return true; }
3853 bool CanThrow() const OVERRIDE { return true; }
3854
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003855 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003856
3857 bool IsEnter() const { return kind_ == kEnter; }
3858
3859 DECLARE_INSTRUCTION(MonitorOperation);
3860
Calin Juravle52c48962014-12-16 17:02:57 +00003861 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003862 const OperationKind kind_;
3863 const uint32_t dex_pc_;
3864
3865 private:
3866 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3867};
3868
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003869class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003870 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003871 MoveOperands(Location source,
3872 Location destination,
3873 Primitive::Type type,
3874 HInstruction* instruction)
3875 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003876
3877 Location GetSource() const { return source_; }
3878 Location GetDestination() const { return destination_; }
3879
3880 void SetSource(Location value) { source_ = value; }
3881 void SetDestination(Location value) { destination_ = value; }
3882
3883 // The parallel move resolver marks moves as "in-progress" by clearing the
3884 // destination (but not the source).
3885 Location MarkPending() {
3886 DCHECK(!IsPending());
3887 Location dest = destination_;
3888 destination_ = Location::NoLocation();
3889 return dest;
3890 }
3891
3892 void ClearPending(Location dest) {
3893 DCHECK(IsPending());
3894 destination_ = dest;
3895 }
3896
3897 bool IsPending() const {
3898 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3899 return destination_.IsInvalid() && !source_.IsInvalid();
3900 }
3901
3902 // True if this blocks a move from the given location.
3903 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003904 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003905 }
3906
3907 // A move is redundant if it's been eliminated, if its source and
3908 // destination are the same, or if its destination is unneeded.
3909 bool IsRedundant() const {
3910 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3911 }
3912
3913 // We clear both operands to indicate move that's been eliminated.
3914 void Eliminate() {
3915 source_ = destination_ = Location::NoLocation();
3916 }
3917
3918 bool IsEliminated() const {
3919 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3920 return source_.IsInvalid();
3921 }
3922
Nicolas Geoffray90218252015-04-15 11:56:51 +01003923 bool Is64BitMove() const {
3924 return Primitive::Is64BitType(type_);
3925 }
3926
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003927 HInstruction* GetInstruction() const { return instruction_; }
3928
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003929 private:
3930 Location source_;
3931 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003932 // The type this move is for.
3933 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003934 // The instruction this move is assocatied with. Null when this move is
3935 // for moving an input in the expected locations of user (including a phi user).
3936 // This is only used in debug mode, to ensure we do not connect interval siblings
3937 // in the same parallel move.
3938 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003939};
3940
3941static constexpr size_t kDefaultNumberOfMoves = 4;
3942
3943class HParallelMove : public HTemplateInstruction<0> {
3944 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003945 explicit HParallelMove(ArenaAllocator* arena)
3946 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003947
Nicolas Geoffray90218252015-04-15 11:56:51 +01003948 void AddMove(Location source,
3949 Location destination,
3950 Primitive::Type type,
3951 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003952 DCHECK(source.IsValid());
3953 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003954 if (kIsDebugBuild) {
3955 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003956 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003957 if (moves_.Get(i).GetInstruction() == instruction) {
3958 // Special case the situation where the move is for the spill slot
3959 // of the instruction.
3960 if ((GetPrevious() == instruction)
3961 || ((GetPrevious() == nullptr)
3962 && instruction->IsPhi()
3963 && instruction->GetBlock() == GetBlock())) {
3964 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3965 << "Doing parallel moves for the same instruction.";
3966 } else {
3967 DCHECK(false) << "Doing parallel moves for the same instruction.";
3968 }
3969 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003970 }
3971 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003972 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003973 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01003974 << "Overlapped destination for two moves in a parallel move: "
3975 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
3976 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003977 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003978 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003979 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003980 }
3981
3982 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003983 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003984 }
3985
3986 size_t NumMoves() const { return moves_.Size(); }
3987
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003988 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003989
3990 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003991 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003992
3993 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3994};
3995
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003996class HGraphVisitor : public ValueObject {
3997 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003998 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3999 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004000
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004001 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004002 virtual void VisitBasicBlock(HBasicBlock* block);
4003
Roland Levillain633021e2014-10-01 14:12:25 +01004004 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004005 void VisitInsertionOrder();
4006
Roland Levillain633021e2014-10-01 14:12:25 +01004007 // Visit the graph following dominator tree reverse post-order.
4008 void VisitReversePostOrder();
4009
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004010 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004011
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004012 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004013#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004014 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4015
4016 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4017
4018#undef DECLARE_VISIT_INSTRUCTION
4019
4020 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004021 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004022
4023 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4024};
4025
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004026class HGraphDelegateVisitor : public HGraphVisitor {
4027 public:
4028 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4029 virtual ~HGraphDelegateVisitor() {}
4030
4031 // Visit functions that delegate to to super class.
4032#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004033 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004034
4035 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4036
4037#undef DECLARE_VISIT_INSTRUCTION
4038
4039 private:
4040 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4041};
4042
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004043class HInsertionOrderIterator : public ValueObject {
4044 public:
4045 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4046
4047 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4048 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4049 void Advance() { ++index_; }
4050
4051 private:
4052 const HGraph& graph_;
4053 size_t index_;
4054
4055 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4056};
4057
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004058class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004059 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004060 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4061 // Check that reverse post order of the graph has been built.
4062 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4063 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004064
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004065 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4066 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004067 void Advance() { ++index_; }
4068
4069 private:
4070 const HGraph& graph_;
4071 size_t index_;
4072
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004073 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004074};
4075
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004076class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004077 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004078 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004079 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4080 // Check that reverse post order of the graph has been built.
4081 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4082 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004083
4084 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004085 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004086 void Advance() { --index_; }
4087
4088 private:
4089 const HGraph& graph_;
4090 size_t index_;
4091
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004092 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004093};
4094
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004095class HLinearPostOrderIterator : public ValueObject {
4096 public:
4097 explicit HLinearPostOrderIterator(const HGraph& graph)
4098 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4099
4100 bool Done() const { return index_ == 0; }
4101
4102 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4103
4104 void Advance() {
4105 --index_;
4106 DCHECK_GE(index_, 0U);
4107 }
4108
4109 private:
4110 const GrowableArray<HBasicBlock*>& order_;
4111 size_t index_;
4112
4113 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4114};
4115
4116class HLinearOrderIterator : public ValueObject {
4117 public:
4118 explicit HLinearOrderIterator(const HGraph& graph)
4119 : order_(graph.GetLinearOrder()), index_(0) {}
4120
4121 bool Done() const { return index_ == order_.Size(); }
4122 HBasicBlock* Current() const { return order_.Get(index_); }
4123 void Advance() { ++index_; }
4124
4125 private:
4126 const GrowableArray<HBasicBlock*>& order_;
4127 size_t index_;
4128
4129 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4130};
4131
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004132// Iterator over the blocks that art part of the loop. Includes blocks part
4133// of an inner loop. The order in which the blocks are iterated is on their
4134// block id.
4135class HBlocksInLoopIterator : public ValueObject {
4136 public:
4137 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4138 : blocks_in_loop_(info.GetBlocks()),
4139 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4140 index_(0) {
4141 if (!blocks_in_loop_.IsBitSet(index_)) {
4142 Advance();
4143 }
4144 }
4145
4146 bool Done() const { return index_ == blocks_.Size(); }
4147 HBasicBlock* Current() const { return blocks_.Get(index_); }
4148 void Advance() {
4149 ++index_;
4150 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4151 if (blocks_in_loop_.IsBitSet(index_)) {
4152 break;
4153 }
4154 }
4155 }
4156
4157 private:
4158 const BitVector& blocks_in_loop_;
4159 const GrowableArray<HBasicBlock*>& blocks_;
4160 size_t index_;
4161
4162 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4163};
4164
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004165inline int64_t Int64FromConstant(HConstant* constant) {
4166 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4167 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4168 : constant->AsLongConstant()->GetValue();
4169}
4170
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004171} // namespace art
4172
4173#endif // ART_COMPILER_OPTIMIZING_NODES_H_