blob: 680fb477e26493d0818aea4306760243641e759e [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,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100123 bool should_generate_constructor_barrier,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100124 bool debuggable = false,
125 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000126 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000127 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100128 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100129 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700130 entry_block_(nullptr),
131 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100132 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100133 number_of_vregs_(0),
134 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000135 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400136 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000137 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000138 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100139 dex_file_(dex_file),
140 method_idx_(method_idx),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100141 should_generate_constructor_barrier_(should_generate_constructor_barrier),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000142 cached_null_constant_(nullptr),
143 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000144 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
145 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
146 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000148 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100149 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100150 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000151
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000152 HBasicBlock* GetEntryBlock() const { return entry_block_; }
153 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000154
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000155 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
156 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000157
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000158 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 // Try building the SSA form of this graph, with dominance computation and loop
161 // recognition. Returns whether it was successful in doing all these steps.
162 bool TryBuildingSsa() {
163 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000164 // The SSA builder requires loops to all be natural. Specifically, the dead phi
165 // elimination phase checks the consistency of the graph when doing a post-order
166 // visit for eliminating dead phis: a dead phi can only have loop header phi
167 // users remaining when being visited.
168 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000169 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000170 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 }
172
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100175 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000177 // Analyze all natural loops in this graph. Returns false if one
178 // loop is not natural, that is the header does not dominate the
179 // back edge.
180 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
183 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
184
David Brazdil2d7352b2015-04-20 14:52:42 +0100185 // Removes `block` from the graph.
186 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000187
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100188 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
189 void SimplifyLoop(HBasicBlock* header);
190
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000191 int32_t GetNextInstructionId() {
192 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000193 return current_instruction_id_++;
194 }
195
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000196 int32_t GetCurrentInstructionId() const {
197 return current_instruction_id_;
198 }
199
200 void SetCurrentInstructionId(int32_t id) {
201 current_instruction_id_ = id;
202 }
203
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100204 uint16_t GetMaximumNumberOfOutVRegs() const {
205 return maximum_number_of_out_vregs_;
206 }
207
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000208 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
209 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100210 }
211
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000212 void UpdateTemporariesVRegSlots(size_t slots) {
213 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100214 }
215
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000216 size_t GetTemporariesVRegSlots() const {
217 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100218 }
219
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100220 void SetNumberOfVRegs(uint16_t number_of_vregs) {
221 number_of_vregs_ = number_of_vregs;
222 }
223
224 uint16_t GetNumberOfVRegs() const {
225 return number_of_vregs_;
226 }
227
228 void SetNumberOfInVRegs(uint16_t value) {
229 number_of_in_vregs_ = value;
230 }
231
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100232 uint16_t GetNumberOfLocalVRegs() const {
233 return number_of_vregs_ - number_of_in_vregs_;
234 }
235
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100236 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
237 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100238 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100239
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100240 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
241 return linear_order_;
242 }
243
Mark Mendell1152c922015-04-24 17:06:35 -0400244 bool HasBoundsChecks() const {
245 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800246 }
247
Mark Mendell1152c922015-04-24 17:06:35 -0400248 void SetHasBoundsChecks(bool value) {
249 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800250 }
251
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100252 bool ShouldGenerateConstructorBarrier() const {
253 return should_generate_constructor_barrier_;
254 }
255
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000256 bool IsDebuggable() const { return debuggable_; }
257
David Brazdil8d5b8b22015-03-24 10:51:52 +0000258 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000259 // already, it is created and inserted into the graph. This method is only for
260 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000261 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000262 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000263 HIntConstant* GetIntConstant(int32_t value) {
264 return CreateConstant(value, &cached_int_constants_);
265 }
266 HLongConstant* GetLongConstant(int64_t value) {
267 return CreateConstant(value, &cached_long_constants_);
268 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000269 HFloatConstant* GetFloatConstant(float value) {
270 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
271 }
272 HDoubleConstant* GetDoubleConstant(double value) {
273 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
274 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000275
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000276 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100277
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100278 const DexFile& GetDexFile() const {
279 return dex_file_;
280 }
281
282 uint32_t GetMethodIdx() const {
283 return method_idx_;
284 }
285
David Brazdil2d7352b2015-04-20 14:52:42 +0100286 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000287 void VisitBlockForDominatorTree(HBasicBlock* block,
288 HBasicBlock* predecessor,
289 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100290 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000291 void VisitBlockForBackEdges(HBasicBlock* block,
292 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100293 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000294 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100295 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000296
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000297 template <class InstructionType, typename ValueType>
298 InstructionType* CreateConstant(ValueType value,
299 ArenaSafeMap<ValueType, InstructionType*>* cache) {
300 // Try to find an existing constant of the given value.
301 InstructionType* constant = nullptr;
302 auto cached_constant = cache->find(value);
303 if (cached_constant != cache->end()) {
304 constant = cached_constant->second;
305 }
306
307 // If not found or previously deleted, create and cache a new instruction.
308 if (constant == nullptr || constant->GetBlock() == nullptr) {
309 constant = new (arena_) InstructionType(value);
310 cache->Overwrite(value, constant);
311 InsertConstant(constant);
312 }
313 return constant;
314 }
315
David Brazdil8d5b8b22015-03-24 10:51:52 +0000316 void InsertConstant(HConstant* instruction);
317
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000318 // Cache a float constant into the graph. This method should only be
319 // called by the SsaBuilder when creating "equivalent" instructions.
320 void CacheFloatConstant(HFloatConstant* constant);
321
322 // See CacheFloatConstant comment.
323 void CacheDoubleConstant(HDoubleConstant* constant);
324
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000325 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326
327 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000328 GrowableArray<HBasicBlock*> blocks_;
329
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100330 // List of blocks to perform a reverse post order tree traversal.
331 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000332
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100333 // List of blocks to perform a linear order tree traversal.
334 GrowableArray<HBasicBlock*> linear_order_;
335
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000336 HBasicBlock* entry_block_;
337 HBasicBlock* exit_block_;
338
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100339 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100340 uint16_t maximum_number_of_out_vregs_;
341
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100342 // The number of virtual registers in this method. Contains the parameters.
343 uint16_t number_of_vregs_;
344
345 // The number of virtual registers used by parameters of this method.
346 uint16_t number_of_in_vregs_;
347
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000348 // Number of vreg size slots that the temporaries use (used in baseline compiler).
349 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100350
Mark Mendell1152c922015-04-24 17:06:35 -0400351 // Has bounds checks. We can totally skip BCE if it's false.
352 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800353
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000354 // Indicates whether the graph should be compiled in a way that
355 // ensures full debuggability. If false, we can apply more
356 // aggressive optimizations that may limit the level of debugging.
357 const bool debuggable_;
358
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000359 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000360 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000361
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100362 // The dex file from which the method is from.
363 const DexFile& dex_file_;
364
365 // The method index in the dex file.
366 const uint32_t method_idx_;
367
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100368 const bool should_generate_constructor_barrier_;
369
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000370 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000371 HNullConstant* cached_null_constant_;
372 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000373 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000374 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000375 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000376
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000377 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100378 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000379 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000380 DISALLOW_COPY_AND_ASSIGN(HGraph);
381};
382
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700383class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000384 public:
385 HLoopInformation(HBasicBlock* header, HGraph* graph)
386 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100387 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100389 // Make bit vector growable, as the number of blocks may change.
390 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100391
392 HBasicBlock* GetHeader() const {
393 return header_;
394 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000395
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000396 void SetHeader(HBasicBlock* block) {
397 header_ = block;
398 }
399
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100400 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
401 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
402 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
403
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000404 void AddBackEdge(HBasicBlock* back_edge) {
405 back_edges_.Add(back_edge);
406 }
407
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100408 void RemoveBackEdge(HBasicBlock* back_edge) {
409 back_edges_.Delete(back_edge);
410 }
411
David Brazdil46e2a392015-03-16 17:31:52 +0000412 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100413 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000414 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100415 }
416 return false;
417 }
418
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000419 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000420 return back_edges_.Size();
421 }
422
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100423 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100424
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100425 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
426 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100427 }
428
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100429 // Returns the lifetime position of the back edge that has the
430 // greatest lifetime position.
431 size_t GetLifetimeEnd() const;
432
433 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
434 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
435 if (back_edges_.Get(i) == existing) {
436 back_edges_.Put(i, new_back_edge);
437 return;
438 }
439 }
440 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100441 }
442
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100443 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100444 // that is the header dominates the back edge.
445 bool Populate();
446
David Brazdila4b8c212015-05-07 09:59:30 +0100447 // Reanalyzes the loop by removing loop info from its blocks and re-running
448 // Populate(). If there are no back edges left, the loop info is completely
449 // removed as well as its SuspendCheck instruction. It must be run on nested
450 // inner loops first.
451 void Update();
452
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100453 // Returns whether this loop information contains `block`.
454 // Note that this loop information *must* be populated before entering this function.
455 bool Contains(const HBasicBlock& block) const;
456
457 // Returns whether this loop information is an inner loop of `other`.
458 // Note that `other` *must* be populated before entering this function.
459 bool IsIn(const HLoopInformation& other) const;
460
461 const ArenaBitVector& GetBlocks() const { return blocks_; }
462
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000463 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000464 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000465
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000466 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100467 // Internal recursive implementation of `Populate`.
468 void PopulateRecursive(HBasicBlock* block);
469
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000470 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100471 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000472 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100473 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000474
475 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
476};
477
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100478static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100479static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100480
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000481// A block in a method. Contains the list of instructions represented
482// as a double linked list. Each block knows its predecessors and
483// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100484
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700485class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000486 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100487 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000488 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000489 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
490 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000491 loop_information_(nullptr),
492 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100493 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100494 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100495 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100496 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000497 lifetime_end_(kNoLifetime),
498 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000499
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100500 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
501 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000502 }
503
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100504 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
505 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000506 }
507
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100508 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
509 return dominated_blocks_;
510 }
511
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100512 bool IsEntryBlock() const {
513 return graph_->GetEntryBlock() == this;
514 }
515
516 bool IsExitBlock() const {
517 return graph_->GetExitBlock() == this;
518 }
519
David Brazdil46e2a392015-03-16 17:31:52 +0000520 bool IsSingleGoto() const;
521
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000522 void AddBackEdge(HBasicBlock* back_edge) {
523 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000524 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000525 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100526 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000527 loop_information_->AddBackEdge(back_edge);
528 }
529
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000530 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000531 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000532
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000533 int GetBlockId() const { return block_id_; }
534 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000535
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000536 HBasicBlock* GetDominator() const { return dominator_; }
537 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100538 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100539 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000540 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
541 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
542 if (dominated_blocks_.Get(i) == existing) {
543 dominated_blocks_.Put(i, new_block);
544 return;
545 }
546 }
547 LOG(FATAL) << "Unreachable";
548 UNREACHABLE();
549 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000550
551 int NumberOfBackEdges() const {
552 return loop_information_ == nullptr
553 ? 0
554 : loop_information_->NumberOfBackEdges();
555 }
556
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100557 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
558 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100559 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100560 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100561 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
562 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000563
564 void AddSuccessor(HBasicBlock* block) {
565 successors_.Add(block);
566 block->predecessors_.Add(this);
567 }
568
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100569 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
570 size_t successor_index = GetSuccessorIndexOf(existing);
571 DCHECK_NE(successor_index, static_cast<size_t>(-1));
572 existing->RemovePredecessor(this);
573 new_block->predecessors_.Add(this);
574 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000575 }
576
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000577 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
578 size_t predecessor_index = GetPredecessorIndexOf(existing);
579 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
580 existing->RemoveSuccessor(this);
581 new_block->successors_.Add(this);
582 predecessors_.Put(predecessor_index, new_block);
583 }
584
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100585 void RemovePredecessor(HBasicBlock* block) {
586 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100587 }
588
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000589 void RemoveSuccessor(HBasicBlock* block) {
590 successors_.Delete(block);
591 }
592
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100593 void ClearAllPredecessors() {
594 predecessors_.Reset();
595 }
596
597 void AddPredecessor(HBasicBlock* block) {
598 predecessors_.Add(block);
599 block->successors_.Add(this);
600 }
601
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100602 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100603 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100604 HBasicBlock* temp = predecessors_.Get(0);
605 predecessors_.Put(0, predecessors_.Get(1));
606 predecessors_.Put(1, temp);
607 }
608
David Brazdil769c9e52015-04-27 13:54:09 +0100609 void SwapSuccessors() {
610 DCHECK_EQ(successors_.Size(), 2u);
611 HBasicBlock* temp = successors_.Get(0);
612 successors_.Put(0, successors_.Get(1));
613 successors_.Put(1, temp);
614 }
615
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100616 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
617 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
618 if (predecessors_.Get(i) == predecessor) {
619 return i;
620 }
621 }
622 return -1;
623 }
624
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100625 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
626 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
627 if (successors_.Get(i) == successor) {
628 return i;
629 }
630 }
631 return -1;
632 }
633
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000634 // Split the block into two blocks just after `cursor`. Returns the newly
635 // created block. Note that this method just updates raw block information,
636 // like predecessors, successors, dominators, and instruction list. It does not
637 // update the graph, reverse post order, loop information, nor make sure the
638 // blocks are consistent (for example ending with a control flow instruction).
639 HBasicBlock* SplitAfter(HInstruction* cursor);
640
641 // Merge `other` at the end of `this`. Successors and dominated blocks of
642 // `other` are changed to be successors and dominated blocks of `this`. Note
643 // that this method does not update the graph, reverse post order, loop
644 // information, nor make sure the blocks are consistent (for example ending
645 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100646 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000647
648 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
649 // of `this` are moved to `other`.
650 // Note that this method does not update the graph, reverse post order, loop
651 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000652 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000653 void ReplaceWith(HBasicBlock* other);
654
David Brazdil2d7352b2015-04-20 14:52:42 +0100655 // Merge `other` at the end of `this`. This method updates loops, reverse post
656 // order, links to predecessors, successors, dominators and deletes the block
657 // from the graph. The two blocks must be successive, i.e. `this` the only
658 // predecessor of `other` and vice versa.
659 void MergeWith(HBasicBlock* other);
660
661 // Disconnects `this` from all its predecessors, successors and dominator,
662 // removes it from all loops it is included in and eventually from the graph.
663 // The block must not dominate any other block. Predecessors and successors
664 // are safely updated.
665 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000666
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000667 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100668 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100669 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100670 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100671 // Replace instruction `initial` with `replacement` within this block.
672 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
673 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100675 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000676 // RemoveInstruction and RemovePhi delete a given instruction from the respective
677 // instruction list. With 'ensure_safety' set to true, it verifies that the
678 // instruction is not in use and removes it from the use lists of its inputs.
679 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
680 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100681 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682
683 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100684 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100685 }
686
Roland Levillain6b879dd2014-09-22 17:13:44 +0100687 bool IsLoopPreHeaderFirstPredecessor() const {
688 DCHECK(IsLoopHeader());
689 DCHECK(!GetPredecessors().IsEmpty());
690 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
691 }
692
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100693 HLoopInformation* GetLoopInformation() const {
694 return loop_information_;
695 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000696
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000697 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100698 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000699 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100700 void SetInLoop(HLoopInformation* info) {
701 if (IsLoopHeader()) {
702 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100703 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100704 loop_information_ = info;
705 } else if (loop_information_->Contains(*info->GetHeader())) {
706 // Block is currently part of an outer loop. Make it part of this inner loop.
707 // Note that a non loop header having a loop information means this loop information
708 // has already been populated
709 loop_information_ = info;
710 } else {
711 // Block is part of an inner loop. Do not update the loop information.
712 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
713 // at this point, because this method is being called while populating `info`.
714 }
715 }
716
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000717 // Raw update of the loop information.
718 void SetLoopInformation(HLoopInformation* info) {
719 loop_information_ = info;
720 }
721
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100722 bool IsInLoop() const { return loop_information_ != nullptr; }
723
David Brazdila4b8c212015-05-07 09:59:30 +0100724 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100725 bool Dominates(HBasicBlock* block) const;
726
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100727 size_t GetLifetimeStart() const { return lifetime_start_; }
728 size_t GetLifetimeEnd() const { return lifetime_end_; }
729
730 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
731 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
732
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100733 uint32_t GetDexPc() const { return dex_pc_; }
734
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000735 bool IsCatchBlock() const { return is_catch_block_; }
736 void SetIsCatchBlock() { is_catch_block_ = true; }
737
David Brazdil8d5b8b22015-03-24 10:51:52 +0000738 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000739 bool EndsWithIf() const;
740 bool HasSinglePhi() const;
741
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000743 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000744 GrowableArray<HBasicBlock*> predecessors_;
745 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100746 HInstructionList instructions_;
747 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000748 HLoopInformation* loop_information_;
749 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100750 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000751 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100752 // The dex program counter of the first instruction of this block.
753 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100754 size_t lifetime_start_;
755 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000756 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000757
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000758 friend class HGraph;
759 friend class HInstruction;
760
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000761 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
762};
763
David Brazdilb2bd1c52015-03-25 11:17:37 +0000764// Iterates over the LoopInformation of all loops which contain 'block'
765// from the innermost to the outermost.
766class HLoopInformationOutwardIterator : public ValueObject {
767 public:
768 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
769 : current_(block.GetLoopInformation()) {}
770
771 bool Done() const { return current_ == nullptr; }
772
773 void Advance() {
774 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100775 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000776 }
777
778 HLoopInformation* Current() const {
779 DCHECK(!Done());
780 return current_;
781 }
782
783 private:
784 HLoopInformation* current_;
785
786 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
787};
788
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100789#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
790 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000791 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000792 M(ArrayGet, Instruction) \
793 M(ArrayLength, Instruction) \
794 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100795 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000796 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000797 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000798 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100799 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000800 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100801 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700802 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000803 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000804 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000805 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100806 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000807 M(Exit, Instruction) \
808 M(FloatConstant, Constant) \
809 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100810 M(GreaterThan, Condition) \
811 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100812 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000813 M(InstanceFieldGet, Instruction) \
814 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000815 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100816 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000817 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000818 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100819 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000820 M(LessThan, Condition) \
821 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000822 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000823 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100824 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000825 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100826 M(Local, Instruction) \
827 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100828 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000829 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000830 M(Mul, BinaryOperation) \
831 M(Neg, UnaryOperation) \
832 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100833 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100834 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000835 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000836 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000837 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000838 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100839 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000840 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100841 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000842 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100843 M(Return, Instruction) \
844 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000845 M(Shl, BinaryOperation) \
846 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100847 M(StaticFieldGet, Instruction) \
848 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100849 M(StoreLocal, Instruction) \
850 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100851 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000852 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000853 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000854 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000855 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000856 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000857
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100858#define FOR_EACH_INSTRUCTION(M) \
859 FOR_EACH_CONCRETE_INSTRUCTION(M) \
860 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100861 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100862 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100863 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700864
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100865#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000866FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
867#undef FORWARD_DECLARATION
868
Roland Levillainccc07a92014-09-16 14:48:16 +0100869#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000870 InstructionKind GetKind() const OVERRIDE { return k##type; } \
871 const char* DebugName() const OVERRIDE { return #type; } \
872 const H##type* As##type() const OVERRIDE { return this; } \
873 H##type* As##type() OVERRIDE { return this; } \
874 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100875 return other->Is##type(); \
876 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000877 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000878
David Brazdiled596192015-01-23 10:39:45 +0000879template <typename T> class HUseList;
880
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700882class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000883 public:
David Brazdiled596192015-01-23 10:39:45 +0000884 HUseListNode* GetPrevious() const { return prev_; }
885 HUseListNode* GetNext() const { return next_; }
886 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100888 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100889
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000890 private:
David Brazdiled596192015-01-23 10:39:45 +0000891 HUseListNode(T user, size_t index)
892 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
893
894 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100895 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000896 HUseListNode<T>* prev_;
897 HUseListNode<T>* next_;
898
899 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000900
901 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
902};
903
David Brazdiled596192015-01-23 10:39:45 +0000904template <typename T>
905class HUseList : public ValueObject {
906 public:
907 HUseList() : first_(nullptr) {}
908
909 void Clear() {
910 first_ = nullptr;
911 }
912
913 // Adds a new entry at the beginning of the use list and returns
914 // the newly created node.
915 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000916 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000917 if (IsEmpty()) {
918 first_ = new_node;
919 } else {
920 first_->prev_ = new_node;
921 new_node->next_ = first_;
922 first_ = new_node;
923 }
924 return new_node;
925 }
926
927 HUseListNode<T>* GetFirst() const {
928 return first_;
929 }
930
931 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000932 DCHECK(node != nullptr);
933 DCHECK(Contains(node));
934
David Brazdiled596192015-01-23 10:39:45 +0000935 if (node->prev_ != nullptr) {
936 node->prev_->next_ = node->next_;
937 }
938 if (node->next_ != nullptr) {
939 node->next_->prev_ = node->prev_;
940 }
941 if (node == first_) {
942 first_ = node->next_;
943 }
944 }
945
David Brazdil1abb4192015-02-17 18:33:36 +0000946 bool Contains(const HUseListNode<T>* node) const {
947 if (node == nullptr) {
948 return false;
949 }
950 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
951 if (current == node) {
952 return true;
953 }
954 }
955 return false;
956 }
957
David Brazdiled596192015-01-23 10:39:45 +0000958 bool IsEmpty() const {
959 return first_ == nullptr;
960 }
961
962 bool HasOnlyOneUse() const {
963 return first_ != nullptr && first_->next_ == nullptr;
964 }
965
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100966 size_t SizeSlow() const {
967 size_t count = 0;
968 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
969 ++count;
970 }
971 return count;
972 }
973
David Brazdiled596192015-01-23 10:39:45 +0000974 private:
975 HUseListNode<T>* first_;
976};
977
978template<typename T>
979class HUseIterator : public ValueObject {
980 public:
981 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
982
983 bool Done() const { return current_ == nullptr; }
984
985 void Advance() {
986 DCHECK(!Done());
987 current_ = current_->GetNext();
988 }
989
990 HUseListNode<T>* Current() const {
991 DCHECK(!Done());
992 return current_;
993 }
994
995 private:
996 HUseListNode<T>* current_;
997
998 friend class HValue;
999};
1000
David Brazdil1abb4192015-02-17 18:33:36 +00001001// This class is used by HEnvironment and HInstruction classes to record the
1002// instructions they use and pointers to the corresponding HUseListNodes kept
1003// by the used instructions.
1004template <typename T>
1005class HUserRecord : public ValueObject {
1006 public:
1007 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1008 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1009
1010 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1011 : instruction_(old_record.instruction_), use_node_(use_node) {
1012 DCHECK(instruction_ != nullptr);
1013 DCHECK(use_node_ != nullptr);
1014 DCHECK(old_record.use_node_ == nullptr);
1015 }
1016
1017 HInstruction* GetInstruction() const { return instruction_; }
1018 HUseListNode<T>* GetUseNode() const { return use_node_; }
1019
1020 private:
1021 // Instruction used by the user.
1022 HInstruction* instruction_;
1023
1024 // Corresponding entry in the use list kept by 'instruction_'.
1025 HUseListNode<T>* use_node_;
1026};
1027
Calin Juravle27df7582015-04-17 19:12:31 +01001028// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1029// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1030// flag is consider.
1031// - DependsOn suggests that there is a real dependency between side effects but it only
1032// checks DependendsOnSomething flag.
1033//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001034// Represents the side effects an instruction may have.
1035class SideEffects : public ValueObject {
1036 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001037 SideEffects() : flags_(0) {}
1038
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001039 static SideEffects None() {
1040 return SideEffects(0);
1041 }
1042
1043 static SideEffects All() {
1044 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1045 }
1046
1047 static SideEffects ChangesSomething() {
1048 return SideEffects((1 << kFlagChangesCount) - 1);
1049 }
1050
1051 static SideEffects DependsOnSomething() {
1052 int count = kFlagDependsOnCount - kFlagChangesCount;
1053 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1054 }
1055
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001056 SideEffects Union(SideEffects other) const {
1057 return SideEffects(flags_ | other.flags_);
1058 }
1059
Roland Levillain72bceff2014-09-15 18:29:00 +01001060 bool HasSideEffects() const {
1061 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1062 return (flags_ & all_bits_set) != 0;
1063 }
1064
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001065 bool HasAllSideEffects() const {
1066 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1067 return all_bits_set == (flags_ & all_bits_set);
1068 }
1069
1070 bool DependsOn(SideEffects other) const {
1071 size_t depends_flags = other.ComputeDependsFlags();
1072 return (flags_ & depends_flags) != 0;
1073 }
1074
1075 bool HasDependencies() const {
1076 int count = kFlagDependsOnCount - kFlagChangesCount;
1077 size_t all_bits_set = (1 << count) - 1;
1078 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1079 }
1080
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001081 private:
1082 static constexpr int kFlagChangesSomething = 0;
1083 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1084
1085 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1086 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1087
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001088 explicit SideEffects(size_t flags) : flags_(flags) {}
1089
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001090 size_t ComputeDependsFlags() const {
1091 return flags_ << kFlagChangesCount;
1092 }
1093
1094 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001095};
1096
David Brazdiled596192015-01-23 10:39:45 +00001097// A HEnvironment object contains the values of virtual registers at a given location.
1098class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1099 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001100 HEnvironment(ArenaAllocator* arena,
1101 size_t number_of_vregs,
1102 const DexFile& dex_file,
1103 uint32_t method_idx,
1104 uint32_t dex_pc)
1105 : vregs_(arena, number_of_vregs),
1106 locations_(arena, number_of_vregs),
1107 parent_(nullptr),
1108 dex_file_(dex_file),
1109 method_idx_(method_idx),
1110 dex_pc_(dex_pc) {
David Brazdiled596192015-01-23 10:39:45 +00001111 vregs_.SetSize(number_of_vregs);
1112 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001113 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001114 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001115
1116 locations_.SetSize(number_of_vregs);
1117 for (size_t i = 0; i < number_of_vregs; ++i) {
1118 locations_.Put(i, Location());
1119 }
1120 }
1121
1122 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
1123 parent_ = new (allocator) HEnvironment(allocator,
1124 parent->Size(),
1125 parent->GetDexFile(),
1126 parent->GetMethodIdx(),
1127 parent->GetDexPc());
1128 if (parent->GetParent() != nullptr) {
1129 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1130 }
1131 parent_->CopyFrom(parent);
David Brazdiled596192015-01-23 10:39:45 +00001132 }
1133
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001134 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1135 void CopyFrom(HEnvironment* environment);
1136
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001137 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1138 // input to the loop phi instead. This is for inserting instructions that
1139 // require an environment (like HDeoptimization) in the loop pre-header.
1140 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001141
1142 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001143 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001144 }
1145
1146 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001147 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001148 }
1149
David Brazdil1abb4192015-02-17 18:33:36 +00001150 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001151
1152 size_t Size() const { return vregs_.Size(); }
1153
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001154 HEnvironment* GetParent() const { return parent_; }
1155
1156 void SetLocationAt(size_t index, Location location) {
1157 locations_.Put(index, location);
1158 }
1159
1160 Location GetLocationAt(size_t index) const {
1161 return locations_.Get(index);
1162 }
1163
1164 uint32_t GetDexPc() const {
1165 return dex_pc_;
1166 }
1167
1168 uint32_t GetMethodIdx() const {
1169 return method_idx_;
1170 }
1171
1172 const DexFile& GetDexFile() const {
1173 return dex_file_;
1174 }
1175
David Brazdiled596192015-01-23 10:39:45 +00001176 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001177 // Record instructions' use entries of this environment for constant-time removal.
1178 // It should only be called by HInstruction when a new environment use is added.
1179 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1180 DCHECK(env_use->GetUser() == this);
1181 size_t index = env_use->GetIndex();
1182 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1183 }
David Brazdiled596192015-01-23 10:39:45 +00001184
David Brazdil1abb4192015-02-17 18:33:36 +00001185 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001186 GrowableArray<Location> locations_;
1187 HEnvironment* parent_;
1188 const DexFile& dex_file_;
1189 const uint32_t method_idx_;
1190 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001191
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001192 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001193
1194 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1195};
1196
Calin Juravleacf735c2015-02-12 15:25:22 +00001197class ReferenceTypeInfo : ValueObject {
1198 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001199 typedef Handle<mirror::Class> TypeHandle;
1200
1201 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1203 if (type_handle->IsObjectClass()) {
1204 // Override the type handle to be consistent with the case when we get to
1205 // Top but don't have the Object class available. It avoids having to guess
1206 // what value the type_handle has when it's Top.
1207 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1208 } else {
1209 return ReferenceTypeInfo(type_handle, is_exact, false);
1210 }
1211 }
1212
1213 static ReferenceTypeInfo CreateTop(bool is_exact) {
1214 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001215 }
1216
1217 bool IsExact() const { return is_exact_; }
1218 bool IsTop() const { return is_top_; }
1219
1220 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1221
Calin Juravleb1498f62015-02-16 13:13:29 +00001222 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001223 if (IsTop()) {
1224 // Top (equivalent for java.lang.Object) is supertype of anything.
1225 return true;
1226 }
1227 if (rti.IsTop()) {
1228 // If we get here `this` is not Top() so it can't be a supertype.
1229 return false;
1230 }
1231 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1232 }
1233
1234 // Returns true if the type information provide the same amount of details.
1235 // Note that it does not mean that the instructions have the same actual type
1236 // (e.g. tops are equal but they can be the result of a merge).
1237 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1238 if (IsExact() != rti.IsExact()) {
1239 return false;
1240 }
1241 if (IsTop() && rti.IsTop()) {
1242 // `Top` means java.lang.Object, so the types are equivalent.
1243 return true;
1244 }
1245 if (IsTop() || rti.IsTop()) {
1246 // If only one is top or object than they are not equivalent.
1247 // NB: We need this extra check because the type_handle of `Top` is invalid
1248 // and we cannot inspect its reference.
1249 return false;
1250 }
1251
1252 // Finally check the types.
1253 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1254 }
1255
1256 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001257 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1258 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1259 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1260
Calin Juravleacf735c2015-02-12 15:25:22 +00001261 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001262 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001263 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001264 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001265 bool is_exact_;
1266 // A true value here means that the object type should be java.lang.Object.
1267 // We don't have access to the corresponding mirror object every time so this
1268 // flag acts as a substitute. When true, the TypeHandle refers to a null
1269 // pointer and should not be used.
1270 bool is_top_;
1271};
1272
1273std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1274
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001275class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001276 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001277 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001278 : previous_(nullptr),
1279 next_(nullptr),
1280 block_(nullptr),
1281 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001282 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001283 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001284 locations_(nullptr),
1285 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001286 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001287 side_effects_(side_effects),
1288 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001289
Dave Allison20dfc792014-06-16 20:44:29 -07001290 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001291
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001292#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001293 enum InstructionKind {
1294 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1295 };
1296#undef DECLARE_KIND
1297
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001298 HInstruction* GetNext() const { return next_; }
1299 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001300
Calin Juravle77520bc2015-01-12 18:45:46 +00001301 HInstruction* GetNextDisregardingMoves() const;
1302 HInstruction* GetPreviousDisregardingMoves() const;
1303
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001304 HBasicBlock* GetBlock() const { return block_; }
1305 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001306 bool IsInBlock() const { return block_ != nullptr; }
1307 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001308 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001309
Roland Levillain6b879dd2014-09-22 17:13:44 +01001310 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001311 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001312
1313 virtual void Accept(HGraphVisitor* visitor) = 0;
1314 virtual const char* DebugName() const = 0;
1315
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001316 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001317 void SetRawInputAt(size_t index, HInstruction* input) {
1318 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1319 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001320
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001321 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001322 virtual uint32_t GetDexPc() const {
1323 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1324 " does not need an environment";
1325 UNREACHABLE();
1326 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001327 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001328 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001329 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001330
Calin Juravle10e244f2015-01-26 18:54:32 +00001331 // Does not apply for all instructions, but having this at top level greatly
1332 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001333 virtual bool CanBeNull() const {
1334 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1335 return true;
1336 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001337
Calin Juravle641547a2015-04-21 22:08:51 +01001338 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1339 UNUSED(obj);
1340 return false;
1341 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001342
Calin Juravleacf735c2015-02-12 15:25:22 +00001343 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001344 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001345 reference_type_info_ = reference_type_info;
1346 }
1347
Calin Juravle61d544b2015-02-23 16:46:57 +00001348 ReferenceTypeInfo GetReferenceTypeInfo() const {
1349 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1350 return reference_type_info_;
1351 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001352
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001354 DCHECK(user != nullptr);
1355 HUseListNode<HInstruction*>* use =
1356 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1357 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001358 }
1359
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001360 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001361 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001362 HUseListNode<HEnvironment*>* env_use =
1363 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1364 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001365 }
1366
David Brazdil1abb4192015-02-17 18:33:36 +00001367 void RemoveAsUserOfInput(size_t input) {
1368 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1369 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1370 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001371
David Brazdil1abb4192015-02-17 18:33:36 +00001372 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1373 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374
David Brazdiled596192015-01-23 10:39:45 +00001375 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1376 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001377 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001378 bool HasOnlyOneNonEnvironmentUse() const {
1379 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1380 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001381
Roland Levillain6c82d402014-10-13 16:10:27 +01001382 // Does this instruction strictly dominate `other_instruction`?
1383 // Returns false if this instruction and `other_instruction` are the same.
1384 // Aborts if this instruction and `other_instruction` are both phis.
1385 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001386
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001387 int GetId() const { return id_; }
1388 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001389
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001390 int GetSsaIndex() const { return ssa_index_; }
1391 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1392 bool HasSsaIndex() const { return ssa_index_ != -1; }
1393
1394 bool HasEnvironment() const { return environment_ != nullptr; }
1395 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001396 // Set the `environment_` field. Raw because this method does not
1397 // update the uses lists.
1398 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1399
1400 // Set the environment of this instruction, copying it from `environment`. While
1401 // copying, the uses lists are being updated.
1402 void CopyEnvironmentFrom(HEnvironment* environment) {
1403 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001404 environment_ = new (allocator) HEnvironment(
1405 allocator,
1406 environment->Size(),
1407 environment->GetDexFile(),
1408 environment->GetMethodIdx(),
1409 environment->GetDexPc());
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001410 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001411 if (environment->GetParent() != nullptr) {
1412 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1413 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001414 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001415
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001416 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1417 HBasicBlock* block) {
1418 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001419 environment_ = new (allocator) HEnvironment(
1420 allocator,
1421 environment->Size(),
1422 environment->GetDexFile(),
1423 environment->GetMethodIdx(),
1424 environment->GetDexPc());
1425 if (environment->GetParent() != nullptr) {
1426 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1427 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001428 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1429 }
1430
Nicolas Geoffray39468442014-09-02 15:17:15 +01001431 // Returns the number of entries in the environment. Typically, that is the
1432 // number of dex registers in a method. It could be more in case of inlining.
1433 size_t EnvironmentSize() const;
1434
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001435 LocationSummary* GetLocations() const { return locations_; }
1436 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001437
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001438 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001439 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001440
Alexandre Rames188d4312015-04-09 18:30:21 +01001441 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1442 // uses of this instruction by `other` are *not* updated.
1443 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1444 ReplaceWith(other);
1445 other->ReplaceInput(this, use_index);
1446 }
1447
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001448 // Move `this` instruction before `cursor`.
1449 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001450
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001451#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001452 bool Is##type() const { return (As##type() != nullptr); } \
1453 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001454 virtual H##type* As##type() { return nullptr; }
1455
1456 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1457#undef INSTRUCTION_TYPE_CHECK
1458
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001459 // Returns whether the instruction can be moved within the graph.
1460 virtual bool CanBeMoved() const { return false; }
1461
1462 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001463 virtual bool InstructionTypeEquals(HInstruction* other) const {
1464 UNUSED(other);
1465 return false;
1466 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001467
1468 // Returns whether any data encoded in the two instructions is equal.
1469 // This method does not look at the inputs. Both instructions must be
1470 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001471 virtual bool InstructionDataEquals(HInstruction* other) const {
1472 UNUSED(other);
1473 return false;
1474 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475
1476 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001477 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001478 // 2) Their inputs are identical.
1479 bool Equals(HInstruction* other) const;
1480
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001481 virtual InstructionKind GetKind() const = 0;
1482
1483 virtual size_t ComputeHashCode() const {
1484 size_t result = GetKind();
1485 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1486 result = (result * 31) + InputAt(i)->GetId();
1487 }
1488 return result;
1489 }
1490
1491 SideEffects GetSideEffects() const { return side_effects_; }
1492
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001493 size_t GetLifetimePosition() const { return lifetime_position_; }
1494 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1495 LiveInterval* GetLiveInterval() const { return live_interval_; }
1496 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1497 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1498
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001499 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1500
1501 // Returns whether the code generation of the instruction will require to have access
1502 // to the current method. Such instructions are:
1503 // (1): Instructions that require an environment, as calling the runtime requires
1504 // to walk the stack and have the current method stored at a specific stack address.
1505 // (2): Object literals like classes and strings, that are loaded from the dex cache
1506 // fields of the current method.
1507 bool NeedsCurrentMethod() const {
1508 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1509 }
1510
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001511 virtual bool NeedsDexCache() const { return false; }
1512
David Brazdil1abb4192015-02-17 18:33:36 +00001513 protected:
1514 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1515 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1516
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001517 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001518 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1519
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001520 HInstruction* previous_;
1521 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001522 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001523
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001524 // An instruction gets an id when it is added to the graph.
1525 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001526 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001527 int id_;
1528
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001529 // When doing liveness analysis, instructions that have uses get an SSA index.
1530 int ssa_index_;
1531
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001532 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001533 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001534
1535 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001536 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001537
Nicolas Geoffray39468442014-09-02 15:17:15 +01001538 // The environment associated with this instruction. Not null if the instruction
1539 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001540 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001541
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001542 // Set by the code generator.
1543 LocationSummary* locations_;
1544
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001545 // Set by the liveness analysis.
1546 LiveInterval* live_interval_;
1547
1548 // Set by the liveness analysis, this is the position in a linear
1549 // order of blocks where this instruction's live interval start.
1550 size_t lifetime_position_;
1551
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001552 const SideEffects side_effects_;
1553
Calin Juravleacf735c2015-02-12 15:25:22 +00001554 // TODO: for primitive types this should be marked as invalid.
1555 ReferenceTypeInfo reference_type_info_;
1556
David Brazdil1abb4192015-02-17 18:33:36 +00001557 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001558 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001559 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001560 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001561 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001562
1563 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1564};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001565std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001566
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001567class HInputIterator : public ValueObject {
1568 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001569 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001570
1571 bool Done() const { return index_ == instruction_->InputCount(); }
1572 HInstruction* Current() const { return instruction_->InputAt(index_); }
1573 void Advance() { index_++; }
1574
1575 private:
1576 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001577 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001578
1579 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1580};
1581
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001582class HInstructionIterator : public ValueObject {
1583 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001584 explicit HInstructionIterator(const HInstructionList& instructions)
1585 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001586 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001587 }
1588
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001589 bool Done() const { return instruction_ == nullptr; }
1590 HInstruction* Current() const { return instruction_; }
1591 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001592 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001593 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001594 }
1595
1596 private:
1597 HInstruction* instruction_;
1598 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001599
1600 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001601};
1602
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001603class HBackwardInstructionIterator : public ValueObject {
1604 public:
1605 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1606 : instruction_(instructions.last_instruction_) {
1607 next_ = Done() ? nullptr : instruction_->GetPrevious();
1608 }
1609
1610 bool Done() const { return instruction_ == nullptr; }
1611 HInstruction* Current() const { return instruction_; }
1612 void Advance() {
1613 instruction_ = next_;
1614 next_ = Done() ? nullptr : instruction_->GetPrevious();
1615 }
1616
1617 private:
1618 HInstruction* instruction_;
1619 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001620
1621 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001622};
1623
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001624// An embedded container with N elements of type T. Used (with partial
1625// specialization for N=0) because embedded arrays cannot have size 0.
1626template<typename T, intptr_t N>
1627class EmbeddedArray {
1628 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001629 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001630
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001631 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001632
1633 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001634 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001635 return elements_[i];
1636 }
1637
1638 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001639 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001640 return elements_[i];
1641 }
1642
1643 const T& At(intptr_t i) const {
1644 return (*this)[i];
1645 }
1646
1647 void SetAt(intptr_t i, const T& val) {
1648 (*this)[i] = val;
1649 }
1650
1651 private:
1652 T elements_[N];
1653};
1654
1655template<typename T>
1656class EmbeddedArray<T, 0> {
1657 public:
1658 intptr_t length() const { return 0; }
1659 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001660 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001661 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001662 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001663 }
1664 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001665 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001666 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001667 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001668 }
1669};
1670
1671template<intptr_t N>
1672class HTemplateInstruction: public HInstruction {
1673 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001674 HTemplateInstruction<N>(SideEffects side_effects)
1675 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001676 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001677
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001678 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001679
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001680 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001681 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1682
1683 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1684 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001685 }
1686
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001687 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001688 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001689
1690 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001691};
1692
Dave Allison20dfc792014-06-16 20:44:29 -07001693template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001694class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001695 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001696 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1697 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001698 virtual ~HExpression() {}
1699
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001700 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001701
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001702 protected:
1703 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001704};
1705
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001706// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1707// instruction that branches to the exit block.
1708class HReturnVoid : public HTemplateInstruction<0> {
1709 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001710 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001711
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001712 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001713
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001714 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001715
1716 private:
1717 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1718};
1719
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001720// Represents dex's RETURN opcodes. A HReturn is a control flow
1721// instruction that branches to the exit block.
1722class HReturn : public HTemplateInstruction<1> {
1723 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001724 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001725 SetRawInputAt(0, value);
1726 }
1727
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001728 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001729
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001730 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001731
1732 private:
1733 DISALLOW_COPY_AND_ASSIGN(HReturn);
1734};
1735
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001736// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001737// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001738// exit block.
1739class HExit : public HTemplateInstruction<0> {
1740 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001741 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001742
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001743 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001744
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001745 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001746
1747 private:
1748 DISALLOW_COPY_AND_ASSIGN(HExit);
1749};
1750
1751// Jumps from one block to another.
1752class HGoto : public HTemplateInstruction<0> {
1753 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001754 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1755
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001756 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001757
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001758 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001759 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001760 }
1761
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001762 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001763
1764 private:
1765 DISALLOW_COPY_AND_ASSIGN(HGoto);
1766};
1767
Dave Allison20dfc792014-06-16 20:44:29 -07001768
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001769// Conditional branch. A block ending with an HIf instruction must have
1770// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001771class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001772 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001773 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001774 SetRawInputAt(0, input);
1775 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001776
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001777 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001778
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001779 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001780 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001781 }
1782
1783 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001784 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001785 }
1786
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001787 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001788
1789 private:
1790 DISALLOW_COPY_AND_ASSIGN(HIf);
1791};
1792
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001793// Deoptimize to interpreter, upon checking a condition.
1794class HDeoptimize : public HTemplateInstruction<1> {
1795 public:
1796 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1797 : HTemplateInstruction(SideEffects::None()),
1798 dex_pc_(dex_pc) {
1799 SetRawInputAt(0, cond);
1800 }
1801
1802 bool NeedsEnvironment() const OVERRIDE { return true; }
1803 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001804 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001805
1806 DECLARE_INSTRUCTION(Deoptimize);
1807
1808 private:
1809 uint32_t dex_pc_;
1810
1811 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1812};
1813
Roland Levillain88cb1752014-10-20 16:36:47 +01001814class HUnaryOperation : public HExpression<1> {
1815 public:
1816 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1817 : HExpression(result_type, SideEffects::None()) {
1818 SetRawInputAt(0, input);
1819 }
1820
1821 HInstruction* GetInput() const { return InputAt(0); }
1822 Primitive::Type GetResultType() const { return GetType(); }
1823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001824 bool CanBeMoved() const OVERRIDE { return true; }
1825 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001826 UNUSED(other);
1827 return true;
1828 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001829
Roland Levillain9240d6a2014-10-20 16:47:04 +01001830 // Try to statically evaluate `operation` and return a HConstant
1831 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001832 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001833 HConstant* TryStaticEvaluation() const;
1834
1835 // Apply this operation to `x`.
1836 virtual int32_t Evaluate(int32_t x) const = 0;
1837 virtual int64_t Evaluate(int64_t x) const = 0;
1838
Roland Levillain88cb1752014-10-20 16:36:47 +01001839 DECLARE_INSTRUCTION(UnaryOperation);
1840
1841 private:
1842 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1843};
1844
Dave Allison20dfc792014-06-16 20:44:29 -07001845class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001846 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001847 HBinaryOperation(Primitive::Type result_type,
1848 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001849 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001850 SetRawInputAt(0, left);
1851 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001852 }
1853
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001854 HInstruction* GetLeft() const { return InputAt(0); }
1855 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001856 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001857
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001858 virtual bool IsCommutative() const { return false; }
1859
1860 // Put constant on the right.
1861 // Returns whether order is changed.
1862 bool OrderInputsWithConstantOnTheRight() {
1863 HInstruction* left = InputAt(0);
1864 HInstruction* right = InputAt(1);
1865 if (left->IsConstant() && !right->IsConstant()) {
1866 ReplaceInput(right, 0);
1867 ReplaceInput(left, 1);
1868 return true;
1869 }
1870 return false;
1871 }
1872
1873 // Order inputs by instruction id, but favor constant on the right side.
1874 // This helps GVN for commutative ops.
1875 void OrderInputs() {
1876 DCHECK(IsCommutative());
1877 HInstruction* left = InputAt(0);
1878 HInstruction* right = InputAt(1);
1879 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1880 return;
1881 }
1882 if (OrderInputsWithConstantOnTheRight()) {
1883 return;
1884 }
1885 // Order according to instruction id.
1886 if (left->GetId() > right->GetId()) {
1887 ReplaceInput(right, 0);
1888 ReplaceInput(left, 1);
1889 }
1890 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001891
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001892 bool CanBeMoved() const OVERRIDE { return true; }
1893 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001894 UNUSED(other);
1895 return true;
1896 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001897
Roland Levillain9240d6a2014-10-20 16:47:04 +01001898 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001899 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001900 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001901 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001902
1903 // Apply this operation to `x` and `y`.
1904 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1905 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1906
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001907 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001908 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001909 HConstant* GetConstantRight() const;
1910
1911 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001912 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001913 HInstruction* GetLeastConstantLeft() const;
1914
Roland Levillainccc07a92014-09-16 14:48:16 +01001915 DECLARE_INSTRUCTION(BinaryOperation);
1916
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001917 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001918 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1919};
1920
Dave Allison20dfc792014-06-16 20:44:29 -07001921class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001922 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001923 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001924 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1925 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001926
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001927 bool NeedsMaterialization() const { return needs_materialization_; }
1928 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001929
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001930 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001931 // `instruction`, and disregard moves in between.
1932 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001933
Dave Allison20dfc792014-06-16 20:44:29 -07001934 DECLARE_INSTRUCTION(Condition);
1935
1936 virtual IfCondition GetCondition() const = 0;
1937
1938 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001939 // For register allocation purposes, returns whether this instruction needs to be
1940 // materialized (that is, not just be in the processor flags).
1941 bool needs_materialization_;
1942
Dave Allison20dfc792014-06-16 20:44:29 -07001943 DISALLOW_COPY_AND_ASSIGN(HCondition);
1944};
1945
1946// Instruction to check if two inputs are equal to each other.
1947class HEqual : public HCondition {
1948 public:
1949 HEqual(HInstruction* first, HInstruction* second)
1950 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001951
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001952 bool IsCommutative() const OVERRIDE { return true; }
1953
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001954 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001955 return x == y ? 1 : 0;
1956 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001957 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001958 return x == y ? 1 : 0;
1959 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001960
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001961 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001962
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001963 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001964 return kCondEQ;
1965 }
1966
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001967 private:
1968 DISALLOW_COPY_AND_ASSIGN(HEqual);
1969};
1970
Dave Allison20dfc792014-06-16 20:44:29 -07001971class HNotEqual : public HCondition {
1972 public:
1973 HNotEqual(HInstruction* first, HInstruction* second)
1974 : HCondition(first, second) {}
1975
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001976 bool IsCommutative() const OVERRIDE { return true; }
1977
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001978 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001979 return x != y ? 1 : 0;
1980 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001981 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001982 return x != y ? 1 : 0;
1983 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001984
Dave Allison20dfc792014-06-16 20:44:29 -07001985 DECLARE_INSTRUCTION(NotEqual);
1986
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001987 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001988 return kCondNE;
1989 }
1990
1991 private:
1992 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1993};
1994
1995class HLessThan : public HCondition {
1996 public:
1997 HLessThan(HInstruction* first, HInstruction* second)
1998 : HCondition(first, second) {}
1999
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002000 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002001 return x < y ? 1 : 0;
2002 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002003 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002004 return x < y ? 1 : 0;
2005 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002006
Dave Allison20dfc792014-06-16 20:44:29 -07002007 DECLARE_INSTRUCTION(LessThan);
2008
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002009 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002010 return kCondLT;
2011 }
2012
2013 private:
2014 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2015};
2016
2017class HLessThanOrEqual : public HCondition {
2018 public:
2019 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2020 : HCondition(first, second) {}
2021
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002022 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002023 return x <= y ? 1 : 0;
2024 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002025 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002026 return x <= y ? 1 : 0;
2027 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002028
Dave Allison20dfc792014-06-16 20:44:29 -07002029 DECLARE_INSTRUCTION(LessThanOrEqual);
2030
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002031 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002032 return kCondLE;
2033 }
2034
2035 private:
2036 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2037};
2038
2039class HGreaterThan : public HCondition {
2040 public:
2041 HGreaterThan(HInstruction* first, HInstruction* second)
2042 : HCondition(first, second) {}
2043
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002044 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002045 return x > y ? 1 : 0;
2046 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002047 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002048 return x > y ? 1 : 0;
2049 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002050
Dave Allison20dfc792014-06-16 20:44:29 -07002051 DECLARE_INSTRUCTION(GreaterThan);
2052
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002053 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002054 return kCondGT;
2055 }
2056
2057 private:
2058 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2059};
2060
2061class HGreaterThanOrEqual : public HCondition {
2062 public:
2063 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2064 : HCondition(first, second) {}
2065
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002066 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002067 return x >= y ? 1 : 0;
2068 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002069 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002070 return x >= y ? 1 : 0;
2071 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002072
Dave Allison20dfc792014-06-16 20:44:29 -07002073 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2074
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002075 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002076 return kCondGE;
2077 }
2078
2079 private:
2080 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2081};
2082
2083
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002084// Instruction to check how two inputs compare to each other.
2085// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2086class HCompare : public HBinaryOperation {
2087 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002088 // The bias applies for floating point operations and indicates how NaN
2089 // comparisons are treated:
2090 enum Bias {
2091 kNoBias, // bias is not applicable (i.e. for long operation)
2092 kGtBias, // return 1 for NaN comparisons
2093 kLtBias, // return -1 for NaN comparisons
2094 };
2095
2096 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
2097 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002098 DCHECK_EQ(type, first->GetType());
2099 DCHECK_EQ(type, second->GetType());
2100 }
2101
Calin Juravleddb7df22014-11-25 20:56:51 +00002102 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002103 return
2104 x == y ? 0 :
2105 x > y ? 1 :
2106 -1;
2107 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002108
2109 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002110 return
2111 x == y ? 0 :
2112 x > y ? 1 :
2113 -1;
2114 }
2115
Calin Juravleddb7df22014-11-25 20:56:51 +00002116 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2117 return bias_ == other->AsCompare()->bias_;
2118 }
2119
2120 bool IsGtBias() { return bias_ == kGtBias; }
2121
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002122 DECLARE_INSTRUCTION(Compare);
2123
2124 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002125 const Bias bias_;
2126
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002127 DISALLOW_COPY_AND_ASSIGN(HCompare);
2128};
2129
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002130// A local in the graph. Corresponds to a Dex register.
2131class HLocal : public HTemplateInstruction<0> {
2132 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002133 explicit HLocal(uint16_t reg_number)
2134 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002135
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002136 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002137
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002138 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002139
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002140 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002141 // The Dex register number.
2142 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002143
2144 DISALLOW_COPY_AND_ASSIGN(HLocal);
2145};
2146
2147// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002148class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002149 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002150 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002151 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002152 SetRawInputAt(0, local);
2153 }
2154
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002155 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2156
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002157 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002158
2159 private:
2160 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2161};
2162
2163// Store a value in a given local. This instruction has two inputs: the value
2164// and the local.
2165class HStoreLocal : public HTemplateInstruction<2> {
2166 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002167 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002168 SetRawInputAt(0, local);
2169 SetRawInputAt(1, value);
2170 }
2171
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002172 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2173
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002174 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002175
2176 private:
2177 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2178};
2179
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002180class HConstant : public HExpression<0> {
2181 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002182 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2183
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002184 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002185
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002186 virtual bool IsMinusOne() const { return false; }
2187 virtual bool IsZero() const { return false; }
2188 virtual bool IsOne() const { return false; }
2189
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002190 DECLARE_INSTRUCTION(Constant);
2191
2192 private:
2193 DISALLOW_COPY_AND_ASSIGN(HConstant);
2194};
2195
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002196class HFloatConstant : public HConstant {
2197 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002198 float GetValue() const { return value_; }
2199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002200 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002201 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2202 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002203 }
2204
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002205 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002206
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002207 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002208 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002209 }
2210 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002211 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002212 }
2213 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002214 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2215 }
2216 bool IsNaN() const {
2217 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002218 }
2219
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002220 DECLARE_INSTRUCTION(FloatConstant);
2221
2222 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002223 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002224 explicit HFloatConstant(int32_t value)
2225 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002226
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002227 const float value_;
2228
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002229 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002230 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002231 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002232 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2233};
2234
2235class HDoubleConstant : public HConstant {
2236 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002237 double GetValue() const { return value_; }
2238
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002239 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002240 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2241 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002242 }
2243
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002244 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002245
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002246 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002247 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002248 }
2249 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002250 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002251 }
2252 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002253 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2254 }
2255 bool IsNaN() const {
2256 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002257 }
2258
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002259 DECLARE_INSTRUCTION(DoubleConstant);
2260
2261 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002262 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002263 explicit HDoubleConstant(int64_t value)
2264 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002265
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002266 const double value_;
2267
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002268 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002269 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002270 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002271 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2272};
2273
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002274class HNullConstant : public HConstant {
2275 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002276 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2277 return true;
2278 }
2279
2280 size_t ComputeHashCode() const OVERRIDE { return 0; }
2281
2282 DECLARE_INSTRUCTION(NullConstant);
2283
2284 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002285 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2286
2287 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002288 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2289};
2290
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002291// Constants of the type int. Those can be from Dex instructions, or
2292// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002293class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002294 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002295 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002296
Calin Juravle61d544b2015-02-23 16:46:57 +00002297 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002298 return other->AsIntConstant()->value_ == value_;
2299 }
2300
Calin Juravle61d544b2015-02-23 16:46:57 +00002301 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2302
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002303 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2304 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2305 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2306
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002307 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002308
2309 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002310 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2311
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002312 const int32_t value_;
2313
David Brazdil8d5b8b22015-03-24 10:51:52 +00002314 friend class HGraph;
2315 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002316 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002317 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2318};
2319
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002320class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002321 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002322 int64_t GetValue() const { return value_; }
2323
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002324 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002325 return other->AsLongConstant()->value_ == value_;
2326 }
2327
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002328 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002329
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002330 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2331 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2332 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2333
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002334 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002335
2336 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002337 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2338
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002339 const int64_t value_;
2340
David Brazdil8d5b8b22015-03-24 10:51:52 +00002341 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002342 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2343};
2344
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002345enum class Intrinsics {
2346#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2347#include "intrinsics_list.h"
2348 kNone,
2349 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2350#undef INTRINSICS_LIST
2351#undef OPTIMIZING_INTRINSICS
2352};
2353std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2354
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002355class HInvoke : public HInstruction {
2356 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002357 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002358
2359 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2360 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002361 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002362
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002363 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002364 SetRawInputAt(index, argument);
2365 }
2366
Roland Levillain3e3d7332015-04-28 11:00:54 +01002367 // Return the number of arguments. This number can be lower than
2368 // the number of inputs returned by InputCount(), as some invoke
2369 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2370 // inputs at the end of their list of inputs.
2371 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2372
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002373 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002374
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002375 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002376
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002377 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2378
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002379 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002380 return intrinsic_;
2381 }
2382
2383 void SetIntrinsic(Intrinsics intrinsic) {
2384 intrinsic_ = intrinsic;
2385 }
2386
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002387 DECLARE_INSTRUCTION(Invoke);
2388
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002389 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002390 HInvoke(ArenaAllocator* arena,
2391 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002392 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002393 Primitive::Type return_type,
2394 uint32_t dex_pc,
2395 uint32_t dex_method_index)
2396 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002397 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002398 inputs_(arena, number_of_arguments),
2399 return_type_(return_type),
2400 dex_pc_(dex_pc),
2401 dex_method_index_(dex_method_index),
2402 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002403 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2404 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002405 }
2406
David Brazdil1abb4192015-02-17 18:33:36 +00002407 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2408 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2409 inputs_.Put(index, input);
2410 }
2411
Roland Levillain3e3d7332015-04-28 11:00:54 +01002412 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002413 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002414 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002415 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002416 const uint32_t dex_method_index_;
2417 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002418
2419 private:
2420 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2421};
2422
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002423class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002424 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002425 // Requirements of this method call regarding the class
2426 // initialization (clinit) check of its declaring class.
2427 enum class ClinitCheckRequirement {
2428 kNone, // Class already initialized.
2429 kExplicit, // Static call having explicit clinit check as last input.
2430 kImplicit, // Static call implicitly requiring a clinit check.
2431 };
2432
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002433 HInvokeStaticOrDirect(ArenaAllocator* arena,
2434 uint32_t number_of_arguments,
2435 Primitive::Type return_type,
2436 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002437 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002438 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002439 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002440 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002441 InvokeType invoke_type,
2442 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002443 : HInvoke(arena,
2444 number_of_arguments,
2445 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2446 return_type,
2447 dex_pc,
2448 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002449 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002450 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002451 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002452 clinit_check_requirement_(clinit_check_requirement),
2453 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002454
Calin Juravle641547a2015-04-21 22:08:51 +01002455 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2456 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002457 // We access the method via the dex cache so we can't do an implicit null check.
2458 // TODO: for intrinsics we can generate implicit null checks.
2459 return false;
2460 }
2461
Nicolas Geoffray79041292015-03-26 10:05:54 +00002462 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002463 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002464 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002465 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002466 bool IsStringInit() const { return string_init_offset_ != 0; }
2467 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002468
Roland Levillain4c0eb422015-04-24 16:43:49 +01002469 // Is this instruction a call to a static method?
2470 bool IsStatic() const {
2471 return GetInvokeType() == kStatic;
2472 }
2473
Roland Levillain3e3d7332015-04-28 11:00:54 +01002474 // Remove the art::HLoadClass instruction set as last input by
2475 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2476 // the initial art::HClinitCheck instruction (only relevant for
2477 // static calls with explicit clinit check).
2478 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002479 DCHECK(IsStaticWithExplicitClinitCheck());
2480 size_t last_input_index = InputCount() - 1;
2481 HInstruction* last_input = InputAt(last_input_index);
2482 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002483 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002484 RemoveAsUserOfInput(last_input_index);
2485 inputs_.DeleteAt(last_input_index);
2486 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2487 DCHECK(IsStaticWithImplicitClinitCheck());
2488 }
2489
2490 // Is this a call to a static method whose declaring class has an
2491 // explicit intialization check in the graph?
2492 bool IsStaticWithExplicitClinitCheck() const {
2493 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2494 }
2495
2496 // Is this a call to a static method whose declaring class has an
2497 // implicit intialization check requirement?
2498 bool IsStaticWithImplicitClinitCheck() const {
2499 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2500 }
2501
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002502 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002503
Roland Levillain4c0eb422015-04-24 16:43:49 +01002504 protected:
2505 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2506 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2507 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2508 HInstruction* input = input_record.GetInstruction();
2509 // `input` is the last input of a static invoke marked as having
2510 // an explicit clinit check. It must either be:
2511 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2512 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2513 DCHECK(input != nullptr);
2514 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2515 }
2516 return input_record;
2517 }
2518
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002519 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002520 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002521 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002522 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002523 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002524 // Thread entrypoint offset for string init method if this is a string init invoke.
2525 // Note that there are multiple string init methods, each having its own offset.
2526 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002527
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002528 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002529};
2530
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002531class HInvokeVirtual : public HInvoke {
2532 public:
2533 HInvokeVirtual(ArenaAllocator* arena,
2534 uint32_t number_of_arguments,
2535 Primitive::Type return_type,
2536 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002537 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002538 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002539 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002540 vtable_index_(vtable_index) {}
2541
Calin Juravle641547a2015-04-21 22:08:51 +01002542 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002543 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002544 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002545 }
2546
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002547 uint32_t GetVTableIndex() const { return vtable_index_; }
2548
2549 DECLARE_INSTRUCTION(InvokeVirtual);
2550
2551 private:
2552 const uint32_t vtable_index_;
2553
2554 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2555};
2556
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002557class HInvokeInterface : public HInvoke {
2558 public:
2559 HInvokeInterface(ArenaAllocator* arena,
2560 uint32_t number_of_arguments,
2561 Primitive::Type return_type,
2562 uint32_t dex_pc,
2563 uint32_t dex_method_index,
2564 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002565 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002566 imt_index_(imt_index) {}
2567
Calin Juravle641547a2015-04-21 22:08:51 +01002568 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002569 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002570 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002571 }
2572
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002573 uint32_t GetImtIndex() const { return imt_index_; }
2574 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2575
2576 DECLARE_INSTRUCTION(InvokeInterface);
2577
2578 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002579 const uint32_t imt_index_;
2580
2581 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2582};
2583
Dave Allison20dfc792014-06-16 20:44:29 -07002584class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002585 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002586 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002587 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2588 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002589 type_index_(type_index),
2590 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002591
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002592 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002593 uint16_t GetTypeIndex() const { return type_index_; }
2594
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002595 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002596 bool NeedsEnvironment() const OVERRIDE { return true; }
2597 // It may throw when called on:
2598 // - interfaces
2599 // - abstract/innaccessible/unknown classes
2600 // TODO: optimize when possible.
2601 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002602
Calin Juravle10e244f2015-01-26 18:54:32 +00002603 bool CanBeNull() const OVERRIDE { return false; }
2604
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002605 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2606
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002607 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002608
2609 private:
2610 const uint32_t dex_pc_;
2611 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002612 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002613
2614 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2615};
2616
Roland Levillain88cb1752014-10-20 16:36:47 +01002617class HNeg : public HUnaryOperation {
2618 public:
2619 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2620 : HUnaryOperation(result_type, input) {}
2621
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002622 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2623 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002624
Roland Levillain88cb1752014-10-20 16:36:47 +01002625 DECLARE_INSTRUCTION(Neg);
2626
2627 private:
2628 DISALLOW_COPY_AND_ASSIGN(HNeg);
2629};
2630
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002631class HNewArray : public HExpression<1> {
2632 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002633 HNewArray(HInstruction* length,
2634 uint32_t dex_pc,
2635 uint16_t type_index,
2636 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002637 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2638 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002639 type_index_(type_index),
2640 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002641 SetRawInputAt(0, length);
2642 }
2643
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002644 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002645 uint16_t GetTypeIndex() const { return type_index_; }
2646
2647 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002648 bool NeedsEnvironment() const OVERRIDE { return true; }
2649
Mingyao Yang0c365e62015-03-31 15:09:29 -07002650 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2651 bool CanThrow() const OVERRIDE { return true; }
2652
Calin Juravle10e244f2015-01-26 18:54:32 +00002653 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002654
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002655 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2656
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002657 DECLARE_INSTRUCTION(NewArray);
2658
2659 private:
2660 const uint32_t dex_pc_;
2661 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002662 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002663
2664 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2665};
2666
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002667class HAdd : public HBinaryOperation {
2668 public:
2669 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2670 : HBinaryOperation(result_type, left, right) {}
2671
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002672 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002673
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002674 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002675 return x + y;
2676 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002677 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002678 return x + y;
2679 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002680
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002681 DECLARE_INSTRUCTION(Add);
2682
2683 private:
2684 DISALLOW_COPY_AND_ASSIGN(HAdd);
2685};
2686
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002687class HSub : public HBinaryOperation {
2688 public:
2689 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2690 : HBinaryOperation(result_type, left, right) {}
2691
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002692 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002693 return x - y;
2694 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002695 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002696 return x - y;
2697 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002698
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002699 DECLARE_INSTRUCTION(Sub);
2700
2701 private:
2702 DISALLOW_COPY_AND_ASSIGN(HSub);
2703};
2704
Calin Juravle34bacdf2014-10-07 20:23:36 +01002705class HMul : public HBinaryOperation {
2706 public:
2707 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2708 : HBinaryOperation(result_type, left, right) {}
2709
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002710 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002711
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002712 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2713 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002714
2715 DECLARE_INSTRUCTION(Mul);
2716
2717 private:
2718 DISALLOW_COPY_AND_ASSIGN(HMul);
2719};
2720
Calin Juravle7c4954d2014-10-28 16:57:40 +00002721class HDiv : public HBinaryOperation {
2722 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002723 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2724 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002725
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002726 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002727 // Our graph structure ensures we never have 0 for `y` during constant folding.
2728 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002729 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002730 return (y == -1) ? -x : x / y;
2731 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002732
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002733 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002734 DCHECK_NE(y, 0);
2735 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2736 return (y == -1) ? -x : x / y;
2737 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002738
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002739 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002740
Calin Juravle7c4954d2014-10-28 16:57:40 +00002741 DECLARE_INSTRUCTION(Div);
2742
2743 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002744 const uint32_t dex_pc_;
2745
Calin Juravle7c4954d2014-10-28 16:57:40 +00002746 DISALLOW_COPY_AND_ASSIGN(HDiv);
2747};
2748
Calin Juravlebacfec32014-11-14 15:54:36 +00002749class HRem : public HBinaryOperation {
2750 public:
2751 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2752 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2753
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002754 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002755 DCHECK_NE(y, 0);
2756 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2757 return (y == -1) ? 0 : x % y;
2758 }
2759
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002760 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002761 DCHECK_NE(y, 0);
2762 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2763 return (y == -1) ? 0 : x % y;
2764 }
2765
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002766 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002767
2768 DECLARE_INSTRUCTION(Rem);
2769
2770 private:
2771 const uint32_t dex_pc_;
2772
2773 DISALLOW_COPY_AND_ASSIGN(HRem);
2774};
2775
Calin Juravled0d48522014-11-04 16:40:20 +00002776class HDivZeroCheck : public HExpression<1> {
2777 public:
2778 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2779 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2780 SetRawInputAt(0, value);
2781 }
2782
2783 bool CanBeMoved() const OVERRIDE { return true; }
2784
2785 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2786 UNUSED(other);
2787 return true;
2788 }
2789
2790 bool NeedsEnvironment() const OVERRIDE { return true; }
2791 bool CanThrow() const OVERRIDE { return true; }
2792
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002793 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002794
2795 DECLARE_INSTRUCTION(DivZeroCheck);
2796
2797 private:
2798 const uint32_t dex_pc_;
2799
2800 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2801};
2802
Calin Juravle9aec02f2014-11-18 23:06:35 +00002803class HShl : public HBinaryOperation {
2804 public:
2805 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2806 : HBinaryOperation(result_type, left, right) {}
2807
2808 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2809 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2810
2811 DECLARE_INSTRUCTION(Shl);
2812
2813 private:
2814 DISALLOW_COPY_AND_ASSIGN(HShl);
2815};
2816
2817class HShr : public HBinaryOperation {
2818 public:
2819 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2820 : HBinaryOperation(result_type, left, right) {}
2821
2822 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2823 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2824
2825 DECLARE_INSTRUCTION(Shr);
2826
2827 private:
2828 DISALLOW_COPY_AND_ASSIGN(HShr);
2829};
2830
2831class HUShr : public HBinaryOperation {
2832 public:
2833 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2834 : HBinaryOperation(result_type, left, right) {}
2835
2836 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2837 uint32_t ux = static_cast<uint32_t>(x);
2838 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2839 return static_cast<int32_t>(ux >> uy);
2840 }
2841
2842 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2843 uint64_t ux = static_cast<uint64_t>(x);
2844 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2845 return static_cast<int64_t>(ux >> uy);
2846 }
2847
2848 DECLARE_INSTRUCTION(UShr);
2849
2850 private:
2851 DISALLOW_COPY_AND_ASSIGN(HUShr);
2852};
2853
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002854class HAnd : public HBinaryOperation {
2855 public:
2856 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2857 : HBinaryOperation(result_type, left, right) {}
2858
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002859 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002860
2861 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2862 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2863
2864 DECLARE_INSTRUCTION(And);
2865
2866 private:
2867 DISALLOW_COPY_AND_ASSIGN(HAnd);
2868};
2869
2870class HOr : public HBinaryOperation {
2871 public:
2872 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2873 : HBinaryOperation(result_type, left, right) {}
2874
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002875 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002876
2877 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2878 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2879
2880 DECLARE_INSTRUCTION(Or);
2881
2882 private:
2883 DISALLOW_COPY_AND_ASSIGN(HOr);
2884};
2885
2886class HXor : public HBinaryOperation {
2887 public:
2888 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2889 : HBinaryOperation(result_type, left, right) {}
2890
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002891 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002892
2893 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2894 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2895
2896 DECLARE_INSTRUCTION(Xor);
2897
2898 private:
2899 DISALLOW_COPY_AND_ASSIGN(HXor);
2900};
2901
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002902// The value of a parameter in this method. Its location depends on
2903// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002904class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002905 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002906 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2907 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002908
2909 uint8_t GetIndex() const { return index_; }
2910
Calin Juravle10e244f2015-01-26 18:54:32 +00002911 bool CanBeNull() const OVERRIDE { return !is_this_; }
2912
Calin Juravle3cd4fc82015-05-14 15:15:42 +01002913 bool IsThis() const { return is_this_; }
2914
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002915 DECLARE_INSTRUCTION(ParameterValue);
2916
2917 private:
2918 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002919 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002920 const uint8_t index_;
2921
Calin Juravle10e244f2015-01-26 18:54:32 +00002922 // Whether or not the parameter value corresponds to 'this' argument.
2923 const bool is_this_;
2924
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002925 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2926};
2927
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002928class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002929 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002930 explicit HNot(Primitive::Type result_type, HInstruction* input)
2931 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002933 bool CanBeMoved() const OVERRIDE { return true; }
2934 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002935 UNUSED(other);
2936 return true;
2937 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002938
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002939 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2940 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002941
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002942 DECLARE_INSTRUCTION(Not);
2943
2944 private:
2945 DISALLOW_COPY_AND_ASSIGN(HNot);
2946};
2947
David Brazdil66d126e2015-04-03 16:02:44 +01002948class HBooleanNot : public HUnaryOperation {
2949 public:
2950 explicit HBooleanNot(HInstruction* input)
2951 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2952
2953 bool CanBeMoved() const OVERRIDE { return true; }
2954 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2955 UNUSED(other);
2956 return true;
2957 }
2958
2959 int32_t Evaluate(int32_t x) const OVERRIDE {
2960 DCHECK(IsUint<1>(x));
2961 return !x;
2962 }
2963
2964 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2965 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2966 UNREACHABLE();
2967 }
2968
2969 DECLARE_INSTRUCTION(BooleanNot);
2970
2971 private:
2972 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2973};
2974
Roland Levillaindff1f282014-11-05 14:15:05 +00002975class HTypeConversion : public HExpression<1> {
2976 public:
2977 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002978 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2979 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002980 SetRawInputAt(0, input);
2981 DCHECK_NE(input->GetType(), result_type);
2982 }
2983
2984 HInstruction* GetInput() const { return InputAt(0); }
2985 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2986 Primitive::Type GetResultType() const { return GetType(); }
2987
Roland Levillain624279f2014-12-04 11:54:28 +00002988 // Required by the x86 and ARM code generators when producing calls
2989 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002990 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00002991
Roland Levillaindff1f282014-11-05 14:15:05 +00002992 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002993 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002994
Mark Mendelle82549b2015-05-06 10:55:34 -04002995 // Try to statically evaluate the conversion and return a HConstant
2996 // containing the result. If the input cannot be converted, return nullptr.
2997 HConstant* TryStaticEvaluation() const;
2998
Roland Levillaindff1f282014-11-05 14:15:05 +00002999 DECLARE_INSTRUCTION(TypeConversion);
3000
3001 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003002 const uint32_t dex_pc_;
3003
Roland Levillaindff1f282014-11-05 14:15:05 +00003004 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3005};
3006
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003007static constexpr uint32_t kNoRegNumber = -1;
3008
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003009class HPhi : public HInstruction {
3010 public:
3011 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003012 : HInstruction(SideEffects::None()),
3013 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003014 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003015 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003016 is_live_(false),
3017 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003018 inputs_.SetSize(number_of_inputs);
3019 }
3020
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003021 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3022 static Primitive::Type ToPhiType(Primitive::Type type) {
3023 switch (type) {
3024 case Primitive::kPrimBoolean:
3025 case Primitive::kPrimByte:
3026 case Primitive::kPrimShort:
3027 case Primitive::kPrimChar:
3028 return Primitive::kPrimInt;
3029 default:
3030 return type;
3031 }
3032 }
3033
Calin Juravle10e244f2015-01-26 18:54:32 +00003034 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003035
3036 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003037 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003038
Calin Juravle10e244f2015-01-26 18:54:32 +00003039 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003040 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003041
Calin Juravle10e244f2015-01-26 18:54:32 +00003042 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3043 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3044
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003045 uint32_t GetRegNumber() const { return reg_number_; }
3046
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003047 void SetDead() { is_live_ = false; }
3048 void SetLive() { is_live_ = true; }
3049 bool IsDead() const { return !is_live_; }
3050 bool IsLive() const { return is_live_; }
3051
Calin Juravlea4f88312015-04-16 12:57:19 +01003052 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3053 // An equivalent phi is a phi having the same dex register and type.
3054 // It assumes that phis with the same dex register are adjacent.
3055 HPhi* GetNextEquivalentPhiWithSameType() {
3056 HInstruction* next = GetNext();
3057 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3058 if (next->GetType() == GetType()) {
3059 return next->AsPhi();
3060 }
3061 next = next->GetNext();
3062 }
3063 return nullptr;
3064 }
3065
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003066 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003067
David Brazdil1abb4192015-02-17 18:33:36 +00003068 protected:
3069 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3070
3071 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3072 inputs_.Put(index, input);
3073 }
3074
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003075 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003076 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003077 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003078 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003079 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003080 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003081
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003082 DISALLOW_COPY_AND_ASSIGN(HPhi);
3083};
3084
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003085class HNullCheck : public HExpression<1> {
3086 public:
3087 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003088 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003089 SetRawInputAt(0, value);
3090 }
3091
Calin Juravle10e244f2015-01-26 18:54:32 +00003092 bool CanBeMoved() const OVERRIDE { return true; }
3093 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003094 UNUSED(other);
3095 return true;
3096 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003097
Calin Juravle10e244f2015-01-26 18:54:32 +00003098 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003099
Calin Juravle10e244f2015-01-26 18:54:32 +00003100 bool CanThrow() const OVERRIDE { return true; }
3101
3102 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003103
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003104 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003105
3106 DECLARE_INSTRUCTION(NullCheck);
3107
3108 private:
3109 const uint32_t dex_pc_;
3110
3111 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3112};
3113
3114class FieldInfo : public ValueObject {
3115 public:
Calin Juravle52c48962014-12-16 17:02:57 +00003116 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3117 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003118
3119 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003120 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003121 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003122
3123 private:
3124 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003125 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003126 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003127};
3128
3129class HInstanceFieldGet : public HExpression<1> {
3130 public:
3131 HInstanceFieldGet(HInstruction* value,
3132 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003133 MemberOffset field_offset,
3134 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003135 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003136 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003137 SetRawInputAt(0, value);
3138 }
3139
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003140 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003141
3142 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3143 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3144 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003145 }
3146
Calin Juravle641547a2015-04-21 22:08:51 +01003147 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3148 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003149 }
3150
3151 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003152 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3153 }
3154
Calin Juravle52c48962014-12-16 17:02:57 +00003155 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003156 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003157 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003158 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003159
3160 DECLARE_INSTRUCTION(InstanceFieldGet);
3161
3162 private:
3163 const FieldInfo field_info_;
3164
3165 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3166};
3167
3168class HInstanceFieldSet : public HTemplateInstruction<2> {
3169 public:
3170 HInstanceFieldSet(HInstruction* object,
3171 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003172 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003173 MemberOffset field_offset,
3174 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003175 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003176 field_info_(field_offset, field_type, is_volatile),
3177 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003178 SetRawInputAt(0, object);
3179 SetRawInputAt(1, value);
3180 }
3181
Calin Juravle641547a2015-04-21 22:08:51 +01003182 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3183 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003184 }
3185
Calin Juravle52c48962014-12-16 17:02:57 +00003186 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003187 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003188 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003189 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003190 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003191 bool GetValueCanBeNull() const { return value_can_be_null_; }
3192 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003193
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003194 DECLARE_INSTRUCTION(InstanceFieldSet);
3195
3196 private:
3197 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003198 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003199
3200 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3201};
3202
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003203class HArrayGet : public HExpression<2> {
3204 public:
3205 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003206 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003207 SetRawInputAt(0, array);
3208 SetRawInputAt(1, index);
3209 }
3210
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003211 bool CanBeMoved() const OVERRIDE { return true; }
3212 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003213 UNUSED(other);
3214 return true;
3215 }
Calin Juravle641547a2015-04-21 22:08:51 +01003216 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3217 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003218 // TODO: We can be smarter here.
3219 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3220 // which generates the implicit null check. There are cases when these can be removed
3221 // to produce better code. If we ever add optimizations to do so we should allow an
3222 // implicit check here (as long as the address falls in the first page).
3223 return false;
3224 }
3225
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003226 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003227
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003228 HInstruction* GetArray() const { return InputAt(0); }
3229 HInstruction* GetIndex() const { return InputAt(1); }
3230
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003231 DECLARE_INSTRUCTION(ArrayGet);
3232
3233 private:
3234 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3235};
3236
3237class HArraySet : public HTemplateInstruction<3> {
3238 public:
3239 HArraySet(HInstruction* array,
3240 HInstruction* index,
3241 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003242 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003243 uint32_t dex_pc)
3244 : HTemplateInstruction(SideEffects::ChangesSomething()),
3245 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003246 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003247 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3248 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003249 SetRawInputAt(0, array);
3250 SetRawInputAt(1, index);
3251 SetRawInputAt(2, value);
3252 }
3253
Calin Juravle77520bc2015-01-12 18:45:46 +00003254 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003255 // We currently always call a runtime method to catch array store
3256 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003257 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003258 }
3259
Calin Juravle641547a2015-04-21 22:08:51 +01003260 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3261 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003262 // TODO: Same as for ArrayGet.
3263 return false;
3264 }
3265
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003266 void ClearNeedsTypeCheck() {
3267 needs_type_check_ = false;
3268 }
3269
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003270 void ClearValueCanBeNull() {
3271 value_can_be_null_ = false;
3272 }
3273
3274 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003275 bool NeedsTypeCheck() const { return needs_type_check_; }
3276
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003277 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003279 HInstruction* GetArray() const { return InputAt(0); }
3280 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003281 HInstruction* GetValue() const { return InputAt(2); }
3282
3283 Primitive::Type GetComponentType() const {
3284 // The Dex format does not type floating point index operations. Since the
3285 // `expected_component_type_` is set during building and can therefore not
3286 // be correct, we also check what is the value type. If it is a floating
3287 // point type, we must use that type.
3288 Primitive::Type value_type = GetValue()->GetType();
3289 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3290 ? value_type
3291 : expected_component_type_;
3292 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003293
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003294 DECLARE_INSTRUCTION(ArraySet);
3295
3296 private:
3297 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003298 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003299 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003300 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003301
3302 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3303};
3304
3305class HArrayLength : public HExpression<1> {
3306 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003307 explicit HArrayLength(HInstruction* array)
3308 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3309 // Note that arrays do not change length, so the instruction does not
3310 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003311 SetRawInputAt(0, array);
3312 }
3313
Calin Juravle77520bc2015-01-12 18:45:46 +00003314 bool CanBeMoved() const OVERRIDE { return true; }
3315 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003316 UNUSED(other);
3317 return true;
3318 }
Calin Juravle641547a2015-04-21 22:08:51 +01003319 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3320 return obj == InputAt(0);
3321 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003322
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003323 DECLARE_INSTRUCTION(ArrayLength);
3324
3325 private:
3326 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3327};
3328
3329class HBoundsCheck : public HExpression<2> {
3330 public:
3331 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003332 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003333 DCHECK(index->GetType() == Primitive::kPrimInt);
3334 SetRawInputAt(0, index);
3335 SetRawInputAt(1, length);
3336 }
3337
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003338 bool CanBeMoved() const OVERRIDE { return true; }
3339 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003340 UNUSED(other);
3341 return true;
3342 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003343
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003344 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003345
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003346 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003347
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003348 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003349
3350 DECLARE_INSTRUCTION(BoundsCheck);
3351
3352 private:
3353 const uint32_t dex_pc_;
3354
3355 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3356};
3357
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003358/**
3359 * Some DEX instructions are folded into multiple HInstructions that need
3360 * to stay live until the last HInstruction. This class
3361 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003362 * HInstruction stays live. `index` represents the stack location index of the
3363 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003364 */
3365class HTemporary : public HTemplateInstruction<0> {
3366 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003367 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003368
3369 size_t GetIndex() const { return index_; }
3370
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003371 Primitive::Type GetType() const OVERRIDE {
3372 // The previous instruction is the one that will be stored in the temporary location.
3373 DCHECK(GetPrevious() != nullptr);
3374 return GetPrevious()->GetType();
3375 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003376
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003377 DECLARE_INSTRUCTION(Temporary);
3378
3379 private:
3380 const size_t index_;
3381
3382 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3383};
3384
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003385class HSuspendCheck : public HTemplateInstruction<0> {
3386 public:
3387 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003388 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003389
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003390 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003391 return true;
3392 }
3393
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003394 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003395 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3396 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003397
3398 DECLARE_INSTRUCTION(SuspendCheck);
3399
3400 private:
3401 const uint32_t dex_pc_;
3402
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003403 // Only used for code generation, in order to share the same slow path between back edges
3404 // of a same loop.
3405 SlowPathCode* slow_path_;
3406
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003407 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3408};
3409
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003410/**
3411 * Instruction to load a Class object.
3412 */
3413class HLoadClass : public HExpression<0> {
3414 public:
3415 HLoadClass(uint16_t type_index,
3416 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003417 uint32_t dex_pc)
3418 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3419 type_index_(type_index),
3420 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003421 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003422 generate_clinit_check_(false),
3423 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003424
3425 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003426
3427 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3428 return other->AsLoadClass()->type_index_ == type_index_;
3429 }
3430
3431 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3432
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003433 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003434 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003435 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003436
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003437 bool NeedsEnvironment() const OVERRIDE {
3438 // Will call runtime and load the class if the class is not loaded yet.
3439 // TODO: finer grain decision.
3440 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003441 }
3442
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003443 bool MustGenerateClinitCheck() const {
3444 return generate_clinit_check_;
3445 }
3446
3447 void SetMustGenerateClinitCheck() {
3448 generate_clinit_check_ = true;
3449 }
3450
3451 bool CanCallRuntime() const {
3452 return MustGenerateClinitCheck() || !is_referrers_class_;
3453 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003454
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003455 bool CanThrow() const OVERRIDE {
3456 // May call runtime and and therefore can throw.
3457 // TODO: finer grain decision.
3458 return !is_referrers_class_;
3459 }
3460
Calin Juravleacf735c2015-02-12 15:25:22 +00003461 ReferenceTypeInfo GetLoadedClassRTI() {
3462 return loaded_class_rti_;
3463 }
3464
3465 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3466 // Make sure we only set exact types (the loaded class should never be merged).
3467 DCHECK(rti.IsExact());
3468 loaded_class_rti_ = rti;
3469 }
3470
3471 bool IsResolved() {
3472 return loaded_class_rti_.IsExact();
3473 }
3474
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003475 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3476
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003477 DECLARE_INSTRUCTION(LoadClass);
3478
3479 private:
3480 const uint16_t type_index_;
3481 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003482 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003483 // Whether this instruction must generate the initialization check.
3484 // Used for code generation.
3485 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003486
Calin Juravleacf735c2015-02-12 15:25:22 +00003487 ReferenceTypeInfo loaded_class_rti_;
3488
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003489 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3490};
3491
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003492class HLoadString : public HExpression<0> {
3493 public:
3494 HLoadString(uint32_t string_index, uint32_t dex_pc)
3495 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3496 string_index_(string_index),
3497 dex_pc_(dex_pc) {}
3498
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003499 bool CanBeMoved() const OVERRIDE { return true; }
3500
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003501 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3502 return other->AsLoadString()->string_index_ == string_index_;
3503 }
3504
3505 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3506
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003507 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003508 uint32_t GetStringIndex() const { return string_index_; }
3509
3510 // TODO: Can we deopt or debug when we resolve a string?
3511 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003512 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003513
3514 DECLARE_INSTRUCTION(LoadString);
3515
3516 private:
3517 const uint32_t string_index_;
3518 const uint32_t dex_pc_;
3519
3520 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3521};
3522
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003523/**
3524 * Performs an initialization check on its Class object input.
3525 */
3526class HClinitCheck : public HExpression<1> {
3527 public:
3528 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003529 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003530 dex_pc_(dex_pc) {
3531 SetRawInputAt(0, constant);
3532 }
3533
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003534 bool CanBeMoved() const OVERRIDE { return true; }
3535 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3536 UNUSED(other);
3537 return true;
3538 }
3539
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003540 bool NeedsEnvironment() const OVERRIDE {
3541 // May call runtime to initialize the class.
3542 return true;
3543 }
3544
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003545 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003546
3547 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3548
3549 DECLARE_INSTRUCTION(ClinitCheck);
3550
3551 private:
3552 const uint32_t dex_pc_;
3553
3554 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3555};
3556
3557class HStaticFieldGet : public HExpression<1> {
3558 public:
3559 HStaticFieldGet(HInstruction* cls,
3560 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003561 MemberOffset field_offset,
3562 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003563 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003564 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003565 SetRawInputAt(0, cls);
3566 }
3567
Calin Juravle52c48962014-12-16 17:02:57 +00003568
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003569 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003570
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003571 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003572 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3573 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003574 }
3575
3576 size_t ComputeHashCode() const OVERRIDE {
3577 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3578 }
3579
Calin Juravle52c48962014-12-16 17:02:57 +00003580 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003581 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3582 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003583 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003584
3585 DECLARE_INSTRUCTION(StaticFieldGet);
3586
3587 private:
3588 const FieldInfo field_info_;
3589
3590 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3591};
3592
3593class HStaticFieldSet : public HTemplateInstruction<2> {
3594 public:
3595 HStaticFieldSet(HInstruction* cls,
3596 HInstruction* value,
3597 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003598 MemberOffset field_offset,
3599 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003600 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003601 field_info_(field_offset, field_type, is_volatile),
3602 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003603 SetRawInputAt(0, cls);
3604 SetRawInputAt(1, value);
3605 }
3606
Calin Juravle52c48962014-12-16 17:02:57 +00003607 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003608 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3609 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003610 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003611
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003612 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003613 bool GetValueCanBeNull() const { return value_can_be_null_; }
3614 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003615
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003616 DECLARE_INSTRUCTION(StaticFieldSet);
3617
3618 private:
3619 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003620 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003621
3622 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3623};
3624
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003625// Implement the move-exception DEX instruction.
3626class HLoadException : public HExpression<0> {
3627 public:
3628 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3629
3630 DECLARE_INSTRUCTION(LoadException);
3631
3632 private:
3633 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3634};
3635
3636class HThrow : public HTemplateInstruction<1> {
3637 public:
3638 HThrow(HInstruction* exception, uint32_t dex_pc)
3639 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3640 SetRawInputAt(0, exception);
3641 }
3642
3643 bool IsControlFlow() const OVERRIDE { return true; }
3644
3645 bool NeedsEnvironment() const OVERRIDE { return true; }
3646
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003647 bool CanThrow() const OVERRIDE { return true; }
3648
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003649 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003650
3651 DECLARE_INSTRUCTION(Throw);
3652
3653 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003654 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003655
3656 DISALLOW_COPY_AND_ASSIGN(HThrow);
3657};
3658
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003659class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003660 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003661 HInstanceOf(HInstruction* object,
3662 HLoadClass* constant,
3663 bool class_is_final,
3664 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003665 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3666 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003667 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003668 dex_pc_(dex_pc) {
3669 SetRawInputAt(0, object);
3670 SetRawInputAt(1, constant);
3671 }
3672
3673 bool CanBeMoved() const OVERRIDE { return true; }
3674
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003675 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003676 return true;
3677 }
3678
3679 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003680 return false;
3681 }
3682
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003683 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003684
3685 bool IsClassFinal() const { return class_is_final_; }
3686
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003687 // Used only in code generation.
3688 bool MustDoNullCheck() const { return must_do_null_check_; }
3689 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3690
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003691 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003692
3693 private:
3694 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003695 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003696 const uint32_t dex_pc_;
3697
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003698 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3699};
3700
Calin Juravleb1498f62015-02-16 13:13:29 +00003701class HBoundType : public HExpression<1> {
3702 public:
3703 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3704 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3705 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003706 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003707 SetRawInputAt(0, input);
3708 }
3709
3710 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3711
3712 bool CanBeNull() const OVERRIDE {
3713 // `null instanceof ClassX` always return false so we can't be null.
3714 return false;
3715 }
3716
3717 DECLARE_INSTRUCTION(BoundType);
3718
3719 private:
3720 // Encodes the most upper class that this instruction can have. In other words
3721 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3722 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3723 const ReferenceTypeInfo bound_type_;
3724
3725 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3726};
3727
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003728class HCheckCast : public HTemplateInstruction<2> {
3729 public:
3730 HCheckCast(HInstruction* object,
3731 HLoadClass* constant,
3732 bool class_is_final,
3733 uint32_t dex_pc)
3734 : HTemplateInstruction(SideEffects::None()),
3735 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003736 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003737 dex_pc_(dex_pc) {
3738 SetRawInputAt(0, object);
3739 SetRawInputAt(1, constant);
3740 }
3741
3742 bool CanBeMoved() const OVERRIDE { return true; }
3743
3744 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3745 return true;
3746 }
3747
3748 bool NeedsEnvironment() const OVERRIDE {
3749 // Instruction may throw a CheckCastError.
3750 return true;
3751 }
3752
3753 bool CanThrow() const OVERRIDE { return true; }
3754
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003755 bool MustDoNullCheck() const { return must_do_null_check_; }
3756 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3757
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003758 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003759
3760 bool IsClassFinal() const { return class_is_final_; }
3761
3762 DECLARE_INSTRUCTION(CheckCast);
3763
3764 private:
3765 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003766 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003767 const uint32_t dex_pc_;
3768
3769 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003770};
3771
Calin Juravle27df7582015-04-17 19:12:31 +01003772class HMemoryBarrier : public HTemplateInstruction<0> {
3773 public:
3774 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3775 : HTemplateInstruction(SideEffects::None()),
3776 barrier_kind_(barrier_kind) {}
3777
3778 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3779
3780 DECLARE_INSTRUCTION(MemoryBarrier);
3781
3782 private:
3783 const MemBarrierKind barrier_kind_;
3784
3785 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3786};
3787
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003788class HMonitorOperation : public HTemplateInstruction<1> {
3789 public:
3790 enum OperationKind {
3791 kEnter,
3792 kExit,
3793 };
3794
3795 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3796 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3797 SetRawInputAt(0, object);
3798 }
3799
3800 // Instruction may throw a Java exception, so we need an environment.
3801 bool NeedsEnvironment() const OVERRIDE { return true; }
3802 bool CanThrow() const OVERRIDE { return true; }
3803
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003804 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003805
3806 bool IsEnter() const { return kind_ == kEnter; }
3807
3808 DECLARE_INSTRUCTION(MonitorOperation);
3809
Calin Juravle52c48962014-12-16 17:02:57 +00003810 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003811 const OperationKind kind_;
3812 const uint32_t dex_pc_;
3813
3814 private:
3815 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3816};
3817
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003818class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003819 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003820 MoveOperands(Location source,
3821 Location destination,
3822 Primitive::Type type,
3823 HInstruction* instruction)
3824 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003825
3826 Location GetSource() const { return source_; }
3827 Location GetDestination() const { return destination_; }
3828
3829 void SetSource(Location value) { source_ = value; }
3830 void SetDestination(Location value) { destination_ = value; }
3831
3832 // The parallel move resolver marks moves as "in-progress" by clearing the
3833 // destination (but not the source).
3834 Location MarkPending() {
3835 DCHECK(!IsPending());
3836 Location dest = destination_;
3837 destination_ = Location::NoLocation();
3838 return dest;
3839 }
3840
3841 void ClearPending(Location dest) {
3842 DCHECK(IsPending());
3843 destination_ = dest;
3844 }
3845
3846 bool IsPending() const {
3847 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3848 return destination_.IsInvalid() && !source_.IsInvalid();
3849 }
3850
3851 // True if this blocks a move from the given location.
3852 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003853 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003854 }
3855
3856 // A move is redundant if it's been eliminated, if its source and
3857 // destination are the same, or if its destination is unneeded.
3858 bool IsRedundant() const {
3859 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3860 }
3861
3862 // We clear both operands to indicate move that's been eliminated.
3863 void Eliminate() {
3864 source_ = destination_ = Location::NoLocation();
3865 }
3866
3867 bool IsEliminated() const {
3868 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3869 return source_.IsInvalid();
3870 }
3871
Nicolas Geoffray90218252015-04-15 11:56:51 +01003872 bool Is64BitMove() const {
3873 return Primitive::Is64BitType(type_);
3874 }
3875
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003876 HInstruction* GetInstruction() const { return instruction_; }
3877
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003878 private:
3879 Location source_;
3880 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003881 // The type this move is for.
3882 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003883 // The instruction this move is assocatied with. Null when this move is
3884 // for moving an input in the expected locations of user (including a phi user).
3885 // This is only used in debug mode, to ensure we do not connect interval siblings
3886 // in the same parallel move.
3887 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003888};
3889
3890static constexpr size_t kDefaultNumberOfMoves = 4;
3891
3892class HParallelMove : public HTemplateInstruction<0> {
3893 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003894 explicit HParallelMove(ArenaAllocator* arena)
3895 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003896
Nicolas Geoffray90218252015-04-15 11:56:51 +01003897 void AddMove(Location source,
3898 Location destination,
3899 Primitive::Type type,
3900 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003901 DCHECK(source.IsValid());
3902 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003903 if (kIsDebugBuild) {
3904 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003905 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003906 if (moves_.Get(i).GetInstruction() == instruction) {
3907 // Special case the situation where the move is for the spill slot
3908 // of the instruction.
3909 if ((GetPrevious() == instruction)
3910 || ((GetPrevious() == nullptr)
3911 && instruction->IsPhi()
3912 && instruction->GetBlock() == GetBlock())) {
3913 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3914 << "Doing parallel moves for the same instruction.";
3915 } else {
3916 DCHECK(false) << "Doing parallel moves for the same instruction.";
3917 }
3918 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003919 }
3920 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003921 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003922 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01003923 << "Overlapped destination for two moves in a parallel move: "
3924 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
3925 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003926 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003927 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003928 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003929 }
3930
3931 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003932 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003933 }
3934
3935 size_t NumMoves() const { return moves_.Size(); }
3936
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003937 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003938
3939 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003940 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003941
3942 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3943};
3944
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003945class HGraphVisitor : public ValueObject {
3946 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003947 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3948 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003949
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003950 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003951 virtual void VisitBasicBlock(HBasicBlock* block);
3952
Roland Levillain633021e2014-10-01 14:12:25 +01003953 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003954 void VisitInsertionOrder();
3955
Roland Levillain633021e2014-10-01 14:12:25 +01003956 // Visit the graph following dominator tree reverse post-order.
3957 void VisitReversePostOrder();
3958
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003959 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003960
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003961 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003962#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003963 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3964
3965 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3966
3967#undef DECLARE_VISIT_INSTRUCTION
3968
3969 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003970 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003971
3972 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3973};
3974
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003975class HGraphDelegateVisitor : public HGraphVisitor {
3976 public:
3977 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3978 virtual ~HGraphDelegateVisitor() {}
3979
3980 // Visit functions that delegate to to super class.
3981#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003982 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003983
3984 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3985
3986#undef DECLARE_VISIT_INSTRUCTION
3987
3988 private:
3989 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3990};
3991
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003992class HInsertionOrderIterator : public ValueObject {
3993 public:
3994 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3995
3996 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3997 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3998 void Advance() { ++index_; }
3999
4000 private:
4001 const HGraph& graph_;
4002 size_t index_;
4003
4004 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4005};
4006
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004007class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004008 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004009 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4010 // Check that reverse post order of the graph has been built.
4011 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4012 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004013
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004014 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4015 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004016 void Advance() { ++index_; }
4017
4018 private:
4019 const HGraph& graph_;
4020 size_t index_;
4021
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004022 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004023};
4024
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004025class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004026 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004027 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004028 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4029 // Check that reverse post order of the graph has been built.
4030 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4031 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004032
4033 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004034 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004035 void Advance() { --index_; }
4036
4037 private:
4038 const HGraph& graph_;
4039 size_t index_;
4040
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004041 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004042};
4043
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004044class HLinearPostOrderIterator : public ValueObject {
4045 public:
4046 explicit HLinearPostOrderIterator(const HGraph& graph)
4047 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4048
4049 bool Done() const { return index_ == 0; }
4050
4051 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4052
4053 void Advance() {
4054 --index_;
4055 DCHECK_GE(index_, 0U);
4056 }
4057
4058 private:
4059 const GrowableArray<HBasicBlock*>& order_;
4060 size_t index_;
4061
4062 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4063};
4064
4065class HLinearOrderIterator : public ValueObject {
4066 public:
4067 explicit HLinearOrderIterator(const HGraph& graph)
4068 : order_(graph.GetLinearOrder()), index_(0) {}
4069
4070 bool Done() const { return index_ == order_.Size(); }
4071 HBasicBlock* Current() const { return order_.Get(index_); }
4072 void Advance() { ++index_; }
4073
4074 private:
4075 const GrowableArray<HBasicBlock*>& order_;
4076 size_t index_;
4077
4078 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4079};
4080
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004081// Iterator over the blocks that art part of the loop. Includes blocks part
4082// of an inner loop. The order in which the blocks are iterated is on their
4083// block id.
4084class HBlocksInLoopIterator : public ValueObject {
4085 public:
4086 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4087 : blocks_in_loop_(info.GetBlocks()),
4088 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4089 index_(0) {
4090 if (!blocks_in_loop_.IsBitSet(index_)) {
4091 Advance();
4092 }
4093 }
4094
4095 bool Done() const { return index_ == blocks_.Size(); }
4096 HBasicBlock* Current() const { return blocks_.Get(index_); }
4097 void Advance() {
4098 ++index_;
4099 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4100 if (blocks_in_loop_.IsBitSet(index_)) {
4101 break;
4102 }
4103 }
4104 }
4105
4106 private:
4107 const BitVector& blocks_in_loop_;
4108 const GrowableArray<HBasicBlock*>& blocks_;
4109 size_t index_;
4110
4111 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4112};
4113
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004114inline int64_t Int64FromConstant(HConstant* constant) {
4115 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4116 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4117 : constant->AsLongConstant()->GetValue();
4118}
4119
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004120} // namespace art
4121
4122#endif // ART_COMPILER_OPTIMIZING_NODES_H_