blob: 031761e7f7247cb830b7034ec5b6f6ed0ced0d94 [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,
123 bool debuggable = false,
124 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000125 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000126 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100127 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100128 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700129 entry_block_(nullptr),
130 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100131 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100132 number_of_vregs_(0),
133 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000134 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400135 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000136 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000137 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100138 dex_file_(dex_file),
139 method_idx_(method_idx),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000140 cached_null_constant_(nullptr),
141 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000142 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
143 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
144 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000146 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100147 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100148 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000150 HBasicBlock* GetEntryBlock() const { return entry_block_; }
151 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000152
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000153 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
154 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000155
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100157
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000158 // Try building the SSA form of this graph, with dominance computation and loop
159 // recognition. Returns whether it was successful in doing all these steps.
160 bool TryBuildingSsa() {
161 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000162 // The SSA builder requires loops to all be natural. Specifically, the dead phi
163 // elimination phase checks the consistency of the graph when doing a post-order
164 // visit for eliminating dead phis: a dead phi can only have loop header phi
165 // users remaining when being visited.
166 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000167 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000168 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000169 }
170
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000171 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000172 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000174
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000175 // Analyze all natural loops in this graph. Returns false if one
176 // loop is not natural, that is the header does not dominate the
177 // back edge.
178 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100179
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
181 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
182
David Brazdil2d7352b2015-04-20 14:52:42 +0100183 // Removes `block` from the graph.
184 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000185
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100186 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
187 void SimplifyLoop(HBasicBlock* header);
188
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000189 int32_t GetNextInstructionId() {
190 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000191 return current_instruction_id_++;
192 }
193
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000194 int32_t GetCurrentInstructionId() const {
195 return current_instruction_id_;
196 }
197
198 void SetCurrentInstructionId(int32_t id) {
199 current_instruction_id_ = id;
200 }
201
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100202 uint16_t GetMaximumNumberOfOutVRegs() const {
203 return maximum_number_of_out_vregs_;
204 }
205
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000206 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
207 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100208 }
209
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000210 void UpdateTemporariesVRegSlots(size_t slots) {
211 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100212 }
213
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000214 size_t GetTemporariesVRegSlots() const {
215 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100216 }
217
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100218 void SetNumberOfVRegs(uint16_t number_of_vregs) {
219 number_of_vregs_ = number_of_vregs;
220 }
221
222 uint16_t GetNumberOfVRegs() const {
223 return number_of_vregs_;
224 }
225
226 void SetNumberOfInVRegs(uint16_t value) {
227 number_of_in_vregs_ = value;
228 }
229
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100230 uint16_t GetNumberOfLocalVRegs() const {
231 return number_of_vregs_ - number_of_in_vregs_;
232 }
233
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100234 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
235 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100236 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100237
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100238 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
239 return linear_order_;
240 }
241
Mark Mendell1152c922015-04-24 17:06:35 -0400242 bool HasBoundsChecks() const {
243 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800244 }
245
Mark Mendell1152c922015-04-24 17:06:35 -0400246 void SetHasBoundsChecks(bool value) {
247 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800248 }
249
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000250 bool IsDebuggable() const { return debuggable_; }
251
David Brazdil8d5b8b22015-03-24 10:51:52 +0000252 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000253 // already, it is created and inserted into the graph. This method is only for
254 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000255 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000256 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000257 HIntConstant* GetIntConstant(int32_t value) {
258 return CreateConstant(value, &cached_int_constants_);
259 }
260 HLongConstant* GetLongConstant(int64_t value) {
261 return CreateConstant(value, &cached_long_constants_);
262 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000263 HFloatConstant* GetFloatConstant(float value) {
264 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
265 }
266 HDoubleConstant* GetDoubleConstant(double value) {
267 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
268 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000269
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100271
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100272 const DexFile& GetDexFile() const {
273 return dex_file_;
274 }
275
276 uint32_t GetMethodIdx() const {
277 return method_idx_;
278 }
279
David Brazdil2d7352b2015-04-20 14:52:42 +0100280 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000281 void VisitBlockForDominatorTree(HBasicBlock* block,
282 HBasicBlock* predecessor,
283 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100284 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000285 void VisitBlockForBackEdges(HBasicBlock* block,
286 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100287 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000288 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100289 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000291 template <class InstructionType, typename ValueType>
292 InstructionType* CreateConstant(ValueType value,
293 ArenaSafeMap<ValueType, InstructionType*>* cache) {
294 // Try to find an existing constant of the given value.
295 InstructionType* constant = nullptr;
296 auto cached_constant = cache->find(value);
297 if (cached_constant != cache->end()) {
298 constant = cached_constant->second;
299 }
300
301 // If not found or previously deleted, create and cache a new instruction.
302 if (constant == nullptr || constant->GetBlock() == nullptr) {
303 constant = new (arena_) InstructionType(value);
304 cache->Overwrite(value, constant);
305 InsertConstant(constant);
306 }
307 return constant;
308 }
309
David Brazdil8d5b8b22015-03-24 10:51:52 +0000310 void InsertConstant(HConstant* instruction);
311
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000312 // Cache a float constant into the graph. This method should only be
313 // called by the SsaBuilder when creating "equivalent" instructions.
314 void CacheFloatConstant(HFloatConstant* constant);
315
316 // See CacheFloatConstant comment.
317 void CacheDoubleConstant(HDoubleConstant* constant);
318
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000319 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320
321 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000322 GrowableArray<HBasicBlock*> blocks_;
323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 // List of blocks to perform a reverse post order tree traversal.
325 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100327 // List of blocks to perform a linear order tree traversal.
328 GrowableArray<HBasicBlock*> linear_order_;
329
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000330 HBasicBlock* entry_block_;
331 HBasicBlock* exit_block_;
332
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100333 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100334 uint16_t maximum_number_of_out_vregs_;
335
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100336 // The number of virtual registers in this method. Contains the parameters.
337 uint16_t number_of_vregs_;
338
339 // The number of virtual registers used by parameters of this method.
340 uint16_t number_of_in_vregs_;
341
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000342 // Number of vreg size slots that the temporaries use (used in baseline compiler).
343 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100344
Mark Mendell1152c922015-04-24 17:06:35 -0400345 // Has bounds checks. We can totally skip BCE if it's false.
346 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800347
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000348 // Indicates whether the graph should be compiled in a way that
349 // ensures full debuggability. If false, we can apply more
350 // aggressive optimizations that may limit the level of debugging.
351 const bool debuggable_;
352
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000353 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000354 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000355
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100356 // The dex file from which the method is from.
357 const DexFile& dex_file_;
358
359 // The method index in the dex file.
360 const uint32_t method_idx_;
361
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000362 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000363 HNullConstant* cached_null_constant_;
364 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000365 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000366 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000367 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000368
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000369 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100370 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000371 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372 DISALLOW_COPY_AND_ASSIGN(HGraph);
373};
374
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700375class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000376 public:
377 HLoopInformation(HBasicBlock* header, HGraph* graph)
378 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100379 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100380 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100381 // Make bit vector growable, as the number of blocks may change.
382 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100383
384 HBasicBlock* GetHeader() const {
385 return header_;
386 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000387
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000388 void SetHeader(HBasicBlock* block) {
389 header_ = block;
390 }
391
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100392 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
393 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
394 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
395
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000396 void AddBackEdge(HBasicBlock* back_edge) {
397 back_edges_.Add(back_edge);
398 }
399
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100400 void RemoveBackEdge(HBasicBlock* back_edge) {
401 back_edges_.Delete(back_edge);
402 }
403
David Brazdil46e2a392015-03-16 17:31:52 +0000404 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100405 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000406 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100407 }
408 return false;
409 }
410
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000411 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000412 return back_edges_.Size();
413 }
414
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100415 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100416
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100417 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
418 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100419 }
420
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100421 // Returns the lifetime position of the back edge that has the
422 // greatest lifetime position.
423 size_t GetLifetimeEnd() const;
424
425 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
426 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
427 if (back_edges_.Get(i) == existing) {
428 back_edges_.Put(i, new_back_edge);
429 return;
430 }
431 }
432 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100433 }
434
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100435 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100436 // that is the header dominates the back edge.
437 bool Populate();
438
439 // Returns whether this loop information contains `block`.
440 // Note that this loop information *must* be populated before entering this function.
441 bool Contains(const HBasicBlock& block) const;
442
443 // Returns whether this loop information is an inner loop of `other`.
444 // Note that `other` *must* be populated before entering this function.
445 bool IsIn(const HLoopInformation& other) const;
446
447 const ArenaBitVector& GetBlocks() const { return blocks_; }
448
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000449 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000450 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000451
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000452 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100453 // Internal recursive implementation of `Populate`.
454 void PopulateRecursive(HBasicBlock* block);
455
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000456 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100457 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000458 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100459 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000460
461 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
462};
463
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100464static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100465static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100466
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000467// A block in a method. Contains the list of instructions represented
468// as a double linked list. Each block knows its predecessors and
469// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100470
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700471class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100473 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000474 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000475 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
476 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000477 loop_information_(nullptr),
478 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100479 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100480 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100481 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100482 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000483 lifetime_end_(kNoLifetime),
484 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000485
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100486 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
487 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000488 }
489
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100490 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
491 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492 }
493
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100494 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
495 return dominated_blocks_;
496 }
497
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100498 bool IsEntryBlock() const {
499 return graph_->GetEntryBlock() == this;
500 }
501
502 bool IsExitBlock() const {
503 return graph_->GetExitBlock() == this;
504 }
505
David Brazdil46e2a392015-03-16 17:31:52 +0000506 bool IsSingleGoto() const;
507
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000508 void AddBackEdge(HBasicBlock* back_edge) {
509 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000510 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000511 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100512 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000513 loop_information_->AddBackEdge(back_edge);
514 }
515
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000516 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000517 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000518
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000519 int GetBlockId() const { return block_id_; }
520 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000521
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000522 HBasicBlock* GetDominator() const { return dominator_; }
523 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100524 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100525 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000526 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
527 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
528 if (dominated_blocks_.Get(i) == existing) {
529 dominated_blocks_.Put(i, new_block);
530 return;
531 }
532 }
533 LOG(FATAL) << "Unreachable";
534 UNREACHABLE();
535 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536
537 int NumberOfBackEdges() const {
538 return loop_information_ == nullptr
539 ? 0
540 : loop_information_->NumberOfBackEdges();
541 }
542
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100543 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
544 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100545 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100546 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100547 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
548 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000549
550 void AddSuccessor(HBasicBlock* block) {
551 successors_.Add(block);
552 block->predecessors_.Add(this);
553 }
554
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100555 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
556 size_t successor_index = GetSuccessorIndexOf(existing);
557 DCHECK_NE(successor_index, static_cast<size_t>(-1));
558 existing->RemovePredecessor(this);
559 new_block->predecessors_.Add(this);
560 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561 }
562
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000563 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
564 size_t predecessor_index = GetPredecessorIndexOf(existing);
565 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
566 existing->RemoveSuccessor(this);
567 new_block->successors_.Add(this);
568 predecessors_.Put(predecessor_index, new_block);
569 }
570
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100571 void RemovePredecessor(HBasicBlock* block) {
572 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100573 }
574
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000575 void RemoveSuccessor(HBasicBlock* block) {
576 successors_.Delete(block);
577 }
578
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100579 void ClearAllPredecessors() {
580 predecessors_.Reset();
581 }
582
583 void AddPredecessor(HBasicBlock* block) {
584 predecessors_.Add(block);
585 block->successors_.Add(this);
586 }
587
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100588 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100589 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100590 HBasicBlock* temp = predecessors_.Get(0);
591 predecessors_.Put(0, predecessors_.Get(1));
592 predecessors_.Put(1, temp);
593 }
594
David Brazdil769c9e52015-04-27 13:54:09 +0100595 void SwapSuccessors() {
596 DCHECK_EQ(successors_.Size(), 2u);
597 HBasicBlock* temp = successors_.Get(0);
598 successors_.Put(0, successors_.Get(1));
599 successors_.Put(1, temp);
600 }
601
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100602 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
603 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
604 if (predecessors_.Get(i) == predecessor) {
605 return i;
606 }
607 }
608 return -1;
609 }
610
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100611 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
612 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
613 if (successors_.Get(i) == successor) {
614 return i;
615 }
616 }
617 return -1;
618 }
619
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000620 // Split the block into two blocks just after `cursor`. Returns the newly
621 // created block. Note that this method just updates raw block information,
622 // like predecessors, successors, dominators, and instruction list. It does not
623 // update the graph, reverse post order, loop information, nor make sure the
624 // blocks are consistent (for example ending with a control flow instruction).
625 HBasicBlock* SplitAfter(HInstruction* cursor);
626
627 // Merge `other` at the end of `this`. Successors and dominated blocks of
628 // `other` are changed to be successors and dominated blocks of `this`. Note
629 // that this method does not update the graph, reverse post order, loop
630 // information, nor make sure the blocks are consistent (for example ending
631 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100632 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000633
634 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
635 // of `this` are moved to `other`.
636 // Note that this method does not update the graph, reverse post order, loop
637 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000638 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000639 void ReplaceWith(HBasicBlock* other);
640
David Brazdil2d7352b2015-04-20 14:52:42 +0100641 // Merge `other` at the end of `this`. This method updates loops, reverse post
642 // order, links to predecessors, successors, dominators and deletes the block
643 // from the graph. The two blocks must be successive, i.e. `this` the only
644 // predecessor of `other` and vice versa.
645 void MergeWith(HBasicBlock* other);
646
647 // Disconnects `this` from all its predecessors, successors and dominator,
648 // removes it from all loops it is included in and eventually from the graph.
649 // The block must not dominate any other block. Predecessors and successors
650 // are safely updated.
651 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000652
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100654 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100655 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100656 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100657 // Replace instruction `initial` with `replacement` within this block.
658 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
659 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100660 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100661 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000662 // RemoveInstruction and RemovePhi delete a given instruction from the respective
663 // instruction list. With 'ensure_safety' set to true, it verifies that the
664 // instruction is not in use and removes it from the use lists of its inputs.
665 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
666 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100667 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100668
669 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100670 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100671 }
672
Roland Levillain6b879dd2014-09-22 17:13:44 +0100673 bool IsLoopPreHeaderFirstPredecessor() const {
674 DCHECK(IsLoopHeader());
675 DCHECK(!GetPredecessors().IsEmpty());
676 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
677 }
678
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100679 HLoopInformation* GetLoopInformation() const {
680 return loop_information_;
681 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000682
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000683 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100684 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000685 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100686 void SetInLoop(HLoopInformation* info) {
687 if (IsLoopHeader()) {
688 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100689 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100690 loop_information_ = info;
691 } else if (loop_information_->Contains(*info->GetHeader())) {
692 // Block is currently part of an outer loop. Make it part of this inner loop.
693 // Note that a non loop header having a loop information means this loop information
694 // has already been populated
695 loop_information_ = info;
696 } else {
697 // Block is part of an inner loop. Do not update the loop information.
698 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
699 // at this point, because this method is being called while populating `info`.
700 }
701 }
702
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000703 // Raw update of the loop information.
704 void SetLoopInformation(HLoopInformation* info) {
705 loop_information_ = info;
706 }
707
David Brazdil69a28042015-04-29 17:16:07 +0100708 // Checks if the loop information points to a valid loop. If the loop has been
709 // dismantled (does not have a back edge any more), loop information is
710 // removed or replaced with the information of the first valid outer loop.
711 void UpdateLoopInformation();
712
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100713 bool IsInLoop() const { return loop_information_ != nullptr; }
714
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100715 // Returns wheter this block dominates the blocked passed as parameter.
716 bool Dominates(HBasicBlock* block) const;
717
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100718 size_t GetLifetimeStart() const { return lifetime_start_; }
719 size_t GetLifetimeEnd() const { return lifetime_end_; }
720
721 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
722 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
723
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100724 uint32_t GetDexPc() const { return dex_pc_; }
725
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000726 bool IsCatchBlock() const { return is_catch_block_; }
727 void SetIsCatchBlock() { is_catch_block_ = true; }
728
David Brazdil8d5b8b22015-03-24 10:51:52 +0000729 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000730 bool EndsWithIf() const;
731 bool HasSinglePhi() const;
732
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000733 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000734 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000735 GrowableArray<HBasicBlock*> predecessors_;
736 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100737 HInstructionList instructions_;
738 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000739 HLoopInformation* loop_information_;
740 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100741 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100743 // The dex program counter of the first instruction of this block.
744 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100745 size_t lifetime_start_;
746 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000747 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000748
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000749 friend class HGraph;
750 friend class HInstruction;
751
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000752 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
753};
754
David Brazdilb2bd1c52015-03-25 11:17:37 +0000755// Iterates over the LoopInformation of all loops which contain 'block'
756// from the innermost to the outermost.
757class HLoopInformationOutwardIterator : public ValueObject {
758 public:
759 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
760 : current_(block.GetLoopInformation()) {}
761
762 bool Done() const { return current_ == nullptr; }
763
764 void Advance() {
765 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100766 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000767 }
768
769 HLoopInformation* Current() const {
770 DCHECK(!Done());
771 return current_;
772 }
773
774 private:
775 HLoopInformation* current_;
776
777 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
778};
779
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100780#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
781 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000782 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000783 M(ArrayGet, Instruction) \
784 M(ArrayLength, Instruction) \
785 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100786 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000787 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000788 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000789 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100790 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000791 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100792 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700793 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000794 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000795 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000796 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100797 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000798 M(Exit, Instruction) \
799 M(FloatConstant, Constant) \
800 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100801 M(GreaterThan, Condition) \
802 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100803 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000804 M(InstanceFieldGet, Instruction) \
805 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000806 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100807 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000808 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000809 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100810 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000811 M(LessThan, Condition) \
812 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000813 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000814 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100815 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000816 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100817 M(Local, Instruction) \
818 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100819 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000820 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000821 M(Mul, BinaryOperation) \
822 M(Neg, UnaryOperation) \
823 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100824 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100825 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000826 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000827 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000828 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000829 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100830 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000831 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100832 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000833 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100834 M(Return, Instruction) \
835 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000836 M(Shl, BinaryOperation) \
837 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100838 M(StaticFieldGet, Instruction) \
839 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100840 M(StoreLocal, Instruction) \
841 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100842 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000843 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000844 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000845 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000846 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000847 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000848
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100849#define FOR_EACH_INSTRUCTION(M) \
850 FOR_EACH_CONCRETE_INSTRUCTION(M) \
851 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100852 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100853 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100854 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700855
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100856#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000857FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
858#undef FORWARD_DECLARATION
859
Roland Levillainccc07a92014-09-16 14:48:16 +0100860#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000861 InstructionKind GetKind() const OVERRIDE { return k##type; } \
862 const char* DebugName() const OVERRIDE { return #type; } \
863 const H##type* As##type() const OVERRIDE { return this; } \
864 H##type* As##type() OVERRIDE { return this; } \
865 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100866 return other->Is##type(); \
867 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000868 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869
David Brazdiled596192015-01-23 10:39:45 +0000870template <typename T> class HUseList;
871
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100872template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700873class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000874 public:
David Brazdiled596192015-01-23 10:39:45 +0000875 HUseListNode* GetPrevious() const { return prev_; }
876 HUseListNode* GetNext() const { return next_; }
877 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100879 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100880
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000881 private:
David Brazdiled596192015-01-23 10:39:45 +0000882 HUseListNode(T user, size_t index)
883 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
884
885 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100886 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000887 HUseListNode<T>* prev_;
888 HUseListNode<T>* next_;
889
890 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000891
892 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
893};
894
David Brazdiled596192015-01-23 10:39:45 +0000895template <typename T>
896class HUseList : public ValueObject {
897 public:
898 HUseList() : first_(nullptr) {}
899
900 void Clear() {
901 first_ = nullptr;
902 }
903
904 // Adds a new entry at the beginning of the use list and returns
905 // the newly created node.
906 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000907 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000908 if (IsEmpty()) {
909 first_ = new_node;
910 } else {
911 first_->prev_ = new_node;
912 new_node->next_ = first_;
913 first_ = new_node;
914 }
915 return new_node;
916 }
917
918 HUseListNode<T>* GetFirst() const {
919 return first_;
920 }
921
922 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000923 DCHECK(node != nullptr);
924 DCHECK(Contains(node));
925
David Brazdiled596192015-01-23 10:39:45 +0000926 if (node->prev_ != nullptr) {
927 node->prev_->next_ = node->next_;
928 }
929 if (node->next_ != nullptr) {
930 node->next_->prev_ = node->prev_;
931 }
932 if (node == first_) {
933 first_ = node->next_;
934 }
935 }
936
David Brazdil1abb4192015-02-17 18:33:36 +0000937 bool Contains(const HUseListNode<T>* node) const {
938 if (node == nullptr) {
939 return false;
940 }
941 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
942 if (current == node) {
943 return true;
944 }
945 }
946 return false;
947 }
948
David Brazdiled596192015-01-23 10:39:45 +0000949 bool IsEmpty() const {
950 return first_ == nullptr;
951 }
952
953 bool HasOnlyOneUse() const {
954 return first_ != nullptr && first_->next_ == nullptr;
955 }
956
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100957 size_t SizeSlow() const {
958 size_t count = 0;
959 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
960 ++count;
961 }
962 return count;
963 }
964
David Brazdiled596192015-01-23 10:39:45 +0000965 private:
966 HUseListNode<T>* first_;
967};
968
969template<typename T>
970class HUseIterator : public ValueObject {
971 public:
972 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
973
974 bool Done() const { return current_ == nullptr; }
975
976 void Advance() {
977 DCHECK(!Done());
978 current_ = current_->GetNext();
979 }
980
981 HUseListNode<T>* Current() const {
982 DCHECK(!Done());
983 return current_;
984 }
985
986 private:
987 HUseListNode<T>* current_;
988
989 friend class HValue;
990};
991
David Brazdil1abb4192015-02-17 18:33:36 +0000992// This class is used by HEnvironment and HInstruction classes to record the
993// instructions they use and pointers to the corresponding HUseListNodes kept
994// by the used instructions.
995template <typename T>
996class HUserRecord : public ValueObject {
997 public:
998 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
999 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1000
1001 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1002 : instruction_(old_record.instruction_), use_node_(use_node) {
1003 DCHECK(instruction_ != nullptr);
1004 DCHECK(use_node_ != nullptr);
1005 DCHECK(old_record.use_node_ == nullptr);
1006 }
1007
1008 HInstruction* GetInstruction() const { return instruction_; }
1009 HUseListNode<T>* GetUseNode() const { return use_node_; }
1010
1011 private:
1012 // Instruction used by the user.
1013 HInstruction* instruction_;
1014
1015 // Corresponding entry in the use list kept by 'instruction_'.
1016 HUseListNode<T>* use_node_;
1017};
1018
Calin Juravle27df7582015-04-17 19:12:31 +01001019// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1020// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1021// flag is consider.
1022// - DependsOn suggests that there is a real dependency between side effects but it only
1023// checks DependendsOnSomething flag.
1024//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001025// Represents the side effects an instruction may have.
1026class SideEffects : public ValueObject {
1027 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001028 SideEffects() : flags_(0) {}
1029
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001030 static SideEffects None() {
1031 return SideEffects(0);
1032 }
1033
1034 static SideEffects All() {
1035 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1036 }
1037
1038 static SideEffects ChangesSomething() {
1039 return SideEffects((1 << kFlagChangesCount) - 1);
1040 }
1041
1042 static SideEffects DependsOnSomething() {
1043 int count = kFlagDependsOnCount - kFlagChangesCount;
1044 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1045 }
1046
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001047 SideEffects Union(SideEffects other) const {
1048 return SideEffects(flags_ | other.flags_);
1049 }
1050
Roland Levillain72bceff2014-09-15 18:29:00 +01001051 bool HasSideEffects() const {
1052 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1053 return (flags_ & all_bits_set) != 0;
1054 }
1055
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001056 bool HasAllSideEffects() const {
1057 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1058 return all_bits_set == (flags_ & all_bits_set);
1059 }
1060
1061 bool DependsOn(SideEffects other) const {
1062 size_t depends_flags = other.ComputeDependsFlags();
1063 return (flags_ & depends_flags) != 0;
1064 }
1065
1066 bool HasDependencies() const {
1067 int count = kFlagDependsOnCount - kFlagChangesCount;
1068 size_t all_bits_set = (1 << count) - 1;
1069 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1070 }
1071
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001072 private:
1073 static constexpr int kFlagChangesSomething = 0;
1074 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1075
1076 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1077 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1078
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001079 explicit SideEffects(size_t flags) : flags_(flags) {}
1080
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001081 size_t ComputeDependsFlags() const {
1082 return flags_ << kFlagChangesCount;
1083 }
1084
1085 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001086};
1087
David Brazdiled596192015-01-23 10:39:45 +00001088// A HEnvironment object contains the values of virtual registers at a given location.
1089class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1090 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001091 HEnvironment(ArenaAllocator* arena,
1092 size_t number_of_vregs,
1093 const DexFile& dex_file,
1094 uint32_t method_idx,
1095 uint32_t dex_pc)
1096 : vregs_(arena, number_of_vregs),
1097 locations_(arena, number_of_vregs),
1098 parent_(nullptr),
1099 dex_file_(dex_file),
1100 method_idx_(method_idx),
1101 dex_pc_(dex_pc) {
David Brazdiled596192015-01-23 10:39:45 +00001102 vregs_.SetSize(number_of_vregs);
1103 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001104 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001105 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001106
1107 locations_.SetSize(number_of_vregs);
1108 for (size_t i = 0; i < number_of_vregs; ++i) {
1109 locations_.Put(i, Location());
1110 }
1111 }
1112
1113 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
1114 parent_ = new (allocator) HEnvironment(allocator,
1115 parent->Size(),
1116 parent->GetDexFile(),
1117 parent->GetMethodIdx(),
1118 parent->GetDexPc());
1119 if (parent->GetParent() != nullptr) {
1120 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1121 }
1122 parent_->CopyFrom(parent);
David Brazdiled596192015-01-23 10:39:45 +00001123 }
1124
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001125 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1126 void CopyFrom(HEnvironment* environment);
1127
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001128 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1129 // input to the loop phi instead. This is for inserting instructions that
1130 // require an environment (like HDeoptimization) in the loop pre-header.
1131 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001132
1133 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001134 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001135 }
1136
1137 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001138 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001139 }
1140
David Brazdil1abb4192015-02-17 18:33:36 +00001141 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001142
1143 size_t Size() const { return vregs_.Size(); }
1144
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001145 HEnvironment* GetParent() const { return parent_; }
1146
1147 void SetLocationAt(size_t index, Location location) {
1148 locations_.Put(index, location);
1149 }
1150
1151 Location GetLocationAt(size_t index) const {
1152 return locations_.Get(index);
1153 }
1154
1155 uint32_t GetDexPc() const {
1156 return dex_pc_;
1157 }
1158
1159 uint32_t GetMethodIdx() const {
1160 return method_idx_;
1161 }
1162
1163 const DexFile& GetDexFile() const {
1164 return dex_file_;
1165 }
1166
David Brazdiled596192015-01-23 10:39:45 +00001167 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001168 // Record instructions' use entries of this environment for constant-time removal.
1169 // It should only be called by HInstruction when a new environment use is added.
1170 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1171 DCHECK(env_use->GetUser() == this);
1172 size_t index = env_use->GetIndex();
1173 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1174 }
David Brazdiled596192015-01-23 10:39:45 +00001175
David Brazdil1abb4192015-02-17 18:33:36 +00001176 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001177 GrowableArray<Location> locations_;
1178 HEnvironment* parent_;
1179 const DexFile& dex_file_;
1180 const uint32_t method_idx_;
1181 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001182
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001183 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001184
1185 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1186};
1187
Calin Juravleacf735c2015-02-12 15:25:22 +00001188class ReferenceTypeInfo : ValueObject {
1189 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001190 typedef Handle<mirror::Class> TypeHandle;
1191
1192 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1194 if (type_handle->IsObjectClass()) {
1195 // Override the type handle to be consistent with the case when we get to
1196 // Top but don't have the Object class available. It avoids having to guess
1197 // what value the type_handle has when it's Top.
1198 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1199 } else {
1200 return ReferenceTypeInfo(type_handle, is_exact, false);
1201 }
1202 }
1203
1204 static ReferenceTypeInfo CreateTop(bool is_exact) {
1205 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001206 }
1207
1208 bool IsExact() const { return is_exact_; }
1209 bool IsTop() const { return is_top_; }
1210
1211 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1212
Calin Juravleb1498f62015-02-16 13:13:29 +00001213 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001214 if (IsTop()) {
1215 // Top (equivalent for java.lang.Object) is supertype of anything.
1216 return true;
1217 }
1218 if (rti.IsTop()) {
1219 // If we get here `this` is not Top() so it can't be a supertype.
1220 return false;
1221 }
1222 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1223 }
1224
1225 // Returns true if the type information provide the same amount of details.
1226 // Note that it does not mean that the instructions have the same actual type
1227 // (e.g. tops are equal but they can be the result of a merge).
1228 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1229 if (IsExact() != rti.IsExact()) {
1230 return false;
1231 }
1232 if (IsTop() && rti.IsTop()) {
1233 // `Top` means java.lang.Object, so the types are equivalent.
1234 return true;
1235 }
1236 if (IsTop() || rti.IsTop()) {
1237 // If only one is top or object than they are not equivalent.
1238 // NB: We need this extra check because the type_handle of `Top` is invalid
1239 // and we cannot inspect its reference.
1240 return false;
1241 }
1242
1243 // Finally check the types.
1244 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1245 }
1246
1247 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001248 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1249 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1250 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1251
Calin Juravleacf735c2015-02-12 15:25:22 +00001252 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001253 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001254 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001255 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001256 bool is_exact_;
1257 // A true value here means that the object type should be java.lang.Object.
1258 // We don't have access to the corresponding mirror object every time so this
1259 // flag acts as a substitute. When true, the TypeHandle refers to a null
1260 // pointer and should not be used.
1261 bool is_top_;
1262};
1263
1264std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1265
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001266class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001267 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001268 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001269 : previous_(nullptr),
1270 next_(nullptr),
1271 block_(nullptr),
1272 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001273 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001274 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001275 locations_(nullptr),
1276 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001277 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001278 side_effects_(side_effects),
1279 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001280
Dave Allison20dfc792014-06-16 20:44:29 -07001281 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001282
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001283#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001284 enum InstructionKind {
1285 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1286 };
1287#undef DECLARE_KIND
1288
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001289 HInstruction* GetNext() const { return next_; }
1290 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001291
Calin Juravle77520bc2015-01-12 18:45:46 +00001292 HInstruction* GetNextDisregardingMoves() const;
1293 HInstruction* GetPreviousDisregardingMoves() const;
1294
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001295 HBasicBlock* GetBlock() const { return block_; }
1296 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001297 bool IsInBlock() const { return block_ != nullptr; }
1298 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001299 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001300
Roland Levillain6b879dd2014-09-22 17:13:44 +01001301 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001302 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001303
1304 virtual void Accept(HGraphVisitor* visitor) = 0;
1305 virtual const char* DebugName() const = 0;
1306
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001307 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001308 void SetRawInputAt(size_t index, HInstruction* input) {
1309 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1310 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001311
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001312 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001313 virtual uint32_t GetDexPc() const {
1314 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1315 " does not need an environment";
1316 UNREACHABLE();
1317 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001318 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001319 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001320 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001321
Calin Juravle10e244f2015-01-26 18:54:32 +00001322 // Does not apply for all instructions, but having this at top level greatly
1323 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001324 virtual bool CanBeNull() const {
1325 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1326 return true;
1327 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001328
Calin Juravle641547a2015-04-21 22:08:51 +01001329 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1330 UNUSED(obj);
1331 return false;
1332 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001333
Calin Juravleacf735c2015-02-12 15:25:22 +00001334 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001335 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001336 reference_type_info_ = reference_type_info;
1337 }
1338
Calin Juravle61d544b2015-02-23 16:46:57 +00001339 ReferenceTypeInfo GetReferenceTypeInfo() const {
1340 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1341 return reference_type_info_;
1342 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001343
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001344 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001345 DCHECK(user != nullptr);
1346 HUseListNode<HInstruction*>* use =
1347 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1348 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349 }
1350
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001351 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001352 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001353 HUseListNode<HEnvironment*>* env_use =
1354 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1355 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001356 }
1357
David Brazdil1abb4192015-02-17 18:33:36 +00001358 void RemoveAsUserOfInput(size_t input) {
1359 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1360 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1361 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001362
David Brazdil1abb4192015-02-17 18:33:36 +00001363 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1364 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001365
David Brazdiled596192015-01-23 10:39:45 +00001366 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1367 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001368 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001369 bool HasOnlyOneNonEnvironmentUse() const {
1370 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1371 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001372
Roland Levillain6c82d402014-10-13 16:10:27 +01001373 // Does this instruction strictly dominate `other_instruction`?
1374 // Returns false if this instruction and `other_instruction` are the same.
1375 // Aborts if this instruction and `other_instruction` are both phis.
1376 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001377
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001378 int GetId() const { return id_; }
1379 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001381 int GetSsaIndex() const { return ssa_index_; }
1382 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1383 bool HasSsaIndex() const { return ssa_index_ != -1; }
1384
1385 bool HasEnvironment() const { return environment_ != nullptr; }
1386 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001387 // Set the `environment_` field. Raw because this method does not
1388 // update the uses lists.
1389 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1390
1391 // Set the environment of this instruction, copying it from `environment`. While
1392 // copying, the uses lists are being updated.
1393 void CopyEnvironmentFrom(HEnvironment* environment) {
1394 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001395 environment_ = new (allocator) HEnvironment(
1396 allocator,
1397 environment->Size(),
1398 environment->GetDexFile(),
1399 environment->GetMethodIdx(),
1400 environment->GetDexPc());
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001401 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001402 if (environment->GetParent() != nullptr) {
1403 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1404 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001405 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001406
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001407 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1408 HBasicBlock* block) {
1409 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001410 environment_ = new (allocator) HEnvironment(
1411 allocator,
1412 environment->Size(),
1413 environment->GetDexFile(),
1414 environment->GetMethodIdx(),
1415 environment->GetDexPc());
1416 if (environment->GetParent() != nullptr) {
1417 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1418 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001419 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1420 }
1421
Nicolas Geoffray39468442014-09-02 15:17:15 +01001422 // Returns the number of entries in the environment. Typically, that is the
1423 // number of dex registers in a method. It could be more in case of inlining.
1424 size_t EnvironmentSize() const;
1425
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001426 LocationSummary* GetLocations() const { return locations_; }
1427 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001428
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001429 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001430 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001431
Alexandre Rames188d4312015-04-09 18:30:21 +01001432 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1433 // uses of this instruction by `other` are *not* updated.
1434 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1435 ReplaceWith(other);
1436 other->ReplaceInput(this, use_index);
1437 }
1438
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001439 // Move `this` instruction before `cursor`.
1440 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001441
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001442#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001443 bool Is##type() const { return (As##type() != nullptr); } \
1444 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001445 virtual H##type* As##type() { return nullptr; }
1446
1447 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1448#undef INSTRUCTION_TYPE_CHECK
1449
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001450 // Returns whether the instruction can be moved within the graph.
1451 virtual bool CanBeMoved() const { return false; }
1452
1453 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001454 virtual bool InstructionTypeEquals(HInstruction* other) const {
1455 UNUSED(other);
1456 return false;
1457 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001458
1459 // Returns whether any data encoded in the two instructions is equal.
1460 // This method does not look at the inputs. Both instructions must be
1461 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001462 virtual bool InstructionDataEquals(HInstruction* other) const {
1463 UNUSED(other);
1464 return false;
1465 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001466
1467 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001468 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001469 // 2) Their inputs are identical.
1470 bool Equals(HInstruction* other) const;
1471
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001472 virtual InstructionKind GetKind() const = 0;
1473
1474 virtual size_t ComputeHashCode() const {
1475 size_t result = GetKind();
1476 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1477 result = (result * 31) + InputAt(i)->GetId();
1478 }
1479 return result;
1480 }
1481
1482 SideEffects GetSideEffects() const { return side_effects_; }
1483
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001484 size_t GetLifetimePosition() const { return lifetime_position_; }
1485 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1486 LiveInterval* GetLiveInterval() const { return live_interval_; }
1487 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1488 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1489
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001490 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1491
1492 // Returns whether the code generation of the instruction will require to have access
1493 // to the current method. Such instructions are:
1494 // (1): Instructions that require an environment, as calling the runtime requires
1495 // to walk the stack and have the current method stored at a specific stack address.
1496 // (2): Object literals like classes and strings, that are loaded from the dex cache
1497 // fields of the current method.
1498 bool NeedsCurrentMethod() const {
1499 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1500 }
1501
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001502 virtual bool NeedsDexCache() const { return false; }
1503
David Brazdil1abb4192015-02-17 18:33:36 +00001504 protected:
1505 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1506 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1507
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001508 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001509 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1510
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001511 HInstruction* previous_;
1512 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001513 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001514
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001515 // An instruction gets an id when it is added to the graph.
1516 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001517 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001518 int id_;
1519
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001520 // When doing liveness analysis, instructions that have uses get an SSA index.
1521 int ssa_index_;
1522
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001523 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001524 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001525
1526 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001527 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001528
Nicolas Geoffray39468442014-09-02 15:17:15 +01001529 // The environment associated with this instruction. Not null if the instruction
1530 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001531 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001532
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001533 // Set by the code generator.
1534 LocationSummary* locations_;
1535
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001536 // Set by the liveness analysis.
1537 LiveInterval* live_interval_;
1538
1539 // Set by the liveness analysis, this is the position in a linear
1540 // order of blocks where this instruction's live interval start.
1541 size_t lifetime_position_;
1542
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001543 const SideEffects side_effects_;
1544
Calin Juravleacf735c2015-02-12 15:25:22 +00001545 // TODO: for primitive types this should be marked as invalid.
1546 ReferenceTypeInfo reference_type_info_;
1547
David Brazdil1abb4192015-02-17 18:33:36 +00001548 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001549 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001550 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001551 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001553
1554 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1555};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001556std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001557
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001558class HInputIterator : public ValueObject {
1559 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001560 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001561
1562 bool Done() const { return index_ == instruction_->InputCount(); }
1563 HInstruction* Current() const { return instruction_->InputAt(index_); }
1564 void Advance() { index_++; }
1565
1566 private:
1567 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001568 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001569
1570 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1571};
1572
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001573class HInstructionIterator : public ValueObject {
1574 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001575 explicit HInstructionIterator(const HInstructionList& instructions)
1576 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001577 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001578 }
1579
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001580 bool Done() const { return instruction_ == nullptr; }
1581 HInstruction* Current() const { return instruction_; }
1582 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001583 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001584 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001585 }
1586
1587 private:
1588 HInstruction* instruction_;
1589 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001590
1591 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001592};
1593
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001594class HBackwardInstructionIterator : public ValueObject {
1595 public:
1596 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1597 : instruction_(instructions.last_instruction_) {
1598 next_ = Done() ? nullptr : instruction_->GetPrevious();
1599 }
1600
1601 bool Done() const { return instruction_ == nullptr; }
1602 HInstruction* Current() const { return instruction_; }
1603 void Advance() {
1604 instruction_ = next_;
1605 next_ = Done() ? nullptr : instruction_->GetPrevious();
1606 }
1607
1608 private:
1609 HInstruction* instruction_;
1610 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001611
1612 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001613};
1614
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001615// An embedded container with N elements of type T. Used (with partial
1616// specialization for N=0) because embedded arrays cannot have size 0.
1617template<typename T, intptr_t N>
1618class EmbeddedArray {
1619 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001620 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001621
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001622 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001623
1624 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001625 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001626 return elements_[i];
1627 }
1628
1629 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001630 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001631 return elements_[i];
1632 }
1633
1634 const T& At(intptr_t i) const {
1635 return (*this)[i];
1636 }
1637
1638 void SetAt(intptr_t i, const T& val) {
1639 (*this)[i] = val;
1640 }
1641
1642 private:
1643 T elements_[N];
1644};
1645
1646template<typename T>
1647class EmbeddedArray<T, 0> {
1648 public:
1649 intptr_t length() const { return 0; }
1650 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001651 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001652 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001653 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001654 }
1655 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001656 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001657 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001658 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001659 }
1660};
1661
1662template<intptr_t N>
1663class HTemplateInstruction: public HInstruction {
1664 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001665 HTemplateInstruction<N>(SideEffects side_effects)
1666 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001667 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001668
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001669 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001670
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001671 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001672 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1673
1674 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1675 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001676 }
1677
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001678 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001679 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001680
1681 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001682};
1683
Dave Allison20dfc792014-06-16 20:44:29 -07001684template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001685class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001686 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001687 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1688 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001689 virtual ~HExpression() {}
1690
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001691 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001692
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001693 protected:
1694 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001695};
1696
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001697// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1698// instruction that branches to the exit block.
1699class HReturnVoid : public HTemplateInstruction<0> {
1700 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001701 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001702
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001703 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001704
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001705 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001706
1707 private:
1708 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1709};
1710
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001711// Represents dex's RETURN opcodes. A HReturn is a control flow
1712// instruction that branches to the exit block.
1713class HReturn : public HTemplateInstruction<1> {
1714 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001715 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001716 SetRawInputAt(0, value);
1717 }
1718
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001719 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001720
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001721 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001722
1723 private:
1724 DISALLOW_COPY_AND_ASSIGN(HReturn);
1725};
1726
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001727// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001728// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001729// exit block.
1730class HExit : public HTemplateInstruction<0> {
1731 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001732 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001733
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001734 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001735
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001736 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001737
1738 private:
1739 DISALLOW_COPY_AND_ASSIGN(HExit);
1740};
1741
1742// Jumps from one block to another.
1743class HGoto : public HTemplateInstruction<0> {
1744 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001745 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1746
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001747 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001748
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001749 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001750 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001751 }
1752
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001753 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001754
1755 private:
1756 DISALLOW_COPY_AND_ASSIGN(HGoto);
1757};
1758
Dave Allison20dfc792014-06-16 20:44:29 -07001759
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001760// Conditional branch. A block ending with an HIf instruction must have
1761// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001762class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001763 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001764 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001765 SetRawInputAt(0, input);
1766 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001767
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001768 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001769
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001770 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001771 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001772 }
1773
1774 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001775 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001776 }
1777
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001778 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001779
1780 private:
1781 DISALLOW_COPY_AND_ASSIGN(HIf);
1782};
1783
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001784// Deoptimize to interpreter, upon checking a condition.
1785class HDeoptimize : public HTemplateInstruction<1> {
1786 public:
1787 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1788 : HTemplateInstruction(SideEffects::None()),
1789 dex_pc_(dex_pc) {
1790 SetRawInputAt(0, cond);
1791 }
1792
1793 bool NeedsEnvironment() const OVERRIDE { return true; }
1794 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001795 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001796
1797 DECLARE_INSTRUCTION(Deoptimize);
1798
1799 private:
1800 uint32_t dex_pc_;
1801
1802 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1803};
1804
Roland Levillain88cb1752014-10-20 16:36:47 +01001805class HUnaryOperation : public HExpression<1> {
1806 public:
1807 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1808 : HExpression(result_type, SideEffects::None()) {
1809 SetRawInputAt(0, input);
1810 }
1811
1812 HInstruction* GetInput() const { return InputAt(0); }
1813 Primitive::Type GetResultType() const { return GetType(); }
1814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001815 bool CanBeMoved() const OVERRIDE { return true; }
1816 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001817 UNUSED(other);
1818 return true;
1819 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001820
Roland Levillain9240d6a2014-10-20 16:47:04 +01001821 // Try to statically evaluate `operation` and return a HConstant
1822 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001823 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001824 HConstant* TryStaticEvaluation() const;
1825
1826 // Apply this operation to `x`.
1827 virtual int32_t Evaluate(int32_t x) const = 0;
1828 virtual int64_t Evaluate(int64_t x) const = 0;
1829
Roland Levillain88cb1752014-10-20 16:36:47 +01001830 DECLARE_INSTRUCTION(UnaryOperation);
1831
1832 private:
1833 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1834};
1835
Dave Allison20dfc792014-06-16 20:44:29 -07001836class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001837 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001838 HBinaryOperation(Primitive::Type result_type,
1839 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001840 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001841 SetRawInputAt(0, left);
1842 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001843 }
1844
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001845 HInstruction* GetLeft() const { return InputAt(0); }
1846 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001847 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001848
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001849 virtual bool IsCommutative() const { return false; }
1850
1851 // Put constant on the right.
1852 // Returns whether order is changed.
1853 bool OrderInputsWithConstantOnTheRight() {
1854 HInstruction* left = InputAt(0);
1855 HInstruction* right = InputAt(1);
1856 if (left->IsConstant() && !right->IsConstant()) {
1857 ReplaceInput(right, 0);
1858 ReplaceInput(left, 1);
1859 return true;
1860 }
1861 return false;
1862 }
1863
1864 // Order inputs by instruction id, but favor constant on the right side.
1865 // This helps GVN for commutative ops.
1866 void OrderInputs() {
1867 DCHECK(IsCommutative());
1868 HInstruction* left = InputAt(0);
1869 HInstruction* right = InputAt(1);
1870 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1871 return;
1872 }
1873 if (OrderInputsWithConstantOnTheRight()) {
1874 return;
1875 }
1876 // Order according to instruction id.
1877 if (left->GetId() > right->GetId()) {
1878 ReplaceInput(right, 0);
1879 ReplaceInput(left, 1);
1880 }
1881 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001882
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001883 bool CanBeMoved() const OVERRIDE { return true; }
1884 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001885 UNUSED(other);
1886 return true;
1887 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001888
Roland Levillain9240d6a2014-10-20 16:47:04 +01001889 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001890 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001891 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001892 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001893
1894 // Apply this operation to `x` and `y`.
1895 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1896 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1897
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001898 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001899 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001900 HConstant* GetConstantRight() const;
1901
1902 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001903 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001904 HInstruction* GetLeastConstantLeft() const;
1905
Roland Levillainccc07a92014-09-16 14:48:16 +01001906 DECLARE_INSTRUCTION(BinaryOperation);
1907
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001908 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001909 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1910};
1911
Dave Allison20dfc792014-06-16 20:44:29 -07001912class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001913 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001914 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001915 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1916 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001917
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001918 bool NeedsMaterialization() const { return needs_materialization_; }
1919 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001920
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001921 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001922 // `instruction`, and disregard moves in between.
1923 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001924
Dave Allison20dfc792014-06-16 20:44:29 -07001925 DECLARE_INSTRUCTION(Condition);
1926
1927 virtual IfCondition GetCondition() const = 0;
1928
1929 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001930 // For register allocation purposes, returns whether this instruction needs to be
1931 // materialized (that is, not just be in the processor flags).
1932 bool needs_materialization_;
1933
Dave Allison20dfc792014-06-16 20:44:29 -07001934 DISALLOW_COPY_AND_ASSIGN(HCondition);
1935};
1936
1937// Instruction to check if two inputs are equal to each other.
1938class HEqual : public HCondition {
1939 public:
1940 HEqual(HInstruction* first, HInstruction* second)
1941 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001942
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001943 bool IsCommutative() const OVERRIDE { return true; }
1944
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001945 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001946 return x == y ? 1 : 0;
1947 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001948 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001949 return x == y ? 1 : 0;
1950 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001951
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001952 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001953
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001954 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001955 return kCondEQ;
1956 }
1957
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001958 private:
1959 DISALLOW_COPY_AND_ASSIGN(HEqual);
1960};
1961
Dave Allison20dfc792014-06-16 20:44:29 -07001962class HNotEqual : public HCondition {
1963 public:
1964 HNotEqual(HInstruction* first, HInstruction* second)
1965 : HCondition(first, second) {}
1966
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001967 bool IsCommutative() const OVERRIDE { return true; }
1968
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001969 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001970 return x != y ? 1 : 0;
1971 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001972 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001973 return x != y ? 1 : 0;
1974 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001975
Dave Allison20dfc792014-06-16 20:44:29 -07001976 DECLARE_INSTRUCTION(NotEqual);
1977
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001978 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001979 return kCondNE;
1980 }
1981
1982 private:
1983 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1984};
1985
1986class HLessThan : public HCondition {
1987 public:
1988 HLessThan(HInstruction* first, HInstruction* second)
1989 : HCondition(first, second) {}
1990
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001991 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001992 return x < y ? 1 : 0;
1993 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001994 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001995 return x < y ? 1 : 0;
1996 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001997
Dave Allison20dfc792014-06-16 20:44:29 -07001998 DECLARE_INSTRUCTION(LessThan);
1999
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002000 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002001 return kCondLT;
2002 }
2003
2004 private:
2005 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2006};
2007
2008class HLessThanOrEqual : public HCondition {
2009 public:
2010 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2011 : HCondition(first, second) {}
2012
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002013 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002014 return x <= y ? 1 : 0;
2015 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002016 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002017 return x <= y ? 1 : 0;
2018 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002019
Dave Allison20dfc792014-06-16 20:44:29 -07002020 DECLARE_INSTRUCTION(LessThanOrEqual);
2021
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002022 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002023 return kCondLE;
2024 }
2025
2026 private:
2027 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2028};
2029
2030class HGreaterThan : public HCondition {
2031 public:
2032 HGreaterThan(HInstruction* first, HInstruction* second)
2033 : HCondition(first, second) {}
2034
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002035 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002036 return x > y ? 1 : 0;
2037 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002038 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002039 return x > y ? 1 : 0;
2040 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002041
Dave Allison20dfc792014-06-16 20:44:29 -07002042 DECLARE_INSTRUCTION(GreaterThan);
2043
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002044 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002045 return kCondGT;
2046 }
2047
2048 private:
2049 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2050};
2051
2052class HGreaterThanOrEqual : public HCondition {
2053 public:
2054 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2055 : HCondition(first, second) {}
2056
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002057 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002058 return x >= y ? 1 : 0;
2059 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002060 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002061 return x >= y ? 1 : 0;
2062 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002063
Dave Allison20dfc792014-06-16 20:44:29 -07002064 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2065
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002066 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002067 return kCondGE;
2068 }
2069
2070 private:
2071 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2072};
2073
2074
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002075// Instruction to check how two inputs compare to each other.
2076// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2077class HCompare : public HBinaryOperation {
2078 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002079 // The bias applies for floating point operations and indicates how NaN
2080 // comparisons are treated:
2081 enum Bias {
2082 kNoBias, // bias is not applicable (i.e. for long operation)
2083 kGtBias, // return 1 for NaN comparisons
2084 kLtBias, // return -1 for NaN comparisons
2085 };
2086
2087 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
2088 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002089 DCHECK_EQ(type, first->GetType());
2090 DCHECK_EQ(type, second->GetType());
2091 }
2092
Calin Juravleddb7df22014-11-25 20:56:51 +00002093 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002094 return
2095 x == y ? 0 :
2096 x > y ? 1 :
2097 -1;
2098 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002099
2100 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002101 return
2102 x == y ? 0 :
2103 x > y ? 1 :
2104 -1;
2105 }
2106
Calin Juravleddb7df22014-11-25 20:56:51 +00002107 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2108 return bias_ == other->AsCompare()->bias_;
2109 }
2110
2111 bool IsGtBias() { return bias_ == kGtBias; }
2112
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002113 DECLARE_INSTRUCTION(Compare);
2114
2115 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002116 const Bias bias_;
2117
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002118 DISALLOW_COPY_AND_ASSIGN(HCompare);
2119};
2120
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002121// A local in the graph. Corresponds to a Dex register.
2122class HLocal : public HTemplateInstruction<0> {
2123 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002124 explicit HLocal(uint16_t reg_number)
2125 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002126
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002127 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002128
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002129 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002130
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002131 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002132 // The Dex register number.
2133 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002134
2135 DISALLOW_COPY_AND_ASSIGN(HLocal);
2136};
2137
2138// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002139class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002140 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002141 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002142 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002143 SetRawInputAt(0, local);
2144 }
2145
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002146 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2147
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002148 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002149
2150 private:
2151 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2152};
2153
2154// Store a value in a given local. This instruction has two inputs: the value
2155// and the local.
2156class HStoreLocal : public HTemplateInstruction<2> {
2157 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002158 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002159 SetRawInputAt(0, local);
2160 SetRawInputAt(1, value);
2161 }
2162
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002163 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2164
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002165 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002166
2167 private:
2168 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2169};
2170
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002171class HConstant : public HExpression<0> {
2172 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002173 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2174
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002175 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002176
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002177 virtual bool IsMinusOne() const { return false; }
2178 virtual bool IsZero() const { return false; }
2179 virtual bool IsOne() const { return false; }
2180
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002181 DECLARE_INSTRUCTION(Constant);
2182
2183 private:
2184 DISALLOW_COPY_AND_ASSIGN(HConstant);
2185};
2186
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002187class HFloatConstant : public HConstant {
2188 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002189 float GetValue() const { return value_; }
2190
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002191 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002192 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2193 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002194 }
2195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002196 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002197
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002198 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002199 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2200 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002201 }
2202 bool IsZero() const OVERRIDE {
2203 return AsFloatConstant()->GetValue() == 0.0f;
2204 }
2205 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002206 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2207 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002208 }
2209
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002210 DECLARE_INSTRUCTION(FloatConstant);
2211
2212 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002213 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002214 explicit HFloatConstant(int32_t value)
2215 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002216
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002217 const float value_;
2218
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002219 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002220 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002221 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002222 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2223};
2224
2225class HDoubleConstant : public HConstant {
2226 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002227 double GetValue() const { return value_; }
2228
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002229 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002230 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2231 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002232 }
2233
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002234 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002235
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002236 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002237 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2238 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002239 }
2240 bool IsZero() const OVERRIDE {
2241 return AsDoubleConstant()->GetValue() == 0.0;
2242 }
2243 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002244 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2245 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002246 }
2247
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002248 DECLARE_INSTRUCTION(DoubleConstant);
2249
2250 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002251 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002252 explicit HDoubleConstant(int64_t value)
2253 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002254
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002255 const double value_;
2256
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002257 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002258 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002259 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002260 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2261};
2262
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002263class HNullConstant : public HConstant {
2264 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002265 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2266 return true;
2267 }
2268
2269 size_t ComputeHashCode() const OVERRIDE { return 0; }
2270
2271 DECLARE_INSTRUCTION(NullConstant);
2272
2273 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002274 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2275
2276 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002277 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2278};
2279
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002280// Constants of the type int. Those can be from Dex instructions, or
2281// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002282class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002283 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002284 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002285
Calin Juravle61d544b2015-02-23 16:46:57 +00002286 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287 return other->AsIntConstant()->value_ == value_;
2288 }
2289
Calin Juravle61d544b2015-02-23 16:46:57 +00002290 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2291
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002292 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2293 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2294 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2295
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002296 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002297
2298 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002299 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2300
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002301 const int32_t value_;
2302
David Brazdil8d5b8b22015-03-24 10:51:52 +00002303 friend class HGraph;
2304 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002305 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002306 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2307};
2308
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002309class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002310 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002311 int64_t GetValue() const { return value_; }
2312
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002313 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002314 return other->AsLongConstant()->value_ == value_;
2315 }
2316
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002317 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002318
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002319 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2320 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2321 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2322
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002323 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002324
2325 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002326 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2327
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002328 const int64_t value_;
2329
David Brazdil8d5b8b22015-03-24 10:51:52 +00002330 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002331 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2332};
2333
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002334enum class Intrinsics {
2335#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2336#include "intrinsics_list.h"
2337 kNone,
2338 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2339#undef INTRINSICS_LIST
2340#undef OPTIMIZING_INTRINSICS
2341};
2342std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2343
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002344class HInvoke : public HInstruction {
2345 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002346 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002347
2348 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2349 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002350 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002351
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002352 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002353 SetRawInputAt(index, argument);
2354 }
2355
Roland Levillain3e3d7332015-04-28 11:00:54 +01002356 // Return the number of arguments. This number can be lower than
2357 // the number of inputs returned by InputCount(), as some invoke
2358 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2359 // inputs at the end of their list of inputs.
2360 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002362 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002363
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002364 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002365
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002366 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2367
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002368 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002369 return intrinsic_;
2370 }
2371
2372 void SetIntrinsic(Intrinsics intrinsic) {
2373 intrinsic_ = intrinsic;
2374 }
2375
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002376 DECLARE_INSTRUCTION(Invoke);
2377
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002378 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002379 HInvoke(ArenaAllocator* arena,
2380 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002381 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002382 Primitive::Type return_type,
2383 uint32_t dex_pc,
2384 uint32_t dex_method_index)
2385 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002386 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002387 inputs_(arena, number_of_arguments),
2388 return_type_(return_type),
2389 dex_pc_(dex_pc),
2390 dex_method_index_(dex_method_index),
2391 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002392 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2393 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002394 }
2395
David Brazdil1abb4192015-02-17 18:33:36 +00002396 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2397 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2398 inputs_.Put(index, input);
2399 }
2400
Roland Levillain3e3d7332015-04-28 11:00:54 +01002401 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002402 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002403 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002404 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002405 const uint32_t dex_method_index_;
2406 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002407
2408 private:
2409 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2410};
2411
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002412class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002413 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002414 // Requirements of this method call regarding the class
2415 // initialization (clinit) check of its declaring class.
2416 enum class ClinitCheckRequirement {
2417 kNone, // Class already initialized.
2418 kExplicit, // Static call having explicit clinit check as last input.
2419 kImplicit, // Static call implicitly requiring a clinit check.
2420 };
2421
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002422 HInvokeStaticOrDirect(ArenaAllocator* arena,
2423 uint32_t number_of_arguments,
2424 Primitive::Type return_type,
2425 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002426 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002427 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002428 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002429 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002430 InvokeType invoke_type,
2431 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002432 : HInvoke(arena,
2433 number_of_arguments,
2434 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2435 return_type,
2436 dex_pc,
2437 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002438 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002439 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002440 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002441 clinit_check_requirement_(clinit_check_requirement),
2442 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002443
Calin Juravle641547a2015-04-21 22:08:51 +01002444 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2445 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002446 // We access the method via the dex cache so we can't do an implicit null check.
2447 // TODO: for intrinsics we can generate implicit null checks.
2448 return false;
2449 }
2450
Nicolas Geoffray79041292015-03-26 10:05:54 +00002451 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002452 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002453 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002454 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002455 bool IsStringInit() const { return string_init_offset_ != 0; }
2456 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002457
Roland Levillain4c0eb422015-04-24 16:43:49 +01002458 // Is this instruction a call to a static method?
2459 bool IsStatic() const {
2460 return GetInvokeType() == kStatic;
2461 }
2462
Roland Levillain3e3d7332015-04-28 11:00:54 +01002463 // Remove the art::HLoadClass instruction set as last input by
2464 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2465 // the initial art::HClinitCheck instruction (only relevant for
2466 // static calls with explicit clinit check).
2467 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002468 DCHECK(IsStaticWithExplicitClinitCheck());
2469 size_t last_input_index = InputCount() - 1;
2470 HInstruction* last_input = InputAt(last_input_index);
2471 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002472 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002473 RemoveAsUserOfInput(last_input_index);
2474 inputs_.DeleteAt(last_input_index);
2475 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2476 DCHECK(IsStaticWithImplicitClinitCheck());
2477 }
2478
2479 // Is this a call to a static method whose declaring class has an
2480 // explicit intialization check in the graph?
2481 bool IsStaticWithExplicitClinitCheck() const {
2482 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2483 }
2484
2485 // Is this a call to a static method whose declaring class has an
2486 // implicit intialization check requirement?
2487 bool IsStaticWithImplicitClinitCheck() const {
2488 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2489 }
2490
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002491 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002492
Roland Levillain4c0eb422015-04-24 16:43:49 +01002493 protected:
2494 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2495 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2496 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2497 HInstruction* input = input_record.GetInstruction();
2498 // `input` is the last input of a static invoke marked as having
2499 // an explicit clinit check. It must either be:
2500 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2501 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2502 DCHECK(input != nullptr);
2503 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2504 }
2505 return input_record;
2506 }
2507
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002508 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002509 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002510 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002511 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002512 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002513 // Thread entrypoint offset for string init method if this is a string init invoke.
2514 // Note that there are multiple string init methods, each having its own offset.
2515 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002516
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002517 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002518};
2519
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002520class HInvokeVirtual : public HInvoke {
2521 public:
2522 HInvokeVirtual(ArenaAllocator* arena,
2523 uint32_t number_of_arguments,
2524 Primitive::Type return_type,
2525 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002526 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002527 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002528 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002529 vtable_index_(vtable_index) {}
2530
Calin Juravle641547a2015-04-21 22:08:51 +01002531 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002532 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002533 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002534 }
2535
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002536 uint32_t GetVTableIndex() const { return vtable_index_; }
2537
2538 DECLARE_INSTRUCTION(InvokeVirtual);
2539
2540 private:
2541 const uint32_t vtable_index_;
2542
2543 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2544};
2545
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002546class HInvokeInterface : public HInvoke {
2547 public:
2548 HInvokeInterface(ArenaAllocator* arena,
2549 uint32_t number_of_arguments,
2550 Primitive::Type return_type,
2551 uint32_t dex_pc,
2552 uint32_t dex_method_index,
2553 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002554 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002555 imt_index_(imt_index) {}
2556
Calin Juravle641547a2015-04-21 22:08:51 +01002557 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002558 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002559 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002560 }
2561
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002562 uint32_t GetImtIndex() const { return imt_index_; }
2563 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2564
2565 DECLARE_INSTRUCTION(InvokeInterface);
2566
2567 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002568 const uint32_t imt_index_;
2569
2570 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2571};
2572
Dave Allison20dfc792014-06-16 20:44:29 -07002573class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002574 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002575 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002576 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2577 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002578 type_index_(type_index),
2579 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002580
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002581 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002582 uint16_t GetTypeIndex() const { return type_index_; }
2583
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002584 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002585 bool NeedsEnvironment() const OVERRIDE { return true; }
2586 // It may throw when called on:
2587 // - interfaces
2588 // - abstract/innaccessible/unknown classes
2589 // TODO: optimize when possible.
2590 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002591
Calin Juravle10e244f2015-01-26 18:54:32 +00002592 bool CanBeNull() const OVERRIDE { return false; }
2593
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002594 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2595
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002596 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002597
2598 private:
2599 const uint32_t dex_pc_;
2600 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002601 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002602
2603 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2604};
2605
Roland Levillain88cb1752014-10-20 16:36:47 +01002606class HNeg : public HUnaryOperation {
2607 public:
2608 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2609 : HUnaryOperation(result_type, input) {}
2610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002611 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2612 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002613
Roland Levillain88cb1752014-10-20 16:36:47 +01002614 DECLARE_INSTRUCTION(Neg);
2615
2616 private:
2617 DISALLOW_COPY_AND_ASSIGN(HNeg);
2618};
2619
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002620class HNewArray : public HExpression<1> {
2621 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002622 HNewArray(HInstruction* length,
2623 uint32_t dex_pc,
2624 uint16_t type_index,
2625 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002626 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2627 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002628 type_index_(type_index),
2629 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002630 SetRawInputAt(0, length);
2631 }
2632
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002633 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002634 uint16_t GetTypeIndex() const { return type_index_; }
2635
2636 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002637 bool NeedsEnvironment() const OVERRIDE { return true; }
2638
Mingyao Yang0c365e62015-03-31 15:09:29 -07002639 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2640 bool CanThrow() const OVERRIDE { return true; }
2641
Calin Juravle10e244f2015-01-26 18:54:32 +00002642 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002643
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002644 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2645
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002646 DECLARE_INSTRUCTION(NewArray);
2647
2648 private:
2649 const uint32_t dex_pc_;
2650 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002651 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002652
2653 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2654};
2655
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002656class HAdd : public HBinaryOperation {
2657 public:
2658 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2659 : HBinaryOperation(result_type, left, right) {}
2660
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002661 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002662
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002663 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002664 return x + y;
2665 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002666 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002667 return x + y;
2668 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002669
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002670 DECLARE_INSTRUCTION(Add);
2671
2672 private:
2673 DISALLOW_COPY_AND_ASSIGN(HAdd);
2674};
2675
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002676class HSub : public HBinaryOperation {
2677 public:
2678 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2679 : HBinaryOperation(result_type, left, right) {}
2680
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002681 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002682 return x - y;
2683 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002684 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002685 return x - y;
2686 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002687
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002688 DECLARE_INSTRUCTION(Sub);
2689
2690 private:
2691 DISALLOW_COPY_AND_ASSIGN(HSub);
2692};
2693
Calin Juravle34bacdf2014-10-07 20:23:36 +01002694class HMul : public HBinaryOperation {
2695 public:
2696 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2697 : HBinaryOperation(result_type, left, right) {}
2698
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002699 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002700
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002701 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2702 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002703
2704 DECLARE_INSTRUCTION(Mul);
2705
2706 private:
2707 DISALLOW_COPY_AND_ASSIGN(HMul);
2708};
2709
Calin Juravle7c4954d2014-10-28 16:57:40 +00002710class HDiv : public HBinaryOperation {
2711 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002712 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2713 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002714
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002715 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002716 // Our graph structure ensures we never have 0 for `y` during constant folding.
2717 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002718 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002719 return (y == -1) ? -x : x / y;
2720 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002721
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002722 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002723 DCHECK_NE(y, 0);
2724 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2725 return (y == -1) ? -x : x / y;
2726 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002727
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002728 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002729
Calin Juravle7c4954d2014-10-28 16:57:40 +00002730 DECLARE_INSTRUCTION(Div);
2731
2732 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002733 const uint32_t dex_pc_;
2734
Calin Juravle7c4954d2014-10-28 16:57:40 +00002735 DISALLOW_COPY_AND_ASSIGN(HDiv);
2736};
2737
Calin Juravlebacfec32014-11-14 15:54:36 +00002738class HRem : public HBinaryOperation {
2739 public:
2740 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2741 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2742
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002743 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002744 DCHECK_NE(y, 0);
2745 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2746 return (y == -1) ? 0 : x % y;
2747 }
2748
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002749 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002750 DCHECK_NE(y, 0);
2751 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2752 return (y == -1) ? 0 : x % y;
2753 }
2754
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002755 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002756
2757 DECLARE_INSTRUCTION(Rem);
2758
2759 private:
2760 const uint32_t dex_pc_;
2761
2762 DISALLOW_COPY_AND_ASSIGN(HRem);
2763};
2764
Calin Juravled0d48522014-11-04 16:40:20 +00002765class HDivZeroCheck : public HExpression<1> {
2766 public:
2767 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2768 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2769 SetRawInputAt(0, value);
2770 }
2771
2772 bool CanBeMoved() const OVERRIDE { return true; }
2773
2774 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2775 UNUSED(other);
2776 return true;
2777 }
2778
2779 bool NeedsEnvironment() const OVERRIDE { return true; }
2780 bool CanThrow() const OVERRIDE { return true; }
2781
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002782 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002783
2784 DECLARE_INSTRUCTION(DivZeroCheck);
2785
2786 private:
2787 const uint32_t dex_pc_;
2788
2789 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2790};
2791
Calin Juravle9aec02f2014-11-18 23:06:35 +00002792class HShl : public HBinaryOperation {
2793 public:
2794 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2795 : HBinaryOperation(result_type, left, right) {}
2796
2797 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2798 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2799
2800 DECLARE_INSTRUCTION(Shl);
2801
2802 private:
2803 DISALLOW_COPY_AND_ASSIGN(HShl);
2804};
2805
2806class HShr : public HBinaryOperation {
2807 public:
2808 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2809 : HBinaryOperation(result_type, left, right) {}
2810
2811 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2812 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2813
2814 DECLARE_INSTRUCTION(Shr);
2815
2816 private:
2817 DISALLOW_COPY_AND_ASSIGN(HShr);
2818};
2819
2820class HUShr : public HBinaryOperation {
2821 public:
2822 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2823 : HBinaryOperation(result_type, left, right) {}
2824
2825 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2826 uint32_t ux = static_cast<uint32_t>(x);
2827 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2828 return static_cast<int32_t>(ux >> uy);
2829 }
2830
2831 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2832 uint64_t ux = static_cast<uint64_t>(x);
2833 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2834 return static_cast<int64_t>(ux >> uy);
2835 }
2836
2837 DECLARE_INSTRUCTION(UShr);
2838
2839 private:
2840 DISALLOW_COPY_AND_ASSIGN(HUShr);
2841};
2842
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002843class HAnd : public HBinaryOperation {
2844 public:
2845 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2846 : HBinaryOperation(result_type, left, right) {}
2847
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002848 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002849
2850 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2851 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2852
2853 DECLARE_INSTRUCTION(And);
2854
2855 private:
2856 DISALLOW_COPY_AND_ASSIGN(HAnd);
2857};
2858
2859class HOr : public HBinaryOperation {
2860 public:
2861 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2862 : HBinaryOperation(result_type, left, right) {}
2863
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002864 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002865
2866 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2867 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2868
2869 DECLARE_INSTRUCTION(Or);
2870
2871 private:
2872 DISALLOW_COPY_AND_ASSIGN(HOr);
2873};
2874
2875class HXor : public HBinaryOperation {
2876 public:
2877 HXor(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(Xor);
2886
2887 private:
2888 DISALLOW_COPY_AND_ASSIGN(HXor);
2889};
2890
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002891// The value of a parameter in this method. Its location depends on
2892// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002893class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002894 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002895 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2896 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002897
2898 uint8_t GetIndex() const { return index_; }
2899
Calin Juravle10e244f2015-01-26 18:54:32 +00002900 bool CanBeNull() const OVERRIDE { return !is_this_; }
2901
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002902 DECLARE_INSTRUCTION(ParameterValue);
2903
2904 private:
2905 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002906 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002907 const uint8_t index_;
2908
Calin Juravle10e244f2015-01-26 18:54:32 +00002909 // Whether or not the parameter value corresponds to 'this' argument.
2910 const bool is_this_;
2911
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002912 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2913};
2914
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002915class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002916 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002917 explicit HNot(Primitive::Type result_type, HInstruction* input)
2918 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002919
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002920 bool CanBeMoved() const OVERRIDE { return true; }
2921 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002922 UNUSED(other);
2923 return true;
2924 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002925
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002926 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2927 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002928
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002929 DECLARE_INSTRUCTION(Not);
2930
2931 private:
2932 DISALLOW_COPY_AND_ASSIGN(HNot);
2933};
2934
David Brazdil66d126e2015-04-03 16:02:44 +01002935class HBooleanNot : public HUnaryOperation {
2936 public:
2937 explicit HBooleanNot(HInstruction* input)
2938 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2939
2940 bool CanBeMoved() const OVERRIDE { return true; }
2941 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2942 UNUSED(other);
2943 return true;
2944 }
2945
2946 int32_t Evaluate(int32_t x) const OVERRIDE {
2947 DCHECK(IsUint<1>(x));
2948 return !x;
2949 }
2950
2951 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2952 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2953 UNREACHABLE();
2954 }
2955
2956 DECLARE_INSTRUCTION(BooleanNot);
2957
2958 private:
2959 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2960};
2961
Roland Levillaindff1f282014-11-05 14:15:05 +00002962class HTypeConversion : public HExpression<1> {
2963 public:
2964 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002965 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2966 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002967 SetRawInputAt(0, input);
2968 DCHECK_NE(input->GetType(), result_type);
2969 }
2970
2971 HInstruction* GetInput() const { return InputAt(0); }
2972 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2973 Primitive::Type GetResultType() const { return GetType(); }
2974
Roland Levillain624279f2014-12-04 11:54:28 +00002975 // Required by the x86 and ARM code generators when producing calls
2976 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002977 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00002978
Roland Levillaindff1f282014-11-05 14:15:05 +00002979 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002980 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002981
2982 DECLARE_INSTRUCTION(TypeConversion);
2983
2984 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002985 const uint32_t dex_pc_;
2986
Roland Levillaindff1f282014-11-05 14:15:05 +00002987 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2988};
2989
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002990static constexpr uint32_t kNoRegNumber = -1;
2991
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002992class HPhi : public HInstruction {
2993 public:
2994 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002995 : HInstruction(SideEffects::None()),
2996 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002997 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002998 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002999 is_live_(false),
3000 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003001 inputs_.SetSize(number_of_inputs);
3002 }
3003
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003004 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3005 static Primitive::Type ToPhiType(Primitive::Type type) {
3006 switch (type) {
3007 case Primitive::kPrimBoolean:
3008 case Primitive::kPrimByte:
3009 case Primitive::kPrimShort:
3010 case Primitive::kPrimChar:
3011 return Primitive::kPrimInt;
3012 default:
3013 return type;
3014 }
3015 }
3016
Calin Juravle10e244f2015-01-26 18:54:32 +00003017 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003018
3019 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003020 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003021
Calin Juravle10e244f2015-01-26 18:54:32 +00003022 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003023 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003024
Calin Juravle10e244f2015-01-26 18:54:32 +00003025 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3026 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3027
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003028 uint32_t GetRegNumber() const { return reg_number_; }
3029
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003030 void SetDead() { is_live_ = false; }
3031 void SetLive() { is_live_ = true; }
3032 bool IsDead() const { return !is_live_; }
3033 bool IsLive() const { return is_live_; }
3034
Calin Juravlea4f88312015-04-16 12:57:19 +01003035 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3036 // An equivalent phi is a phi having the same dex register and type.
3037 // It assumes that phis with the same dex register are adjacent.
3038 HPhi* GetNextEquivalentPhiWithSameType() {
3039 HInstruction* next = GetNext();
3040 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3041 if (next->GetType() == GetType()) {
3042 return next->AsPhi();
3043 }
3044 next = next->GetNext();
3045 }
3046 return nullptr;
3047 }
3048
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003049 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003050
David Brazdil1abb4192015-02-17 18:33:36 +00003051 protected:
3052 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3053
3054 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3055 inputs_.Put(index, input);
3056 }
3057
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003058 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003059 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003060 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003061 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003062 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003063 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003064
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003065 DISALLOW_COPY_AND_ASSIGN(HPhi);
3066};
3067
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003068class HNullCheck : public HExpression<1> {
3069 public:
3070 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003071 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003072 SetRawInputAt(0, value);
3073 }
3074
Calin Juravle10e244f2015-01-26 18:54:32 +00003075 bool CanBeMoved() const OVERRIDE { return true; }
3076 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003077 UNUSED(other);
3078 return true;
3079 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003080
Calin Juravle10e244f2015-01-26 18:54:32 +00003081 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003082
Calin Juravle10e244f2015-01-26 18:54:32 +00003083 bool CanThrow() const OVERRIDE { return true; }
3084
3085 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003086
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003087 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003088
3089 DECLARE_INSTRUCTION(NullCheck);
3090
3091 private:
3092 const uint32_t dex_pc_;
3093
3094 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3095};
3096
3097class FieldInfo : public ValueObject {
3098 public:
Calin Juravle52c48962014-12-16 17:02:57 +00003099 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3100 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003101
3102 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003103 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003104 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003105
3106 private:
3107 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003108 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003109 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003110};
3111
3112class HInstanceFieldGet : public HExpression<1> {
3113 public:
3114 HInstanceFieldGet(HInstruction* value,
3115 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003116 MemberOffset field_offset,
3117 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003118 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003119 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003120 SetRawInputAt(0, value);
3121 }
3122
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003123 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003124
3125 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3126 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3127 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003128 }
3129
Calin Juravle641547a2015-04-21 22:08:51 +01003130 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3131 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003132 }
3133
3134 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003135 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3136 }
3137
Calin Juravle52c48962014-12-16 17:02:57 +00003138 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003139 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003140 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003141 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003142
3143 DECLARE_INSTRUCTION(InstanceFieldGet);
3144
3145 private:
3146 const FieldInfo field_info_;
3147
3148 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3149};
3150
3151class HInstanceFieldSet : public HTemplateInstruction<2> {
3152 public:
3153 HInstanceFieldSet(HInstruction* object,
3154 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003155 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003156 MemberOffset field_offset,
3157 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003158 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003159 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003160 SetRawInputAt(0, object);
3161 SetRawInputAt(1, value);
3162 }
3163
Calin Juravle641547a2015-04-21 22:08:51 +01003164 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3165 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003166 }
3167
Calin Juravle52c48962014-12-16 17:02:57 +00003168 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003169 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003170 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003171 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003172 HInstruction* GetValue() const { return InputAt(1); }
3173
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003174 DECLARE_INSTRUCTION(InstanceFieldSet);
3175
3176 private:
3177 const FieldInfo field_info_;
3178
3179 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3180};
3181
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003182class HArrayGet : public HExpression<2> {
3183 public:
3184 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003185 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003186 SetRawInputAt(0, array);
3187 SetRawInputAt(1, index);
3188 }
3189
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003190 bool CanBeMoved() const OVERRIDE { return true; }
3191 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003192 UNUSED(other);
3193 return true;
3194 }
Calin Juravle641547a2015-04-21 22:08:51 +01003195 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3196 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003197 // TODO: We can be smarter here.
3198 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3199 // which generates the implicit null check. There are cases when these can be removed
3200 // to produce better code. If we ever add optimizations to do so we should allow an
3201 // implicit check here (as long as the address falls in the first page).
3202 return false;
3203 }
3204
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003205 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003206
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003207 HInstruction* GetArray() const { return InputAt(0); }
3208 HInstruction* GetIndex() const { return InputAt(1); }
3209
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003210 DECLARE_INSTRUCTION(ArrayGet);
3211
3212 private:
3213 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3214};
3215
3216class HArraySet : public HTemplateInstruction<3> {
3217 public:
3218 HArraySet(HInstruction* array,
3219 HInstruction* index,
3220 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003221 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003222 uint32_t dex_pc)
3223 : HTemplateInstruction(SideEffects::ChangesSomething()),
3224 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003225 expected_component_type_(expected_component_type),
3226 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003227 SetRawInputAt(0, array);
3228 SetRawInputAt(1, index);
3229 SetRawInputAt(2, value);
3230 }
3231
Calin Juravle77520bc2015-01-12 18:45:46 +00003232 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003233 // We currently always call a runtime method to catch array store
3234 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003235 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003236 }
3237
Calin Juravle641547a2015-04-21 22:08:51 +01003238 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3239 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003240 // TODO: Same as for ArrayGet.
3241 return false;
3242 }
3243
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003244 void ClearNeedsTypeCheck() {
3245 needs_type_check_ = false;
3246 }
3247
3248 bool NeedsTypeCheck() const { return needs_type_check_; }
3249
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003250 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003251
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003252 HInstruction* GetArray() const { return InputAt(0); }
3253 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003254 HInstruction* GetValue() const { return InputAt(2); }
3255
3256 Primitive::Type GetComponentType() const {
3257 // The Dex format does not type floating point index operations. Since the
3258 // `expected_component_type_` is set during building and can therefore not
3259 // be correct, we also check what is the value type. If it is a floating
3260 // point type, we must use that type.
3261 Primitive::Type value_type = GetValue()->GetType();
3262 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3263 ? value_type
3264 : expected_component_type_;
3265 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003266
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003267 DECLARE_INSTRUCTION(ArraySet);
3268
3269 private:
3270 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003271 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003272 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003273
3274 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3275};
3276
3277class HArrayLength : public HExpression<1> {
3278 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003279 explicit HArrayLength(HInstruction* array)
3280 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3281 // Note that arrays do not change length, so the instruction does not
3282 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003283 SetRawInputAt(0, array);
3284 }
3285
Calin Juravle77520bc2015-01-12 18:45:46 +00003286 bool CanBeMoved() const OVERRIDE { return true; }
3287 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003288 UNUSED(other);
3289 return true;
3290 }
Calin Juravle641547a2015-04-21 22:08:51 +01003291 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3292 return obj == InputAt(0);
3293 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003294
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003295 DECLARE_INSTRUCTION(ArrayLength);
3296
3297 private:
3298 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3299};
3300
3301class HBoundsCheck : public HExpression<2> {
3302 public:
3303 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003304 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003305 DCHECK(index->GetType() == Primitive::kPrimInt);
3306 SetRawInputAt(0, index);
3307 SetRawInputAt(1, length);
3308 }
3309
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003310 bool CanBeMoved() const OVERRIDE { return true; }
3311 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003312 UNUSED(other);
3313 return true;
3314 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003315
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003316 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003317
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003318 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003319
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003320 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003321
3322 DECLARE_INSTRUCTION(BoundsCheck);
3323
3324 private:
3325 const uint32_t dex_pc_;
3326
3327 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3328};
3329
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003330/**
3331 * Some DEX instructions are folded into multiple HInstructions that need
3332 * to stay live until the last HInstruction. This class
3333 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003334 * HInstruction stays live. `index` represents the stack location index of the
3335 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003336 */
3337class HTemporary : public HTemplateInstruction<0> {
3338 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003339 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003340
3341 size_t GetIndex() const { return index_; }
3342
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003343 Primitive::Type GetType() const OVERRIDE {
3344 // The previous instruction is the one that will be stored in the temporary location.
3345 DCHECK(GetPrevious() != nullptr);
3346 return GetPrevious()->GetType();
3347 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003348
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003349 DECLARE_INSTRUCTION(Temporary);
3350
3351 private:
3352 const size_t index_;
3353
3354 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3355};
3356
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003357class HSuspendCheck : public HTemplateInstruction<0> {
3358 public:
3359 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003360 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003362 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003363 return true;
3364 }
3365
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003366 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003367 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3368 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003369
3370 DECLARE_INSTRUCTION(SuspendCheck);
3371
3372 private:
3373 const uint32_t dex_pc_;
3374
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003375 // Only used for code generation, in order to share the same slow path between back edges
3376 // of a same loop.
3377 SlowPathCode* slow_path_;
3378
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003379 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3380};
3381
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003382/**
3383 * Instruction to load a Class object.
3384 */
3385class HLoadClass : public HExpression<0> {
3386 public:
3387 HLoadClass(uint16_t type_index,
3388 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003389 uint32_t dex_pc)
3390 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3391 type_index_(type_index),
3392 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003393 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003394 generate_clinit_check_(false),
3395 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003396
3397 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003398
3399 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3400 return other->AsLoadClass()->type_index_ == type_index_;
3401 }
3402
3403 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3404
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003405 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003406 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003407 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003408
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003409 bool NeedsEnvironment() const OVERRIDE {
3410 // Will call runtime and load the class if the class is not loaded yet.
3411 // TODO: finer grain decision.
3412 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003413 }
3414
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003415 bool MustGenerateClinitCheck() const {
3416 return generate_clinit_check_;
3417 }
3418
3419 void SetMustGenerateClinitCheck() {
3420 generate_clinit_check_ = true;
3421 }
3422
3423 bool CanCallRuntime() const {
3424 return MustGenerateClinitCheck() || !is_referrers_class_;
3425 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003426
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003427 bool CanThrow() const OVERRIDE {
3428 // May call runtime and and therefore can throw.
3429 // TODO: finer grain decision.
3430 return !is_referrers_class_;
3431 }
3432
Calin Juravleacf735c2015-02-12 15:25:22 +00003433 ReferenceTypeInfo GetLoadedClassRTI() {
3434 return loaded_class_rti_;
3435 }
3436
3437 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3438 // Make sure we only set exact types (the loaded class should never be merged).
3439 DCHECK(rti.IsExact());
3440 loaded_class_rti_ = rti;
3441 }
3442
3443 bool IsResolved() {
3444 return loaded_class_rti_.IsExact();
3445 }
3446
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003447 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3448
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003449 DECLARE_INSTRUCTION(LoadClass);
3450
3451 private:
3452 const uint16_t type_index_;
3453 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003454 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003455 // Whether this instruction must generate the initialization check.
3456 // Used for code generation.
3457 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003458
Calin Juravleacf735c2015-02-12 15:25:22 +00003459 ReferenceTypeInfo loaded_class_rti_;
3460
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003461 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3462};
3463
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003464class HLoadString : public HExpression<0> {
3465 public:
3466 HLoadString(uint32_t string_index, uint32_t dex_pc)
3467 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3468 string_index_(string_index),
3469 dex_pc_(dex_pc) {}
3470
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003471 bool CanBeMoved() const OVERRIDE { return true; }
3472
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003473 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3474 return other->AsLoadString()->string_index_ == string_index_;
3475 }
3476
3477 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3478
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003479 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003480 uint32_t GetStringIndex() const { return string_index_; }
3481
3482 // TODO: Can we deopt or debug when we resolve a string?
3483 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003484 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003485
3486 DECLARE_INSTRUCTION(LoadString);
3487
3488 private:
3489 const uint32_t string_index_;
3490 const uint32_t dex_pc_;
3491
3492 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3493};
3494
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003495/**
3496 * Performs an initialization check on its Class object input.
3497 */
3498class HClinitCheck : public HExpression<1> {
3499 public:
3500 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003501 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003502 dex_pc_(dex_pc) {
3503 SetRawInputAt(0, constant);
3504 }
3505
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003506 bool CanBeMoved() const OVERRIDE { return true; }
3507 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3508 UNUSED(other);
3509 return true;
3510 }
3511
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003512 bool NeedsEnvironment() const OVERRIDE {
3513 // May call runtime to initialize the class.
3514 return true;
3515 }
3516
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003517 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003518
3519 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3520
3521 DECLARE_INSTRUCTION(ClinitCheck);
3522
3523 private:
3524 const uint32_t dex_pc_;
3525
3526 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3527};
3528
3529class HStaticFieldGet : public HExpression<1> {
3530 public:
3531 HStaticFieldGet(HInstruction* cls,
3532 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003533 MemberOffset field_offset,
3534 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003535 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003536 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003537 SetRawInputAt(0, cls);
3538 }
3539
Calin Juravle52c48962014-12-16 17:02:57 +00003540
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003541 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003542
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003543 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003544 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3545 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003546 }
3547
3548 size_t ComputeHashCode() const OVERRIDE {
3549 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3550 }
3551
Calin Juravle52c48962014-12-16 17:02:57 +00003552 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003553 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3554 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003555 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003556
3557 DECLARE_INSTRUCTION(StaticFieldGet);
3558
3559 private:
3560 const FieldInfo field_info_;
3561
3562 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3563};
3564
3565class HStaticFieldSet : public HTemplateInstruction<2> {
3566 public:
3567 HStaticFieldSet(HInstruction* cls,
3568 HInstruction* value,
3569 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003570 MemberOffset field_offset,
3571 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003572 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003573 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003574 SetRawInputAt(0, cls);
3575 SetRawInputAt(1, value);
3576 }
3577
Calin Juravle52c48962014-12-16 17:02:57 +00003578 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003579 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3580 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003581 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003582
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003583 HInstruction* GetValue() const { return InputAt(1); }
3584
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003585 DECLARE_INSTRUCTION(StaticFieldSet);
3586
3587 private:
3588 const FieldInfo field_info_;
3589
3590 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3591};
3592
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003593// Implement the move-exception DEX instruction.
3594class HLoadException : public HExpression<0> {
3595 public:
3596 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3597
3598 DECLARE_INSTRUCTION(LoadException);
3599
3600 private:
3601 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3602};
3603
3604class HThrow : public HTemplateInstruction<1> {
3605 public:
3606 HThrow(HInstruction* exception, uint32_t dex_pc)
3607 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3608 SetRawInputAt(0, exception);
3609 }
3610
3611 bool IsControlFlow() const OVERRIDE { return true; }
3612
3613 bool NeedsEnvironment() const OVERRIDE { return true; }
3614
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003615 bool CanThrow() const OVERRIDE { return true; }
3616
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003617 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003618
3619 DECLARE_INSTRUCTION(Throw);
3620
3621 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003622 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003623
3624 DISALLOW_COPY_AND_ASSIGN(HThrow);
3625};
3626
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003627class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003628 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003629 HInstanceOf(HInstruction* object,
3630 HLoadClass* constant,
3631 bool class_is_final,
3632 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003633 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3634 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003635 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003636 dex_pc_(dex_pc) {
3637 SetRawInputAt(0, object);
3638 SetRawInputAt(1, constant);
3639 }
3640
3641 bool CanBeMoved() const OVERRIDE { return true; }
3642
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003643 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003644 return true;
3645 }
3646
3647 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003648 return false;
3649 }
3650
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003651 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003652
3653 bool IsClassFinal() const { return class_is_final_; }
3654
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003655 // Used only in code generation.
3656 bool MustDoNullCheck() const { return must_do_null_check_; }
3657 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3658
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003659 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003660
3661 private:
3662 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003663 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003664 const uint32_t dex_pc_;
3665
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003666 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3667};
3668
Calin Juravleb1498f62015-02-16 13:13:29 +00003669class HBoundType : public HExpression<1> {
3670 public:
3671 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3672 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3673 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003674 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003675 SetRawInputAt(0, input);
3676 }
3677
3678 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3679
3680 bool CanBeNull() const OVERRIDE {
3681 // `null instanceof ClassX` always return false so we can't be null.
3682 return false;
3683 }
3684
3685 DECLARE_INSTRUCTION(BoundType);
3686
3687 private:
3688 // Encodes the most upper class that this instruction can have. In other words
3689 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3690 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3691 const ReferenceTypeInfo bound_type_;
3692
3693 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3694};
3695
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003696class HCheckCast : public HTemplateInstruction<2> {
3697 public:
3698 HCheckCast(HInstruction* object,
3699 HLoadClass* constant,
3700 bool class_is_final,
3701 uint32_t dex_pc)
3702 : HTemplateInstruction(SideEffects::None()),
3703 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003704 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003705 dex_pc_(dex_pc) {
3706 SetRawInputAt(0, object);
3707 SetRawInputAt(1, constant);
3708 }
3709
3710 bool CanBeMoved() const OVERRIDE { return true; }
3711
3712 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3713 return true;
3714 }
3715
3716 bool NeedsEnvironment() const OVERRIDE {
3717 // Instruction may throw a CheckCastError.
3718 return true;
3719 }
3720
3721 bool CanThrow() const OVERRIDE { return true; }
3722
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003723 bool MustDoNullCheck() const { return must_do_null_check_; }
3724 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3725
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003726 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003727
3728 bool IsClassFinal() const { return class_is_final_; }
3729
3730 DECLARE_INSTRUCTION(CheckCast);
3731
3732 private:
3733 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003734 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003735 const uint32_t dex_pc_;
3736
3737 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003738};
3739
Calin Juravle27df7582015-04-17 19:12:31 +01003740class HMemoryBarrier : public HTemplateInstruction<0> {
3741 public:
3742 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3743 : HTemplateInstruction(SideEffects::None()),
3744 barrier_kind_(barrier_kind) {}
3745
3746 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3747
3748 DECLARE_INSTRUCTION(MemoryBarrier);
3749
3750 private:
3751 const MemBarrierKind barrier_kind_;
3752
3753 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3754};
3755
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003756class HMonitorOperation : public HTemplateInstruction<1> {
3757 public:
3758 enum OperationKind {
3759 kEnter,
3760 kExit,
3761 };
3762
3763 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3764 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3765 SetRawInputAt(0, object);
3766 }
3767
3768 // Instruction may throw a Java exception, so we need an environment.
3769 bool NeedsEnvironment() const OVERRIDE { return true; }
3770 bool CanThrow() const OVERRIDE { return true; }
3771
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003772 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003773
3774 bool IsEnter() const { return kind_ == kEnter; }
3775
3776 DECLARE_INSTRUCTION(MonitorOperation);
3777
Calin Juravle52c48962014-12-16 17:02:57 +00003778 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003779 const OperationKind kind_;
3780 const uint32_t dex_pc_;
3781
3782 private:
3783 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3784};
3785
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003786class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003787 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003788 MoveOperands(Location source,
3789 Location destination,
3790 Primitive::Type type,
3791 HInstruction* instruction)
3792 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003793
3794 Location GetSource() const { return source_; }
3795 Location GetDestination() const { return destination_; }
3796
3797 void SetSource(Location value) { source_ = value; }
3798 void SetDestination(Location value) { destination_ = value; }
3799
3800 // The parallel move resolver marks moves as "in-progress" by clearing the
3801 // destination (but not the source).
3802 Location MarkPending() {
3803 DCHECK(!IsPending());
3804 Location dest = destination_;
3805 destination_ = Location::NoLocation();
3806 return dest;
3807 }
3808
3809 void ClearPending(Location dest) {
3810 DCHECK(IsPending());
3811 destination_ = dest;
3812 }
3813
3814 bool IsPending() const {
3815 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3816 return destination_.IsInvalid() && !source_.IsInvalid();
3817 }
3818
3819 // True if this blocks a move from the given location.
3820 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003821 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003822 }
3823
3824 // A move is redundant if it's been eliminated, if its source and
3825 // destination are the same, or if its destination is unneeded.
3826 bool IsRedundant() const {
3827 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3828 }
3829
3830 // We clear both operands to indicate move that's been eliminated.
3831 void Eliminate() {
3832 source_ = destination_ = Location::NoLocation();
3833 }
3834
3835 bool IsEliminated() const {
3836 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3837 return source_.IsInvalid();
3838 }
3839
Nicolas Geoffray90218252015-04-15 11:56:51 +01003840 bool Is64BitMove() const {
3841 return Primitive::Is64BitType(type_);
3842 }
3843
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003844 HInstruction* GetInstruction() const { return instruction_; }
3845
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003846 private:
3847 Location source_;
3848 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003849 // The type this move is for.
3850 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003851 // The instruction this move is assocatied with. Null when this move is
3852 // for moving an input in the expected locations of user (including a phi user).
3853 // This is only used in debug mode, to ensure we do not connect interval siblings
3854 // in the same parallel move.
3855 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003856};
3857
3858static constexpr size_t kDefaultNumberOfMoves = 4;
3859
3860class HParallelMove : public HTemplateInstruction<0> {
3861 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003862 explicit HParallelMove(ArenaAllocator* arena)
3863 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003864
Nicolas Geoffray90218252015-04-15 11:56:51 +01003865 void AddMove(Location source,
3866 Location destination,
3867 Primitive::Type type,
3868 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003869 DCHECK(source.IsValid());
3870 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003871 if (kIsDebugBuild) {
3872 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003873 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003874 if (moves_.Get(i).GetInstruction() == instruction) {
3875 // Special case the situation where the move is for the spill slot
3876 // of the instruction.
3877 if ((GetPrevious() == instruction)
3878 || ((GetPrevious() == nullptr)
3879 && instruction->IsPhi()
3880 && instruction->GetBlock() == GetBlock())) {
3881 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3882 << "Doing parallel moves for the same instruction.";
3883 } else {
3884 DCHECK(false) << "Doing parallel moves for the same instruction.";
3885 }
3886 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003887 }
3888 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003889 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003890 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3891 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003892 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003893 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003894 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003895 }
3896
3897 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003898 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003899 }
3900
3901 size_t NumMoves() const { return moves_.Size(); }
3902
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003903 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003904
3905 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003906 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003907
3908 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3909};
3910
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003911class HGraphVisitor : public ValueObject {
3912 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003913 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3914 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003915
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003916 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003917 virtual void VisitBasicBlock(HBasicBlock* block);
3918
Roland Levillain633021e2014-10-01 14:12:25 +01003919 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003920 void VisitInsertionOrder();
3921
Roland Levillain633021e2014-10-01 14:12:25 +01003922 // Visit the graph following dominator tree reverse post-order.
3923 void VisitReversePostOrder();
3924
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003925 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003926
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003927 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003928#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003929 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3930
3931 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3932
3933#undef DECLARE_VISIT_INSTRUCTION
3934
3935 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003936 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003937
3938 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3939};
3940
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003941class HGraphDelegateVisitor : public HGraphVisitor {
3942 public:
3943 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3944 virtual ~HGraphDelegateVisitor() {}
3945
3946 // Visit functions that delegate to to super class.
3947#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003948 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003949
3950 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3951
3952#undef DECLARE_VISIT_INSTRUCTION
3953
3954 private:
3955 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3956};
3957
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003958class HInsertionOrderIterator : public ValueObject {
3959 public:
3960 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3961
3962 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3963 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3964 void Advance() { ++index_; }
3965
3966 private:
3967 const HGraph& graph_;
3968 size_t index_;
3969
3970 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3971};
3972
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003973class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003974 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003975 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3976 // Check that reverse post order of the graph has been built.
3977 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3978 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003979
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003980 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3981 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003982 void Advance() { ++index_; }
3983
3984 private:
3985 const HGraph& graph_;
3986 size_t index_;
3987
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003988 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003989};
3990
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003991class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003992 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003993 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003994 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3995 // Check that reverse post order of the graph has been built.
3996 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3997 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003998
3999 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004000 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004001 void Advance() { --index_; }
4002
4003 private:
4004 const HGraph& graph_;
4005 size_t index_;
4006
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004007 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004008};
4009
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004010class HLinearPostOrderIterator : public ValueObject {
4011 public:
4012 explicit HLinearPostOrderIterator(const HGraph& graph)
4013 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4014
4015 bool Done() const { return index_ == 0; }
4016
4017 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4018
4019 void Advance() {
4020 --index_;
4021 DCHECK_GE(index_, 0U);
4022 }
4023
4024 private:
4025 const GrowableArray<HBasicBlock*>& order_;
4026 size_t index_;
4027
4028 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4029};
4030
4031class HLinearOrderIterator : public ValueObject {
4032 public:
4033 explicit HLinearOrderIterator(const HGraph& graph)
4034 : order_(graph.GetLinearOrder()), index_(0) {}
4035
4036 bool Done() const { return index_ == order_.Size(); }
4037 HBasicBlock* Current() const { return order_.Get(index_); }
4038 void Advance() { ++index_; }
4039
4040 private:
4041 const GrowableArray<HBasicBlock*>& order_;
4042 size_t index_;
4043
4044 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4045};
4046
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004047// Iterator over the blocks that art part of the loop. Includes blocks part
4048// of an inner loop. The order in which the blocks are iterated is on their
4049// block id.
4050class HBlocksInLoopIterator : public ValueObject {
4051 public:
4052 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4053 : blocks_in_loop_(info.GetBlocks()),
4054 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4055 index_(0) {
4056 if (!blocks_in_loop_.IsBitSet(index_)) {
4057 Advance();
4058 }
4059 }
4060
4061 bool Done() const { return index_ == blocks_.Size(); }
4062 HBasicBlock* Current() const { return blocks_.Get(index_); }
4063 void Advance() {
4064 ++index_;
4065 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4066 if (blocks_in_loop_.IsBitSet(index_)) {
4067 break;
4068 }
4069 }
4070 }
4071
4072 private:
4073 const BitVector& blocks_in_loop_;
4074 const GrowableArray<HBasicBlock*>& blocks_;
4075 size_t index_;
4076
4077 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4078};
4079
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004080inline int64_t Int64FromConstant(HConstant* constant) {
4081 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4082 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4083 : constant->AsLongConstant()->GetValue();
4084}
4085
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004086} // namespace art
4087
4088#endif // ART_COMPILER_OPTIMIZING_NODES_H_