blob: f8149d16e368317d1b0afc414779409e51fb410e [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000038class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000040class HFloatConstant;
41class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000046class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010048class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010051class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053
54static const int kDefaultNumberOfBlocks = 8;
55static const int kDefaultNumberOfSuccessors = 2;
56static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010057static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000058static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000059
Calin Juravle9aec02f2014-11-18 23:06:35 +000060static constexpr uint32_t kMaxIntShiftValue = 0x1f;
61static constexpr uint64_t kMaxLongShiftValue = 0x3f;
62
Dave Allison20dfc792014-06-16 20:44:29 -070063enum IfCondition {
64 kCondEQ,
65 kCondNE,
66 kCondLT,
67 kCondLE,
68 kCondGT,
69 kCondGE,
70};
71
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010072class HInstructionList {
73 public:
74 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
75
76 void AddInstruction(HInstruction* instruction);
77 void RemoveInstruction(HInstruction* instruction);
78
David Brazdilc3d743f2015-04-22 13:40:50 +010079 // Insert `instruction` before/after an existing instruction `cursor`.
80 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
81 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
82
Roland Levillain6b469232014-09-25 10:10:38 +010083 // Return true if this list contains `instruction`.
84 bool Contains(HInstruction* instruction) const;
85
Roland Levillainccc07a92014-09-16 14:48:16 +010086 // Return true if `instruction1` is found before `instruction2` in
87 // this instruction list and false otherwise. Abort if none
88 // of these instructions is found.
89 bool FoundBefore(const HInstruction* instruction1,
90 const HInstruction* instruction2) const;
91
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000092 bool IsEmpty() const { return first_instruction_ == nullptr; }
93 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
94
95 // Update the block of all instructions to be `block`.
96 void SetBlockOfInstructions(HBasicBlock* block) const;
97
98 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
99 void Add(const HInstructionList& instruction_list);
100
David Brazdil2d7352b2015-04-20 14:52:42 +0100101 // Return the number of instructions in the list. This is an expensive operation.
102 size_t CountSize() const;
103
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100104 private:
105 HInstruction* first_instruction_;
106 HInstruction* last_instruction_;
107
108 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000109 friend class HGraph;
110 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100111 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100112 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100113
114 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
115};
116
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000117// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700118class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100120 HGraph(ArenaAllocator* arena,
121 const DexFile& dex_file,
122 uint32_t method_idx,
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700123 InstructionSet instruction_set,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100124 bool debuggable = false,
125 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000126 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000127 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100128 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100129 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700130 entry_block_(nullptr),
131 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100132 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100133 number_of_vregs_(0),
134 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000135 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400136 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000137 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000138 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100139 dex_file_(dex_file),
140 method_idx_(method_idx),
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700141 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000142 cached_null_constant_(nullptr),
143 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000144 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
145 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
146 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000148 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100149 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100150 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000151
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000152 HBasicBlock* GetEntryBlock() const { return entry_block_; }
153 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000154
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000155 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
156 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000157
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000158 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 // Try building the SSA form of this graph, with dominance computation and loop
161 // recognition. Returns whether it was successful in doing all these steps.
162 bool TryBuildingSsa() {
163 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000164 // The SSA builder requires loops to all be natural. Specifically, the dead phi
165 // elimination phase checks the consistency of the graph when doing a post-order
166 // visit for eliminating dead phis: a dead phi can only have loop header phi
167 // users remaining when being visited.
168 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000169 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000170 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 }
172
Nicolas Geoffray18b236e2015-06-24 12:20:24 +0100173 void ComputeDominanceInformation();
174 void ClearDominanceInformation();
175
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100178 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000179
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000180 // Analyze all natural loops in this graph. Returns false if one
181 // loop is not natural, that is the header does not dominate the
182 // back edge.
183 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
186 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
187
Mingyao Yangbca381a2015-05-19 16:01:59 -0700188 // Need to add a couple of blocks to test if the loop body is entered and
189 // put deoptimization instructions, etc.
190 void TransformLoopHeaderForBCE(HBasicBlock* header);
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
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000259 bool IsDebuggable() const { return debuggable_; }
260
David Brazdil8d5b8b22015-03-24 10:51:52 +0000261 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000262 // already, it is created and inserted into the graph. This method is only for
263 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000264 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000265 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000266 HIntConstant* GetIntConstant(int32_t value) {
267 return CreateConstant(value, &cached_int_constants_);
268 }
269 HLongConstant* GetLongConstant(int64_t value) {
270 return CreateConstant(value, &cached_long_constants_);
271 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000272 HFloatConstant* GetFloatConstant(float value) {
273 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
274 }
275 HDoubleConstant* GetDoubleConstant(double value) {
276 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
277 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000278
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000279 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100280
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100281 const DexFile& GetDexFile() const {
282 return dex_file_;
283 }
284
285 uint32_t GetMethodIdx() const {
286 return method_idx_;
287 }
288
David Brazdil2d7352b2015-04-20 14:52:42 +0100289 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290 void VisitBlockForDominatorTree(HBasicBlock* block,
291 HBasicBlock* predecessor,
292 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100293 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000294 void VisitBlockForBackEdges(HBasicBlock* block,
295 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100296 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000297 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100298 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000299
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000300 template <class InstructionType, typename ValueType>
301 InstructionType* CreateConstant(ValueType value,
302 ArenaSafeMap<ValueType, InstructionType*>* cache) {
303 // Try to find an existing constant of the given value.
304 InstructionType* constant = nullptr;
305 auto cached_constant = cache->find(value);
306 if (cached_constant != cache->end()) {
307 constant = cached_constant->second;
308 }
309
310 // If not found or previously deleted, create and cache a new instruction.
311 if (constant == nullptr || constant->GetBlock() == nullptr) {
312 constant = new (arena_) InstructionType(value);
313 cache->Overwrite(value, constant);
314 InsertConstant(constant);
315 }
316 return constant;
317 }
318
David Brazdil8d5b8b22015-03-24 10:51:52 +0000319 void InsertConstant(HConstant* instruction);
320
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000321 // Cache a float constant into the graph. This method should only be
322 // called by the SsaBuilder when creating "equivalent" instructions.
323 void CacheFloatConstant(HFloatConstant* constant);
324
325 // See CacheFloatConstant comment.
326 void CacheDoubleConstant(HDoubleConstant* constant);
327
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000328 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000329
330 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000331 GrowableArray<HBasicBlock*> blocks_;
332
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100333 // List of blocks to perform a reverse post order tree traversal.
334 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000335
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100336 // List of blocks to perform a linear order tree traversal.
337 GrowableArray<HBasicBlock*> linear_order_;
338
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000339 HBasicBlock* entry_block_;
340 HBasicBlock* exit_block_;
341
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100342 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100343 uint16_t maximum_number_of_out_vregs_;
344
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100345 // The number of virtual registers in this method. Contains the parameters.
346 uint16_t number_of_vregs_;
347
348 // The number of virtual registers used by parameters of this method.
349 uint16_t number_of_in_vregs_;
350
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000351 // Number of vreg size slots that the temporaries use (used in baseline compiler).
352 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100353
Mark Mendell1152c922015-04-24 17:06:35 -0400354 // Has bounds checks. We can totally skip BCE if it's false.
355 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800356
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000357 // Indicates whether the graph should be compiled in a way that
358 // ensures full debuggability. If false, we can apply more
359 // aggressive optimizations that may limit the level of debugging.
360 const bool debuggable_;
361
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000362 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000363 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000364
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100365 // The dex file from which the method is from.
366 const DexFile& dex_file_;
367
368 // The method index in the dex file.
369 const uint32_t method_idx_;
370
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700371 const InstructionSet instruction_set_;
372
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000373 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000374 HNullConstant* cached_null_constant_;
375 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000376 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000377 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000378 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000379
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000380 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100381 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000382 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383 DISALLOW_COPY_AND_ASSIGN(HGraph);
384};
385
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700386class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000387 public:
388 HLoopInformation(HBasicBlock* header, HGraph* graph)
389 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100390 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100391 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100392 // Make bit vector growable, as the number of blocks may change.
393 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100394
395 HBasicBlock* GetHeader() const {
396 return header_;
397 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000398
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000399 void SetHeader(HBasicBlock* block) {
400 header_ = block;
401 }
402
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100403 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
404 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
405 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
406
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000407 void AddBackEdge(HBasicBlock* back_edge) {
408 back_edges_.Add(back_edge);
409 }
410
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100411 void RemoveBackEdge(HBasicBlock* back_edge) {
412 back_edges_.Delete(back_edge);
413 }
414
David Brazdil46e2a392015-03-16 17:31:52 +0000415 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100416 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000417 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100418 }
419 return false;
420 }
421
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000422 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000423 return back_edges_.Size();
424 }
425
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100427
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100428 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
429 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100430 }
431
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100432 // Returns the lifetime position of the back edge that has the
433 // greatest lifetime position.
434 size_t GetLifetimeEnd() const;
435
436 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
437 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
438 if (back_edges_.Get(i) == existing) {
439 back_edges_.Put(i, new_back_edge);
440 return;
441 }
442 }
443 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100444 }
445
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100446 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100447 // that is the header dominates the back edge.
448 bool Populate();
449
David Brazdile8ff50d2015-05-07 09:59:30 +0100450 // Reanalyzes the loop by removing loop info from its blocks and re-running
451 // Populate(). If there are no back edges left, the loop info is completely
452 // removed as well as its SuspendCheck instruction. It must be run on nested
453 // inner loops first.
454 void Update();
455
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100456 // Returns whether this loop information contains `block`.
457 // Note that this loop information *must* be populated before entering this function.
458 bool Contains(const HBasicBlock& block) const;
459
460 // Returns whether this loop information is an inner loop of `other`.
461 // Note that `other` *must* be populated before entering this function.
462 bool IsIn(const HLoopInformation& other) const;
463
464 const ArenaBitVector& GetBlocks() const { return blocks_; }
465
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000466 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000467 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000468
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000469 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100470 // Internal recursive implementation of `Populate`.
471 void PopulateRecursive(HBasicBlock* block);
472
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000473 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100474 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000475 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100476 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000477
478 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
479};
480
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100481static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100482static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100483
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000484// A block in a method. Contains the list of instructions represented
485// as a double linked list. Each block knows its predecessors and
486// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100487
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700488class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000489 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100490 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000491 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000492 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
493 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000494 loop_information_(nullptr),
495 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100496 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100497 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100498 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100499 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000500 lifetime_end_(kNoLifetime),
501 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000502
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100503 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
504 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000505 }
506
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100507 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
508 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000509 }
510
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100511 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
512 return dominated_blocks_;
513 }
514
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100515 bool IsEntryBlock() const {
516 return graph_->GetEntryBlock() == this;
517 }
518
519 bool IsExitBlock() const {
520 return graph_->GetExitBlock() == this;
521 }
522
David Brazdil46e2a392015-03-16 17:31:52 +0000523 bool IsSingleGoto() const;
524
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000525 void AddBackEdge(HBasicBlock* back_edge) {
526 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000527 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000528 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100529 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000530 loop_information_->AddBackEdge(back_edge);
531 }
532
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000533 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000534 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000535
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000536 int GetBlockId() const { return block_id_; }
537 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000538
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000539 HBasicBlock* GetDominator() const { return dominator_; }
540 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100541 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100542 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000543 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
544 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
545 if (dominated_blocks_.Get(i) == existing) {
546 dominated_blocks_.Put(i, new_block);
547 return;
548 }
549 }
550 LOG(FATAL) << "Unreachable";
551 UNREACHABLE();
552 }
Nicolas Geoffray18b236e2015-06-24 12:20:24 +0100553 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000554
555 int NumberOfBackEdges() const {
Nicolas Geoffray18b236e2015-06-24 12:20:24 +0100556 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557 }
558
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100559 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
560 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100561 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100562 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100563 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
564 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000565
566 void AddSuccessor(HBasicBlock* block) {
567 successors_.Add(block);
568 block->predecessors_.Add(this);
569 }
570
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100571 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
572 size_t successor_index = GetSuccessorIndexOf(existing);
573 DCHECK_NE(successor_index, static_cast<size_t>(-1));
574 existing->RemovePredecessor(this);
575 new_block->predecessors_.Add(this);
576 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000577 }
578
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000579 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
580 size_t predecessor_index = GetPredecessorIndexOf(existing);
581 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
582 existing->RemoveSuccessor(this);
583 new_block->successors_.Add(this);
584 predecessors_.Put(predecessor_index, new_block);
585 }
586
Nicolas Geoffray1e256bf2015-06-19 16:17:05 +0100587 // Insert `this` between `predecessor` and `successor. This method
588 // preserves the indicies, and will update the first edge found between
589 // `predecessor` and `successor`.
590 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
591 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
592 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
593 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
594 DCHECK_NE(successor_index, static_cast<size_t>(-1));
595 successor->predecessors_.Put(predecessor_index, this);
596 predecessor->successors_.Put(successor_index, this);
597 successors_.Add(successor);
598 predecessors_.Add(predecessor);
599 }
600
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100601 void RemovePredecessor(HBasicBlock* block) {
602 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100603 }
604
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000605 void RemoveSuccessor(HBasicBlock* block) {
606 successors_.Delete(block);
607 }
608
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100609 void ClearAllPredecessors() {
610 predecessors_.Reset();
611 }
612
613 void AddPredecessor(HBasicBlock* block) {
614 predecessors_.Add(block);
615 block->successors_.Add(this);
616 }
617
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100618 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100619 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100620 HBasicBlock* temp = predecessors_.Get(0);
621 predecessors_.Put(0, predecessors_.Get(1));
622 predecessors_.Put(1, temp);
623 }
624
David Brazdil769c9e52015-04-27 13:54:09 +0100625 void SwapSuccessors() {
626 DCHECK_EQ(successors_.Size(), 2u);
627 HBasicBlock* temp = successors_.Get(0);
628 successors_.Put(0, successors_.Get(1));
629 successors_.Put(1, temp);
630 }
631
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100632 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
633 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
634 if (predecessors_.Get(i) == predecessor) {
635 return i;
636 }
637 }
638 return -1;
639 }
640
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100641 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
642 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
643 if (successors_.Get(i) == successor) {
644 return i;
645 }
646 }
647 return -1;
648 }
649
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000650 // Split the block into two blocks just after `cursor`. Returns the newly
651 // created block. Note that this method just updates raw block information,
652 // like predecessors, successors, dominators, and instruction list. It does not
653 // update the graph, reverse post order, loop information, nor make sure the
654 // blocks are consistent (for example ending with a control flow instruction).
655 HBasicBlock* SplitAfter(HInstruction* cursor);
656
657 // Merge `other` at the end of `this`. Successors and dominated blocks of
658 // `other` are changed to be successors and dominated blocks of `this`. Note
659 // that this method does not update the graph, reverse post order, loop
660 // information, nor make sure the blocks are consistent (for example ending
661 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100662 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000663
664 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
665 // of `this` are moved to `other`.
666 // Note that this method does not update the graph, reverse post order, loop
667 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000668 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000669 void ReplaceWith(HBasicBlock* other);
670
David Brazdil2d7352b2015-04-20 14:52:42 +0100671 // Merge `other` at the end of `this`. This method updates loops, reverse post
672 // order, links to predecessors, successors, dominators and deletes the block
673 // from the graph. The two blocks must be successive, i.e. `this` the only
674 // predecessor of `other` and vice versa.
675 void MergeWith(HBasicBlock* other);
676
677 // Disconnects `this` from all its predecessors, successors and dominator,
678 // removes it from all loops it is included in and eventually from the graph.
679 // The block must not dominate any other block. Predecessors and successors
680 // are safely updated.
681 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000682
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000683 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100684 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100685 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100686 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100687 // Replace instruction `initial` with `replacement` within this block.
688 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
689 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100690 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100691 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000692 // RemoveInstruction and RemovePhi delete a given instruction from the respective
693 // instruction list. With 'ensure_safety' set to true, it verifies that the
694 // instruction is not in use and removes it from the use lists of its inputs.
695 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
696 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100697 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100698
699 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100700 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100701 }
702
Roland Levillain6b879dd2014-09-22 17:13:44 +0100703 bool IsLoopPreHeaderFirstPredecessor() const {
704 DCHECK(IsLoopHeader());
705 DCHECK(!GetPredecessors().IsEmpty());
706 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
707 }
708
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709 HLoopInformation* GetLoopInformation() const {
710 return loop_information_;
711 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000712
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000713 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100714 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000715 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100716 void SetInLoop(HLoopInformation* info) {
717 if (IsLoopHeader()) {
718 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100719 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100720 loop_information_ = info;
721 } else if (loop_information_->Contains(*info->GetHeader())) {
722 // Block is currently part of an outer loop. Make it part of this inner loop.
723 // Note that a non loop header having a loop information means this loop information
724 // has already been populated
725 loop_information_ = info;
726 } else {
727 // Block is part of an inner loop. Do not update the loop information.
728 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
729 // at this point, because this method is being called while populating `info`.
730 }
731 }
732
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000733 // Raw update of the loop information.
734 void SetLoopInformation(HLoopInformation* info) {
735 loop_information_ = info;
736 }
737
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100738 bool IsInLoop() const { return loop_information_ != nullptr; }
739
David Brazdile8ff50d2015-05-07 09:59:30 +0100740 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100741 bool Dominates(HBasicBlock* block) const;
742
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100743 size_t GetLifetimeStart() const { return lifetime_start_; }
744 size_t GetLifetimeEnd() const { return lifetime_end_; }
745
746 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
747 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
748
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100749 uint32_t GetDexPc() const { return dex_pc_; }
750
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000751 bool IsCatchBlock() const { return is_catch_block_; }
752 void SetIsCatchBlock() { is_catch_block_ = true; }
753
David Brazdil8d5b8b22015-03-24 10:51:52 +0000754 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000755 bool EndsWithIf() const;
756 bool HasSinglePhi() const;
757
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000758 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000759 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760 GrowableArray<HBasicBlock*> predecessors_;
761 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100762 HInstructionList instructions_;
763 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000764 HLoopInformation* loop_information_;
765 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100766 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000767 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100768 // The dex program counter of the first instruction of this block.
769 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100770 size_t lifetime_start_;
771 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000772 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000773
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000774 friend class HGraph;
775 friend class HInstruction;
776
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000777 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
778};
779
David Brazdilb2bd1c52015-03-25 11:17:37 +0000780// Iterates over the LoopInformation of all loops which contain 'block'
781// from the innermost to the outermost.
782class HLoopInformationOutwardIterator : public ValueObject {
783 public:
784 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
785 : current_(block.GetLoopInformation()) {}
786
787 bool Done() const { return current_ == nullptr; }
788
789 void Advance() {
790 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100791 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000792 }
793
794 HLoopInformation* Current() const {
795 DCHECK(!Done());
796 return current_;
797 }
798
799 private:
800 HLoopInformation* current_;
801
802 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
803};
804
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100805#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
806 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000807 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000808 M(ArrayGet, Instruction) \
809 M(ArrayLength, Instruction) \
810 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100811 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000812 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000813 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000814 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100815 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000816 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100817 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700818 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000819 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000820 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000821 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100822 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000823 M(Exit, Instruction) \
824 M(FloatConstant, Constant) \
825 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100826 M(GreaterThan, Condition) \
827 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100828 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000829 M(InstanceFieldGet, Instruction) \
830 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000831 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100832 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000833 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000834 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100835 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000836 M(LessThan, Condition) \
837 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000838 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000839 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100840 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000841 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100842 M(Local, Instruction) \
843 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100844 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000845 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000846 M(Mul, BinaryOperation) \
847 M(Neg, UnaryOperation) \
848 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100849 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100850 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000851 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000852 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000853 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000854 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100855 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000856 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100857 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000858 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100859 M(Return, Instruction) \
860 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000861 M(Shl, BinaryOperation) \
862 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100863 M(StaticFieldGet, Instruction) \
864 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100865 M(StoreLocal, Instruction) \
866 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100867 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000868 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000869 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000870 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000871 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000872 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000873
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100874#define FOR_EACH_INSTRUCTION(M) \
875 FOR_EACH_CONCRETE_INSTRUCTION(M) \
876 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100877 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100878 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100879 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700880
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100881#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000882FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
883#undef FORWARD_DECLARATION
884
Roland Levillainccc07a92014-09-16 14:48:16 +0100885#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000886 InstructionKind GetKind() const OVERRIDE { return k##type; } \
887 const char* DebugName() const OVERRIDE { return #type; } \
888 const H##type* As##type() const OVERRIDE { return this; } \
889 H##type* As##type() OVERRIDE { return this; } \
890 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100891 return other->Is##type(); \
892 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000893 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894
David Brazdiled596192015-01-23 10:39:45 +0000895template <typename T> class HUseList;
896
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100897template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700898class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000899 public:
David Brazdiled596192015-01-23 10:39:45 +0000900 HUseListNode* GetPrevious() const { return prev_; }
901 HUseListNode* GetNext() const { return next_; }
902 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100903 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100904 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100905
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000906 private:
David Brazdiled596192015-01-23 10:39:45 +0000907 HUseListNode(T user, size_t index)
908 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
909
910 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100911 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000912 HUseListNode<T>* prev_;
913 HUseListNode<T>* next_;
914
915 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000916
917 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
918};
919
David Brazdiled596192015-01-23 10:39:45 +0000920template <typename T>
921class HUseList : public ValueObject {
922 public:
923 HUseList() : first_(nullptr) {}
924
925 void Clear() {
926 first_ = nullptr;
927 }
928
929 // Adds a new entry at the beginning of the use list and returns
930 // the newly created node.
931 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000932 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000933 if (IsEmpty()) {
934 first_ = new_node;
935 } else {
936 first_->prev_ = new_node;
937 new_node->next_ = first_;
938 first_ = new_node;
939 }
940 return new_node;
941 }
942
943 HUseListNode<T>* GetFirst() const {
944 return first_;
945 }
946
947 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000948 DCHECK(node != nullptr);
949 DCHECK(Contains(node));
950
David Brazdiled596192015-01-23 10:39:45 +0000951 if (node->prev_ != nullptr) {
952 node->prev_->next_ = node->next_;
953 }
954 if (node->next_ != nullptr) {
955 node->next_->prev_ = node->prev_;
956 }
957 if (node == first_) {
958 first_ = node->next_;
959 }
960 }
961
David Brazdil1abb4192015-02-17 18:33:36 +0000962 bool Contains(const HUseListNode<T>* node) const {
963 if (node == nullptr) {
964 return false;
965 }
966 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
967 if (current == node) {
968 return true;
969 }
970 }
971 return false;
972 }
973
David Brazdiled596192015-01-23 10:39:45 +0000974 bool IsEmpty() const {
975 return first_ == nullptr;
976 }
977
978 bool HasOnlyOneUse() const {
979 return first_ != nullptr && first_->next_ == nullptr;
980 }
981
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100982 size_t SizeSlow() const {
983 size_t count = 0;
984 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
985 ++count;
986 }
987 return count;
988 }
989
David Brazdiled596192015-01-23 10:39:45 +0000990 private:
991 HUseListNode<T>* first_;
992};
993
994template<typename T>
995class HUseIterator : public ValueObject {
996 public:
997 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
998
999 bool Done() const { return current_ == nullptr; }
1000
1001 void Advance() {
1002 DCHECK(!Done());
1003 current_ = current_->GetNext();
1004 }
1005
1006 HUseListNode<T>* Current() const {
1007 DCHECK(!Done());
1008 return current_;
1009 }
1010
1011 private:
1012 HUseListNode<T>* current_;
1013
1014 friend class HValue;
1015};
1016
David Brazdil1abb4192015-02-17 18:33:36 +00001017// This class is used by HEnvironment and HInstruction classes to record the
1018// instructions they use and pointers to the corresponding HUseListNodes kept
1019// by the used instructions.
1020template <typename T>
1021class HUserRecord : public ValueObject {
1022 public:
1023 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1024 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1025
1026 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1027 : instruction_(old_record.instruction_), use_node_(use_node) {
1028 DCHECK(instruction_ != nullptr);
1029 DCHECK(use_node_ != nullptr);
1030 DCHECK(old_record.use_node_ == nullptr);
1031 }
1032
1033 HInstruction* GetInstruction() const { return instruction_; }
1034 HUseListNode<T>* GetUseNode() const { return use_node_; }
1035
1036 private:
1037 // Instruction used by the user.
1038 HInstruction* instruction_;
1039
1040 // Corresponding entry in the use list kept by 'instruction_'.
1041 HUseListNode<T>* use_node_;
1042};
1043
Calin Juravle27df7582015-04-17 19:12:31 +01001044// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1045// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1046// flag is consider.
1047// - DependsOn suggests that there is a real dependency between side effects but it only
1048// checks DependendsOnSomething flag.
1049//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001050// Represents the side effects an instruction may have.
1051class SideEffects : public ValueObject {
1052 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001053 SideEffects() : flags_(0) {}
1054
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001055 static SideEffects None() {
1056 return SideEffects(0);
1057 }
1058
1059 static SideEffects All() {
1060 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1061 }
1062
1063 static SideEffects ChangesSomething() {
1064 return SideEffects((1 << kFlagChangesCount) - 1);
1065 }
1066
1067 static SideEffects DependsOnSomething() {
1068 int count = kFlagDependsOnCount - kFlagChangesCount;
1069 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1070 }
1071
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001072 SideEffects Union(SideEffects other) const {
1073 return SideEffects(flags_ | other.flags_);
1074 }
1075
Roland Levillain72bceff2014-09-15 18:29:00 +01001076 bool HasSideEffects() const {
1077 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1078 return (flags_ & all_bits_set) != 0;
1079 }
1080
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001081 bool HasAllSideEffects() const {
1082 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1083 return all_bits_set == (flags_ & all_bits_set);
1084 }
1085
1086 bool DependsOn(SideEffects other) const {
1087 size_t depends_flags = other.ComputeDependsFlags();
1088 return (flags_ & depends_flags) != 0;
1089 }
1090
1091 bool HasDependencies() const {
1092 int count = kFlagDependsOnCount - kFlagChangesCount;
1093 size_t all_bits_set = (1 << count) - 1;
1094 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1095 }
1096
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001097 private:
1098 static constexpr int kFlagChangesSomething = 0;
1099 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1100
1101 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1102 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1103
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001104 explicit SideEffects(size_t flags) : flags_(flags) {}
1105
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001106 size_t ComputeDependsFlags() const {
1107 return flags_ << kFlagChangesCount;
1108 }
1109
1110 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001111};
1112
David Brazdiled596192015-01-23 10:39:45 +00001113// A HEnvironment object contains the values of virtual registers at a given location.
1114class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1115 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001116 HEnvironment(ArenaAllocator* arena,
1117 size_t number_of_vregs,
1118 const DexFile& dex_file,
1119 uint32_t method_idx,
1120 uint32_t dex_pc)
1121 : vregs_(arena, number_of_vregs),
1122 locations_(arena, number_of_vregs),
1123 parent_(nullptr),
1124 dex_file_(dex_file),
1125 method_idx_(method_idx),
1126 dex_pc_(dex_pc) {
David Brazdiled596192015-01-23 10:39:45 +00001127 vregs_.SetSize(number_of_vregs);
1128 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001129 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001130 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001131
1132 locations_.SetSize(number_of_vregs);
1133 for (size_t i = 0; i < number_of_vregs; ++i) {
1134 locations_.Put(i, Location());
1135 }
1136 }
1137
1138 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
1139 parent_ = new (allocator) HEnvironment(allocator,
1140 parent->Size(),
1141 parent->GetDexFile(),
1142 parent->GetMethodIdx(),
1143 parent->GetDexPc());
1144 if (parent->GetParent() != nullptr) {
1145 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1146 }
1147 parent_->CopyFrom(parent);
David Brazdiled596192015-01-23 10:39:45 +00001148 }
1149
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001150 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1151 void CopyFrom(HEnvironment* environment);
1152
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001153 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1154 // input to the loop phi instead. This is for inserting instructions that
1155 // require an environment (like HDeoptimization) in the loop pre-header.
1156 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001157
1158 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001159 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001160 }
1161
1162 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001163 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001164 }
1165
David Brazdil1abb4192015-02-17 18:33:36 +00001166 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001167
1168 size_t Size() const { return vregs_.Size(); }
1169
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001170 HEnvironment* GetParent() const { return parent_; }
1171
1172 void SetLocationAt(size_t index, Location location) {
1173 locations_.Put(index, location);
1174 }
1175
1176 Location GetLocationAt(size_t index) const {
1177 return locations_.Get(index);
1178 }
1179
1180 uint32_t GetDexPc() const {
1181 return dex_pc_;
1182 }
1183
1184 uint32_t GetMethodIdx() const {
1185 return method_idx_;
1186 }
1187
1188 const DexFile& GetDexFile() const {
1189 return dex_file_;
1190 }
1191
David Brazdiled596192015-01-23 10:39:45 +00001192 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001193 // Record instructions' use entries of this environment for constant-time removal.
1194 // It should only be called by HInstruction when a new environment use is added.
1195 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1196 DCHECK(env_use->GetUser() == this);
1197 size_t index = env_use->GetIndex();
1198 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1199 }
David Brazdiled596192015-01-23 10:39:45 +00001200
David Brazdil1abb4192015-02-17 18:33:36 +00001201 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001202 GrowableArray<Location> locations_;
1203 HEnvironment* parent_;
1204 const DexFile& dex_file_;
1205 const uint32_t method_idx_;
1206 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001207
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001208 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001209
1210 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1211};
1212
Calin Juravleacf735c2015-02-12 15:25:22 +00001213class ReferenceTypeInfo : ValueObject {
1214 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001215 typedef Handle<mirror::Class> TypeHandle;
1216
1217 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1219 if (type_handle->IsObjectClass()) {
1220 // Override the type handle to be consistent with the case when we get to
1221 // Top but don't have the Object class available. It avoids having to guess
1222 // what value the type_handle has when it's Top.
1223 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1224 } else {
1225 return ReferenceTypeInfo(type_handle, is_exact, false);
1226 }
1227 }
1228
1229 static ReferenceTypeInfo CreateTop(bool is_exact) {
1230 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001231 }
1232
1233 bool IsExact() const { return is_exact_; }
1234 bool IsTop() const { return is_top_; }
1235
1236 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1237
Calin Juravleb1498f62015-02-16 13:13:29 +00001238 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001239 if (IsTop()) {
1240 // Top (equivalent for java.lang.Object) is supertype of anything.
1241 return true;
1242 }
1243 if (rti.IsTop()) {
1244 // If we get here `this` is not Top() so it can't be a supertype.
1245 return false;
1246 }
1247 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1248 }
1249
1250 // Returns true if the type information provide the same amount of details.
1251 // Note that it does not mean that the instructions have the same actual type
1252 // (e.g. tops are equal but they can be the result of a merge).
1253 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1254 if (IsExact() != rti.IsExact()) {
1255 return false;
1256 }
1257 if (IsTop() && rti.IsTop()) {
1258 // `Top` means java.lang.Object, so the types are equivalent.
1259 return true;
1260 }
1261 if (IsTop() || rti.IsTop()) {
1262 // If only one is top or object than they are not equivalent.
1263 // NB: We need this extra check because the type_handle of `Top` is invalid
1264 // and we cannot inspect its reference.
1265 return false;
1266 }
1267
1268 // Finally check the types.
1269 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1270 }
1271
1272 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001273 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1274 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1275 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1276
Calin Juravleacf735c2015-02-12 15:25:22 +00001277 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001278 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001279 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001280 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001281 bool is_exact_;
1282 // A true value here means that the object type should be java.lang.Object.
1283 // We don't have access to the corresponding mirror object every time so this
1284 // flag acts as a substitute. When true, the TypeHandle refers to a null
1285 // pointer and should not be used.
1286 bool is_top_;
1287};
1288
1289std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1290
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001291class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001292 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001293 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001294 : previous_(nullptr),
1295 next_(nullptr),
1296 block_(nullptr),
1297 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001298 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001299 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001300 locations_(nullptr),
1301 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001302 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001303 side_effects_(side_effects),
1304 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001305
Dave Allison20dfc792014-06-16 20:44:29 -07001306 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001307
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001308#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001309 enum InstructionKind {
1310 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1311 };
1312#undef DECLARE_KIND
1313
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001314 HInstruction* GetNext() const { return next_; }
1315 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001316
Calin Juravle77520bc2015-01-12 18:45:46 +00001317 HInstruction* GetNextDisregardingMoves() const;
1318 HInstruction* GetPreviousDisregardingMoves() const;
1319
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001320 HBasicBlock* GetBlock() const { return block_; }
1321 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001322 bool IsInBlock() const { return block_ != nullptr; }
1323 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001324 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001325
Roland Levillain6b879dd2014-09-22 17:13:44 +01001326 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001327 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001328
1329 virtual void Accept(HGraphVisitor* visitor) = 0;
1330 virtual const char* DebugName() const = 0;
1331
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001332 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001333 void SetRawInputAt(size_t index, HInstruction* input) {
1334 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1335 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001336
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001337 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001338 virtual uint32_t GetDexPc() const {
1339 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1340 " does not need an environment";
1341 UNREACHABLE();
1342 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001343 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001344 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001345 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001346
Calin Juravle10e244f2015-01-26 18:54:32 +00001347 // Does not apply for all instructions, but having this at top level greatly
1348 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001349 virtual bool CanBeNull() const {
1350 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1351 return true;
1352 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001353
Calin Juravle641547a2015-04-21 22:08:51 +01001354 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1355 UNUSED(obj);
1356 return false;
1357 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001358
Calin Juravleacf735c2015-02-12 15:25:22 +00001359 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001360 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001361 reference_type_info_ = reference_type_info;
1362 }
1363
Calin Juravle61d544b2015-02-23 16:46:57 +00001364 ReferenceTypeInfo GetReferenceTypeInfo() const {
1365 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1366 return reference_type_info_;
1367 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001368
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001369 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001370 DCHECK(user != nullptr);
1371 HUseListNode<HInstruction*>* use =
1372 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1373 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374 }
1375
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001376 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001377 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001378 HUseListNode<HEnvironment*>* env_use =
1379 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1380 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001381 }
1382
David Brazdil1abb4192015-02-17 18:33:36 +00001383 void RemoveAsUserOfInput(size_t input) {
1384 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1385 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1386 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001387
David Brazdil1abb4192015-02-17 18:33:36 +00001388 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1389 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001390
David Brazdiled596192015-01-23 10:39:45 +00001391 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1392 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001393 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001394 bool HasOnlyOneNonEnvironmentUse() const {
1395 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1396 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001397
Roland Levillain6c82d402014-10-13 16:10:27 +01001398 // Does this instruction strictly dominate `other_instruction`?
1399 // Returns false if this instruction and `other_instruction` are the same.
1400 // Aborts if this instruction and `other_instruction` are both phis.
1401 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001402
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001403 int GetId() const { return id_; }
1404 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001405
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001406 int GetSsaIndex() const { return ssa_index_; }
1407 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1408 bool HasSsaIndex() const { return ssa_index_ != -1; }
1409
1410 bool HasEnvironment() const { return environment_ != nullptr; }
1411 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001412 // Set the `environment_` field. Raw because this method does not
1413 // update the uses lists.
1414 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1415
1416 // Set the environment of this instruction, copying it from `environment`. While
1417 // copying, the uses lists are being updated.
1418 void CopyEnvironmentFrom(HEnvironment* environment) {
1419 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001420 environment_ = new (allocator) HEnvironment(
1421 allocator,
1422 environment->Size(),
1423 environment->GetDexFile(),
1424 environment->GetMethodIdx(),
1425 environment->GetDexPc());
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001426 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001427 if (environment->GetParent() != nullptr) {
1428 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1429 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001430 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001431
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001432 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1433 HBasicBlock* block) {
1434 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001435 environment_ = new (allocator) HEnvironment(
1436 allocator,
1437 environment->Size(),
1438 environment->GetDexFile(),
1439 environment->GetMethodIdx(),
1440 environment->GetDexPc());
1441 if (environment->GetParent() != nullptr) {
1442 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1443 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001444 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1445 }
1446
Nicolas Geoffray39468442014-09-02 15:17:15 +01001447 // Returns the number of entries in the environment. Typically, that is the
1448 // number of dex registers in a method. It could be more in case of inlining.
1449 size_t EnvironmentSize() const;
1450
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001451 LocationSummary* GetLocations() const { return locations_; }
1452 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001453
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001454 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001455 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001456
Alexandre Rames188d4312015-04-09 18:30:21 +01001457 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1458 // uses of this instruction by `other` are *not* updated.
1459 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1460 ReplaceWith(other);
1461 other->ReplaceInput(this, use_index);
1462 }
1463
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001464 // Move `this` instruction before `cursor`.
1465 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001466
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001467#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001468 bool Is##type() const { return (As##type() != nullptr); } \
1469 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001470 virtual H##type* As##type() { return nullptr; }
1471
1472 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1473#undef INSTRUCTION_TYPE_CHECK
1474
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475 // Returns whether the instruction can be moved within the graph.
1476 virtual bool CanBeMoved() const { return false; }
1477
1478 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001479 virtual bool InstructionTypeEquals(HInstruction* other) const {
1480 UNUSED(other);
1481 return false;
1482 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001483
1484 // Returns whether any data encoded in the two instructions is equal.
1485 // This method does not look at the inputs. Both instructions must be
1486 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001487 virtual bool InstructionDataEquals(HInstruction* other) const {
1488 UNUSED(other);
1489 return false;
1490 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491
1492 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001493 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001494 // 2) Their inputs are identical.
1495 bool Equals(HInstruction* other) const;
1496
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001497 virtual InstructionKind GetKind() const = 0;
1498
1499 virtual size_t ComputeHashCode() const {
1500 size_t result = GetKind();
1501 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1502 result = (result * 31) + InputAt(i)->GetId();
1503 }
1504 return result;
1505 }
1506
1507 SideEffects GetSideEffects() const { return side_effects_; }
1508
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001509 size_t GetLifetimePosition() const { return lifetime_position_; }
1510 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1511 LiveInterval* GetLiveInterval() const { return live_interval_; }
1512 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1513 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1514
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001515 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1516
1517 // Returns whether the code generation of the instruction will require to have access
1518 // to the current method. Such instructions are:
1519 // (1): Instructions that require an environment, as calling the runtime requires
1520 // to walk the stack and have the current method stored at a specific stack address.
1521 // (2): Object literals like classes and strings, that are loaded from the dex cache
1522 // fields of the current method.
1523 bool NeedsCurrentMethod() const {
1524 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1525 }
1526
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001527 virtual bool NeedsDexCache() const { return false; }
1528
David Brazdil1abb4192015-02-17 18:33:36 +00001529 protected:
1530 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1531 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1532
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001533 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001534 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1535
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001536 HInstruction* previous_;
1537 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001538 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001539
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001540 // An instruction gets an id when it is added to the graph.
1541 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001542 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001543 int id_;
1544
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001545 // When doing liveness analysis, instructions that have uses get an SSA index.
1546 int ssa_index_;
1547
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001548 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001549 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550
1551 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001552 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001553
Nicolas Geoffray39468442014-09-02 15:17:15 +01001554 // The environment associated with this instruction. Not null if the instruction
1555 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001556 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001557
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001558 // Set by the code generator.
1559 LocationSummary* locations_;
1560
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001561 // Set by the liveness analysis.
1562 LiveInterval* live_interval_;
1563
1564 // Set by the liveness analysis, this is the position in a linear
1565 // order of blocks where this instruction's live interval start.
1566 size_t lifetime_position_;
1567
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001568 const SideEffects side_effects_;
1569
Calin Juravleacf735c2015-02-12 15:25:22 +00001570 // TODO: for primitive types this should be marked as invalid.
1571 ReferenceTypeInfo reference_type_info_;
1572
David Brazdil1abb4192015-02-17 18:33:36 +00001573 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001574 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001575 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001576 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001577 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001578
1579 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1580};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001581std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001582
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001583class HInputIterator : public ValueObject {
1584 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001585 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001586
1587 bool Done() const { return index_ == instruction_->InputCount(); }
1588 HInstruction* Current() const { return instruction_->InputAt(index_); }
1589 void Advance() { index_++; }
1590
1591 private:
1592 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001593 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001594
1595 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1596};
1597
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001598class HInstructionIterator : public ValueObject {
1599 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001600 explicit HInstructionIterator(const HInstructionList& instructions)
1601 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001602 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001603 }
1604
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001605 bool Done() const { return instruction_ == nullptr; }
1606 HInstruction* Current() const { return instruction_; }
1607 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001608 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001609 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001610 }
1611
1612 private:
1613 HInstruction* instruction_;
1614 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001615
1616 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001617};
1618
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001619class HBackwardInstructionIterator : public ValueObject {
1620 public:
1621 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1622 : instruction_(instructions.last_instruction_) {
1623 next_ = Done() ? nullptr : instruction_->GetPrevious();
1624 }
1625
1626 bool Done() const { return instruction_ == nullptr; }
1627 HInstruction* Current() const { return instruction_; }
1628 void Advance() {
1629 instruction_ = next_;
1630 next_ = Done() ? nullptr : instruction_->GetPrevious();
1631 }
1632
1633 private:
1634 HInstruction* instruction_;
1635 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001636
1637 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001638};
1639
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001640// An embedded container with N elements of type T. Used (with partial
1641// specialization for N=0) because embedded arrays cannot have size 0.
1642template<typename T, intptr_t N>
1643class EmbeddedArray {
1644 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001645 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001646
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001647 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001648
1649 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001650 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001651 return elements_[i];
1652 }
1653
1654 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001655 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001656 return elements_[i];
1657 }
1658
1659 const T& At(intptr_t i) const {
1660 return (*this)[i];
1661 }
1662
1663 void SetAt(intptr_t i, const T& val) {
1664 (*this)[i] = val;
1665 }
1666
1667 private:
1668 T elements_[N];
1669};
1670
1671template<typename T>
1672class EmbeddedArray<T, 0> {
1673 public:
1674 intptr_t length() const { return 0; }
1675 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001676 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001677 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001678 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001679 }
1680 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001681 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001682 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001683 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001684 }
1685};
1686
1687template<intptr_t N>
1688class HTemplateInstruction: public HInstruction {
1689 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001690 HTemplateInstruction<N>(SideEffects side_effects)
1691 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001692 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001694 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001695
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001696 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001697 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1698
1699 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1700 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001701 }
1702
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001703 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001704 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001705
1706 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001707};
1708
Dave Allison20dfc792014-06-16 20:44:29 -07001709template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001710class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001711 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001712 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1713 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001714 virtual ~HExpression() {}
1715
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001716 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001717
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001718 protected:
1719 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001720};
1721
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001722// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1723// instruction that branches to the exit block.
1724class HReturnVoid : public HTemplateInstruction<0> {
1725 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001726 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001727
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001728 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001729
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001730 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001731
1732 private:
1733 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1734};
1735
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001736// Represents dex's RETURN opcodes. A HReturn is a control flow
1737// instruction that branches to the exit block.
1738class HReturn : public HTemplateInstruction<1> {
1739 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001740 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001741 SetRawInputAt(0, value);
1742 }
1743
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001744 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001745
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001746 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001747
1748 private:
1749 DISALLOW_COPY_AND_ASSIGN(HReturn);
1750};
1751
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001752// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001753// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001754// exit block.
1755class HExit : public HTemplateInstruction<0> {
1756 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001757 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001758
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001759 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001760
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001761 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001762
1763 private:
1764 DISALLOW_COPY_AND_ASSIGN(HExit);
1765};
1766
1767// Jumps from one block to another.
1768class HGoto : public HTemplateInstruction<0> {
1769 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001770 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1771
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001772 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001773
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001774 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001775 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001776 }
1777
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001778 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001779
1780 private:
1781 DISALLOW_COPY_AND_ASSIGN(HGoto);
1782};
1783
Dave Allison20dfc792014-06-16 20:44:29 -07001784
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001785// Conditional branch. A block ending with an HIf instruction must have
1786// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001787class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001788 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001789 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001790 SetRawInputAt(0, input);
1791 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001792
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001793 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001794
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001796 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001797 }
1798
1799 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001800 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801 }
1802
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001803 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001804
1805 private:
1806 DISALLOW_COPY_AND_ASSIGN(HIf);
1807};
1808
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001809// Deoptimize to interpreter, upon checking a condition.
1810class HDeoptimize : public HTemplateInstruction<1> {
1811 public:
1812 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1813 : HTemplateInstruction(SideEffects::None()),
1814 dex_pc_(dex_pc) {
1815 SetRawInputAt(0, cond);
1816 }
1817
1818 bool NeedsEnvironment() const OVERRIDE { return true; }
1819 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001820 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001821
1822 DECLARE_INSTRUCTION(Deoptimize);
1823
1824 private:
1825 uint32_t dex_pc_;
1826
1827 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1828};
1829
Roland Levillain88cb1752014-10-20 16:36:47 +01001830class HUnaryOperation : public HExpression<1> {
1831 public:
1832 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1833 : HExpression(result_type, SideEffects::None()) {
1834 SetRawInputAt(0, input);
1835 }
1836
1837 HInstruction* GetInput() const { return InputAt(0); }
1838 Primitive::Type GetResultType() const { return GetType(); }
1839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001840 bool CanBeMoved() const OVERRIDE { return true; }
1841 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001842 UNUSED(other);
1843 return true;
1844 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001845
Roland Levillain9240d6a2014-10-20 16:47:04 +01001846 // Try to statically evaluate `operation` and return a HConstant
1847 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001848 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001849 HConstant* TryStaticEvaluation() const;
1850
1851 // Apply this operation to `x`.
1852 virtual int32_t Evaluate(int32_t x) const = 0;
1853 virtual int64_t Evaluate(int64_t x) const = 0;
1854
Roland Levillain88cb1752014-10-20 16:36:47 +01001855 DECLARE_INSTRUCTION(UnaryOperation);
1856
1857 private:
1858 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1859};
1860
Dave Allison20dfc792014-06-16 20:44:29 -07001861class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001862 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001863 HBinaryOperation(Primitive::Type result_type,
1864 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001865 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001866 SetRawInputAt(0, left);
1867 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001868 }
1869
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001870 HInstruction* GetLeft() const { return InputAt(0); }
1871 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001872 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001873
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001874 virtual bool IsCommutative() const { return false; }
1875
1876 // Put constant on the right.
1877 // Returns whether order is changed.
1878 bool OrderInputsWithConstantOnTheRight() {
1879 HInstruction* left = InputAt(0);
1880 HInstruction* right = InputAt(1);
1881 if (left->IsConstant() && !right->IsConstant()) {
1882 ReplaceInput(right, 0);
1883 ReplaceInput(left, 1);
1884 return true;
1885 }
1886 return false;
1887 }
1888
1889 // Order inputs by instruction id, but favor constant on the right side.
1890 // This helps GVN for commutative ops.
1891 void OrderInputs() {
1892 DCHECK(IsCommutative());
1893 HInstruction* left = InputAt(0);
1894 HInstruction* right = InputAt(1);
1895 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1896 return;
1897 }
1898 if (OrderInputsWithConstantOnTheRight()) {
1899 return;
1900 }
1901 // Order according to instruction id.
1902 if (left->GetId() > right->GetId()) {
1903 ReplaceInput(right, 0);
1904 ReplaceInput(left, 1);
1905 }
1906 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001907
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001908 bool CanBeMoved() const OVERRIDE { return true; }
1909 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001910 UNUSED(other);
1911 return true;
1912 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001913
Roland Levillain9240d6a2014-10-20 16:47:04 +01001914 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001915 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001916 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001917 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001918
1919 // Apply this operation to `x` and `y`.
1920 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1921 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1922
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001923 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001924 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001925 HConstant* GetConstantRight() const;
1926
1927 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001928 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001929 HInstruction* GetLeastConstantLeft() const;
1930
Roland Levillainccc07a92014-09-16 14:48:16 +01001931 DECLARE_INSTRUCTION(BinaryOperation);
1932
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001933 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001934 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1935};
1936
Dave Allison20dfc792014-06-16 20:44:29 -07001937class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001938 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001939 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001940 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1941 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001942
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001943 bool NeedsMaterialization() const { return needs_materialization_; }
1944 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001945
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001946 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001947 // `instruction`, and disregard moves in between.
1948 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001949
Dave Allison20dfc792014-06-16 20:44:29 -07001950 DECLARE_INSTRUCTION(Condition);
1951
1952 virtual IfCondition GetCondition() const = 0;
1953
1954 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001955 // For register allocation purposes, returns whether this instruction needs to be
1956 // materialized (that is, not just be in the processor flags).
1957 bool needs_materialization_;
1958
Dave Allison20dfc792014-06-16 20:44:29 -07001959 DISALLOW_COPY_AND_ASSIGN(HCondition);
1960};
1961
1962// Instruction to check if two inputs are equal to each other.
1963class HEqual : public HCondition {
1964 public:
1965 HEqual(HInstruction* first, HInstruction* second)
1966 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001967
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001968 bool IsCommutative() const OVERRIDE { return true; }
1969
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001970 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001971 return x == y ? 1 : 0;
1972 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001973 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001974 return x == y ? 1 : 0;
1975 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001976
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001977 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001978
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001979 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001980 return kCondEQ;
1981 }
1982
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001983 private:
1984 DISALLOW_COPY_AND_ASSIGN(HEqual);
1985};
1986
Dave Allison20dfc792014-06-16 20:44:29 -07001987class HNotEqual : public HCondition {
1988 public:
1989 HNotEqual(HInstruction* first, HInstruction* second)
1990 : HCondition(first, second) {}
1991
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001992 bool IsCommutative() const OVERRIDE { return true; }
1993
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001994 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001995 return x != y ? 1 : 0;
1996 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001997 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001998 return x != y ? 1 : 0;
1999 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002000
Dave Allison20dfc792014-06-16 20:44:29 -07002001 DECLARE_INSTRUCTION(NotEqual);
2002
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002003 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002004 return kCondNE;
2005 }
2006
2007 private:
2008 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2009};
2010
2011class HLessThan : public HCondition {
2012 public:
2013 HLessThan(HInstruction* first, HInstruction* second)
2014 : HCondition(first, second) {}
2015
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002016 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002017 return x < y ? 1 : 0;
2018 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002019 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002020 return x < y ? 1 : 0;
2021 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002022
Dave Allison20dfc792014-06-16 20:44:29 -07002023 DECLARE_INSTRUCTION(LessThan);
2024
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002025 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002026 return kCondLT;
2027 }
2028
2029 private:
2030 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2031};
2032
2033class HLessThanOrEqual : public HCondition {
2034 public:
2035 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2036 : HCondition(first, second) {}
2037
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002038 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002039 return x <= y ? 1 : 0;
2040 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002041 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002042 return x <= y ? 1 : 0;
2043 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002044
Dave Allison20dfc792014-06-16 20:44:29 -07002045 DECLARE_INSTRUCTION(LessThanOrEqual);
2046
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002047 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002048 return kCondLE;
2049 }
2050
2051 private:
2052 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2053};
2054
2055class HGreaterThan : public HCondition {
2056 public:
2057 HGreaterThan(HInstruction* first, HInstruction* second)
2058 : HCondition(first, second) {}
2059
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002060 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002061 return x > y ? 1 : 0;
2062 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002063 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002064 return x > y ? 1 : 0;
2065 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002066
Dave Allison20dfc792014-06-16 20:44:29 -07002067 DECLARE_INSTRUCTION(GreaterThan);
2068
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002069 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002070 return kCondGT;
2071 }
2072
2073 private:
2074 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2075};
2076
2077class HGreaterThanOrEqual : public HCondition {
2078 public:
2079 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2080 : HCondition(first, second) {}
2081
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002082 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002083 return x >= y ? 1 : 0;
2084 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002085 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002086 return x >= y ? 1 : 0;
2087 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002088
Dave Allison20dfc792014-06-16 20:44:29 -07002089 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2090
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002091 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002092 return kCondGE;
2093 }
2094
2095 private:
2096 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2097};
2098
2099
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002100// Instruction to check how two inputs compare to each other.
2101// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2102class HCompare : public HBinaryOperation {
2103 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002104 // The bias applies for floating point operations and indicates how NaN
2105 // comparisons are treated:
2106 enum Bias {
2107 kNoBias, // bias is not applicable (i.e. for long operation)
2108 kGtBias, // return 1 for NaN comparisons
2109 kLtBias, // return -1 for NaN comparisons
2110 };
2111
Roland Levillaina1935c42015-06-26 16:12:18 +01002112 HCompare(Primitive::Type type,
2113 HInstruction* first,
2114 HInstruction* second,
2115 Bias bias,
2116 uint32_t dex_pc)
2117 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002118 DCHECK_EQ(type, first->GetType());
2119 DCHECK_EQ(type, second->GetType());
2120 }
2121
Calin Juravleddb7df22014-11-25 20:56:51 +00002122 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002123 return
2124 x == y ? 0 :
2125 x > y ? 1 :
2126 -1;
2127 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002128
2129 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002130 return
2131 x == y ? 0 :
2132 x > y ? 1 :
2133 -1;
2134 }
2135
Calin Juravleddb7df22014-11-25 20:56:51 +00002136 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2137 return bias_ == other->AsCompare()->bias_;
2138 }
2139
2140 bool IsGtBias() { return bias_ == kGtBias; }
2141
Roland Levillaina1935c42015-06-26 16:12:18 +01002142 uint32_t GetDexPc() const { return dex_pc_; }
2143
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002144 DECLARE_INSTRUCTION(Compare);
2145
2146 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002147 const Bias bias_;
Roland Levillaina1935c42015-06-26 16:12:18 +01002148 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002149
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002150 DISALLOW_COPY_AND_ASSIGN(HCompare);
2151};
2152
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002153// A local in the graph. Corresponds to a Dex register.
2154class HLocal : public HTemplateInstruction<0> {
2155 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002156 explicit HLocal(uint16_t reg_number)
2157 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002158
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002159 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002161 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002162
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002163 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002164 // The Dex register number.
2165 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002166
2167 DISALLOW_COPY_AND_ASSIGN(HLocal);
2168};
2169
2170// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002171class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002172 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002173 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002174 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002175 SetRawInputAt(0, local);
2176 }
2177
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002178 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2179
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002180 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002181
2182 private:
2183 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2184};
2185
2186// Store a value in a given local. This instruction has two inputs: the value
2187// and the local.
2188class HStoreLocal : public HTemplateInstruction<2> {
2189 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002190 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002191 SetRawInputAt(0, local);
2192 SetRawInputAt(1, value);
2193 }
2194
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002195 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2196
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002197 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002198
2199 private:
2200 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2201};
2202
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002203class HConstant : public HExpression<0> {
2204 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002205 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2206
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002207 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002208
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002209 virtual bool IsMinusOne() const { return false; }
2210 virtual bool IsZero() const { return false; }
2211 virtual bool IsOne() const { return false; }
2212
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002213 DECLARE_INSTRUCTION(Constant);
2214
2215 private:
2216 DISALLOW_COPY_AND_ASSIGN(HConstant);
2217};
2218
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002219class HFloatConstant : public HConstant {
2220 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002221 float GetValue() const { return value_; }
2222
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002223 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002224 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2225 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002226 }
2227
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002228 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002229
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002230 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002231 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2232 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002233 }
2234 bool IsZero() const OVERRIDE {
2235 return AsFloatConstant()->GetValue() == 0.0f;
2236 }
2237 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002238 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2239 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002240 }
2241
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002242 DECLARE_INSTRUCTION(FloatConstant);
2243
2244 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002245 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002246 explicit HFloatConstant(int32_t value)
2247 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002248
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002249 const float value_;
2250
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002251 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002252 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002253 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002254 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2255};
2256
2257class HDoubleConstant : public HConstant {
2258 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002259 double GetValue() const { return value_; }
2260
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002261 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002262 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2263 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002264 }
2265
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002266 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002267
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002268 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002269 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2270 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002271 }
2272 bool IsZero() const OVERRIDE {
2273 return AsDoubleConstant()->GetValue() == 0.0;
2274 }
2275 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002276 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2277 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002278 }
2279
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002280 DECLARE_INSTRUCTION(DoubleConstant);
2281
2282 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002283 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002284 explicit HDoubleConstant(int64_t value)
2285 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002286
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002287 const double value_;
2288
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002289 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002290 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002291 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002292 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2293};
2294
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002295class HNullConstant : public HConstant {
2296 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002297 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2298 return true;
2299 }
2300
2301 size_t ComputeHashCode() const OVERRIDE { return 0; }
2302
2303 DECLARE_INSTRUCTION(NullConstant);
2304
2305 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002306 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2307
2308 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002309 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2310};
2311
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002312// Constants of the type int. Those can be from Dex instructions, or
2313// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002314class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002315 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002316 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002317
Calin Juravle61d544b2015-02-23 16:46:57 +00002318 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002319 return other->AsIntConstant()->value_ == value_;
2320 }
2321
Calin Juravle61d544b2015-02-23 16:46:57 +00002322 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2323
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002324 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2325 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2326 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2327
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002328 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002329
2330 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002331 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2332
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002333 const int32_t value_;
2334
David Brazdil8d5b8b22015-03-24 10:51:52 +00002335 friend class HGraph;
2336 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002337 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002338 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2339};
2340
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002341class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002342 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002343 int64_t GetValue() const { return value_; }
2344
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002345 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002346 return other->AsLongConstant()->value_ == value_;
2347 }
2348
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002349 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002350
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002351 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2352 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2353 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2354
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002355 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002356
2357 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002358 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2359
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002360 const int64_t value_;
2361
David Brazdil8d5b8b22015-03-24 10:51:52 +00002362 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002363 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2364};
2365
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002366enum class Intrinsics {
2367#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2368#include "intrinsics_list.h"
2369 kNone,
2370 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2371#undef INTRINSICS_LIST
2372#undef OPTIMIZING_INTRINSICS
2373};
2374std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2375
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002376class HInvoke : public HInstruction {
2377 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002378 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002379
2380 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2381 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002382 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002383
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002384 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002385 SetRawInputAt(index, argument);
2386 }
2387
Roland Levillain3e3d7332015-04-28 11:00:54 +01002388 // Return the number of arguments. This number can be lower than
2389 // the number of inputs returned by InputCount(), as some invoke
2390 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2391 // inputs at the end of their list of inputs.
2392 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2393
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002394 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002395
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002396 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002397
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002398 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2399
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002400 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002401 return intrinsic_;
2402 }
2403
2404 void SetIntrinsic(Intrinsics intrinsic) {
2405 intrinsic_ = intrinsic;
2406 }
2407
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002408 DECLARE_INSTRUCTION(Invoke);
2409
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002410 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002411 HInvoke(ArenaAllocator* arena,
2412 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002413 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002414 Primitive::Type return_type,
2415 uint32_t dex_pc,
2416 uint32_t dex_method_index)
2417 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002418 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002419 inputs_(arena, number_of_arguments),
2420 return_type_(return_type),
2421 dex_pc_(dex_pc),
2422 dex_method_index_(dex_method_index),
2423 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002424 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2425 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002426 }
2427
David Brazdil1abb4192015-02-17 18:33:36 +00002428 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2429 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2430 inputs_.Put(index, input);
2431 }
2432
Roland Levillain3e3d7332015-04-28 11:00:54 +01002433 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002434 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002435 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002436 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002437 const uint32_t dex_method_index_;
2438 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002439
2440 private:
2441 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2442};
2443
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002444class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002445 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002446 // Requirements of this method call regarding the class
2447 // initialization (clinit) check of its declaring class.
2448 enum class ClinitCheckRequirement {
2449 kNone, // Class already initialized.
2450 kExplicit, // Static call having explicit clinit check as last input.
2451 kImplicit, // Static call implicitly requiring a clinit check.
2452 };
2453
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002454 HInvokeStaticOrDirect(ArenaAllocator* arena,
2455 uint32_t number_of_arguments,
2456 Primitive::Type return_type,
2457 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002458 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002459 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002460 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002461 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002462 InvokeType invoke_type,
2463 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002464 : HInvoke(arena,
2465 number_of_arguments,
2466 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2467 return_type,
2468 dex_pc,
2469 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002470 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002471 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002472 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002473 clinit_check_requirement_(clinit_check_requirement),
2474 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002475
Calin Juravle641547a2015-04-21 22:08:51 +01002476 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2477 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002478 // We access the method via the dex cache so we can't do an implicit null check.
2479 // TODO: for intrinsics we can generate implicit null checks.
2480 return false;
2481 }
2482
Nicolas Geoffray79041292015-03-26 10:05:54 +00002483 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002484 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002485 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002486 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002487 bool IsStringInit() const { return string_init_offset_ != 0; }
2488 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002489
Roland Levillain4c0eb422015-04-24 16:43:49 +01002490 // Is this instruction a call to a static method?
2491 bool IsStatic() const {
2492 return GetInvokeType() == kStatic;
2493 }
2494
Roland Levillain3e3d7332015-04-28 11:00:54 +01002495 // Remove the art::HLoadClass instruction set as last input by
2496 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2497 // the initial art::HClinitCheck instruction (only relevant for
2498 // static calls with explicit clinit check).
2499 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002500 DCHECK(IsStaticWithExplicitClinitCheck());
2501 size_t last_input_index = InputCount() - 1;
2502 HInstruction* last_input = InputAt(last_input_index);
2503 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002504 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002505 RemoveAsUserOfInput(last_input_index);
2506 inputs_.DeleteAt(last_input_index);
2507 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2508 DCHECK(IsStaticWithImplicitClinitCheck());
2509 }
2510
2511 // Is this a call to a static method whose declaring class has an
2512 // explicit intialization check in the graph?
2513 bool IsStaticWithExplicitClinitCheck() const {
2514 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2515 }
2516
2517 // Is this a call to a static method whose declaring class has an
2518 // implicit intialization check requirement?
2519 bool IsStaticWithImplicitClinitCheck() const {
2520 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2521 }
2522
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002523 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002524
Roland Levillain4c0eb422015-04-24 16:43:49 +01002525 protected:
2526 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2527 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2528 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2529 HInstruction* input = input_record.GetInstruction();
2530 // `input` is the last input of a static invoke marked as having
2531 // an explicit clinit check. It must either be:
2532 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2533 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2534 DCHECK(input != nullptr);
2535 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2536 }
2537 return input_record;
2538 }
2539
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002540 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002541 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002542 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002543 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002544 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002545 // Thread entrypoint offset for string init method if this is a string init invoke.
2546 // Note that there are multiple string init methods, each having its own offset.
2547 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002548
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002549 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002550};
2551
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002552class HInvokeVirtual : public HInvoke {
2553 public:
2554 HInvokeVirtual(ArenaAllocator* arena,
2555 uint32_t number_of_arguments,
2556 Primitive::Type return_type,
2557 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002558 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002559 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002560 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002561 vtable_index_(vtable_index) {}
2562
Calin Juravle641547a2015-04-21 22:08:51 +01002563 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002564 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002565 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002566 }
2567
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002568 uint32_t GetVTableIndex() const { return vtable_index_; }
2569
2570 DECLARE_INSTRUCTION(InvokeVirtual);
2571
2572 private:
2573 const uint32_t vtable_index_;
2574
2575 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2576};
2577
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002578class HInvokeInterface : public HInvoke {
2579 public:
2580 HInvokeInterface(ArenaAllocator* arena,
2581 uint32_t number_of_arguments,
2582 Primitive::Type return_type,
2583 uint32_t dex_pc,
2584 uint32_t dex_method_index,
2585 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002586 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002587 imt_index_(imt_index) {}
2588
Calin Juravle641547a2015-04-21 22:08:51 +01002589 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002590 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002591 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002592 }
2593
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002594 uint32_t GetImtIndex() const { return imt_index_; }
2595 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2596
2597 DECLARE_INSTRUCTION(InvokeInterface);
2598
2599 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002600 const uint32_t imt_index_;
2601
2602 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2603};
2604
Dave Allison20dfc792014-06-16 20:44:29 -07002605class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002606 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002607 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002608 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2609 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002610 type_index_(type_index),
2611 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002612
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002613 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002614 uint16_t GetTypeIndex() const { return type_index_; }
2615
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002616 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002617 bool NeedsEnvironment() const OVERRIDE { return true; }
2618 // It may throw when called on:
2619 // - interfaces
2620 // - abstract/innaccessible/unknown classes
2621 // TODO: optimize when possible.
2622 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002623
Calin Juravle10e244f2015-01-26 18:54:32 +00002624 bool CanBeNull() const OVERRIDE { return false; }
2625
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002626 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2627
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002628 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002629
2630 private:
2631 const uint32_t dex_pc_;
2632 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002633 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002634
2635 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2636};
2637
Roland Levillain88cb1752014-10-20 16:36:47 +01002638class HNeg : public HUnaryOperation {
2639 public:
2640 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2641 : HUnaryOperation(result_type, input) {}
2642
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002643 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2644 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002645
Roland Levillain88cb1752014-10-20 16:36:47 +01002646 DECLARE_INSTRUCTION(Neg);
2647
2648 private:
2649 DISALLOW_COPY_AND_ASSIGN(HNeg);
2650};
2651
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002652class HNewArray : public HExpression<1> {
2653 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002654 HNewArray(HInstruction* length,
2655 uint32_t dex_pc,
2656 uint16_t type_index,
2657 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002658 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2659 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002660 type_index_(type_index),
2661 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002662 SetRawInputAt(0, length);
2663 }
2664
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002665 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002666 uint16_t GetTypeIndex() const { return type_index_; }
2667
2668 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002669 bool NeedsEnvironment() const OVERRIDE { return true; }
2670
Mingyao Yang0c365e62015-03-31 15:09:29 -07002671 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2672 bool CanThrow() const OVERRIDE { return true; }
2673
Calin Juravle10e244f2015-01-26 18:54:32 +00002674 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002675
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002676 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2677
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002678 DECLARE_INSTRUCTION(NewArray);
2679
2680 private:
2681 const uint32_t dex_pc_;
2682 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002683 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002684
2685 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2686};
2687
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002688class HAdd : public HBinaryOperation {
2689 public:
2690 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2691 : HBinaryOperation(result_type, left, right) {}
2692
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002693 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002694
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002695 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002696 return x + y;
2697 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002698 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002699 return x + y;
2700 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002701
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002702 DECLARE_INSTRUCTION(Add);
2703
2704 private:
2705 DISALLOW_COPY_AND_ASSIGN(HAdd);
2706};
2707
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002708class HSub : public HBinaryOperation {
2709 public:
2710 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2711 : HBinaryOperation(result_type, left, right) {}
2712
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002713 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002714 return x - y;
2715 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002716 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002717 return x - y;
2718 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002719
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002720 DECLARE_INSTRUCTION(Sub);
2721
2722 private:
2723 DISALLOW_COPY_AND_ASSIGN(HSub);
2724};
2725
Calin Juravle34bacdf2014-10-07 20:23:36 +01002726class HMul : public HBinaryOperation {
2727 public:
2728 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2729 : HBinaryOperation(result_type, left, right) {}
2730
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002731 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002732
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002733 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2734 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002735
2736 DECLARE_INSTRUCTION(Mul);
2737
2738 private:
2739 DISALLOW_COPY_AND_ASSIGN(HMul);
2740};
2741
Calin Juravle7c4954d2014-10-28 16:57:40 +00002742class HDiv : public HBinaryOperation {
2743 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002744 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2745 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002746
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002747 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002748 // Our graph structure ensures we never have 0 for `y` during constant folding.
2749 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002750 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002751 return (y == -1) ? -x : x / y;
2752 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002753
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002754 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002755 DCHECK_NE(y, 0);
2756 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2757 return (y == -1) ? -x : x / y;
2758 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002759
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002760 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002761
Calin Juravle7c4954d2014-10-28 16:57:40 +00002762 DECLARE_INSTRUCTION(Div);
2763
2764 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002765 const uint32_t dex_pc_;
2766
Calin Juravle7c4954d2014-10-28 16:57:40 +00002767 DISALLOW_COPY_AND_ASSIGN(HDiv);
2768};
2769
Calin Juravlebacfec32014-11-14 15:54:36 +00002770class HRem : public HBinaryOperation {
2771 public:
2772 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2773 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2774
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002775 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002776 DCHECK_NE(y, 0);
2777 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2778 return (y == -1) ? 0 : x % y;
2779 }
2780
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002781 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002782 DCHECK_NE(y, 0);
2783 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2784 return (y == -1) ? 0 : x % y;
2785 }
2786
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002787 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002788
2789 DECLARE_INSTRUCTION(Rem);
2790
2791 private:
2792 const uint32_t dex_pc_;
2793
2794 DISALLOW_COPY_AND_ASSIGN(HRem);
2795};
2796
Calin Juravled0d48522014-11-04 16:40:20 +00002797class HDivZeroCheck : public HExpression<1> {
2798 public:
2799 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2800 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2801 SetRawInputAt(0, value);
2802 }
2803
2804 bool CanBeMoved() const OVERRIDE { return true; }
2805
2806 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2807 UNUSED(other);
2808 return true;
2809 }
2810
2811 bool NeedsEnvironment() const OVERRIDE { return true; }
2812 bool CanThrow() const OVERRIDE { return true; }
2813
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002814 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002815
2816 DECLARE_INSTRUCTION(DivZeroCheck);
2817
2818 private:
2819 const uint32_t dex_pc_;
2820
2821 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2822};
2823
Calin Juravle9aec02f2014-11-18 23:06:35 +00002824class HShl : public HBinaryOperation {
2825 public:
2826 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2827 : HBinaryOperation(result_type, left, right) {}
2828
2829 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2830 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2831
2832 DECLARE_INSTRUCTION(Shl);
2833
2834 private:
2835 DISALLOW_COPY_AND_ASSIGN(HShl);
2836};
2837
2838class HShr : public HBinaryOperation {
2839 public:
2840 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2841 : HBinaryOperation(result_type, left, right) {}
2842
2843 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2844 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2845
2846 DECLARE_INSTRUCTION(Shr);
2847
2848 private:
2849 DISALLOW_COPY_AND_ASSIGN(HShr);
2850};
2851
2852class HUShr : public HBinaryOperation {
2853 public:
2854 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2855 : HBinaryOperation(result_type, left, right) {}
2856
2857 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2858 uint32_t ux = static_cast<uint32_t>(x);
2859 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2860 return static_cast<int32_t>(ux >> uy);
2861 }
2862
2863 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2864 uint64_t ux = static_cast<uint64_t>(x);
2865 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2866 return static_cast<int64_t>(ux >> uy);
2867 }
2868
2869 DECLARE_INSTRUCTION(UShr);
2870
2871 private:
2872 DISALLOW_COPY_AND_ASSIGN(HUShr);
2873};
2874
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002875class HAnd : public HBinaryOperation {
2876 public:
2877 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2878 : HBinaryOperation(result_type, left, right) {}
2879
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002880 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002881
2882 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2883 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2884
2885 DECLARE_INSTRUCTION(And);
2886
2887 private:
2888 DISALLOW_COPY_AND_ASSIGN(HAnd);
2889};
2890
2891class HOr : public HBinaryOperation {
2892 public:
2893 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2894 : HBinaryOperation(result_type, left, right) {}
2895
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002896 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002897
2898 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2899 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2900
2901 DECLARE_INSTRUCTION(Or);
2902
2903 private:
2904 DISALLOW_COPY_AND_ASSIGN(HOr);
2905};
2906
2907class HXor : public HBinaryOperation {
2908 public:
2909 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2910 : HBinaryOperation(result_type, left, right) {}
2911
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002912 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002913
2914 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2915 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2916
2917 DECLARE_INSTRUCTION(Xor);
2918
2919 private:
2920 DISALLOW_COPY_AND_ASSIGN(HXor);
2921};
2922
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002923// The value of a parameter in this method. Its location depends on
2924// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002925class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002926 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002927 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2928 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002929
2930 uint8_t GetIndex() const { return index_; }
2931
Calin Juravle10e244f2015-01-26 18:54:32 +00002932 bool CanBeNull() const OVERRIDE { return !is_this_; }
2933
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002934 DECLARE_INSTRUCTION(ParameterValue);
2935
2936 private:
2937 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002938 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002939 const uint8_t index_;
2940
Calin Juravle10e244f2015-01-26 18:54:32 +00002941 // Whether or not the parameter value corresponds to 'this' argument.
2942 const bool is_this_;
2943
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002944 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2945};
2946
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002947class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002948 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002949 explicit HNot(Primitive::Type result_type, HInstruction* input)
2950 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002951
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002952 bool CanBeMoved() const OVERRIDE { return true; }
2953 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002954 UNUSED(other);
2955 return true;
2956 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002957
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002958 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2959 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002960
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002961 DECLARE_INSTRUCTION(Not);
2962
2963 private:
2964 DISALLOW_COPY_AND_ASSIGN(HNot);
2965};
2966
David Brazdil66d126e2015-04-03 16:02:44 +01002967class HBooleanNot : public HUnaryOperation {
2968 public:
2969 explicit HBooleanNot(HInstruction* input)
2970 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2971
2972 bool CanBeMoved() const OVERRIDE { return true; }
2973 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2974 UNUSED(other);
2975 return true;
2976 }
2977
2978 int32_t Evaluate(int32_t x) const OVERRIDE {
2979 DCHECK(IsUint<1>(x));
2980 return !x;
2981 }
2982
2983 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2984 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2985 UNREACHABLE();
2986 }
2987
2988 DECLARE_INSTRUCTION(BooleanNot);
2989
2990 private:
2991 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2992};
2993
Roland Levillaindff1f282014-11-05 14:15:05 +00002994class HTypeConversion : public HExpression<1> {
2995 public:
2996 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002997 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2998 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002999 SetRawInputAt(0, input);
3000 DCHECK_NE(input->GetType(), result_type);
3001 }
3002
3003 HInstruction* GetInput() const { return InputAt(0); }
3004 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3005 Primitive::Type GetResultType() const { return GetType(); }
3006
Roland Levillain624279f2014-12-04 11:54:28 +00003007 // Required by the x86 and ARM code generators when producing calls
3008 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003009 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003010
Roland Levillaindff1f282014-11-05 14:15:05 +00003011 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003012 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003013
3014 DECLARE_INSTRUCTION(TypeConversion);
3015
3016 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003017 const uint32_t dex_pc_;
3018
Roland Levillaindff1f282014-11-05 14:15:05 +00003019 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3020};
3021
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003022static constexpr uint32_t kNoRegNumber = -1;
3023
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003024class HPhi : public HInstruction {
3025 public:
3026 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003027 : HInstruction(SideEffects::None()),
3028 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003029 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003030 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003031 is_live_(false),
3032 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003033 inputs_.SetSize(number_of_inputs);
3034 }
3035
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003036 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3037 static Primitive::Type ToPhiType(Primitive::Type type) {
3038 switch (type) {
3039 case Primitive::kPrimBoolean:
3040 case Primitive::kPrimByte:
3041 case Primitive::kPrimShort:
3042 case Primitive::kPrimChar:
3043 return Primitive::kPrimInt;
3044 default:
3045 return type;
3046 }
3047 }
3048
Calin Juravle10e244f2015-01-26 18:54:32 +00003049 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003050
3051 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003052 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003053
Calin Juravle10e244f2015-01-26 18:54:32 +00003054 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003055 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003056
Calin Juravle10e244f2015-01-26 18:54:32 +00003057 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3058 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3059
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003060 uint32_t GetRegNumber() const { return reg_number_; }
3061
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003062 void SetDead() { is_live_ = false; }
3063 void SetLive() { is_live_ = true; }
3064 bool IsDead() const { return !is_live_; }
3065 bool IsLive() const { return is_live_; }
3066
Calin Juravlea4f88312015-04-16 12:57:19 +01003067 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3068 // An equivalent phi is a phi having the same dex register and type.
3069 // It assumes that phis with the same dex register are adjacent.
3070 HPhi* GetNextEquivalentPhiWithSameType() {
3071 HInstruction* next = GetNext();
3072 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3073 if (next->GetType() == GetType()) {
3074 return next->AsPhi();
3075 }
3076 next = next->GetNext();
3077 }
3078 return nullptr;
3079 }
3080
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003081 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003082
David Brazdil1abb4192015-02-17 18:33:36 +00003083 protected:
3084 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3085
3086 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3087 inputs_.Put(index, input);
3088 }
3089
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003090 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003091 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003092 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003093 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003094 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003095 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003096
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003097 DISALLOW_COPY_AND_ASSIGN(HPhi);
3098};
3099
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003100class HNullCheck : public HExpression<1> {
3101 public:
3102 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003103 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003104 SetRawInputAt(0, value);
3105 }
3106
Calin Juravle10e244f2015-01-26 18:54:32 +00003107 bool CanBeMoved() const OVERRIDE { return true; }
3108 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003109 UNUSED(other);
3110 return true;
3111 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003112
Calin Juravle10e244f2015-01-26 18:54:32 +00003113 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003114
Calin Juravle10e244f2015-01-26 18:54:32 +00003115 bool CanThrow() const OVERRIDE { return true; }
3116
3117 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003118
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003119 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003120
3121 DECLARE_INSTRUCTION(NullCheck);
3122
3123 private:
3124 const uint32_t dex_pc_;
3125
3126 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3127};
3128
3129class FieldInfo : public ValueObject {
3130 public:
Calin Juravle52c48962014-12-16 17:02:57 +00003131 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3132 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003133
3134 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003135 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003136 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003137
3138 private:
3139 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003140 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003141 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003142};
3143
3144class HInstanceFieldGet : public HExpression<1> {
3145 public:
3146 HInstanceFieldGet(HInstruction* value,
3147 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003148 MemberOffset field_offset,
3149 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003150 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003151 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003152 SetRawInputAt(0, value);
3153 }
3154
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003155 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003156
3157 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3158 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3159 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003160 }
3161
Calin Juravle641547a2015-04-21 22:08:51 +01003162 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3163 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003164 }
3165
3166 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003167 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3168 }
3169
Calin Juravle52c48962014-12-16 17:02:57 +00003170 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003171 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003172 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003173 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003174
3175 DECLARE_INSTRUCTION(InstanceFieldGet);
3176
3177 private:
3178 const FieldInfo field_info_;
3179
3180 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3181};
3182
3183class HInstanceFieldSet : public HTemplateInstruction<2> {
3184 public:
3185 HInstanceFieldSet(HInstruction* object,
3186 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003187 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003188 MemberOffset field_offset,
3189 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003190 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003191 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003192 SetRawInputAt(0, object);
3193 SetRawInputAt(1, value);
3194 }
3195
Calin Juravle641547a2015-04-21 22:08:51 +01003196 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3197 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003198 }
3199
Calin Juravle52c48962014-12-16 17:02:57 +00003200 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003201 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003202 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003203 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003204 HInstruction* GetValue() const { return InputAt(1); }
3205
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003206 DECLARE_INSTRUCTION(InstanceFieldSet);
3207
3208 private:
3209 const FieldInfo field_info_;
3210
3211 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3212};
3213
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003214class HArrayGet : public HExpression<2> {
3215 public:
3216 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003217 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003218 SetRawInputAt(0, array);
3219 SetRawInputAt(1, index);
3220 }
3221
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003222 bool CanBeMoved() const OVERRIDE { return true; }
3223 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003224 UNUSED(other);
3225 return true;
3226 }
Calin Juravle641547a2015-04-21 22:08:51 +01003227 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3228 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003229 // TODO: We can be smarter here.
3230 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3231 // which generates the implicit null check. There are cases when these can be removed
3232 // to produce better code. If we ever add optimizations to do so we should allow an
3233 // implicit check here (as long as the address falls in the first page).
3234 return false;
3235 }
3236
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003237 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003238
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003239 HInstruction* GetArray() const { return InputAt(0); }
3240 HInstruction* GetIndex() const { return InputAt(1); }
3241
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003242 DECLARE_INSTRUCTION(ArrayGet);
3243
3244 private:
3245 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3246};
3247
3248class HArraySet : public HTemplateInstruction<3> {
3249 public:
3250 HArraySet(HInstruction* array,
3251 HInstruction* index,
3252 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003253 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003254 uint32_t dex_pc)
3255 : HTemplateInstruction(SideEffects::ChangesSomething()),
3256 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003257 expected_component_type_(expected_component_type),
3258 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003259 SetRawInputAt(0, array);
3260 SetRawInputAt(1, index);
3261 SetRawInputAt(2, value);
3262 }
3263
Calin Juravle77520bc2015-01-12 18:45:46 +00003264 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003265 // We currently always call a runtime method to catch array store
3266 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003267 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003268 }
3269
Calin Juravle641547a2015-04-21 22:08:51 +01003270 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3271 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003272 // TODO: Same as for ArrayGet.
3273 return false;
3274 }
3275
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003276 void ClearNeedsTypeCheck() {
3277 needs_type_check_ = false;
3278 }
3279
3280 bool NeedsTypeCheck() const { return needs_type_check_; }
3281
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003282 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003283
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003284 HInstruction* GetArray() const { return InputAt(0); }
3285 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003286 HInstruction* GetValue() const { return InputAt(2); }
3287
3288 Primitive::Type GetComponentType() const {
3289 // The Dex format does not type floating point index operations. Since the
3290 // `expected_component_type_` is set during building and can therefore not
3291 // be correct, we also check what is the value type. If it is a floating
3292 // point type, we must use that type.
3293 Primitive::Type value_type = GetValue()->GetType();
3294 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3295 ? value_type
3296 : expected_component_type_;
3297 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003298
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003299 DECLARE_INSTRUCTION(ArraySet);
3300
3301 private:
3302 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003303 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003304 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003305
3306 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3307};
3308
3309class HArrayLength : public HExpression<1> {
3310 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003311 explicit HArrayLength(HInstruction* array)
3312 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3313 // Note that arrays do not change length, so the instruction does not
3314 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003315 SetRawInputAt(0, array);
3316 }
3317
Calin Juravle77520bc2015-01-12 18:45:46 +00003318 bool CanBeMoved() const OVERRIDE { return true; }
3319 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003320 UNUSED(other);
3321 return true;
3322 }
Calin Juravle641547a2015-04-21 22:08:51 +01003323 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3324 return obj == InputAt(0);
3325 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003326
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003327 DECLARE_INSTRUCTION(ArrayLength);
3328
3329 private:
3330 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3331};
3332
3333class HBoundsCheck : public HExpression<2> {
3334 public:
3335 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003336 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003337 DCHECK(index->GetType() == Primitive::kPrimInt);
3338 SetRawInputAt(0, index);
3339 SetRawInputAt(1, length);
3340 }
3341
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003342 bool CanBeMoved() const OVERRIDE { return true; }
3343 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003344 UNUSED(other);
3345 return true;
3346 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003347
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003348 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003350 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003351
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003352 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003353
3354 DECLARE_INSTRUCTION(BoundsCheck);
3355
3356 private:
3357 const uint32_t dex_pc_;
3358
3359 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3360};
3361
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003362/**
3363 * Some DEX instructions are folded into multiple HInstructions that need
3364 * to stay live until the last HInstruction. This class
3365 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003366 * HInstruction stays live. `index` represents the stack location index of the
3367 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003368 */
3369class HTemporary : public HTemplateInstruction<0> {
3370 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003371 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003372
3373 size_t GetIndex() const { return index_; }
3374
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003375 Primitive::Type GetType() const OVERRIDE {
3376 // The previous instruction is the one that will be stored in the temporary location.
3377 DCHECK(GetPrevious() != nullptr);
3378 return GetPrevious()->GetType();
3379 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003380
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003381 DECLARE_INSTRUCTION(Temporary);
3382
3383 private:
3384 const size_t index_;
3385
3386 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3387};
3388
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003389class HSuspendCheck : public HTemplateInstruction<0> {
3390 public:
3391 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003392 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003393
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003394 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003395 return true;
3396 }
3397
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003398 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003399 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3400 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003401
3402 DECLARE_INSTRUCTION(SuspendCheck);
3403
3404 private:
3405 const uint32_t dex_pc_;
3406
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003407 // Only used for code generation, in order to share the same slow path between back edges
3408 // of a same loop.
3409 SlowPathCode* slow_path_;
3410
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003411 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3412};
3413
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003414/**
3415 * Instruction to load a Class object.
3416 */
3417class HLoadClass : public HExpression<0> {
3418 public:
3419 HLoadClass(uint16_t type_index,
3420 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003421 uint32_t dex_pc)
3422 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3423 type_index_(type_index),
3424 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003425 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003426 generate_clinit_check_(false),
3427 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003428
3429 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003430
3431 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3432 return other->AsLoadClass()->type_index_ == type_index_;
3433 }
3434
3435 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3436
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003437 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003438 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003439 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray3abd4372015-07-02 15:48:27 +01003440 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003441
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003442 bool NeedsEnvironment() const OVERRIDE {
3443 // Will call runtime and load the class if the class is not loaded yet.
3444 // TODO: finer grain decision.
3445 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003446 }
3447
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003448 bool MustGenerateClinitCheck() const {
3449 return generate_clinit_check_;
3450 }
3451
3452 void SetMustGenerateClinitCheck() {
3453 generate_clinit_check_ = true;
3454 }
3455
3456 bool CanCallRuntime() const {
3457 return MustGenerateClinitCheck() || !is_referrers_class_;
3458 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003459
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003460 bool CanThrow() const OVERRIDE {
3461 // May call runtime and and therefore can throw.
3462 // TODO: finer grain decision.
3463 return !is_referrers_class_;
3464 }
3465
Calin Juravleacf735c2015-02-12 15:25:22 +00003466 ReferenceTypeInfo GetLoadedClassRTI() {
3467 return loaded_class_rti_;
3468 }
3469
3470 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3471 // Make sure we only set exact types (the loaded class should never be merged).
3472 DCHECK(rti.IsExact());
3473 loaded_class_rti_ = rti;
3474 }
3475
3476 bool IsResolved() {
3477 return loaded_class_rti_.IsExact();
3478 }
3479
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003480 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3481
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003482 DECLARE_INSTRUCTION(LoadClass);
3483
3484 private:
3485 const uint16_t type_index_;
3486 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003487 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003488 // Whether this instruction must generate the initialization check.
3489 // Used for code generation.
3490 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003491
Calin Juravleacf735c2015-02-12 15:25:22 +00003492 ReferenceTypeInfo loaded_class_rti_;
3493
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003494 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3495};
3496
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003497class HLoadString : public HExpression<0> {
3498 public:
3499 HLoadString(uint32_t string_index, uint32_t dex_pc)
3500 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3501 string_index_(string_index),
3502 dex_pc_(dex_pc) {}
3503
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003504 bool CanBeMoved() const OVERRIDE { return true; }
3505
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003506 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3507 return other->AsLoadString()->string_index_ == string_index_;
3508 }
3509
3510 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3511
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003512 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003513 uint32_t GetStringIndex() const { return string_index_; }
3514
3515 // TODO: Can we deopt or debug when we resolve a string?
3516 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003517 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003518
3519 DECLARE_INSTRUCTION(LoadString);
3520
3521 private:
3522 const uint32_t string_index_;
3523 const uint32_t dex_pc_;
3524
3525 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3526};
3527
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003528/**
3529 * Performs an initialization check on its Class object input.
3530 */
3531class HClinitCheck : public HExpression<1> {
3532 public:
3533 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003534 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003535 dex_pc_(dex_pc) {
3536 SetRawInputAt(0, constant);
3537 }
3538
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003539 bool CanBeMoved() const OVERRIDE { return true; }
3540 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3541 UNUSED(other);
3542 return true;
3543 }
3544
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003545 bool NeedsEnvironment() const OVERRIDE {
3546 // May call runtime to initialize the class.
3547 return true;
3548 }
3549
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003550 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003551
3552 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3553
3554 DECLARE_INSTRUCTION(ClinitCheck);
3555
3556 private:
3557 const uint32_t dex_pc_;
3558
3559 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3560};
3561
3562class HStaticFieldGet : public HExpression<1> {
3563 public:
3564 HStaticFieldGet(HInstruction* cls,
3565 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003566 MemberOffset field_offset,
3567 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003568 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003569 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003570 SetRawInputAt(0, cls);
3571 }
3572
Calin Juravle52c48962014-12-16 17:02:57 +00003573
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003574 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003575
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003576 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003577 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3578 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003579 }
3580
3581 size_t ComputeHashCode() const OVERRIDE {
3582 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3583 }
3584
Calin Juravle52c48962014-12-16 17:02:57 +00003585 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003586 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3587 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003588 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003589
3590 DECLARE_INSTRUCTION(StaticFieldGet);
3591
3592 private:
3593 const FieldInfo field_info_;
3594
3595 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3596};
3597
3598class HStaticFieldSet : public HTemplateInstruction<2> {
3599 public:
3600 HStaticFieldSet(HInstruction* cls,
3601 HInstruction* value,
3602 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003603 MemberOffset field_offset,
3604 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003605 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003606 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003607 SetRawInputAt(0, cls);
3608 SetRawInputAt(1, value);
3609 }
3610
Calin Juravle52c48962014-12-16 17:02:57 +00003611 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003612 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3613 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003614 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003615
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003616 HInstruction* GetValue() const { return InputAt(1); }
3617
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003618 DECLARE_INSTRUCTION(StaticFieldSet);
3619
3620 private:
3621 const FieldInfo field_info_;
3622
3623 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3624};
3625
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003626// Implement the move-exception DEX instruction.
3627class HLoadException : public HExpression<0> {
3628 public:
3629 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3630
3631 DECLARE_INSTRUCTION(LoadException);
3632
3633 private:
3634 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3635};
3636
3637class HThrow : public HTemplateInstruction<1> {
3638 public:
3639 HThrow(HInstruction* exception, uint32_t dex_pc)
3640 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3641 SetRawInputAt(0, exception);
3642 }
3643
3644 bool IsControlFlow() const OVERRIDE { return true; }
3645
3646 bool NeedsEnvironment() const OVERRIDE { return true; }
3647
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003648 bool CanThrow() const OVERRIDE { return true; }
3649
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003650 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003651
3652 DECLARE_INSTRUCTION(Throw);
3653
3654 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003655 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003656
3657 DISALLOW_COPY_AND_ASSIGN(HThrow);
3658};
3659
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003660class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003661 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003662 HInstanceOf(HInstruction* object,
3663 HLoadClass* constant,
3664 bool class_is_final,
3665 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003666 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3667 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003668 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003669 dex_pc_(dex_pc) {
3670 SetRawInputAt(0, object);
3671 SetRawInputAt(1, constant);
3672 }
3673
3674 bool CanBeMoved() const OVERRIDE { return true; }
3675
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003676 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003677 return true;
3678 }
3679
3680 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003681 return false;
3682 }
3683
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003684 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003685
3686 bool IsClassFinal() const { return class_is_final_; }
3687
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003688 // Used only in code generation.
3689 bool MustDoNullCheck() const { return must_do_null_check_; }
3690 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3691
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003692 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003693
3694 private:
3695 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003696 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003697 const uint32_t dex_pc_;
3698
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003699 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3700};
3701
Calin Juravleb1498f62015-02-16 13:13:29 +00003702class HBoundType : public HExpression<1> {
3703 public:
3704 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3705 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3706 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003707 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003708 SetRawInputAt(0, input);
3709 }
3710
3711 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3712
3713 bool CanBeNull() const OVERRIDE {
3714 // `null instanceof ClassX` always return false so we can't be null.
3715 return false;
3716 }
3717
3718 DECLARE_INSTRUCTION(BoundType);
3719
3720 private:
3721 // Encodes the most upper class that this instruction can have. In other words
3722 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3723 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3724 const ReferenceTypeInfo bound_type_;
3725
3726 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3727};
3728
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003729class HCheckCast : public HTemplateInstruction<2> {
3730 public:
3731 HCheckCast(HInstruction* object,
3732 HLoadClass* constant,
3733 bool class_is_final,
3734 uint32_t dex_pc)
3735 : HTemplateInstruction(SideEffects::None()),
3736 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003737 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003738 dex_pc_(dex_pc) {
3739 SetRawInputAt(0, object);
3740 SetRawInputAt(1, constant);
3741 }
3742
3743 bool CanBeMoved() const OVERRIDE { return true; }
3744
3745 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3746 return true;
3747 }
3748
3749 bool NeedsEnvironment() const OVERRIDE {
3750 // Instruction may throw a CheckCastError.
3751 return true;
3752 }
3753
3754 bool CanThrow() const OVERRIDE { return true; }
3755
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003756 bool MustDoNullCheck() const { return must_do_null_check_; }
3757 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3758
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003759 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003760
3761 bool IsClassFinal() const { return class_is_final_; }
3762
3763 DECLARE_INSTRUCTION(CheckCast);
3764
3765 private:
3766 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003767 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003768 const uint32_t dex_pc_;
3769
3770 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003771};
3772
Calin Juravle27df7582015-04-17 19:12:31 +01003773class HMemoryBarrier : public HTemplateInstruction<0> {
3774 public:
3775 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3776 : HTemplateInstruction(SideEffects::None()),
3777 barrier_kind_(barrier_kind) {}
3778
3779 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3780
3781 DECLARE_INSTRUCTION(MemoryBarrier);
3782
3783 private:
3784 const MemBarrierKind barrier_kind_;
3785
3786 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3787};
3788
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003789class HMonitorOperation : public HTemplateInstruction<1> {
3790 public:
3791 enum OperationKind {
3792 kEnter,
3793 kExit,
3794 };
3795
3796 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3797 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3798 SetRawInputAt(0, object);
3799 }
3800
3801 // Instruction may throw a Java exception, so we need an environment.
3802 bool NeedsEnvironment() const OVERRIDE { return true; }
3803 bool CanThrow() const OVERRIDE { return true; }
3804
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003805 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003806
3807 bool IsEnter() const { return kind_ == kEnter; }
3808
3809 DECLARE_INSTRUCTION(MonitorOperation);
3810
Calin Juravle52c48962014-12-16 17:02:57 +00003811 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003812 const OperationKind kind_;
3813 const uint32_t dex_pc_;
3814
3815 private:
3816 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3817};
3818
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003819class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003820 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003821 MoveOperands(Location source,
3822 Location destination,
3823 Primitive::Type type,
3824 HInstruction* instruction)
3825 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003826
3827 Location GetSource() const { return source_; }
3828 Location GetDestination() const { return destination_; }
3829
3830 void SetSource(Location value) { source_ = value; }
3831 void SetDestination(Location value) { destination_ = value; }
3832
3833 // The parallel move resolver marks moves as "in-progress" by clearing the
3834 // destination (but not the source).
3835 Location MarkPending() {
3836 DCHECK(!IsPending());
3837 Location dest = destination_;
3838 destination_ = Location::NoLocation();
3839 return dest;
3840 }
3841
3842 void ClearPending(Location dest) {
3843 DCHECK(IsPending());
3844 destination_ = dest;
3845 }
3846
3847 bool IsPending() const {
3848 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3849 return destination_.IsInvalid() && !source_.IsInvalid();
3850 }
3851
3852 // True if this blocks a move from the given location.
3853 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003854 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003855 }
3856
3857 // A move is redundant if it's been eliminated, if its source and
3858 // destination are the same, or if its destination is unneeded.
3859 bool IsRedundant() const {
3860 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3861 }
3862
3863 // We clear both operands to indicate move that's been eliminated.
3864 void Eliminate() {
3865 source_ = destination_ = Location::NoLocation();
3866 }
3867
3868 bool IsEliminated() const {
3869 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3870 return source_.IsInvalid();
3871 }
3872
Roland Levillaina1935c42015-06-26 16:12:18 +01003873 Primitive::Type GetType() const { return type_; }
3874
Nicolas Geoffray90218252015-04-15 11:56:51 +01003875 bool Is64BitMove() const {
3876 return Primitive::Is64BitType(type_);
3877 }
3878
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003879 HInstruction* GetInstruction() const { return instruction_; }
3880
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003881 private:
3882 Location source_;
3883 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003884 // The type this move is for.
3885 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003886 // The instruction this move is assocatied with. Null when this move is
3887 // for moving an input in the expected locations of user (including a phi user).
3888 // This is only used in debug mode, to ensure we do not connect interval siblings
3889 // in the same parallel move.
3890 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003891};
3892
3893static constexpr size_t kDefaultNumberOfMoves = 4;
3894
3895class HParallelMove : public HTemplateInstruction<0> {
3896 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003897 explicit HParallelMove(ArenaAllocator* arena)
3898 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003899
Nicolas Geoffray90218252015-04-15 11:56:51 +01003900 void AddMove(Location source,
3901 Location destination,
3902 Primitive::Type type,
3903 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003904 DCHECK(source.IsValid());
3905 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003906 if (kIsDebugBuild) {
3907 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003908 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003909 if (moves_.Get(i).GetInstruction() == instruction) {
3910 // Special case the situation where the move is for the spill slot
3911 // of the instruction.
3912 if ((GetPrevious() == instruction)
3913 || ((GetPrevious() == nullptr)
3914 && instruction->IsPhi()
3915 && instruction->GetBlock() == GetBlock())) {
3916 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3917 << "Doing parallel moves for the same instruction.";
3918 } else {
3919 DCHECK(false) << "Doing parallel moves for the same instruction.";
3920 }
3921 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003922 }
3923 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003924 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003925 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3926 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003927 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003928 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003929 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003930 }
3931
3932 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003933 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003934 }
3935
3936 size_t NumMoves() const { return moves_.Size(); }
3937
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003938 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003939
3940 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003941 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003942
3943 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3944};
3945
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003946class HGraphVisitor : public ValueObject {
3947 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003948 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3949 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003950
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003951 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003952 virtual void VisitBasicBlock(HBasicBlock* block);
3953
Roland Levillain633021e2014-10-01 14:12:25 +01003954 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003955 void VisitInsertionOrder();
3956
Roland Levillain633021e2014-10-01 14:12:25 +01003957 // Visit the graph following dominator tree reverse post-order.
3958 void VisitReversePostOrder();
3959
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003960 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003961
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003962 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003963#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003964 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3965
3966 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3967
3968#undef DECLARE_VISIT_INSTRUCTION
3969
3970 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003971 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003972
3973 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3974};
3975
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003976class HGraphDelegateVisitor : public HGraphVisitor {
3977 public:
3978 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3979 virtual ~HGraphDelegateVisitor() {}
3980
3981 // Visit functions that delegate to to super class.
3982#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003983 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003984
3985 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3986
3987#undef DECLARE_VISIT_INSTRUCTION
3988
3989 private:
3990 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3991};
3992
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003993class HInsertionOrderIterator : public ValueObject {
3994 public:
3995 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3996
3997 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3998 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3999 void Advance() { ++index_; }
4000
4001 private:
4002 const HGraph& graph_;
4003 size_t index_;
4004
4005 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4006};
4007
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004008class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004009 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004010 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4011 // Check that reverse post order of the graph has been built.
4012 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4013 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004014
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004015 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4016 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004017 void Advance() { ++index_; }
4018
4019 private:
4020 const HGraph& graph_;
4021 size_t index_;
4022
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004023 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004024};
4025
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004026class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004027 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004028 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004029 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4030 // Check that reverse post order of the graph has been built.
4031 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4032 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004033
4034 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004035 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004036 void Advance() { --index_; }
4037
4038 private:
4039 const HGraph& graph_;
4040 size_t index_;
4041
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004042 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004043};
4044
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004045class HLinearPostOrderIterator : public ValueObject {
4046 public:
4047 explicit HLinearPostOrderIterator(const HGraph& graph)
4048 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4049
4050 bool Done() const { return index_ == 0; }
4051
4052 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4053
4054 void Advance() {
4055 --index_;
4056 DCHECK_GE(index_, 0U);
4057 }
4058
4059 private:
4060 const GrowableArray<HBasicBlock*>& order_;
4061 size_t index_;
4062
4063 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4064};
4065
4066class HLinearOrderIterator : public ValueObject {
4067 public:
4068 explicit HLinearOrderIterator(const HGraph& graph)
4069 : order_(graph.GetLinearOrder()), index_(0) {}
4070
4071 bool Done() const { return index_ == order_.Size(); }
4072 HBasicBlock* Current() const { return order_.Get(index_); }
4073 void Advance() { ++index_; }
4074
4075 private:
4076 const GrowableArray<HBasicBlock*>& order_;
4077 size_t index_;
4078
4079 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4080};
4081
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004082// Iterator over the blocks that art part of the loop. Includes blocks part
4083// of an inner loop. The order in which the blocks are iterated is on their
4084// block id.
4085class HBlocksInLoopIterator : public ValueObject {
4086 public:
4087 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4088 : blocks_in_loop_(info.GetBlocks()),
4089 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4090 index_(0) {
4091 if (!blocks_in_loop_.IsBitSet(index_)) {
4092 Advance();
4093 }
4094 }
4095
4096 bool Done() const { return index_ == blocks_.Size(); }
4097 HBasicBlock* Current() const { return blocks_.Get(index_); }
4098 void Advance() {
4099 ++index_;
4100 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4101 if (blocks_in_loop_.IsBitSet(index_)) {
4102 break;
4103 }
4104 }
4105 }
4106
4107 private:
4108 const BitVector& blocks_in_loop_;
4109 const GrowableArray<HBasicBlock*>& blocks_;
4110 size_t index_;
4111
4112 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4113};
4114
Mingyao Yangbca381a2015-05-19 16:01:59 -07004115// Iterator over the blocks that art part of the loop. Includes blocks part
4116// of an inner loop. The order in which the blocks are iterated is reverse
4117// post order.
4118class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4119 public:
4120 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4121 : blocks_in_loop_(info.GetBlocks()),
4122 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4123 index_(0) {
4124 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4125 Advance();
4126 }
4127 }
4128
4129 bool Done() const { return index_ == blocks_.Size(); }
4130 HBasicBlock* Current() const { return blocks_.Get(index_); }
4131 void Advance() {
4132 ++index_;
4133 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4134 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4135 break;
4136 }
4137 }
4138 }
4139
4140 private:
4141 const BitVector& blocks_in_loop_;
4142 const GrowableArray<HBasicBlock*>& blocks_;
4143 size_t index_;
4144
4145 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4146};
4147
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004148inline int64_t Int64FromConstant(HConstant* constant) {
4149 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4150 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4151 : constant->AsLongConstant()->GetValue();
4152}
4153
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004154} // namespace art
4155
4156#endif // ART_COMPILER_OPTIMIZING_NODES_H_