blob: 869809d69f1e51df25012afb434b73ec3adbc71f [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,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002679 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002680 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002681 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2682 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002683 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002684 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002685 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002686 SetRawInputAt(0, length);
2687 }
2688
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002689 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002690 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002691 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002692
2693 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002694 bool NeedsEnvironment() const OVERRIDE { return true; }
2695
Mingyao Yang0c365e62015-03-31 15:09:29 -07002696 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2697 bool CanThrow() const OVERRIDE { return true; }
2698
Calin Juravle10e244f2015-01-26 18:54:32 +00002699 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002700
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002701 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2702
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002703 DECLARE_INSTRUCTION(NewArray);
2704
2705 private:
2706 const uint32_t dex_pc_;
2707 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002708 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002709 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002710
2711 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2712};
2713
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002714class HAdd : public HBinaryOperation {
2715 public:
2716 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2717 : HBinaryOperation(result_type, left, right) {}
2718
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002719 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002720
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002721 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002722 return x + y;
2723 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002724 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002725 return x + y;
2726 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002727
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002728 DECLARE_INSTRUCTION(Add);
2729
2730 private:
2731 DISALLOW_COPY_AND_ASSIGN(HAdd);
2732};
2733
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002734class HSub : public HBinaryOperation {
2735 public:
2736 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2737 : HBinaryOperation(result_type, left, right) {}
2738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002739 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002740 return x - y;
2741 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002742 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002743 return x - y;
2744 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002745
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002746 DECLARE_INSTRUCTION(Sub);
2747
2748 private:
2749 DISALLOW_COPY_AND_ASSIGN(HSub);
2750};
2751
Calin Juravle34bacdf2014-10-07 20:23:36 +01002752class HMul : public HBinaryOperation {
2753 public:
2754 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2755 : HBinaryOperation(result_type, left, right) {}
2756
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002757 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002758
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002759 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2760 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002761
2762 DECLARE_INSTRUCTION(Mul);
2763
2764 private:
2765 DISALLOW_COPY_AND_ASSIGN(HMul);
2766};
2767
Calin Juravle7c4954d2014-10-28 16:57:40 +00002768class HDiv : public HBinaryOperation {
2769 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002770 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2771 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002772
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002773 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002774 // Our graph structure ensures we never have 0 for `y` during constant folding.
2775 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002776 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002777 return (y == -1) ? -x : x / y;
2778 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002779
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002780 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002781 DCHECK_NE(y, 0);
2782 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2783 return (y == -1) ? -x : x / y;
2784 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002785
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002786 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002787
Calin Juravle7c4954d2014-10-28 16:57:40 +00002788 DECLARE_INSTRUCTION(Div);
2789
2790 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002791 const uint32_t dex_pc_;
2792
Calin Juravle7c4954d2014-10-28 16:57:40 +00002793 DISALLOW_COPY_AND_ASSIGN(HDiv);
2794};
2795
Calin Juravlebacfec32014-11-14 15:54:36 +00002796class HRem : public HBinaryOperation {
2797 public:
2798 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2799 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002801 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002802 DCHECK_NE(y, 0);
2803 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2804 return (y == -1) ? 0 : x % y;
2805 }
2806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002807 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002808 DCHECK_NE(y, 0);
2809 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2810 return (y == -1) ? 0 : x % y;
2811 }
2812
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002813 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002814
2815 DECLARE_INSTRUCTION(Rem);
2816
2817 private:
2818 const uint32_t dex_pc_;
2819
2820 DISALLOW_COPY_AND_ASSIGN(HRem);
2821};
2822
Calin Juravled0d48522014-11-04 16:40:20 +00002823class HDivZeroCheck : public HExpression<1> {
2824 public:
2825 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2826 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2827 SetRawInputAt(0, value);
2828 }
2829
2830 bool CanBeMoved() const OVERRIDE { return true; }
2831
2832 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2833 UNUSED(other);
2834 return true;
2835 }
2836
2837 bool NeedsEnvironment() const OVERRIDE { return true; }
2838 bool CanThrow() const OVERRIDE { return true; }
2839
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002840 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002841
2842 DECLARE_INSTRUCTION(DivZeroCheck);
2843
2844 private:
2845 const uint32_t dex_pc_;
2846
2847 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2848};
2849
Calin Juravle9aec02f2014-11-18 23:06:35 +00002850class HShl : public HBinaryOperation {
2851 public:
2852 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2853 : HBinaryOperation(result_type, left, right) {}
2854
2855 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2856 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2857
2858 DECLARE_INSTRUCTION(Shl);
2859
2860 private:
2861 DISALLOW_COPY_AND_ASSIGN(HShl);
2862};
2863
2864class HShr : public HBinaryOperation {
2865 public:
2866 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2867 : HBinaryOperation(result_type, left, right) {}
2868
2869 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2870 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2871
2872 DECLARE_INSTRUCTION(Shr);
2873
2874 private:
2875 DISALLOW_COPY_AND_ASSIGN(HShr);
2876};
2877
2878class HUShr : public HBinaryOperation {
2879 public:
2880 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2881 : HBinaryOperation(result_type, left, right) {}
2882
2883 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2884 uint32_t ux = static_cast<uint32_t>(x);
2885 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2886 return static_cast<int32_t>(ux >> uy);
2887 }
2888
2889 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2890 uint64_t ux = static_cast<uint64_t>(x);
2891 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2892 return static_cast<int64_t>(ux >> uy);
2893 }
2894
2895 DECLARE_INSTRUCTION(UShr);
2896
2897 private:
2898 DISALLOW_COPY_AND_ASSIGN(HUShr);
2899};
2900
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002901class HAnd : public HBinaryOperation {
2902 public:
2903 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2904 : HBinaryOperation(result_type, left, right) {}
2905
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002906 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002907
2908 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2909 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2910
2911 DECLARE_INSTRUCTION(And);
2912
2913 private:
2914 DISALLOW_COPY_AND_ASSIGN(HAnd);
2915};
2916
2917class HOr : public HBinaryOperation {
2918 public:
2919 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2920 : HBinaryOperation(result_type, left, right) {}
2921
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002922 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002923
2924 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2925 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2926
2927 DECLARE_INSTRUCTION(Or);
2928
2929 private:
2930 DISALLOW_COPY_AND_ASSIGN(HOr);
2931};
2932
2933class HXor : public HBinaryOperation {
2934 public:
2935 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2936 : HBinaryOperation(result_type, left, right) {}
2937
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002938 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002939
2940 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2941 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2942
2943 DECLARE_INSTRUCTION(Xor);
2944
2945 private:
2946 DISALLOW_COPY_AND_ASSIGN(HXor);
2947};
2948
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002949// The value of a parameter in this method. Its location depends on
2950// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002951class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002952 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002953 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2954 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002955
2956 uint8_t GetIndex() const { return index_; }
2957
Calin Juravle10e244f2015-01-26 18:54:32 +00002958 bool CanBeNull() const OVERRIDE { return !is_this_; }
2959
Calin Juravle3cd4fc82015-05-14 15:15:42 +01002960 bool IsThis() const { return is_this_; }
2961
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002962 DECLARE_INSTRUCTION(ParameterValue);
2963
2964 private:
2965 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002966 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002967 const uint8_t index_;
2968
Calin Juravle10e244f2015-01-26 18:54:32 +00002969 // Whether or not the parameter value corresponds to 'this' argument.
2970 const bool is_this_;
2971
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002972 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2973};
2974
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002975class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002976 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002977 explicit HNot(Primitive::Type result_type, HInstruction* input)
2978 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002979
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002980 bool CanBeMoved() const OVERRIDE { return true; }
2981 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002982 UNUSED(other);
2983 return true;
2984 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002985
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002986 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2987 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002988
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002989 DECLARE_INSTRUCTION(Not);
2990
2991 private:
2992 DISALLOW_COPY_AND_ASSIGN(HNot);
2993};
2994
David Brazdil66d126e2015-04-03 16:02:44 +01002995class HBooleanNot : public HUnaryOperation {
2996 public:
2997 explicit HBooleanNot(HInstruction* input)
2998 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2999
3000 bool CanBeMoved() const OVERRIDE { return true; }
3001 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3002 UNUSED(other);
3003 return true;
3004 }
3005
3006 int32_t Evaluate(int32_t x) const OVERRIDE {
3007 DCHECK(IsUint<1>(x));
3008 return !x;
3009 }
3010
3011 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3012 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3013 UNREACHABLE();
3014 }
3015
3016 DECLARE_INSTRUCTION(BooleanNot);
3017
3018 private:
3019 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3020};
3021
Roland Levillaindff1f282014-11-05 14:15:05 +00003022class HTypeConversion : public HExpression<1> {
3023 public:
3024 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003025 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3026 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003027 SetRawInputAt(0, input);
3028 DCHECK_NE(input->GetType(), result_type);
3029 }
3030
3031 HInstruction* GetInput() const { return InputAt(0); }
3032 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3033 Primitive::Type GetResultType() const { return GetType(); }
3034
Roland Levillain624279f2014-12-04 11:54:28 +00003035 // Required by the x86 and ARM code generators when producing calls
3036 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003037 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003038
Roland Levillaindff1f282014-11-05 14:15:05 +00003039 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003040 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003041
Mark Mendelle82549b2015-05-06 10:55:34 -04003042 // Try to statically evaluate the conversion and return a HConstant
3043 // containing the result. If the input cannot be converted, return nullptr.
3044 HConstant* TryStaticEvaluation() const;
3045
Roland Levillaindff1f282014-11-05 14:15:05 +00003046 DECLARE_INSTRUCTION(TypeConversion);
3047
3048 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003049 const uint32_t dex_pc_;
3050
Roland Levillaindff1f282014-11-05 14:15:05 +00003051 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3052};
3053
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003054static constexpr uint32_t kNoRegNumber = -1;
3055
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003056class HPhi : public HInstruction {
3057 public:
3058 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003059 : HInstruction(SideEffects::None()),
3060 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003061 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003062 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003063 is_live_(false),
3064 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003065 inputs_.SetSize(number_of_inputs);
3066 }
3067
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003068 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3069 static Primitive::Type ToPhiType(Primitive::Type type) {
3070 switch (type) {
3071 case Primitive::kPrimBoolean:
3072 case Primitive::kPrimByte:
3073 case Primitive::kPrimShort:
3074 case Primitive::kPrimChar:
3075 return Primitive::kPrimInt;
3076 default:
3077 return type;
3078 }
3079 }
3080
Calin Juravle10e244f2015-01-26 18:54:32 +00003081 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003082
3083 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003084 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003085
Calin Juravle10e244f2015-01-26 18:54:32 +00003086 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003087 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003088
Calin Juravle10e244f2015-01-26 18:54:32 +00003089 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3090 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3091
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003092 uint32_t GetRegNumber() const { return reg_number_; }
3093
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003094 void SetDead() { is_live_ = false; }
3095 void SetLive() { is_live_ = true; }
3096 bool IsDead() const { return !is_live_; }
3097 bool IsLive() const { return is_live_; }
3098
Calin Juravlea4f88312015-04-16 12:57:19 +01003099 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3100 // An equivalent phi is a phi having the same dex register and type.
3101 // It assumes that phis with the same dex register are adjacent.
3102 HPhi* GetNextEquivalentPhiWithSameType() {
3103 HInstruction* next = GetNext();
3104 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3105 if (next->GetType() == GetType()) {
3106 return next->AsPhi();
3107 }
3108 next = next->GetNext();
3109 }
3110 return nullptr;
3111 }
3112
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003113 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003114
David Brazdil1abb4192015-02-17 18:33:36 +00003115 protected:
3116 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3117
3118 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3119 inputs_.Put(index, input);
3120 }
3121
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003122 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003123 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003124 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003125 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003126 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003127 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003128
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003129 DISALLOW_COPY_AND_ASSIGN(HPhi);
3130};
3131
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003132class HNullCheck : public HExpression<1> {
3133 public:
3134 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003135 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003136 SetRawInputAt(0, value);
3137 }
3138
Calin Juravle10e244f2015-01-26 18:54:32 +00003139 bool CanBeMoved() const OVERRIDE { return true; }
3140 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003141 UNUSED(other);
3142 return true;
3143 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003144
Calin Juravle10e244f2015-01-26 18:54:32 +00003145 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003146
Calin Juravle10e244f2015-01-26 18:54:32 +00003147 bool CanThrow() const OVERRIDE { return true; }
3148
3149 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003150
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003151 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003152
3153 DECLARE_INSTRUCTION(NullCheck);
3154
3155 private:
3156 const uint32_t dex_pc_;
3157
3158 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3159};
3160
3161class FieldInfo : public ValueObject {
3162 public:
Calin Juravle52c48962014-12-16 17:02:57 +00003163 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3164 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003165
3166 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003167 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003168 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003169
3170 private:
3171 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003172 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003173 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003174};
3175
3176class HInstanceFieldGet : public HExpression<1> {
3177 public:
3178 HInstanceFieldGet(HInstruction* value,
3179 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003180 MemberOffset field_offset,
3181 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003182 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003183 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003184 SetRawInputAt(0, value);
3185 }
3186
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003187 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003188
3189 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3190 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3191 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003192 }
3193
Calin Juravle641547a2015-04-21 22:08:51 +01003194 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3195 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003196 }
3197
3198 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003199 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3200 }
3201
Calin Juravle52c48962014-12-16 17:02:57 +00003202 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003203 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003204 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003205 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003206
3207 DECLARE_INSTRUCTION(InstanceFieldGet);
3208
3209 private:
3210 const FieldInfo field_info_;
3211
3212 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3213};
3214
3215class HInstanceFieldSet : public HTemplateInstruction<2> {
3216 public:
3217 HInstanceFieldSet(HInstruction* object,
3218 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003219 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003220 MemberOffset field_offset,
3221 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003222 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003223 field_info_(field_offset, field_type, is_volatile),
3224 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003225 SetRawInputAt(0, object);
3226 SetRawInputAt(1, value);
3227 }
3228
Calin Juravle641547a2015-04-21 22:08:51 +01003229 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3230 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003231 }
3232
Calin Juravle52c48962014-12-16 17:02:57 +00003233 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003234 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003235 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003236 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003237 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003238 bool GetValueCanBeNull() const { return value_can_be_null_; }
3239 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003240
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003241 DECLARE_INSTRUCTION(InstanceFieldSet);
3242
3243 private:
3244 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003245 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003246
3247 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3248};
3249
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003250class HArrayGet : public HExpression<2> {
3251 public:
3252 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003253 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003254 SetRawInputAt(0, array);
3255 SetRawInputAt(1, index);
3256 }
3257
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003258 bool CanBeMoved() const OVERRIDE { return true; }
3259 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003260 UNUSED(other);
3261 return true;
3262 }
Calin Juravle641547a2015-04-21 22:08:51 +01003263 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3264 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003265 // TODO: We can be smarter here.
3266 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3267 // which generates the implicit null check. There are cases when these can be removed
3268 // to produce better code. If we ever add optimizations to do so we should allow an
3269 // implicit check here (as long as the address falls in the first page).
3270 return false;
3271 }
3272
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003273 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003274
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003275 HInstruction* GetArray() const { return InputAt(0); }
3276 HInstruction* GetIndex() const { return InputAt(1); }
3277
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278 DECLARE_INSTRUCTION(ArrayGet);
3279
3280 private:
3281 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3282};
3283
3284class HArraySet : public HTemplateInstruction<3> {
3285 public:
3286 HArraySet(HInstruction* array,
3287 HInstruction* index,
3288 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003289 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003290 uint32_t dex_pc)
3291 : HTemplateInstruction(SideEffects::ChangesSomething()),
3292 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003293 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003294 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3295 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003296 SetRawInputAt(0, array);
3297 SetRawInputAt(1, index);
3298 SetRawInputAt(2, value);
3299 }
3300
Calin Juravle77520bc2015-01-12 18:45:46 +00003301 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003302 // We currently always call a runtime method to catch array store
3303 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003304 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003305 }
3306
Calin Juravle641547a2015-04-21 22:08:51 +01003307 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3308 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003309 // TODO: Same as for ArrayGet.
3310 return false;
3311 }
3312
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003313 void ClearNeedsTypeCheck() {
3314 needs_type_check_ = false;
3315 }
3316
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003317 void ClearValueCanBeNull() {
3318 value_can_be_null_ = false;
3319 }
3320
3321 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003322 bool NeedsTypeCheck() const { return needs_type_check_; }
3323
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003324 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003325
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003326 HInstruction* GetArray() const { return InputAt(0); }
3327 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003328 HInstruction* GetValue() const { return InputAt(2); }
3329
3330 Primitive::Type GetComponentType() const {
3331 // The Dex format does not type floating point index operations. Since the
3332 // `expected_component_type_` is set during building and can therefore not
3333 // be correct, we also check what is the value type. If it is a floating
3334 // point type, we must use that type.
3335 Primitive::Type value_type = GetValue()->GetType();
3336 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3337 ? value_type
3338 : expected_component_type_;
3339 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003340
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003341 DECLARE_INSTRUCTION(ArraySet);
3342
3343 private:
3344 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003345 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003346 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003347 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003348
3349 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3350};
3351
3352class HArrayLength : public HExpression<1> {
3353 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003354 explicit HArrayLength(HInstruction* array)
3355 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3356 // Note that arrays do not change length, so the instruction does not
3357 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003358 SetRawInputAt(0, array);
3359 }
3360
Calin Juravle77520bc2015-01-12 18:45:46 +00003361 bool CanBeMoved() const OVERRIDE { return true; }
3362 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003363 UNUSED(other);
3364 return true;
3365 }
Calin Juravle641547a2015-04-21 22:08:51 +01003366 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3367 return obj == InputAt(0);
3368 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003369
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003370 DECLARE_INSTRUCTION(ArrayLength);
3371
3372 private:
3373 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3374};
3375
3376class HBoundsCheck : public HExpression<2> {
3377 public:
3378 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003379 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003380 DCHECK(index->GetType() == Primitive::kPrimInt);
3381 SetRawInputAt(0, index);
3382 SetRawInputAt(1, length);
3383 }
3384
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003385 bool CanBeMoved() const OVERRIDE { return true; }
3386 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003387 UNUSED(other);
3388 return true;
3389 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003390
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003391 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003392
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003393 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003394
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003395 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003396
3397 DECLARE_INSTRUCTION(BoundsCheck);
3398
3399 private:
3400 const uint32_t dex_pc_;
3401
3402 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3403};
3404
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003405/**
3406 * Some DEX instructions are folded into multiple HInstructions that need
3407 * to stay live until the last HInstruction. This class
3408 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003409 * HInstruction stays live. `index` represents the stack location index of the
3410 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003411 */
3412class HTemporary : public HTemplateInstruction<0> {
3413 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003414 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003415
3416 size_t GetIndex() const { return index_; }
3417
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003418 Primitive::Type GetType() const OVERRIDE {
3419 // The previous instruction is the one that will be stored in the temporary location.
3420 DCHECK(GetPrevious() != nullptr);
3421 return GetPrevious()->GetType();
3422 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003423
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003424 DECLARE_INSTRUCTION(Temporary);
3425
3426 private:
3427 const size_t index_;
3428
3429 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3430};
3431
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003432class HSuspendCheck : public HTemplateInstruction<0> {
3433 public:
3434 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003435 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003436
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003437 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003438 return true;
3439 }
3440
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003441 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003442 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3443 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003444
3445 DECLARE_INSTRUCTION(SuspendCheck);
3446
3447 private:
3448 const uint32_t dex_pc_;
3449
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003450 // Only used for code generation, in order to share the same slow path between back edges
3451 // of a same loop.
3452 SlowPathCode* slow_path_;
3453
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003454 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3455};
3456
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003457/**
3458 * Instruction to load a Class object.
3459 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003460class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003461 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003462 HLoadClass(HCurrentMethod* current_method,
3463 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003464 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003465 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003466 uint32_t dex_pc)
3467 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3468 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003469 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003470 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003471 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003472 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003473 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3474 SetRawInputAt(0, current_method);
3475 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003476
3477 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003478
3479 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3480 return other->AsLoadClass()->type_index_ == type_index_;
3481 }
3482
3483 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3484
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003485 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003486 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003487 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003488
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003489 bool NeedsEnvironment() const OVERRIDE {
3490 // Will call runtime and load the class if the class is not loaded yet.
3491 // TODO: finer grain decision.
3492 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003493 }
3494
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003495 bool MustGenerateClinitCheck() const {
3496 return generate_clinit_check_;
3497 }
3498
Calin Juravle0ba218d2015-05-19 18:46:01 +01003499 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3500 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003501 }
3502
3503 bool CanCallRuntime() const {
3504 return MustGenerateClinitCheck() || !is_referrers_class_;
3505 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003506
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003507 bool CanThrow() const OVERRIDE {
3508 // May call runtime and and therefore can throw.
3509 // TODO: finer grain decision.
3510 return !is_referrers_class_;
3511 }
3512
Calin Juravleacf735c2015-02-12 15:25:22 +00003513 ReferenceTypeInfo GetLoadedClassRTI() {
3514 return loaded_class_rti_;
3515 }
3516
3517 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3518 // Make sure we only set exact types (the loaded class should never be merged).
3519 DCHECK(rti.IsExact());
3520 loaded_class_rti_ = rti;
3521 }
3522
3523 bool IsResolved() {
3524 return loaded_class_rti_.IsExact();
3525 }
3526
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003527 const DexFile& GetDexFile() { return dex_file_; }
3528
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003529 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3530
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003531 DECLARE_INSTRUCTION(LoadClass);
3532
3533 private:
3534 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003535 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003536 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003537 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003538 // Whether this instruction must generate the initialization check.
3539 // Used for code generation.
3540 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003541
Calin Juravleacf735c2015-02-12 15:25:22 +00003542 ReferenceTypeInfo loaded_class_rti_;
3543
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003544 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3545};
3546
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003547class HLoadString : public HExpression<0> {
3548 public:
3549 HLoadString(uint32_t string_index, uint32_t dex_pc)
3550 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3551 string_index_(string_index),
3552 dex_pc_(dex_pc) {}
3553
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003554 bool CanBeMoved() const OVERRIDE { return true; }
3555
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003556 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3557 return other->AsLoadString()->string_index_ == string_index_;
3558 }
3559
3560 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3561
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003562 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003563 uint32_t GetStringIndex() const { return string_index_; }
3564
3565 // TODO: Can we deopt or debug when we resolve a string?
3566 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003567 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003568
3569 DECLARE_INSTRUCTION(LoadString);
3570
3571 private:
3572 const uint32_t string_index_;
3573 const uint32_t dex_pc_;
3574
3575 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3576};
3577
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003578/**
3579 * Performs an initialization check on its Class object input.
3580 */
3581class HClinitCheck : public HExpression<1> {
3582 public:
3583 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003584 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003585 dex_pc_(dex_pc) {
3586 SetRawInputAt(0, constant);
3587 }
3588
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003589 bool CanBeMoved() const OVERRIDE { return true; }
3590 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3591 UNUSED(other);
3592 return true;
3593 }
3594
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003595 bool NeedsEnvironment() const OVERRIDE {
3596 // May call runtime to initialize the class.
3597 return true;
3598 }
3599
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003600 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003601
3602 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3603
3604 DECLARE_INSTRUCTION(ClinitCheck);
3605
3606 private:
3607 const uint32_t dex_pc_;
3608
3609 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3610};
3611
3612class HStaticFieldGet : public HExpression<1> {
3613 public:
3614 HStaticFieldGet(HInstruction* cls,
3615 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003616 MemberOffset field_offset,
3617 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003618 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003619 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003620 SetRawInputAt(0, cls);
3621 }
3622
Calin Juravle52c48962014-12-16 17:02:57 +00003623
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003624 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003625
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003626 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003627 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3628 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003629 }
3630
3631 size_t ComputeHashCode() const OVERRIDE {
3632 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3633 }
3634
Calin Juravle52c48962014-12-16 17:02:57 +00003635 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003636 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3637 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003638 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003639
3640 DECLARE_INSTRUCTION(StaticFieldGet);
3641
3642 private:
3643 const FieldInfo field_info_;
3644
3645 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3646};
3647
3648class HStaticFieldSet : public HTemplateInstruction<2> {
3649 public:
3650 HStaticFieldSet(HInstruction* cls,
3651 HInstruction* value,
3652 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003653 MemberOffset field_offset,
3654 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003655 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003656 field_info_(field_offset, field_type, is_volatile),
3657 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003658 SetRawInputAt(0, cls);
3659 SetRawInputAt(1, value);
3660 }
3661
Calin Juravle52c48962014-12-16 17:02:57 +00003662 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003663 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3664 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003665 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003666
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003667 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003668 bool GetValueCanBeNull() const { return value_can_be_null_; }
3669 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003670
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003671 DECLARE_INSTRUCTION(StaticFieldSet);
3672
3673 private:
3674 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003675 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003676
3677 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3678};
3679
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003680// Implement the move-exception DEX instruction.
3681class HLoadException : public HExpression<0> {
3682 public:
3683 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3684
3685 DECLARE_INSTRUCTION(LoadException);
3686
3687 private:
3688 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3689};
3690
3691class HThrow : public HTemplateInstruction<1> {
3692 public:
3693 HThrow(HInstruction* exception, uint32_t dex_pc)
3694 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3695 SetRawInputAt(0, exception);
3696 }
3697
3698 bool IsControlFlow() const OVERRIDE { return true; }
3699
3700 bool NeedsEnvironment() const OVERRIDE { return true; }
3701
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003702 bool CanThrow() const OVERRIDE { return true; }
3703
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003704 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003705
3706 DECLARE_INSTRUCTION(Throw);
3707
3708 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003709 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003710
3711 DISALLOW_COPY_AND_ASSIGN(HThrow);
3712};
3713
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003714class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003715 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003716 HInstanceOf(HInstruction* object,
3717 HLoadClass* constant,
3718 bool class_is_final,
3719 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003720 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3721 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003722 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003723 dex_pc_(dex_pc) {
3724 SetRawInputAt(0, object);
3725 SetRawInputAt(1, constant);
3726 }
3727
3728 bool CanBeMoved() const OVERRIDE { return true; }
3729
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003730 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003731 return true;
3732 }
3733
3734 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003735 return false;
3736 }
3737
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003738 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003739
3740 bool IsClassFinal() const { return class_is_final_; }
3741
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003742 // Used only in code generation.
3743 bool MustDoNullCheck() const { return must_do_null_check_; }
3744 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3745
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003746 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003747
3748 private:
3749 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003750 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003751 const uint32_t dex_pc_;
3752
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003753 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3754};
3755
Calin Juravleb1498f62015-02-16 13:13:29 +00003756class HBoundType : public HExpression<1> {
3757 public:
3758 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3759 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3760 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003761 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003762 SetRawInputAt(0, input);
3763 }
3764
3765 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3766
3767 bool CanBeNull() const OVERRIDE {
3768 // `null instanceof ClassX` always return false so we can't be null.
3769 return false;
3770 }
3771
3772 DECLARE_INSTRUCTION(BoundType);
3773
3774 private:
3775 // Encodes the most upper class that this instruction can have. In other words
3776 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3777 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3778 const ReferenceTypeInfo bound_type_;
3779
3780 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3781};
3782
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003783class HCheckCast : public HTemplateInstruction<2> {
3784 public:
3785 HCheckCast(HInstruction* object,
3786 HLoadClass* constant,
3787 bool class_is_final,
3788 uint32_t dex_pc)
3789 : HTemplateInstruction(SideEffects::None()),
3790 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003791 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003792 dex_pc_(dex_pc) {
3793 SetRawInputAt(0, object);
3794 SetRawInputAt(1, constant);
3795 }
3796
3797 bool CanBeMoved() const OVERRIDE { return true; }
3798
3799 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3800 return true;
3801 }
3802
3803 bool NeedsEnvironment() const OVERRIDE {
3804 // Instruction may throw a CheckCastError.
3805 return true;
3806 }
3807
3808 bool CanThrow() const OVERRIDE { return true; }
3809
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003810 bool MustDoNullCheck() const { return must_do_null_check_; }
3811 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3812
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003813 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003814
3815 bool IsClassFinal() const { return class_is_final_; }
3816
3817 DECLARE_INSTRUCTION(CheckCast);
3818
3819 private:
3820 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003821 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003822 const uint32_t dex_pc_;
3823
3824 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003825};
3826
Calin Juravle27df7582015-04-17 19:12:31 +01003827class HMemoryBarrier : public HTemplateInstruction<0> {
3828 public:
3829 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3830 : HTemplateInstruction(SideEffects::None()),
3831 barrier_kind_(barrier_kind) {}
3832
3833 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3834
3835 DECLARE_INSTRUCTION(MemoryBarrier);
3836
3837 private:
3838 const MemBarrierKind barrier_kind_;
3839
3840 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3841};
3842
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003843class HMonitorOperation : public HTemplateInstruction<1> {
3844 public:
3845 enum OperationKind {
3846 kEnter,
3847 kExit,
3848 };
3849
3850 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3851 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3852 SetRawInputAt(0, object);
3853 }
3854
3855 // Instruction may throw a Java exception, so we need an environment.
3856 bool NeedsEnvironment() const OVERRIDE { return true; }
3857 bool CanThrow() const OVERRIDE { return true; }
3858
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003859 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003860
3861 bool IsEnter() const { return kind_ == kEnter; }
3862
3863 DECLARE_INSTRUCTION(MonitorOperation);
3864
Calin Juravle52c48962014-12-16 17:02:57 +00003865 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003866 const OperationKind kind_;
3867 const uint32_t dex_pc_;
3868
3869 private:
3870 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3871};
3872
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003873class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003874 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003875 MoveOperands(Location source,
3876 Location destination,
3877 Primitive::Type type,
3878 HInstruction* instruction)
3879 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003880
3881 Location GetSource() const { return source_; }
3882 Location GetDestination() const { return destination_; }
3883
3884 void SetSource(Location value) { source_ = value; }
3885 void SetDestination(Location value) { destination_ = value; }
3886
3887 // The parallel move resolver marks moves as "in-progress" by clearing the
3888 // destination (but not the source).
3889 Location MarkPending() {
3890 DCHECK(!IsPending());
3891 Location dest = destination_;
3892 destination_ = Location::NoLocation();
3893 return dest;
3894 }
3895
3896 void ClearPending(Location dest) {
3897 DCHECK(IsPending());
3898 destination_ = dest;
3899 }
3900
3901 bool IsPending() const {
3902 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3903 return destination_.IsInvalid() && !source_.IsInvalid();
3904 }
3905
3906 // True if this blocks a move from the given location.
3907 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003908 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003909 }
3910
3911 // A move is redundant if it's been eliminated, if its source and
3912 // destination are the same, or if its destination is unneeded.
3913 bool IsRedundant() const {
3914 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3915 }
3916
3917 // We clear both operands to indicate move that's been eliminated.
3918 void Eliminate() {
3919 source_ = destination_ = Location::NoLocation();
3920 }
3921
3922 bool IsEliminated() const {
3923 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3924 return source_.IsInvalid();
3925 }
3926
Nicolas Geoffray90218252015-04-15 11:56:51 +01003927 bool Is64BitMove() const {
3928 return Primitive::Is64BitType(type_);
3929 }
3930
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003931 HInstruction* GetInstruction() const { return instruction_; }
3932
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003933 private:
3934 Location source_;
3935 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003936 // The type this move is for.
3937 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003938 // The instruction this move is assocatied with. Null when this move is
3939 // for moving an input in the expected locations of user (including a phi user).
3940 // This is only used in debug mode, to ensure we do not connect interval siblings
3941 // in the same parallel move.
3942 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003943};
3944
3945static constexpr size_t kDefaultNumberOfMoves = 4;
3946
3947class HParallelMove : public HTemplateInstruction<0> {
3948 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003949 explicit HParallelMove(ArenaAllocator* arena)
3950 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003951
Nicolas Geoffray90218252015-04-15 11:56:51 +01003952 void AddMove(Location source,
3953 Location destination,
3954 Primitive::Type type,
3955 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003956 DCHECK(source.IsValid());
3957 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003958 if (kIsDebugBuild) {
3959 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003960 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003961 if (moves_.Get(i).GetInstruction() == instruction) {
3962 // Special case the situation where the move is for the spill slot
3963 // of the instruction.
3964 if ((GetPrevious() == instruction)
3965 || ((GetPrevious() == nullptr)
3966 && instruction->IsPhi()
3967 && instruction->GetBlock() == GetBlock())) {
3968 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3969 << "Doing parallel moves for the same instruction.";
3970 } else {
3971 DCHECK(false) << "Doing parallel moves for the same instruction.";
3972 }
3973 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003974 }
3975 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003976 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003977 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01003978 << "Overlapped destination for two moves in a parallel move: "
3979 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
3980 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003981 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003982 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003983 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003984 }
3985
3986 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003987 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003988 }
3989
3990 size_t NumMoves() const { return moves_.Size(); }
3991
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003992 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003993
3994 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003995 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003996
3997 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3998};
3999
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004000class HGraphVisitor : public ValueObject {
4001 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004002 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4003 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004004
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004005 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004006 virtual void VisitBasicBlock(HBasicBlock* block);
4007
Roland Levillain633021e2014-10-01 14:12:25 +01004008 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004009 void VisitInsertionOrder();
4010
Roland Levillain633021e2014-10-01 14:12:25 +01004011 // Visit the graph following dominator tree reverse post-order.
4012 void VisitReversePostOrder();
4013
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004014 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004015
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004016 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004017#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004018 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4019
4020 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4021
4022#undef DECLARE_VISIT_INSTRUCTION
4023
4024 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004025 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004026
4027 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4028};
4029
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004030class HGraphDelegateVisitor : public HGraphVisitor {
4031 public:
4032 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4033 virtual ~HGraphDelegateVisitor() {}
4034
4035 // Visit functions that delegate to to super class.
4036#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004037 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004038
4039 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4040
4041#undef DECLARE_VISIT_INSTRUCTION
4042
4043 private:
4044 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4045};
4046
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004047class HInsertionOrderIterator : public ValueObject {
4048 public:
4049 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4050
4051 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4052 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4053 void Advance() { ++index_; }
4054
4055 private:
4056 const HGraph& graph_;
4057 size_t index_;
4058
4059 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4060};
4061
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004062class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004063 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004064 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4065 // Check that reverse post order of the graph has been built.
4066 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4067 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004068
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004069 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4070 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004071 void Advance() { ++index_; }
4072
4073 private:
4074 const HGraph& graph_;
4075 size_t index_;
4076
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004077 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004078};
4079
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004080class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004081 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004082 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004083 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4084 // Check that reverse post order of the graph has been built.
4085 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4086 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004087
4088 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004089 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004090 void Advance() { --index_; }
4091
4092 private:
4093 const HGraph& graph_;
4094 size_t index_;
4095
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004096 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004097};
4098
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004099class HLinearPostOrderIterator : public ValueObject {
4100 public:
4101 explicit HLinearPostOrderIterator(const HGraph& graph)
4102 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4103
4104 bool Done() const { return index_ == 0; }
4105
4106 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4107
4108 void Advance() {
4109 --index_;
4110 DCHECK_GE(index_, 0U);
4111 }
4112
4113 private:
4114 const GrowableArray<HBasicBlock*>& order_;
4115 size_t index_;
4116
4117 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4118};
4119
4120class HLinearOrderIterator : public ValueObject {
4121 public:
4122 explicit HLinearOrderIterator(const HGraph& graph)
4123 : order_(graph.GetLinearOrder()), index_(0) {}
4124
4125 bool Done() const { return index_ == order_.Size(); }
4126 HBasicBlock* Current() const { return order_.Get(index_); }
4127 void Advance() { ++index_; }
4128
4129 private:
4130 const GrowableArray<HBasicBlock*>& order_;
4131 size_t index_;
4132
4133 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4134};
4135
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004136// Iterator over the blocks that art part of the loop. Includes blocks part
4137// of an inner loop. The order in which the blocks are iterated is on their
4138// block id.
4139class HBlocksInLoopIterator : public ValueObject {
4140 public:
4141 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4142 : blocks_in_loop_(info.GetBlocks()),
4143 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4144 index_(0) {
4145 if (!blocks_in_loop_.IsBitSet(index_)) {
4146 Advance();
4147 }
4148 }
4149
4150 bool Done() const { return index_ == blocks_.Size(); }
4151 HBasicBlock* Current() const { return blocks_.Get(index_); }
4152 void Advance() {
4153 ++index_;
4154 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4155 if (blocks_in_loop_.IsBitSet(index_)) {
4156 break;
4157 }
4158 }
4159 }
4160
4161 private:
4162 const BitVector& blocks_in_loop_;
4163 const GrowableArray<HBasicBlock*>& blocks_;
4164 size_t index_;
4165
4166 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4167};
4168
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004169inline int64_t Int64FromConstant(HConstant* constant) {
4170 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4171 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4172 : constant->AsLongConstant()->GetValue();
4173}
4174
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004175} // namespace art
4176
4177#endif // ART_COMPILER_OPTIMIZING_NODES_H_