blob: 2cffe02d4335816e45e32193d008d3b37dd38f8c [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010038class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000039class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010040class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000041class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000042class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000043class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000045class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000046class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000048class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010050class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010051class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000052class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010053class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055
56static const int kDefaultNumberOfBlocks = 8;
57static const int kDefaultNumberOfSuccessors = 2;
58static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010059static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000060static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000061
Calin Juravle9aec02f2014-11-18 23:06:35 +000062static constexpr uint32_t kMaxIntShiftValue = 0x1f;
63static constexpr uint64_t kMaxLongShiftValue = 0x3f;
64
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010065static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
66
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010067static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
68
Dave Allison20dfc792014-06-16 20:44:29 -070069enum IfCondition {
70 kCondEQ,
71 kCondNE,
72 kCondLT,
73 kCondLE,
74 kCondGT,
75 kCondGE,
76};
77
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010078class HInstructionList {
79 public:
80 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
81
82 void AddInstruction(HInstruction* instruction);
83 void RemoveInstruction(HInstruction* instruction);
84
David Brazdilc3d743f2015-04-22 13:40:50 +010085 // Insert `instruction` before/after an existing instruction `cursor`.
86 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
87 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
88
Roland Levillain6b469232014-09-25 10:10:38 +010089 // Return true if this list contains `instruction`.
90 bool Contains(HInstruction* instruction) const;
91
Roland Levillainccc07a92014-09-16 14:48:16 +010092 // Return true if `instruction1` is found before `instruction2` in
93 // this instruction list and false otherwise. Abort if none
94 // of these instructions is found.
95 bool FoundBefore(const HInstruction* instruction1,
96 const HInstruction* instruction2) const;
97
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000098 bool IsEmpty() const { return first_instruction_ == nullptr; }
99 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
100
101 // Update the block of all instructions to be `block`.
102 void SetBlockOfInstructions(HBasicBlock* block) const;
103
104 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
105 void Add(const HInstructionList& instruction_list);
106
David Brazdil2d7352b2015-04-20 14:52:42 +0100107 // Return the number of instructions in the list. This is an expensive operation.
108 size_t CountSize() const;
109
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110 private:
111 HInstruction* first_instruction_;
112 HInstruction* last_instruction_;
113
114 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000115 friend class HGraph;
116 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100117 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100118 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100119
120 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
121};
122
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700124class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000125 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100126 HGraph(ArenaAllocator* arena,
127 const DexFile& dex_file,
128 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100129 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100131 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100132 bool debuggable = false,
133 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000134 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000135 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100136 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100137 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700138 entry_block_(nullptr),
139 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100140 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100141 number_of_vregs_(0),
142 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000143 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400144 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000145 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000146 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100147 dex_file_(dex_file),
148 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100149 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100150 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100151 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000153 cached_null_constant_(nullptr),
154 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000155 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
156 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100157 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
158 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000159
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000160 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100161 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100162 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000163
David Brazdil69ba7b72015-06-23 18:27:30 +0100164 bool IsInSsaForm() const { return in_ssa_form_; }
165
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000166 HBasicBlock* GetEntryBlock() const { return entry_block_; }
167 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100168 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000169
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000170 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
171 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000172
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000173 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100174
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 // Try building the SSA form of this graph, with dominance computation and loop
176 // recognition. Returns whether it was successful in doing all these steps.
177 bool TryBuildingSsa() {
178 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000179 // The SSA builder requires loops to all be natural. Specifically, the dead phi
180 // elimination phase checks the consistency of the graph when doing a post-order
181 // visit for eliminating dead phis: a dead phi can only have loop header phi
182 // users remaining when being visited.
183 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000184 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100185 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000186 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 }
188
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100189 void ComputeDominanceInformation();
190 void ClearDominanceInformation();
191
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000192 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000193 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100194 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000195
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000196 // Analyze all natural loops in this graph. Returns false if one
197 // loop is not natural, that is the header does not dominate the
198 // back edge.
199 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100200
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000201 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
202 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
203
Mingyao Yang3584bce2015-05-19 16:01:59 -0700204 // Need to add a couple of blocks to test if the loop body is entered and
205 // put deoptimization instructions, etc.
206 void TransformLoopHeaderForBCE(HBasicBlock* header);
207
David Brazdil2d7352b2015-04-20 14:52:42 +0100208 // Removes `block` from the graph.
209 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000210
David Brazdilfc6a86a2015-06-26 10:33:45 +0000211 // Splits the edge between `block` and `successor` while preserving the
212 // indices in the predecessor/successor lists. If there are multiple edges
213 // between the blocks, the lowest indices are used.
214 // Returns the new block which is empty and has the same dex pc as `successor`.
215 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
216
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100217 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
218 void SimplifyLoop(HBasicBlock* header);
219
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000220 int32_t GetNextInstructionId() {
221 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000222 return current_instruction_id_++;
223 }
224
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225 int32_t GetCurrentInstructionId() const {
226 return current_instruction_id_;
227 }
228
229 void SetCurrentInstructionId(int32_t id) {
230 current_instruction_id_ = id;
231 }
232
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100233 uint16_t GetMaximumNumberOfOutVRegs() const {
234 return maximum_number_of_out_vregs_;
235 }
236
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000237 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
238 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100239 }
240
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100241 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
242 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
243 }
244
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000245 void UpdateTemporariesVRegSlots(size_t slots) {
246 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100247 }
248
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000249 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100250 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000251 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100252 }
253
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100254 void SetNumberOfVRegs(uint16_t number_of_vregs) {
255 number_of_vregs_ = number_of_vregs;
256 }
257
258 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100259 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100260 return number_of_vregs_;
261 }
262
263 void SetNumberOfInVRegs(uint16_t value) {
264 number_of_in_vregs_ = value;
265 }
266
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100267 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100268 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100269 return number_of_vregs_ - number_of_in_vregs_;
270 }
271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
273 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100274 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100275
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100276 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
277 return linear_order_;
278 }
279
Mark Mendell1152c922015-04-24 17:06:35 -0400280 bool HasBoundsChecks() const {
281 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800282 }
283
Mark Mendell1152c922015-04-24 17:06:35 -0400284 void SetHasBoundsChecks(bool value) {
285 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800286 }
287
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100288 bool ShouldGenerateConstructorBarrier() const {
289 return should_generate_constructor_barrier_;
290 }
291
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000292 bool IsDebuggable() const { return debuggable_; }
293
David Brazdil8d5b8b22015-03-24 10:51:52 +0000294 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000295 // already, it is created and inserted into the graph. This method is only for
296 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000297 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000298 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000299 HIntConstant* GetIntConstant(int32_t value) {
300 return CreateConstant(value, &cached_int_constants_);
301 }
302 HLongConstant* GetLongConstant(int64_t value) {
303 return CreateConstant(value, &cached_long_constants_);
304 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000305 HFloatConstant* GetFloatConstant(float value) {
306 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
307 }
308 HDoubleConstant* GetDoubleConstant(double value) {
309 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
310 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000311
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100312 HCurrentMethod* GetCurrentMethod();
313
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000314 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100315
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100316 const DexFile& GetDexFile() const {
317 return dex_file_;
318 }
319
320 uint32_t GetMethodIdx() const {
321 return method_idx_;
322 }
323
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100324 InvokeType GetInvokeType() const {
325 return invoke_type_;
326 }
327
David Brazdil2d7352b2015-04-20 14:52:42 +0100328 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000329 void VisitBlockForDominatorTree(HBasicBlock* block,
330 HBasicBlock* predecessor,
331 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100332 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000333 void VisitBlockForBackEdges(HBasicBlock* block,
334 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100335 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000336 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100337 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000338
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000339 template <class InstructionType, typename ValueType>
340 InstructionType* CreateConstant(ValueType value,
341 ArenaSafeMap<ValueType, InstructionType*>* cache) {
342 // Try to find an existing constant of the given value.
343 InstructionType* constant = nullptr;
344 auto cached_constant = cache->find(value);
345 if (cached_constant != cache->end()) {
346 constant = cached_constant->second;
347 }
348
349 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100350 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000351 if (constant == nullptr || constant->GetBlock() == nullptr) {
352 constant = new (arena_) InstructionType(value);
353 cache->Overwrite(value, constant);
354 InsertConstant(constant);
355 }
356 return constant;
357 }
358
David Brazdil8d5b8b22015-03-24 10:51:52 +0000359 void InsertConstant(HConstant* instruction);
360
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000361 // Cache a float constant into the graph. This method should only be
362 // called by the SsaBuilder when creating "equivalent" instructions.
363 void CacheFloatConstant(HFloatConstant* constant);
364
365 // See CacheFloatConstant comment.
366 void CacheDoubleConstant(HDoubleConstant* constant);
367
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000368 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000369
370 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000371 GrowableArray<HBasicBlock*> blocks_;
372
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100373 // List of blocks to perform a reverse post order tree traversal.
374 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000375
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100376 // List of blocks to perform a linear order tree traversal.
377 GrowableArray<HBasicBlock*> linear_order_;
378
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000379 HBasicBlock* entry_block_;
380 HBasicBlock* exit_block_;
381
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100382 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100383 uint16_t maximum_number_of_out_vregs_;
384
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100385 // The number of virtual registers in this method. Contains the parameters.
386 uint16_t number_of_vregs_;
387
388 // The number of virtual registers used by parameters of this method.
389 uint16_t number_of_in_vregs_;
390
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000391 // Number of vreg size slots that the temporaries use (used in baseline compiler).
392 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100393
Mark Mendell1152c922015-04-24 17:06:35 -0400394 // Has bounds checks. We can totally skip BCE if it's false.
395 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800396
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000397 // Indicates whether the graph should be compiled in a way that
398 // ensures full debuggability. If false, we can apply more
399 // aggressive optimizations that may limit the level of debugging.
400 const bool debuggable_;
401
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000402 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000403 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000404
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100405 // The dex file from which the method is from.
406 const DexFile& dex_file_;
407
408 // The method index in the dex file.
409 const uint32_t method_idx_;
410
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100411 // If inlined, this encodes how the callee is being invoked.
412 const InvokeType invoke_type_;
413
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100414 // Whether the graph has been transformed to SSA form. Only used
415 // in debug mode to ensure we are not using properties only valid
416 // for non-SSA form (like the number of temporaries).
417 bool in_ssa_form_;
418
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100419 const bool should_generate_constructor_barrier_;
420
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421 const InstructionSet instruction_set_;
422
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000423 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000424 HNullConstant* cached_null_constant_;
425 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000426 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000427 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000428 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000429
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100430 HCurrentMethod* cached_current_method_;
431
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000432 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100433 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000434 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000435 DISALLOW_COPY_AND_ASSIGN(HGraph);
436};
437
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700438class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000439 public:
440 HLoopInformation(HBasicBlock* header, HGraph* graph)
441 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100442 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100443 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100444 // Make bit vector growable, as the number of blocks may change.
445 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100446
447 HBasicBlock* GetHeader() const {
448 return header_;
449 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000450
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000451 void SetHeader(HBasicBlock* block) {
452 header_ = block;
453 }
454
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100455 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
456 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
457 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
458
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000459 void AddBackEdge(HBasicBlock* back_edge) {
460 back_edges_.Add(back_edge);
461 }
462
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100463 void RemoveBackEdge(HBasicBlock* back_edge) {
464 back_edges_.Delete(back_edge);
465 }
466
David Brazdil46e2a392015-03-16 17:31:52 +0000467 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000469 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100470 }
471 return false;
472 }
473
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000474 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000475 return back_edges_.Size();
476 }
477
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100478 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100479
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100480 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
481 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100482 }
483
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100484 // Returns the lifetime position of the back edge that has the
485 // greatest lifetime position.
486 size_t GetLifetimeEnd() const;
487
488 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
489 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
490 if (back_edges_.Get(i) == existing) {
491 back_edges_.Put(i, new_back_edge);
492 return;
493 }
494 }
495 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100496 }
497
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100498 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100499 // that is the header dominates the back edge.
500 bool Populate();
501
David Brazdila4b8c212015-05-07 09:59:30 +0100502 // Reanalyzes the loop by removing loop info from its blocks and re-running
503 // Populate(). If there are no back edges left, the loop info is completely
504 // removed as well as its SuspendCheck instruction. It must be run on nested
505 // inner loops first.
506 void Update();
507
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100508 // Returns whether this loop information contains `block`.
509 // Note that this loop information *must* be populated before entering this function.
510 bool Contains(const HBasicBlock& block) const;
511
512 // Returns whether this loop information is an inner loop of `other`.
513 // Note that `other` *must* be populated before entering this function.
514 bool IsIn(const HLoopInformation& other) const;
515
516 const ArenaBitVector& GetBlocks() const { return blocks_; }
517
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000518 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000519 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000520
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000521 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 // Internal recursive implementation of `Populate`.
523 void PopulateRecursive(HBasicBlock* block);
524
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000525 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100526 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000527 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100528 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000529
530 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
531};
532
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100533static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100534static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100535
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000536// A block in a method. Contains the list of instructions represented
537// as a double linked list. Each block knows its predecessors and
538// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100539
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700540class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000541 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100542 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000543 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000544 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
545 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000546 loop_information_(nullptr),
547 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100548 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100549 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100550 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100551 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000552 lifetime_end_(kNoLifetime),
553 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000554
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100555 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
556 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000557 }
558
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
560 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000561 }
562
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100563 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
564 return dominated_blocks_;
565 }
566
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100567 bool IsEntryBlock() const {
568 return graph_->GetEntryBlock() == this;
569 }
570
571 bool IsExitBlock() const {
572 return graph_->GetExitBlock() == this;
573 }
574
David Brazdil46e2a392015-03-16 17:31:52 +0000575 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000576 bool IsSingleTryBoundary() const;
577
578 // Returns true if this block emits nothing but a jump.
579 bool IsSingleJump() const {
580 HLoopInformation* loop_info = GetLoopInformation();
581 return (IsSingleGoto() || IsSingleTryBoundary())
582 // Back edges generate a suspend check.
583 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
584 }
David Brazdil46e2a392015-03-16 17:31:52 +0000585
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000586 void AddBackEdge(HBasicBlock* back_edge) {
587 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000588 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000589 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100590 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591 loop_information_->AddBackEdge(back_edge);
592 }
593
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000594 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000595 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000596
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000597 int GetBlockId() const { return block_id_; }
598 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000599
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000600 HBasicBlock* GetDominator() const { return dominator_; }
601 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100602 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100603 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000604 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
605 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
606 if (dominated_blocks_.Get(i) == existing) {
607 dominated_blocks_.Put(i, new_block);
608 return;
609 }
610 }
611 LOG(FATAL) << "Unreachable";
612 UNREACHABLE();
613 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100614 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000615
616 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100617 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618 }
619
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100620 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
621 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100622 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100623 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100624 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
625 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626
627 void AddSuccessor(HBasicBlock* block) {
628 successors_.Add(block);
629 block->predecessors_.Add(this);
630 }
631
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100632 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
633 size_t successor_index = GetSuccessorIndexOf(existing);
634 DCHECK_NE(successor_index, static_cast<size_t>(-1));
635 existing->RemovePredecessor(this);
636 new_block->predecessors_.Add(this);
637 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000638 }
639
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000640 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
641 size_t predecessor_index = GetPredecessorIndexOf(existing);
642 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
643 existing->RemoveSuccessor(this);
644 new_block->successors_.Add(this);
645 predecessors_.Put(predecessor_index, new_block);
646 }
647
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100648 // Insert `this` between `predecessor` and `successor. This method
649 // preserves the indicies, and will update the first edge found between
650 // `predecessor` and `successor`.
651 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
652 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
653 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
654 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
655 DCHECK_NE(successor_index, static_cast<size_t>(-1));
656 successor->predecessors_.Put(predecessor_index, this);
657 predecessor->successors_.Put(successor_index, this);
658 successors_.Add(successor);
659 predecessors_.Add(predecessor);
660 }
661
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100662 void RemovePredecessor(HBasicBlock* block) {
663 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100664 }
665
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000666 void RemoveSuccessor(HBasicBlock* block) {
667 successors_.Delete(block);
668 }
669
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670 void ClearAllPredecessors() {
671 predecessors_.Reset();
672 }
673
674 void AddPredecessor(HBasicBlock* block) {
675 predecessors_.Add(block);
676 block->successors_.Add(this);
677 }
678
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100679 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100680 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100681 HBasicBlock* temp = predecessors_.Get(0);
682 predecessors_.Put(0, predecessors_.Get(1));
683 predecessors_.Put(1, temp);
684 }
685
David Brazdil769c9e52015-04-27 13:54:09 +0100686 void SwapSuccessors() {
687 DCHECK_EQ(successors_.Size(), 2u);
688 HBasicBlock* temp = successors_.Get(0);
689 successors_.Put(0, successors_.Get(1));
690 successors_.Put(1, temp);
691 }
692
David Brazdilfc6a86a2015-06-26 10:33:45 +0000693 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100694 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
695 if (predecessors_.Get(i) == predecessor) {
696 return i;
697 }
698 }
699 return -1;
700 }
701
David Brazdilfc6a86a2015-06-26 10:33:45 +0000702 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100703 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
704 if (successors_.Get(i) == successor) {
705 return i;
706 }
707 }
708 return -1;
709 }
710
David Brazdilfc6a86a2015-06-26 10:33:45 +0000711 HBasicBlock* GetSinglePredecessor() const {
712 DCHECK_EQ(GetPredecessors().Size(), 1u);
713 return GetPredecessors().Get(0);
714 }
715
716 HBasicBlock* GetSingleSuccessor() const {
717 DCHECK_EQ(GetSuccessors().Size(), 1u);
718 return GetSuccessors().Get(0);
719 }
720
721 // Returns whether the first occurrence of `predecessor` in the list of
722 // predecessors is at index `idx`.
723 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
724 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
725 return GetPredecessorIndexOf(predecessor) == idx;
726 }
727
728 // Returns whether successor at index `idx` is an exception handler.
729 bool IsExceptionalSuccessor(size_t idx) const;
730
731 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100732 // created, latter block. Note that this method will add the block to the
733 // graph, create a Goto at the end of the former block and will create an edge
734 // between the blocks. It will not, however, update the reverse post order or
735 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000736 HBasicBlock* SplitBefore(HInstruction* cursor);
737
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000738 // Split the block into two blocks just after `cursor`. Returns the newly
739 // created block. Note that this method just updates raw block information,
740 // like predecessors, successors, dominators, and instruction list. It does not
741 // update the graph, reverse post order, loop information, nor make sure the
742 // blocks are consistent (for example ending with a control flow instruction).
743 HBasicBlock* SplitAfter(HInstruction* cursor);
744
745 // Merge `other` at the end of `this`. Successors and dominated blocks of
746 // `other` are changed to be successors and dominated blocks of `this`. Note
747 // that this method does not update the graph, reverse post order, loop
748 // information, nor make sure the blocks are consistent (for example ending
749 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100750 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751
752 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
753 // of `this` are moved to `other`.
754 // Note that this method does not update the graph, reverse post order, loop
755 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000756 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000757 void ReplaceWith(HBasicBlock* other);
758
David Brazdil2d7352b2015-04-20 14:52:42 +0100759 // Merge `other` at the end of `this`. This method updates loops, reverse post
760 // order, links to predecessors, successors, dominators and deletes the block
761 // from the graph. The two blocks must be successive, i.e. `this` the only
762 // predecessor of `other` and vice versa.
763 void MergeWith(HBasicBlock* other);
764
765 // Disconnects `this` from all its predecessors, successors and dominator,
766 // removes it from all loops it is included in and eventually from the graph.
767 // The block must not dominate any other block. Predecessors and successors
768 // are safely updated.
769 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000770
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000771 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100772 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100773 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100774 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100775 // Replace instruction `initial` with `replacement` within this block.
776 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
777 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100778 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100779 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000780 // RemoveInstruction and RemovePhi delete a given instruction from the respective
781 // instruction list. With 'ensure_safety' set to true, it verifies that the
782 // instruction is not in use and removes it from the use lists of its inputs.
783 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
784 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100785 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786
787 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100788 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100789 }
790
Roland Levillain6b879dd2014-09-22 17:13:44 +0100791 bool IsLoopPreHeaderFirstPredecessor() const {
792 DCHECK(IsLoopHeader());
793 DCHECK(!GetPredecessors().IsEmpty());
794 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
795 }
796
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100797 HLoopInformation* GetLoopInformation() const {
798 return loop_information_;
799 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000801 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000803 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 void SetInLoop(HLoopInformation* info) {
805 if (IsLoopHeader()) {
806 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100807 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 loop_information_ = info;
809 } else if (loop_information_->Contains(*info->GetHeader())) {
810 // Block is currently part of an outer loop. Make it part of this inner loop.
811 // Note that a non loop header having a loop information means this loop information
812 // has already been populated
813 loop_information_ = info;
814 } else {
815 // Block is part of an inner loop. Do not update the loop information.
816 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
817 // at this point, because this method is being called while populating `info`.
818 }
819 }
820
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000821 // Raw update of the loop information.
822 void SetLoopInformation(HLoopInformation* info) {
823 loop_information_ = info;
824 }
825
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100826 bool IsInLoop() const { return loop_information_ != nullptr; }
827
David Brazdila4b8c212015-05-07 09:59:30 +0100828 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100829 bool Dominates(HBasicBlock* block) const;
830
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100831 size_t GetLifetimeStart() const { return lifetime_start_; }
832 size_t GetLifetimeEnd() const { return lifetime_end_; }
833
834 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
835 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
836
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100837 uint32_t GetDexPc() const { return dex_pc_; }
838
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000839 bool IsCatchBlock() const { return is_catch_block_; }
840 void SetIsCatchBlock() { is_catch_block_ = true; }
841
David Brazdil8d5b8b22015-03-24 10:51:52 +0000842 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000843 bool EndsWithIf() const;
844 bool HasSinglePhi() const;
845
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000846 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000848 GrowableArray<HBasicBlock*> predecessors_;
849 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100850 HInstructionList instructions_;
851 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000852 HLoopInformation* loop_information_;
853 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100854 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000855 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100856 // The dex program counter of the first instruction of this block.
857 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100858 size_t lifetime_start_;
859 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000860 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000861
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000862 friend class HGraph;
863 friend class HInstruction;
864
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000865 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
866};
867
David Brazdilb2bd1c52015-03-25 11:17:37 +0000868// Iterates over the LoopInformation of all loops which contain 'block'
869// from the innermost to the outermost.
870class HLoopInformationOutwardIterator : public ValueObject {
871 public:
872 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
873 : current_(block.GetLoopInformation()) {}
874
875 bool Done() const { return current_ == nullptr; }
876
877 void Advance() {
878 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100879 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000880 }
881
882 HLoopInformation* Current() const {
883 DCHECK(!Done());
884 return current_;
885 }
886
887 private:
888 HLoopInformation* current_;
889
890 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
891};
892
Alexandre Ramesef20f712015-06-09 10:29:30 +0100893#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100894 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000895 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000896 M(ArrayGet, Instruction) \
897 M(ArrayLength, Instruction) \
898 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100899 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000900 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000901 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000902 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100903 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000904 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100905 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100906 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700907 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000908 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000909 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000910 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100911 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000912 M(Exit, Instruction) \
913 M(FloatConstant, Constant) \
914 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100915 M(GreaterThan, Condition) \
916 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100917 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000918 M(InstanceFieldGet, Instruction) \
919 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000920 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100921 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000922 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000923 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100924 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000925 M(LessThan, Condition) \
926 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000927 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000928 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100929 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000930 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100931 M(Local, Instruction) \
932 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100933 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000934 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000935 M(Mul, BinaryOperation) \
936 M(Neg, UnaryOperation) \
937 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100938 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100939 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000940 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000941 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000942 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000943 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100944 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000945 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100946 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000947 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100948 M(Return, Instruction) \
949 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000950 M(Shl, BinaryOperation) \
951 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100952 M(StaticFieldGet, Instruction) \
953 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100954 M(StoreLocal, Instruction) \
955 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100956 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000957 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000958 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +0000959 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000960 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000961 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000962 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963
Alexandre Ramesef20f712015-06-09 10:29:30 +0100964#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
965
966#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
967
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100968#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
969
Alexandre Ramesef20f712015-06-09 10:29:30 +0100970#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
971
972#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
973
974#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
975 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
976 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
977 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100978 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +0100979 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
980 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
981
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100982#define FOR_EACH_INSTRUCTION(M) \
983 FOR_EACH_CONCRETE_INSTRUCTION(M) \
984 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100985 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100986 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100987 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700988
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100989#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000990FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
991#undef FORWARD_DECLARATION
992
Roland Levillainccc07a92014-09-16 14:48:16 +0100993#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000994 InstructionKind GetKind() const OVERRIDE { return k##type; } \
995 const char* DebugName() const OVERRIDE { return #type; } \
996 const H##type* As##type() const OVERRIDE { return this; } \
997 H##type* As##type() OVERRIDE { return this; } \
998 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100999 return other->Is##type(); \
1000 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001001 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001002
David Brazdiled596192015-01-23 10:39:45 +00001003template <typename T> class HUseList;
1004
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001005template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001006class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001007 public:
David Brazdiled596192015-01-23 10:39:45 +00001008 HUseListNode* GetPrevious() const { return prev_; }
1009 HUseListNode* GetNext() const { return next_; }
1010 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001011 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001012 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001013
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001014 private:
David Brazdiled596192015-01-23 10:39:45 +00001015 HUseListNode(T user, size_t index)
1016 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1017
1018 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001019 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001020 HUseListNode<T>* prev_;
1021 HUseListNode<T>* next_;
1022
1023 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001024
1025 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1026};
1027
David Brazdiled596192015-01-23 10:39:45 +00001028template <typename T>
1029class HUseList : public ValueObject {
1030 public:
1031 HUseList() : first_(nullptr) {}
1032
1033 void Clear() {
1034 first_ = nullptr;
1035 }
1036
1037 // Adds a new entry at the beginning of the use list and returns
1038 // the newly created node.
1039 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001040 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001041 if (IsEmpty()) {
1042 first_ = new_node;
1043 } else {
1044 first_->prev_ = new_node;
1045 new_node->next_ = first_;
1046 first_ = new_node;
1047 }
1048 return new_node;
1049 }
1050
1051 HUseListNode<T>* GetFirst() const {
1052 return first_;
1053 }
1054
1055 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001056 DCHECK(node != nullptr);
1057 DCHECK(Contains(node));
1058
David Brazdiled596192015-01-23 10:39:45 +00001059 if (node->prev_ != nullptr) {
1060 node->prev_->next_ = node->next_;
1061 }
1062 if (node->next_ != nullptr) {
1063 node->next_->prev_ = node->prev_;
1064 }
1065 if (node == first_) {
1066 first_ = node->next_;
1067 }
1068 }
1069
David Brazdil1abb4192015-02-17 18:33:36 +00001070 bool Contains(const HUseListNode<T>* node) const {
1071 if (node == nullptr) {
1072 return false;
1073 }
1074 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1075 if (current == node) {
1076 return true;
1077 }
1078 }
1079 return false;
1080 }
1081
David Brazdiled596192015-01-23 10:39:45 +00001082 bool IsEmpty() const {
1083 return first_ == nullptr;
1084 }
1085
1086 bool HasOnlyOneUse() const {
1087 return first_ != nullptr && first_->next_ == nullptr;
1088 }
1089
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001090 size_t SizeSlow() const {
1091 size_t count = 0;
1092 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1093 ++count;
1094 }
1095 return count;
1096 }
1097
David Brazdiled596192015-01-23 10:39:45 +00001098 private:
1099 HUseListNode<T>* first_;
1100};
1101
1102template<typename T>
1103class HUseIterator : public ValueObject {
1104 public:
1105 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1106
1107 bool Done() const { return current_ == nullptr; }
1108
1109 void Advance() {
1110 DCHECK(!Done());
1111 current_ = current_->GetNext();
1112 }
1113
1114 HUseListNode<T>* Current() const {
1115 DCHECK(!Done());
1116 return current_;
1117 }
1118
1119 private:
1120 HUseListNode<T>* current_;
1121
1122 friend class HValue;
1123};
1124
David Brazdil1abb4192015-02-17 18:33:36 +00001125// This class is used by HEnvironment and HInstruction classes to record the
1126// instructions they use and pointers to the corresponding HUseListNodes kept
1127// by the used instructions.
1128template <typename T>
1129class HUserRecord : public ValueObject {
1130 public:
1131 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1132 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1133
1134 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1135 : instruction_(old_record.instruction_), use_node_(use_node) {
1136 DCHECK(instruction_ != nullptr);
1137 DCHECK(use_node_ != nullptr);
1138 DCHECK(old_record.use_node_ == nullptr);
1139 }
1140
1141 HInstruction* GetInstruction() const { return instruction_; }
1142 HUseListNode<T>* GetUseNode() const { return use_node_; }
1143
1144 private:
1145 // Instruction used by the user.
1146 HInstruction* instruction_;
1147
1148 // Corresponding entry in the use list kept by 'instruction_'.
1149 HUseListNode<T>* use_node_;
1150};
1151
Calin Juravle27df7582015-04-17 19:12:31 +01001152// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1153// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1154// flag is consider.
1155// - DependsOn suggests that there is a real dependency between side effects but it only
1156// checks DependendsOnSomething flag.
1157//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001158// Represents the side effects an instruction may have.
1159class SideEffects : public ValueObject {
1160 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001161 SideEffects() : flags_(0) {}
1162
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001163 static SideEffects None() {
1164 return SideEffects(0);
1165 }
1166
1167 static SideEffects All() {
1168 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1169 }
1170
1171 static SideEffects ChangesSomething() {
1172 return SideEffects((1 << kFlagChangesCount) - 1);
1173 }
1174
1175 static SideEffects DependsOnSomething() {
1176 int count = kFlagDependsOnCount - kFlagChangesCount;
1177 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1178 }
1179
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001180 SideEffects Union(SideEffects other) const {
1181 return SideEffects(flags_ | other.flags_);
1182 }
1183
Roland Levillain72bceff2014-09-15 18:29:00 +01001184 bool HasSideEffects() const {
1185 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1186 return (flags_ & all_bits_set) != 0;
1187 }
1188
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001189 bool HasAllSideEffects() const {
1190 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1191 return all_bits_set == (flags_ & all_bits_set);
1192 }
1193
1194 bool DependsOn(SideEffects other) const {
1195 size_t depends_flags = other.ComputeDependsFlags();
1196 return (flags_ & depends_flags) != 0;
1197 }
1198
1199 bool HasDependencies() const {
1200 int count = kFlagDependsOnCount - kFlagChangesCount;
1201 size_t all_bits_set = (1 << count) - 1;
1202 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1203 }
1204
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001205 private:
1206 static constexpr int kFlagChangesSomething = 0;
1207 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1208
1209 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1210 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1211
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001212 explicit SideEffects(size_t flags) : flags_(flags) {}
1213
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001214 size_t ComputeDependsFlags() const {
1215 return flags_ << kFlagChangesCount;
1216 }
1217
1218 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001219};
1220
David Brazdiled596192015-01-23 10:39:45 +00001221// A HEnvironment object contains the values of virtual registers at a given location.
1222class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1223 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001224 HEnvironment(ArenaAllocator* arena,
1225 size_t number_of_vregs,
1226 const DexFile& dex_file,
1227 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001228 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001229 InvokeType invoke_type,
1230 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001231 : vregs_(arena, number_of_vregs),
1232 locations_(arena, number_of_vregs),
1233 parent_(nullptr),
1234 dex_file_(dex_file),
1235 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001236 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001237 invoke_type_(invoke_type),
1238 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001239 vregs_.SetSize(number_of_vregs);
1240 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001241 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001242 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001243
1244 locations_.SetSize(number_of_vregs);
1245 for (size_t i = 0; i < number_of_vregs; ++i) {
1246 locations_.Put(i, Location());
1247 }
1248 }
1249
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001250 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001251 : HEnvironment(arena,
1252 to_copy.Size(),
1253 to_copy.GetDexFile(),
1254 to_copy.GetMethodIdx(),
1255 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001256 to_copy.GetInvokeType(),
1257 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001258
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001259 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001260 if (parent_ != nullptr) {
1261 parent_->SetAndCopyParentChain(allocator, parent);
1262 } else {
1263 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1264 parent_->CopyFrom(parent);
1265 if (parent->GetParent() != nullptr) {
1266 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1267 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001268 }
David Brazdiled596192015-01-23 10:39:45 +00001269 }
1270
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001271 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1272 void CopyFrom(HEnvironment* environment);
1273
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001274 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1275 // input to the loop phi instead. This is for inserting instructions that
1276 // require an environment (like HDeoptimization) in the loop pre-header.
1277 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001278
1279 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001280 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001281 }
1282
1283 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001284 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001285 }
1286
David Brazdil1abb4192015-02-17 18:33:36 +00001287 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001288
1289 size_t Size() const { return vregs_.Size(); }
1290
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001291 HEnvironment* GetParent() const { return parent_; }
1292
1293 void SetLocationAt(size_t index, Location location) {
1294 locations_.Put(index, location);
1295 }
1296
1297 Location GetLocationAt(size_t index) const {
1298 return locations_.Get(index);
1299 }
1300
1301 uint32_t GetDexPc() const {
1302 return dex_pc_;
1303 }
1304
1305 uint32_t GetMethodIdx() const {
1306 return method_idx_;
1307 }
1308
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001309 InvokeType GetInvokeType() const {
1310 return invoke_type_;
1311 }
1312
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001313 const DexFile& GetDexFile() const {
1314 return dex_file_;
1315 }
1316
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001317 HInstruction* GetHolder() const {
1318 return holder_;
1319 }
1320
David Brazdiled596192015-01-23 10:39:45 +00001321 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001322 // Record instructions' use entries of this environment for constant-time removal.
1323 // It should only be called by HInstruction when a new environment use is added.
1324 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1325 DCHECK(env_use->GetUser() == this);
1326 size_t index = env_use->GetIndex();
1327 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1328 }
David Brazdiled596192015-01-23 10:39:45 +00001329
David Brazdil1abb4192015-02-17 18:33:36 +00001330 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001331 GrowableArray<Location> locations_;
1332 HEnvironment* parent_;
1333 const DexFile& dex_file_;
1334 const uint32_t method_idx_;
1335 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001336 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001337
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001338 // The instruction that holds this environment. Only used in debug mode
1339 // to ensure the graph is consistent.
1340 HInstruction* const holder_;
1341
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001342 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001343
1344 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1345};
1346
Calin Juravleacf735c2015-02-12 15:25:22 +00001347class ReferenceTypeInfo : ValueObject {
1348 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001349 typedef Handle<mirror::Class> TypeHandle;
1350
1351 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1353 if (type_handle->IsObjectClass()) {
1354 // Override the type handle to be consistent with the case when we get to
1355 // Top but don't have the Object class available. It avoids having to guess
1356 // what value the type_handle has when it's Top.
1357 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1358 } else {
1359 return ReferenceTypeInfo(type_handle, is_exact, false);
1360 }
1361 }
1362
1363 static ReferenceTypeInfo CreateTop(bool is_exact) {
1364 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001365 }
1366
1367 bool IsExact() const { return is_exact_; }
1368 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001369 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1370 return !IsTop() && GetTypeHandle()->IsInterface();
1371 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001372
1373 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1374
Calin Juravleb1498f62015-02-16 13:13:29 +00001375 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001376 if (IsTop()) {
1377 // Top (equivalent for java.lang.Object) is supertype of anything.
1378 return true;
1379 }
1380 if (rti.IsTop()) {
1381 // If we get here `this` is not Top() so it can't be a supertype.
1382 return false;
1383 }
1384 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1385 }
1386
1387 // Returns true if the type information provide the same amount of details.
1388 // Note that it does not mean that the instructions have the same actual type
1389 // (e.g. tops are equal but they can be the result of a merge).
1390 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1391 if (IsExact() != rti.IsExact()) {
1392 return false;
1393 }
1394 if (IsTop() && rti.IsTop()) {
1395 // `Top` means java.lang.Object, so the types are equivalent.
1396 return true;
1397 }
1398 if (IsTop() || rti.IsTop()) {
1399 // If only one is top or object than they are not equivalent.
1400 // NB: We need this extra check because the type_handle of `Top` is invalid
1401 // and we cannot inspect its reference.
1402 return false;
1403 }
1404
1405 // Finally check the types.
1406 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1407 }
1408
1409 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001410 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1411 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1412 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1413
Calin Juravleacf735c2015-02-12 15:25:22 +00001414 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001415 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001416 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001417 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001418 bool is_exact_;
1419 // A true value here means that the object type should be java.lang.Object.
1420 // We don't have access to the corresponding mirror object every time so this
1421 // flag acts as a substitute. When true, the TypeHandle refers to a null
1422 // pointer and should not be used.
1423 bool is_top_;
1424};
1425
1426std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1427
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001428class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001429 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001431 : previous_(nullptr),
1432 next_(nullptr),
1433 block_(nullptr),
1434 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001435 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001436 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001437 locations_(nullptr),
1438 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001439 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001440 side_effects_(side_effects),
1441 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001442
Dave Allison20dfc792014-06-16 20:44:29 -07001443 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001444
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001445#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001446 enum InstructionKind {
1447 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1448 };
1449#undef DECLARE_KIND
1450
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001451 HInstruction* GetNext() const { return next_; }
1452 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453
Calin Juravle77520bc2015-01-12 18:45:46 +00001454 HInstruction* GetNextDisregardingMoves() const;
1455 HInstruction* GetPreviousDisregardingMoves() const;
1456
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001457 HBasicBlock* GetBlock() const { return block_; }
1458 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001459 bool IsInBlock() const { return block_ != nullptr; }
1460 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001461 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001462
Roland Levillain6b879dd2014-09-22 17:13:44 +01001463 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001464 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001465
1466 virtual void Accept(HGraphVisitor* visitor) = 0;
1467 virtual const char* DebugName() const = 0;
1468
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001469 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001470 void SetRawInputAt(size_t index, HInstruction* input) {
1471 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1472 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001473
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001474 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001475 virtual uint32_t GetDexPc() const {
1476 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1477 " does not need an environment";
1478 UNREACHABLE();
1479 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001480 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001481 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001482 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001483
Calin Juravle10e244f2015-01-26 18:54:32 +00001484 // Does not apply for all instructions, but having this at top level greatly
1485 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001486 virtual bool CanBeNull() const {
1487 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1488 return true;
1489 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001490
Calin Juravle641547a2015-04-21 22:08:51 +01001491 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1492 UNUSED(obj);
1493 return false;
1494 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001495
Calin Juravleacf735c2015-02-12 15:25:22 +00001496 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001497 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001498 reference_type_info_ = reference_type_info;
1499 }
1500
Calin Juravle61d544b2015-02-23 16:46:57 +00001501 ReferenceTypeInfo GetReferenceTypeInfo() const {
1502 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1503 return reference_type_info_;
1504 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001505
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001506 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001507 DCHECK(user != nullptr);
1508 HUseListNode<HInstruction*>* use =
1509 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1510 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001511 }
1512
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001513 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001514 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001515 HUseListNode<HEnvironment*>* env_use =
1516 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1517 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001518 }
1519
David Brazdil1abb4192015-02-17 18:33:36 +00001520 void RemoveAsUserOfInput(size_t input) {
1521 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1522 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1523 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001524
David Brazdil1abb4192015-02-17 18:33:36 +00001525 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1526 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001527
David Brazdiled596192015-01-23 10:39:45 +00001528 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1529 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001530 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001531 bool HasOnlyOneNonEnvironmentUse() const {
1532 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1533 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001534
Roland Levillain6c82d402014-10-13 16:10:27 +01001535 // Does this instruction strictly dominate `other_instruction`?
1536 // Returns false if this instruction and `other_instruction` are the same.
1537 // Aborts if this instruction and `other_instruction` are both phis.
1538 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001539
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001540 int GetId() const { return id_; }
1541 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001542
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001543 int GetSsaIndex() const { return ssa_index_; }
1544 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1545 bool HasSsaIndex() const { return ssa_index_ != -1; }
1546
1547 bool HasEnvironment() const { return environment_ != nullptr; }
1548 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001549 // Set the `environment_` field. Raw because this method does not
1550 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001551 void SetRawEnvironment(HEnvironment* environment) {
1552 DCHECK(environment_ == nullptr);
1553 DCHECK_EQ(environment->GetHolder(), this);
1554 environment_ = environment;
1555 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001556
1557 // Set the environment of this instruction, copying it from `environment`. While
1558 // copying, the uses lists are being updated.
1559 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001560 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001561 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001562 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001563 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001564 if (environment->GetParent() != nullptr) {
1565 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1566 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001567 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001568
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001569 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1570 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001571 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001572 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001573 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001574 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001575 if (environment->GetParent() != nullptr) {
1576 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1577 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001578 }
1579
Nicolas Geoffray39468442014-09-02 15:17:15 +01001580 // Returns the number of entries in the environment. Typically, that is the
1581 // number of dex registers in a method. It could be more in case of inlining.
1582 size_t EnvironmentSize() const;
1583
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001584 LocationSummary* GetLocations() const { return locations_; }
1585 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001586
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001587 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001588 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001589
Alexandre Rames188d4312015-04-09 18:30:21 +01001590 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1591 // uses of this instruction by `other` are *not* updated.
1592 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1593 ReplaceWith(other);
1594 other->ReplaceInput(this, use_index);
1595 }
1596
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001597 // Move `this` instruction before `cursor`.
1598 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001599
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001600#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001601 bool Is##type() const { return (As##type() != nullptr); } \
1602 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001603 virtual H##type* As##type() { return nullptr; }
1604
1605 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1606#undef INSTRUCTION_TYPE_CHECK
1607
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001608 // Returns whether the instruction can be moved within the graph.
1609 virtual bool CanBeMoved() const { return false; }
1610
1611 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001612 virtual bool InstructionTypeEquals(HInstruction* other) const {
1613 UNUSED(other);
1614 return false;
1615 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001616
1617 // Returns whether any data encoded in the two instructions is equal.
1618 // This method does not look at the inputs. Both instructions must be
1619 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001620 virtual bool InstructionDataEquals(HInstruction* other) const {
1621 UNUSED(other);
1622 return false;
1623 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001624
1625 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001626 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001627 // 2) Their inputs are identical.
1628 bool Equals(HInstruction* other) const;
1629
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001630 virtual InstructionKind GetKind() const = 0;
1631
1632 virtual size_t ComputeHashCode() const {
1633 size_t result = GetKind();
1634 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1635 result = (result * 31) + InputAt(i)->GetId();
1636 }
1637 return result;
1638 }
1639
1640 SideEffects GetSideEffects() const { return side_effects_; }
1641
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001642 size_t GetLifetimePosition() const { return lifetime_position_; }
1643 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1644 LiveInterval* GetLiveInterval() const { return live_interval_; }
1645 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1646 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1647
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001648 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1649
1650 // Returns whether the code generation of the instruction will require to have access
1651 // to the current method. Such instructions are:
1652 // (1): Instructions that require an environment, as calling the runtime requires
1653 // to walk the stack and have the current method stored at a specific stack address.
1654 // (2): Object literals like classes and strings, that are loaded from the dex cache
1655 // fields of the current method.
1656 bool NeedsCurrentMethod() const {
1657 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1658 }
1659
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001660 virtual bool NeedsDexCache() const { return false; }
1661
David Brazdil1abb4192015-02-17 18:33:36 +00001662 protected:
1663 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1664 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1665
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001666 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001667 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1668
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001669 HInstruction* previous_;
1670 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001671 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001672
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001673 // An instruction gets an id when it is added to the graph.
1674 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001675 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001676 int id_;
1677
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001678 // When doing liveness analysis, instructions that have uses get an SSA index.
1679 int ssa_index_;
1680
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001681 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001682 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001683
1684 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001685 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001686
Nicolas Geoffray39468442014-09-02 15:17:15 +01001687 // The environment associated with this instruction. Not null if the instruction
1688 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001689 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001690
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001691 // Set by the code generator.
1692 LocationSummary* locations_;
1693
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001694 // Set by the liveness analysis.
1695 LiveInterval* live_interval_;
1696
1697 // Set by the liveness analysis, this is the position in a linear
1698 // order of blocks where this instruction's live interval start.
1699 size_t lifetime_position_;
1700
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001701 const SideEffects side_effects_;
1702
Calin Juravleacf735c2015-02-12 15:25:22 +00001703 // TODO: for primitive types this should be marked as invalid.
1704 ReferenceTypeInfo reference_type_info_;
1705
David Brazdil1abb4192015-02-17 18:33:36 +00001706 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001707 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001708 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001709 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001710 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001711
1712 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1713};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001714std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001715
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001716class HInputIterator : public ValueObject {
1717 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001718 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001719
1720 bool Done() const { return index_ == instruction_->InputCount(); }
1721 HInstruction* Current() const { return instruction_->InputAt(index_); }
1722 void Advance() { index_++; }
1723
1724 private:
1725 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001726 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001727
1728 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1729};
1730
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001731class HInstructionIterator : public ValueObject {
1732 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001733 explicit HInstructionIterator(const HInstructionList& instructions)
1734 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001735 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001736 }
1737
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001738 bool Done() const { return instruction_ == nullptr; }
1739 HInstruction* Current() const { return instruction_; }
1740 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001741 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001742 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001743 }
1744
1745 private:
1746 HInstruction* instruction_;
1747 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001748
1749 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001750};
1751
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001752class HBackwardInstructionIterator : public ValueObject {
1753 public:
1754 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1755 : instruction_(instructions.last_instruction_) {
1756 next_ = Done() ? nullptr : instruction_->GetPrevious();
1757 }
1758
1759 bool Done() const { return instruction_ == nullptr; }
1760 HInstruction* Current() const { return instruction_; }
1761 void Advance() {
1762 instruction_ = next_;
1763 next_ = Done() ? nullptr : instruction_->GetPrevious();
1764 }
1765
1766 private:
1767 HInstruction* instruction_;
1768 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001769
1770 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001771};
1772
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001773// An embedded container with N elements of type T. Used (with partial
1774// specialization for N=0) because embedded arrays cannot have size 0.
1775template<typename T, intptr_t N>
1776class EmbeddedArray {
1777 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001778 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001779
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001780 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001781
1782 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001784 return elements_[i];
1785 }
1786
1787 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001788 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001789 return elements_[i];
1790 }
1791
1792 const T& At(intptr_t i) const {
1793 return (*this)[i];
1794 }
1795
1796 void SetAt(intptr_t i, const T& val) {
1797 (*this)[i] = val;
1798 }
1799
1800 private:
1801 T elements_[N];
1802};
1803
1804template<typename T>
1805class EmbeddedArray<T, 0> {
1806 public:
1807 intptr_t length() const { return 0; }
1808 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001809 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001811 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001812 }
1813 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001814 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001815 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001816 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001817 }
1818};
1819
1820template<intptr_t N>
1821class HTemplateInstruction: public HInstruction {
1822 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001823 HTemplateInstruction<N>(SideEffects side_effects)
1824 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001825 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001827 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001828
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001829 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001830 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1831
1832 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1833 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001834 }
1835
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001836 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001837 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001838
1839 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840};
1841
Dave Allison20dfc792014-06-16 20:44:29 -07001842template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001843class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001844 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001845 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1846 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001847 virtual ~HExpression() {}
1848
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001849 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001850
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001851 protected:
1852 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001853};
1854
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001855// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1856// instruction that branches to the exit block.
1857class HReturnVoid : public HTemplateInstruction<0> {
1858 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001859 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001861 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001862
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001863 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001864
1865 private:
1866 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1867};
1868
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001869// Represents dex's RETURN opcodes. A HReturn is a control flow
1870// instruction that branches to the exit block.
1871class HReturn : public HTemplateInstruction<1> {
1872 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001873 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001874 SetRawInputAt(0, value);
1875 }
1876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001877 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001878
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001879 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001880
1881 private:
1882 DISALLOW_COPY_AND_ASSIGN(HReturn);
1883};
1884
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001885// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001886// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001887// exit block.
1888class HExit : public HTemplateInstruction<0> {
1889 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001890 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001891
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001892 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001893
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001894 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001895
1896 private:
1897 DISALLOW_COPY_AND_ASSIGN(HExit);
1898};
1899
1900// Jumps from one block to another.
1901class HGoto : public HTemplateInstruction<0> {
1902 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001903 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1904
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001905 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001907 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001908 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001909 }
1910
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001911 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001912
1913 private:
1914 DISALLOW_COPY_AND_ASSIGN(HGoto);
1915};
1916
Dave Allison20dfc792014-06-16 20:44:29 -07001917
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001918// Conditional branch. A block ending with an HIf instruction must have
1919// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001920class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001921 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001922 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923 SetRawInputAt(0, input);
1924 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001925
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001926 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001927
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001928 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001929 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001930 }
1931
1932 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001933 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001934 }
1935
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001936 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001937
1938 private:
1939 DISALLOW_COPY_AND_ASSIGN(HIf);
1940};
1941
David Brazdilfc6a86a2015-06-26 10:33:45 +00001942
1943// Abstract instruction which marks the beginning and/or end of a try block and
1944// links it to the respective exception handlers. Behaves the same as a Goto in
1945// non-exceptional control flow.
1946// Normal-flow successor is stored at index zero, exception handlers under
1947// higher indices in no particular order.
1948class HTryBoundary : public HTemplateInstruction<0> {
1949 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01001950 enum BoundaryKind {
1951 kEntry,
1952 kExit,
1953 };
1954
1955 explicit HTryBoundary(BoundaryKind kind)
1956 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00001957
1958 bool IsControlFlow() const OVERRIDE { return true; }
1959
1960 // Returns the block's non-exceptional successor (index zero).
1961 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
1962
1963 // Returns whether `handler` is among its exception handlers (non-zero index
1964 // successors).
1965 bool HasExceptionHandler(HBasicBlock* handler) const {
1966 DCHECK(handler->IsCatchBlock());
1967 return GetBlock()->GetSuccessors().Contains(handler, /* start_from */ 1);
1968 }
1969
1970 // Returns whether successor at index `idx` is an exception handler.
1971 bool IsExceptionalSuccessor(size_t idx) const {
1972 DCHECK_LT(idx, GetBlock()->GetSuccessors().Size());
1973 bool is_handler = (idx != 0);
1974 DCHECK(!is_handler || GetBlock()->GetSuccessors().Get(idx)->IsCatchBlock());
1975 return is_handler;
1976 }
1977
1978 // If not present already, adds `handler` to its block's list of exception
1979 // handlers.
1980 void AddExceptionHandler(HBasicBlock* handler) {
1981 if (!HasExceptionHandler(handler)) {
1982 GetBlock()->AddSuccessor(handler);
1983 }
1984 }
1985
David Brazdil56e1acc2015-06-30 15:41:36 +01001986 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001987
1988 DECLARE_INSTRUCTION(TryBoundary);
1989
1990 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01001991 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001992
1993 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
1994};
1995
1996
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001997// Deoptimize to interpreter, upon checking a condition.
1998class HDeoptimize : public HTemplateInstruction<1> {
1999 public:
2000 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2001 : HTemplateInstruction(SideEffects::None()),
2002 dex_pc_(dex_pc) {
2003 SetRawInputAt(0, cond);
2004 }
2005
2006 bool NeedsEnvironment() const OVERRIDE { return true; }
2007 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002008 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002009
2010 DECLARE_INSTRUCTION(Deoptimize);
2011
2012 private:
2013 uint32_t dex_pc_;
2014
2015 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2016};
2017
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002018// Represents the ArtMethod that was passed as a first argument to
2019// the method. It is used by instructions that depend on it, like
2020// instructions that work with the dex cache.
2021class HCurrentMethod : public HExpression<0> {
2022 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002023 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002024
2025 DECLARE_INSTRUCTION(CurrentMethod);
2026
2027 private:
2028 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2029};
2030
Roland Levillain88cb1752014-10-20 16:36:47 +01002031class HUnaryOperation : public HExpression<1> {
2032 public:
2033 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2034 : HExpression(result_type, SideEffects::None()) {
2035 SetRawInputAt(0, input);
2036 }
2037
2038 HInstruction* GetInput() const { return InputAt(0); }
2039 Primitive::Type GetResultType() const { return GetType(); }
2040
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002041 bool CanBeMoved() const OVERRIDE { return true; }
2042 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002043 UNUSED(other);
2044 return true;
2045 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002046
Roland Levillain9240d6a2014-10-20 16:47:04 +01002047 // Try to statically evaluate `operation` and return a HConstant
2048 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002049 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002050 HConstant* TryStaticEvaluation() const;
2051
2052 // Apply this operation to `x`.
2053 virtual int32_t Evaluate(int32_t x) const = 0;
2054 virtual int64_t Evaluate(int64_t x) const = 0;
2055
Roland Levillain88cb1752014-10-20 16:36:47 +01002056 DECLARE_INSTRUCTION(UnaryOperation);
2057
2058 private:
2059 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2060};
2061
Dave Allison20dfc792014-06-16 20:44:29 -07002062class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002063 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002064 HBinaryOperation(Primitive::Type result_type,
2065 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002066 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002067 SetRawInputAt(0, left);
2068 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002069 }
2070
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002071 HInstruction* GetLeft() const { return InputAt(0); }
2072 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002073 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002074
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002075 virtual bool IsCommutative() const { return false; }
2076
2077 // Put constant on the right.
2078 // Returns whether order is changed.
2079 bool OrderInputsWithConstantOnTheRight() {
2080 HInstruction* left = InputAt(0);
2081 HInstruction* right = InputAt(1);
2082 if (left->IsConstant() && !right->IsConstant()) {
2083 ReplaceInput(right, 0);
2084 ReplaceInput(left, 1);
2085 return true;
2086 }
2087 return false;
2088 }
2089
2090 // Order inputs by instruction id, but favor constant on the right side.
2091 // This helps GVN for commutative ops.
2092 void OrderInputs() {
2093 DCHECK(IsCommutative());
2094 HInstruction* left = InputAt(0);
2095 HInstruction* right = InputAt(1);
2096 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2097 return;
2098 }
2099 if (OrderInputsWithConstantOnTheRight()) {
2100 return;
2101 }
2102 // Order according to instruction id.
2103 if (left->GetId() > right->GetId()) {
2104 ReplaceInput(right, 0);
2105 ReplaceInput(left, 1);
2106 }
2107 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002108
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002109 bool CanBeMoved() const OVERRIDE { return true; }
2110 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002111 UNUSED(other);
2112 return true;
2113 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002114
Roland Levillain9240d6a2014-10-20 16:47:04 +01002115 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002116 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002117 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002118 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002119
2120 // Apply this operation to `x` and `y`.
2121 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2122 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2123
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002124 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002125 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002126 HConstant* GetConstantRight() const;
2127
2128 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002129 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002130 HInstruction* GetLeastConstantLeft() const;
2131
Roland Levillainccc07a92014-09-16 14:48:16 +01002132 DECLARE_INSTRUCTION(BinaryOperation);
2133
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002134 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002135 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2136};
2137
Dave Allison20dfc792014-06-16 20:44:29 -07002138class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002139 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002140 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002141 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
2142 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002143
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002144 bool NeedsMaterialization() const { return needs_materialization_; }
2145 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002146
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002147 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002148 // `instruction`, and disregard moves in between.
2149 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002150
Dave Allison20dfc792014-06-16 20:44:29 -07002151 DECLARE_INSTRUCTION(Condition);
2152
2153 virtual IfCondition GetCondition() const = 0;
2154
2155 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002156 // For register allocation purposes, returns whether this instruction needs to be
2157 // materialized (that is, not just be in the processor flags).
2158 bool needs_materialization_;
2159
Dave Allison20dfc792014-06-16 20:44:29 -07002160 DISALLOW_COPY_AND_ASSIGN(HCondition);
2161};
2162
2163// Instruction to check if two inputs are equal to each other.
2164class HEqual : public HCondition {
2165 public:
2166 HEqual(HInstruction* first, HInstruction* second)
2167 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002168
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002169 bool IsCommutative() const OVERRIDE { return true; }
2170
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002171 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002172 return x == y ? 1 : 0;
2173 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002174 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002175 return x == y ? 1 : 0;
2176 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002177
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002178 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002179
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002180 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002181 return kCondEQ;
2182 }
2183
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002184 private:
2185 DISALLOW_COPY_AND_ASSIGN(HEqual);
2186};
2187
Dave Allison20dfc792014-06-16 20:44:29 -07002188class HNotEqual : public HCondition {
2189 public:
2190 HNotEqual(HInstruction* first, HInstruction* second)
2191 : HCondition(first, second) {}
2192
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002193 bool IsCommutative() const OVERRIDE { return true; }
2194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002195 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002196 return x != y ? 1 : 0;
2197 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002198 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002199 return x != y ? 1 : 0;
2200 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002201
Dave Allison20dfc792014-06-16 20:44:29 -07002202 DECLARE_INSTRUCTION(NotEqual);
2203
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002204 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002205 return kCondNE;
2206 }
2207
2208 private:
2209 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2210};
2211
2212class HLessThan : public HCondition {
2213 public:
2214 HLessThan(HInstruction* first, HInstruction* second)
2215 : HCondition(first, second) {}
2216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002217 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002218 return x < y ? 1 : 0;
2219 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002220 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002221 return x < y ? 1 : 0;
2222 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002223
Dave Allison20dfc792014-06-16 20:44:29 -07002224 DECLARE_INSTRUCTION(LessThan);
2225
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002226 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002227 return kCondLT;
2228 }
2229
2230 private:
2231 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2232};
2233
2234class HLessThanOrEqual : public HCondition {
2235 public:
2236 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2237 : HCondition(first, second) {}
2238
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002239 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002240 return x <= y ? 1 : 0;
2241 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002242 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002243 return x <= y ? 1 : 0;
2244 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002245
Dave Allison20dfc792014-06-16 20:44:29 -07002246 DECLARE_INSTRUCTION(LessThanOrEqual);
2247
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002248 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002249 return kCondLE;
2250 }
2251
2252 private:
2253 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2254};
2255
2256class HGreaterThan : public HCondition {
2257 public:
2258 HGreaterThan(HInstruction* first, HInstruction* second)
2259 : HCondition(first, second) {}
2260
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002261 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002262 return x > y ? 1 : 0;
2263 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002264 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002265 return x > y ? 1 : 0;
2266 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002267
Dave Allison20dfc792014-06-16 20:44:29 -07002268 DECLARE_INSTRUCTION(GreaterThan);
2269
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002270 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002271 return kCondGT;
2272 }
2273
2274 private:
2275 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2276};
2277
2278class HGreaterThanOrEqual : public HCondition {
2279 public:
2280 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2281 : HCondition(first, second) {}
2282
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002283 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002284 return x >= y ? 1 : 0;
2285 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002286 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002287 return x >= y ? 1 : 0;
2288 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002289
Dave Allison20dfc792014-06-16 20:44:29 -07002290 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2291
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002292 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002293 return kCondGE;
2294 }
2295
2296 private:
2297 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2298};
2299
2300
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002301// Instruction to check how two inputs compare to each other.
2302// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2303class HCompare : public HBinaryOperation {
2304 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002305 // The bias applies for floating point operations and indicates how NaN
2306 // comparisons are treated:
2307 enum Bias {
2308 kNoBias, // bias is not applicable (i.e. for long operation)
2309 kGtBias, // return 1 for NaN comparisons
2310 kLtBias, // return -1 for NaN comparisons
2311 };
2312
Alexey Frunze4dda3372015-06-01 18:31:49 -07002313 HCompare(Primitive::Type type,
2314 HInstruction* first,
2315 HInstruction* second,
2316 Bias bias,
2317 uint32_t dex_pc)
2318 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002319 DCHECK_EQ(type, first->GetType());
2320 DCHECK_EQ(type, second->GetType());
2321 }
2322
Calin Juravleddb7df22014-11-25 20:56:51 +00002323 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002324 return
2325 x == y ? 0 :
2326 x > y ? 1 :
2327 -1;
2328 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002329
2330 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002331 return
2332 x == y ? 0 :
2333 x > y ? 1 :
2334 -1;
2335 }
2336
Calin Juravleddb7df22014-11-25 20:56:51 +00002337 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2338 return bias_ == other->AsCompare()->bias_;
2339 }
2340
2341 bool IsGtBias() { return bias_ == kGtBias; }
2342
Alexey Frunze4dda3372015-06-01 18:31:49 -07002343 uint32_t GetDexPc() const { return dex_pc_; }
2344
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002345 DECLARE_INSTRUCTION(Compare);
2346
2347 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002348 const Bias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002349 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002350
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002351 DISALLOW_COPY_AND_ASSIGN(HCompare);
2352};
2353
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002354// A local in the graph. Corresponds to a Dex register.
2355class HLocal : public HTemplateInstruction<0> {
2356 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002357 explicit HLocal(uint16_t reg_number)
2358 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002359
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002360 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002361
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002362 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002363
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002364 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365 // The Dex register number.
2366 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002367
2368 DISALLOW_COPY_AND_ASSIGN(HLocal);
2369};
2370
2371// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002372class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002373 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002374 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002375 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002376 SetRawInputAt(0, local);
2377 }
2378
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002379 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2380
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002381 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002382
2383 private:
2384 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2385};
2386
2387// Store a value in a given local. This instruction has two inputs: the value
2388// and the local.
2389class HStoreLocal : public HTemplateInstruction<2> {
2390 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002391 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002392 SetRawInputAt(0, local);
2393 SetRawInputAt(1, value);
2394 }
2395
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002396 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2397
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002398 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002399
2400 private:
2401 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2402};
2403
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002404class HConstant : public HExpression<0> {
2405 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002406 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2407
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002408 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002409
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002410 virtual bool IsMinusOne() const { return false; }
2411 virtual bool IsZero() const { return false; }
2412 virtual bool IsOne() const { return false; }
2413
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002414 DECLARE_INSTRUCTION(Constant);
2415
2416 private:
2417 DISALLOW_COPY_AND_ASSIGN(HConstant);
2418};
2419
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002420class HFloatConstant : public HConstant {
2421 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002422 float GetValue() const { return value_; }
2423
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002424 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002425 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2426 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002427 }
2428
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002429 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002430
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002431 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002432 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002433 }
2434 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002435 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002436 }
2437 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002438 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2439 }
2440 bool IsNaN() const {
2441 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002442 }
2443
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002444 DECLARE_INSTRUCTION(FloatConstant);
2445
2446 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002447 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002448 explicit HFloatConstant(int32_t value)
2449 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002450
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002451 const float value_;
2452
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002453 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002454 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002455 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002456 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2457};
2458
2459class HDoubleConstant : public HConstant {
2460 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002461 double GetValue() const { return value_; }
2462
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002463 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002464 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2465 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002466 }
2467
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002468 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002469
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002470 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002471 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002472 }
2473 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002474 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002475 }
2476 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002477 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2478 }
2479 bool IsNaN() const {
2480 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002481 }
2482
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002483 DECLARE_INSTRUCTION(DoubleConstant);
2484
2485 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002486 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002487 explicit HDoubleConstant(int64_t value)
2488 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002489
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002490 const double value_;
2491
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002492 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002493 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002494 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002495 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2496};
2497
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002498class HNullConstant : public HConstant {
2499 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002500 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2501 return true;
2502 }
2503
2504 size_t ComputeHashCode() const OVERRIDE { return 0; }
2505
2506 DECLARE_INSTRUCTION(NullConstant);
2507
2508 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002509 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2510
2511 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002512 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2513};
2514
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002515// Constants of the type int. Those can be from Dex instructions, or
2516// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002517class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002518 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002519 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002520
Calin Juravle61d544b2015-02-23 16:46:57 +00002521 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002522 return other->AsIntConstant()->value_ == value_;
2523 }
2524
Calin Juravle61d544b2015-02-23 16:46:57 +00002525 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2526
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002527 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2528 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2529 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2530
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002531 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002532
2533 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002534 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2535
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002536 const int32_t value_;
2537
David Brazdil8d5b8b22015-03-24 10:51:52 +00002538 friend class HGraph;
2539 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002540 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002541 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2542};
2543
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002544class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002545 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002546 int64_t GetValue() const { return value_; }
2547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002548 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002549 return other->AsLongConstant()->value_ == value_;
2550 }
2551
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002552 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002553
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002554 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2555 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2556 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2557
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002558 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002559
2560 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002561 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2562
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002563 const int64_t value_;
2564
David Brazdil8d5b8b22015-03-24 10:51:52 +00002565 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002566 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2567};
2568
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002569enum class Intrinsics {
2570#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2571#include "intrinsics_list.h"
2572 kNone,
2573 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2574#undef INTRINSICS_LIST
2575#undef OPTIMIZING_INTRINSICS
2576};
2577std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2578
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002579class HInvoke : public HInstruction {
2580 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002581 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002582
2583 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2584 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002585 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002586
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002587 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002588 SetRawInputAt(index, argument);
2589 }
2590
Roland Levillain3e3d7332015-04-28 11:00:54 +01002591 // Return the number of arguments. This number can be lower than
2592 // the number of inputs returned by InputCount(), as some invoke
2593 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2594 // inputs at the end of their list of inputs.
2595 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002597 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002598
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002599 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002600
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002601 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002602 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002603
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002604 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2605
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002606 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002607 return intrinsic_;
2608 }
2609
2610 void SetIntrinsic(Intrinsics intrinsic) {
2611 intrinsic_ = intrinsic;
2612 }
2613
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002614 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002615 return GetEnvironment()->GetParent() != nullptr;
2616 }
2617
2618 bool CanThrow() const OVERRIDE { return true; }
2619
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002620 DECLARE_INSTRUCTION(Invoke);
2621
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002622 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002623 HInvoke(ArenaAllocator* arena,
2624 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002625 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002626 Primitive::Type return_type,
2627 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002628 uint32_t dex_method_index,
2629 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002630 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002631 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002632 inputs_(arena, number_of_arguments),
2633 return_type_(return_type),
2634 dex_pc_(dex_pc),
2635 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002636 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002637 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002638 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2639 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002640 }
2641
David Brazdil1abb4192015-02-17 18:33:36 +00002642 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2643 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2644 inputs_.Put(index, input);
2645 }
2646
Roland Levillain3e3d7332015-04-28 11:00:54 +01002647 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002648 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002649 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002650 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002651 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002652 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002653 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002654
2655 private:
2656 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2657};
2658
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002659class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002660 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002661 // Requirements of this method call regarding the class
2662 // initialization (clinit) check of its declaring class.
2663 enum class ClinitCheckRequirement {
2664 kNone, // Class already initialized.
2665 kExplicit, // Static call having explicit clinit check as last input.
2666 kImplicit, // Static call implicitly requiring a clinit check.
2667 };
2668
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002669 HInvokeStaticOrDirect(ArenaAllocator* arena,
2670 uint32_t number_of_arguments,
2671 Primitive::Type return_type,
2672 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002673 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002674 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002675 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002676 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002677 InvokeType invoke_type,
2678 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002679 : HInvoke(arena,
2680 number_of_arguments,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002681 // There is one extra argument for the HCurrentMethod node, and
2682 // potentially one other if the clinit check is explicit.
2683 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002684 return_type,
2685 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002686 dex_method_index,
2687 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002688 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002689 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002690 clinit_check_requirement_(clinit_check_requirement),
2691 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002692
Calin Juravle641547a2015-04-21 22:08:51 +01002693 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2694 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002695 // We access the method via the dex cache so we can't do an implicit null check.
2696 // TODO: for intrinsics we can generate implicit null checks.
2697 return false;
2698 }
2699
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002700 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002701 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002702 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002703 bool IsStringInit() const { return string_init_offset_ != 0; }
2704 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002705 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002706
Roland Levillain4c0eb422015-04-24 16:43:49 +01002707 // Is this instruction a call to a static method?
2708 bool IsStatic() const {
2709 return GetInvokeType() == kStatic;
2710 }
2711
Roland Levillain3e3d7332015-04-28 11:00:54 +01002712 // Remove the art::HLoadClass instruction set as last input by
2713 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2714 // the initial art::HClinitCheck instruction (only relevant for
2715 // static calls with explicit clinit check).
2716 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002717 DCHECK(IsStaticWithExplicitClinitCheck());
2718 size_t last_input_index = InputCount() - 1;
2719 HInstruction* last_input = InputAt(last_input_index);
2720 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002721 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002722 RemoveAsUserOfInput(last_input_index);
2723 inputs_.DeleteAt(last_input_index);
2724 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2725 DCHECK(IsStaticWithImplicitClinitCheck());
2726 }
2727
2728 // Is this a call to a static method whose declaring class has an
2729 // explicit intialization check in the graph?
2730 bool IsStaticWithExplicitClinitCheck() const {
2731 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2732 }
2733
2734 // Is this a call to a static method whose declaring class has an
2735 // implicit intialization check requirement?
2736 bool IsStaticWithImplicitClinitCheck() const {
2737 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2738 }
2739
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002740 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002741
Roland Levillain4c0eb422015-04-24 16:43:49 +01002742 protected:
2743 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2744 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2745 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2746 HInstruction* input = input_record.GetInstruction();
2747 // `input` is the last input of a static invoke marked as having
2748 // an explicit clinit check. It must either be:
2749 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2750 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2751 DCHECK(input != nullptr);
2752 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2753 }
2754 return input_record;
2755 }
2756
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002757 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002758 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002759 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002760 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002761 // Thread entrypoint offset for string init method if this is a string init invoke.
2762 // Note that there are multiple string init methods, each having its own offset.
2763 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002764
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002765 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002766};
2767
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002768class HInvokeVirtual : public HInvoke {
2769 public:
2770 HInvokeVirtual(ArenaAllocator* arena,
2771 uint32_t number_of_arguments,
2772 Primitive::Type return_type,
2773 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002774 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002775 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002776 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002777 vtable_index_(vtable_index) {}
2778
Calin Juravle641547a2015-04-21 22:08:51 +01002779 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002780 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002781 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002782 }
2783
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002784 uint32_t GetVTableIndex() const { return vtable_index_; }
2785
2786 DECLARE_INSTRUCTION(InvokeVirtual);
2787
2788 private:
2789 const uint32_t vtable_index_;
2790
2791 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2792};
2793
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002794class HInvokeInterface : public HInvoke {
2795 public:
2796 HInvokeInterface(ArenaAllocator* arena,
2797 uint32_t number_of_arguments,
2798 Primitive::Type return_type,
2799 uint32_t dex_pc,
2800 uint32_t dex_method_index,
2801 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002802 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002803 imt_index_(imt_index) {}
2804
Calin Juravle641547a2015-04-21 22:08:51 +01002805 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002806 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002807 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002808 }
2809
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002810 uint32_t GetImtIndex() const { return imt_index_; }
2811 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2812
2813 DECLARE_INSTRUCTION(InvokeInterface);
2814
2815 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002816 const uint32_t imt_index_;
2817
2818 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2819};
2820
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002821class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002822 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002823 HNewInstance(HCurrentMethod* current_method,
2824 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002825 uint16_t type_index,
2826 const DexFile& dex_file,
2827 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002828 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2829 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002830 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002831 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002832 entrypoint_(entrypoint) {
2833 SetRawInputAt(0, current_method);
2834 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002835
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002836 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002837 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002838 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002839
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002840 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002841 bool NeedsEnvironment() const OVERRIDE { return true; }
2842 // It may throw when called on:
2843 // - interfaces
2844 // - abstract/innaccessible/unknown classes
2845 // TODO: optimize when possible.
2846 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002847
Calin Juravle10e244f2015-01-26 18:54:32 +00002848 bool CanBeNull() const OVERRIDE { return false; }
2849
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002850 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2851
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002852 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002853
2854 private:
2855 const uint32_t dex_pc_;
2856 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002857 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002858 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002859
2860 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2861};
2862
Roland Levillain88cb1752014-10-20 16:36:47 +01002863class HNeg : public HUnaryOperation {
2864 public:
2865 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2866 : HUnaryOperation(result_type, input) {}
2867
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002868 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2869 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002870
Roland Levillain88cb1752014-10-20 16:36:47 +01002871 DECLARE_INSTRUCTION(Neg);
2872
2873 private:
2874 DISALLOW_COPY_AND_ASSIGN(HNeg);
2875};
2876
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002877class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002878 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002879 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002880 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002881 uint32_t dex_pc,
2882 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002883 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002884 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002885 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2886 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002887 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002888 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002889 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002890 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002891 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002892 }
2893
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002894 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002895 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002896 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002897
2898 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002899 bool NeedsEnvironment() const OVERRIDE { return true; }
2900
Mingyao Yang0c365e62015-03-31 15:09:29 -07002901 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2902 bool CanThrow() const OVERRIDE { return true; }
2903
Calin Juravle10e244f2015-01-26 18:54:32 +00002904 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002905
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002906 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2907
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002908 DECLARE_INSTRUCTION(NewArray);
2909
2910 private:
2911 const uint32_t dex_pc_;
2912 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002913 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002914 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002915
2916 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2917};
2918
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002919class HAdd : public HBinaryOperation {
2920 public:
2921 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2922 : HBinaryOperation(result_type, left, right) {}
2923
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002924 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002925
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002926 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002927 return x + y;
2928 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002929 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002930 return x + y;
2931 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002932
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002933 DECLARE_INSTRUCTION(Add);
2934
2935 private:
2936 DISALLOW_COPY_AND_ASSIGN(HAdd);
2937};
2938
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002939class HSub : public HBinaryOperation {
2940 public:
2941 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2942 : HBinaryOperation(result_type, left, right) {}
2943
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002944 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002945 return x - y;
2946 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002947 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002948 return x - y;
2949 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002950
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002951 DECLARE_INSTRUCTION(Sub);
2952
2953 private:
2954 DISALLOW_COPY_AND_ASSIGN(HSub);
2955};
2956
Calin Juravle34bacdf2014-10-07 20:23:36 +01002957class HMul : public HBinaryOperation {
2958 public:
2959 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2960 : HBinaryOperation(result_type, left, right) {}
2961
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002962 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002963
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002964 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2965 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002966
2967 DECLARE_INSTRUCTION(Mul);
2968
2969 private:
2970 DISALLOW_COPY_AND_ASSIGN(HMul);
2971};
2972
Calin Juravle7c4954d2014-10-28 16:57:40 +00002973class HDiv : public HBinaryOperation {
2974 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002975 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2976 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002977
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002978 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002979 // Our graph structure ensures we never have 0 for `y` during constant folding.
2980 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002981 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002982 return (y == -1) ? -x : x / y;
2983 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002984
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002985 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002986 DCHECK_NE(y, 0);
2987 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2988 return (y == -1) ? -x : x / y;
2989 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002990
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002991 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002992
Calin Juravle7c4954d2014-10-28 16:57:40 +00002993 DECLARE_INSTRUCTION(Div);
2994
2995 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002996 const uint32_t dex_pc_;
2997
Calin Juravle7c4954d2014-10-28 16:57:40 +00002998 DISALLOW_COPY_AND_ASSIGN(HDiv);
2999};
3000
Calin Juravlebacfec32014-11-14 15:54:36 +00003001class HRem : public HBinaryOperation {
3002 public:
3003 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3004 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3005
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003006 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003007 DCHECK_NE(y, 0);
3008 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3009 return (y == -1) ? 0 : x % y;
3010 }
3011
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003012 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003013 DCHECK_NE(y, 0);
3014 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3015 return (y == -1) ? 0 : x % y;
3016 }
3017
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003018 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003019
3020 DECLARE_INSTRUCTION(Rem);
3021
3022 private:
3023 const uint32_t dex_pc_;
3024
3025 DISALLOW_COPY_AND_ASSIGN(HRem);
3026};
3027
Calin Juravled0d48522014-11-04 16:40:20 +00003028class HDivZeroCheck : public HExpression<1> {
3029 public:
3030 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3031 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3032 SetRawInputAt(0, value);
3033 }
3034
3035 bool CanBeMoved() const OVERRIDE { return true; }
3036
3037 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3038 UNUSED(other);
3039 return true;
3040 }
3041
3042 bool NeedsEnvironment() const OVERRIDE { return true; }
3043 bool CanThrow() const OVERRIDE { return true; }
3044
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003045 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003046
3047 DECLARE_INSTRUCTION(DivZeroCheck);
3048
3049 private:
3050 const uint32_t dex_pc_;
3051
3052 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3053};
3054
Calin Juravle9aec02f2014-11-18 23:06:35 +00003055class HShl : public HBinaryOperation {
3056 public:
3057 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3058 : HBinaryOperation(result_type, left, right) {}
3059
3060 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3061 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3062
3063 DECLARE_INSTRUCTION(Shl);
3064
3065 private:
3066 DISALLOW_COPY_AND_ASSIGN(HShl);
3067};
3068
3069class HShr : public HBinaryOperation {
3070 public:
3071 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3072 : HBinaryOperation(result_type, left, right) {}
3073
3074 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3075 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3076
3077 DECLARE_INSTRUCTION(Shr);
3078
3079 private:
3080 DISALLOW_COPY_AND_ASSIGN(HShr);
3081};
3082
3083class HUShr : public HBinaryOperation {
3084 public:
3085 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3086 : HBinaryOperation(result_type, left, right) {}
3087
3088 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3089 uint32_t ux = static_cast<uint32_t>(x);
3090 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3091 return static_cast<int32_t>(ux >> uy);
3092 }
3093
3094 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3095 uint64_t ux = static_cast<uint64_t>(x);
3096 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3097 return static_cast<int64_t>(ux >> uy);
3098 }
3099
3100 DECLARE_INSTRUCTION(UShr);
3101
3102 private:
3103 DISALLOW_COPY_AND_ASSIGN(HUShr);
3104};
3105
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003106class HAnd : public HBinaryOperation {
3107 public:
3108 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3109 : HBinaryOperation(result_type, left, right) {}
3110
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003111 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003112
3113 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3114 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3115
3116 DECLARE_INSTRUCTION(And);
3117
3118 private:
3119 DISALLOW_COPY_AND_ASSIGN(HAnd);
3120};
3121
3122class HOr : public HBinaryOperation {
3123 public:
3124 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3125 : HBinaryOperation(result_type, left, right) {}
3126
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003127 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003128
3129 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3130 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3131
3132 DECLARE_INSTRUCTION(Or);
3133
3134 private:
3135 DISALLOW_COPY_AND_ASSIGN(HOr);
3136};
3137
3138class HXor : public HBinaryOperation {
3139 public:
3140 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3141 : HBinaryOperation(result_type, left, right) {}
3142
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003143 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003144
3145 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3146 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3147
3148 DECLARE_INSTRUCTION(Xor);
3149
3150 private:
3151 DISALLOW_COPY_AND_ASSIGN(HXor);
3152};
3153
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003154// The value of a parameter in this method. Its location depends on
3155// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003156class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003157 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003158 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3159 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003160
3161 uint8_t GetIndex() const { return index_; }
3162
Calin Juravle10e244f2015-01-26 18:54:32 +00003163 bool CanBeNull() const OVERRIDE { return !is_this_; }
3164
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003165 bool IsThis() const { return is_this_; }
3166
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003167 DECLARE_INSTRUCTION(ParameterValue);
3168
3169 private:
3170 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003171 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003172 const uint8_t index_;
3173
Calin Juravle10e244f2015-01-26 18:54:32 +00003174 // Whether or not the parameter value corresponds to 'this' argument.
3175 const bool is_this_;
3176
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003177 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3178};
3179
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003180class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003181 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003182 explicit HNot(Primitive::Type result_type, HInstruction* input)
3183 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003184
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003185 bool CanBeMoved() const OVERRIDE { return true; }
3186 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003187 UNUSED(other);
3188 return true;
3189 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003190
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003191 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3192 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003193
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003194 DECLARE_INSTRUCTION(Not);
3195
3196 private:
3197 DISALLOW_COPY_AND_ASSIGN(HNot);
3198};
3199
David Brazdil66d126e2015-04-03 16:02:44 +01003200class HBooleanNot : public HUnaryOperation {
3201 public:
3202 explicit HBooleanNot(HInstruction* input)
3203 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3204
3205 bool CanBeMoved() const OVERRIDE { return true; }
3206 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3207 UNUSED(other);
3208 return true;
3209 }
3210
3211 int32_t Evaluate(int32_t x) const OVERRIDE {
3212 DCHECK(IsUint<1>(x));
3213 return !x;
3214 }
3215
3216 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3217 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3218 UNREACHABLE();
3219 }
3220
3221 DECLARE_INSTRUCTION(BooleanNot);
3222
3223 private:
3224 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3225};
3226
Roland Levillaindff1f282014-11-05 14:15:05 +00003227class HTypeConversion : public HExpression<1> {
3228 public:
3229 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003230 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3231 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003232 SetRawInputAt(0, input);
3233 DCHECK_NE(input->GetType(), result_type);
3234 }
3235
3236 HInstruction* GetInput() const { return InputAt(0); }
3237 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3238 Primitive::Type GetResultType() const { return GetType(); }
3239
Roland Levillain624279f2014-12-04 11:54:28 +00003240 // Required by the x86 and ARM code generators when producing calls
3241 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003242 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003243
Roland Levillaindff1f282014-11-05 14:15:05 +00003244 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003245 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003246
Mark Mendelle82549b2015-05-06 10:55:34 -04003247 // Try to statically evaluate the conversion and return a HConstant
3248 // containing the result. If the input cannot be converted, return nullptr.
3249 HConstant* TryStaticEvaluation() const;
3250
Roland Levillaindff1f282014-11-05 14:15:05 +00003251 DECLARE_INSTRUCTION(TypeConversion);
3252
3253 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003254 const uint32_t dex_pc_;
3255
Roland Levillaindff1f282014-11-05 14:15:05 +00003256 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3257};
3258
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003259static constexpr uint32_t kNoRegNumber = -1;
3260
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003261class HPhi : public HInstruction {
3262 public:
3263 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003264 : HInstruction(SideEffects::None()),
3265 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003266 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003267 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003268 is_live_(false),
3269 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003270 inputs_.SetSize(number_of_inputs);
3271 }
3272
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003273 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3274 static Primitive::Type ToPhiType(Primitive::Type type) {
3275 switch (type) {
3276 case Primitive::kPrimBoolean:
3277 case Primitive::kPrimByte:
3278 case Primitive::kPrimShort:
3279 case Primitive::kPrimChar:
3280 return Primitive::kPrimInt;
3281 default:
3282 return type;
3283 }
3284 }
3285
Calin Juravle10e244f2015-01-26 18:54:32 +00003286 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003287
3288 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003289 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003290
Calin Juravle10e244f2015-01-26 18:54:32 +00003291 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003292 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003293
Calin Juravle10e244f2015-01-26 18:54:32 +00003294 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3295 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3296
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003297 uint32_t GetRegNumber() const { return reg_number_; }
3298
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003299 void SetDead() { is_live_ = false; }
3300 void SetLive() { is_live_ = true; }
3301 bool IsDead() const { return !is_live_; }
3302 bool IsLive() const { return is_live_; }
3303
Calin Juravlea4f88312015-04-16 12:57:19 +01003304 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3305 // An equivalent phi is a phi having the same dex register and type.
3306 // It assumes that phis with the same dex register are adjacent.
3307 HPhi* GetNextEquivalentPhiWithSameType() {
3308 HInstruction* next = GetNext();
3309 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3310 if (next->GetType() == GetType()) {
3311 return next->AsPhi();
3312 }
3313 next = next->GetNext();
3314 }
3315 return nullptr;
3316 }
3317
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003318 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003319
David Brazdil1abb4192015-02-17 18:33:36 +00003320 protected:
3321 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3322
3323 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3324 inputs_.Put(index, input);
3325 }
3326
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003327 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003328 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003329 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003330 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003331 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003332 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003333
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003334 DISALLOW_COPY_AND_ASSIGN(HPhi);
3335};
3336
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003337class HNullCheck : public HExpression<1> {
3338 public:
3339 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003340 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003341 SetRawInputAt(0, value);
3342 }
3343
Calin Juravle10e244f2015-01-26 18:54:32 +00003344 bool CanBeMoved() const OVERRIDE { return true; }
3345 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003346 UNUSED(other);
3347 return true;
3348 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003349
Calin Juravle10e244f2015-01-26 18:54:32 +00003350 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003351
Calin Juravle10e244f2015-01-26 18:54:32 +00003352 bool CanThrow() const OVERRIDE { return true; }
3353
3354 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003355
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003356 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003357
3358 DECLARE_INSTRUCTION(NullCheck);
3359
3360 private:
3361 const uint32_t dex_pc_;
3362
3363 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3364};
3365
3366class FieldInfo : public ValueObject {
3367 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003368 FieldInfo(MemberOffset field_offset,
3369 Primitive::Type field_type,
3370 bool is_volatile,
3371 uint32_t index,
3372 const DexFile& dex_file)
3373 : field_offset_(field_offset),
3374 field_type_(field_type),
3375 is_volatile_(is_volatile),
3376 index_(index),
3377 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003378
3379 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003380 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003381 uint32_t GetFieldIndex() const { return index_; }
3382 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003383 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003384
3385 private:
3386 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003387 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003388 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003389 uint32_t index_;
3390 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003391};
3392
3393class HInstanceFieldGet : public HExpression<1> {
3394 public:
3395 HInstanceFieldGet(HInstruction* value,
3396 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003397 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003398 bool is_volatile,
3399 uint32_t field_idx,
3400 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003401 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003402 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003403 SetRawInputAt(0, value);
3404 }
3405
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003406 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003407
3408 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3409 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3410 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003411 }
3412
Calin Juravle641547a2015-04-21 22:08:51 +01003413 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3414 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003415 }
3416
3417 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003418 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3419 }
3420
Calin Juravle52c48962014-12-16 17:02:57 +00003421 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003422 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003423 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003424 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003425
3426 DECLARE_INSTRUCTION(InstanceFieldGet);
3427
3428 private:
3429 const FieldInfo field_info_;
3430
3431 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3432};
3433
3434class HInstanceFieldSet : public HTemplateInstruction<2> {
3435 public:
3436 HInstanceFieldSet(HInstruction* object,
3437 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003438 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003439 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003440 bool is_volatile,
3441 uint32_t field_idx,
3442 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003443 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003444 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003445 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003446 SetRawInputAt(0, object);
3447 SetRawInputAt(1, value);
3448 }
3449
Calin Juravle641547a2015-04-21 22:08:51 +01003450 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3451 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003452 }
3453
Calin Juravle52c48962014-12-16 17:02:57 +00003454 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003455 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003456 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003457 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003458 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003459 bool GetValueCanBeNull() const { return value_can_be_null_; }
3460 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003461
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003462 DECLARE_INSTRUCTION(InstanceFieldSet);
3463
3464 private:
3465 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003466 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003467
3468 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3469};
3470
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003471class HArrayGet : public HExpression<2> {
3472 public:
3473 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003474 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003475 SetRawInputAt(0, array);
3476 SetRawInputAt(1, index);
3477 }
3478
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003479 bool CanBeMoved() const OVERRIDE { return true; }
3480 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003481 UNUSED(other);
3482 return true;
3483 }
Calin Juravle641547a2015-04-21 22:08:51 +01003484 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3485 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003486 // TODO: We can be smarter here.
3487 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3488 // which generates the implicit null check. There are cases when these can be removed
3489 // to produce better code. If we ever add optimizations to do so we should allow an
3490 // implicit check here (as long as the address falls in the first page).
3491 return false;
3492 }
3493
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003494 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003495
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003496 HInstruction* GetArray() const { return InputAt(0); }
3497 HInstruction* GetIndex() const { return InputAt(1); }
3498
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003499 DECLARE_INSTRUCTION(ArrayGet);
3500
3501 private:
3502 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3503};
3504
3505class HArraySet : public HTemplateInstruction<3> {
3506 public:
3507 HArraySet(HInstruction* array,
3508 HInstruction* index,
3509 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003510 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003511 uint32_t dex_pc)
3512 : HTemplateInstruction(SideEffects::ChangesSomething()),
3513 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003514 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003515 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3516 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003517 SetRawInputAt(0, array);
3518 SetRawInputAt(1, index);
3519 SetRawInputAt(2, value);
3520 }
3521
Calin Juravle77520bc2015-01-12 18:45:46 +00003522 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003523 // We currently always call a runtime method to catch array store
3524 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003525 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003526 }
3527
Mingyao Yang81014cb2015-06-02 03:16:27 -07003528 // Can throw ArrayStoreException.
3529 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3530
Calin Juravle641547a2015-04-21 22:08:51 +01003531 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3532 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003533 // TODO: Same as for ArrayGet.
3534 return false;
3535 }
3536
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003537 void ClearNeedsTypeCheck() {
3538 needs_type_check_ = false;
3539 }
3540
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003541 void ClearValueCanBeNull() {
3542 value_can_be_null_ = false;
3543 }
3544
3545 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003546 bool NeedsTypeCheck() const { return needs_type_check_; }
3547
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003548 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003549
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003550 HInstruction* GetArray() const { return InputAt(0); }
3551 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003552 HInstruction* GetValue() const { return InputAt(2); }
3553
3554 Primitive::Type GetComponentType() const {
3555 // The Dex format does not type floating point index operations. Since the
3556 // `expected_component_type_` is set during building and can therefore not
3557 // be correct, we also check what is the value type. If it is a floating
3558 // point type, we must use that type.
3559 Primitive::Type value_type = GetValue()->GetType();
3560 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3561 ? value_type
3562 : expected_component_type_;
3563 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003564
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003565 DECLARE_INSTRUCTION(ArraySet);
3566
3567 private:
3568 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003569 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003570 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003571 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003572
3573 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3574};
3575
3576class HArrayLength : public HExpression<1> {
3577 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003578 explicit HArrayLength(HInstruction* array)
3579 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3580 // Note that arrays do not change length, so the instruction does not
3581 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003582 SetRawInputAt(0, array);
3583 }
3584
Calin Juravle77520bc2015-01-12 18:45:46 +00003585 bool CanBeMoved() const OVERRIDE { return true; }
3586 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003587 UNUSED(other);
3588 return true;
3589 }
Calin Juravle641547a2015-04-21 22:08:51 +01003590 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3591 return obj == InputAt(0);
3592 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003594 DECLARE_INSTRUCTION(ArrayLength);
3595
3596 private:
3597 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3598};
3599
3600class HBoundsCheck : public HExpression<2> {
3601 public:
3602 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003603 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604 DCHECK(index->GetType() == Primitive::kPrimInt);
3605 SetRawInputAt(0, index);
3606 SetRawInputAt(1, length);
3607 }
3608
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003609 bool CanBeMoved() const OVERRIDE { return true; }
3610 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003611 UNUSED(other);
3612 return true;
3613 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003614
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003615 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003616
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003617 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003618
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003619 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003620
3621 DECLARE_INSTRUCTION(BoundsCheck);
3622
3623 private:
3624 const uint32_t dex_pc_;
3625
3626 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3627};
3628
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003629/**
3630 * Some DEX instructions are folded into multiple HInstructions that need
3631 * to stay live until the last HInstruction. This class
3632 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003633 * HInstruction stays live. `index` represents the stack location index of the
3634 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003635 */
3636class HTemporary : public HTemplateInstruction<0> {
3637 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003638 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003639
3640 size_t GetIndex() const { return index_; }
3641
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003642 Primitive::Type GetType() const OVERRIDE {
3643 // The previous instruction is the one that will be stored in the temporary location.
3644 DCHECK(GetPrevious() != nullptr);
3645 return GetPrevious()->GetType();
3646 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003647
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003648 DECLARE_INSTRUCTION(Temporary);
3649
3650 private:
3651 const size_t index_;
3652
3653 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3654};
3655
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003656class HSuspendCheck : public HTemplateInstruction<0> {
3657 public:
3658 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003659 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003660
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003661 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003662 return true;
3663 }
3664
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003665 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003666 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3667 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003668
3669 DECLARE_INSTRUCTION(SuspendCheck);
3670
3671 private:
3672 const uint32_t dex_pc_;
3673
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003674 // Only used for code generation, in order to share the same slow path between back edges
3675 // of a same loop.
3676 SlowPathCode* slow_path_;
3677
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003678 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3679};
3680
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003681/**
3682 * Instruction to load a Class object.
3683 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003684class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003685 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003686 HLoadClass(HCurrentMethod* current_method,
3687 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003688 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003689 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003690 uint32_t dex_pc)
3691 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3692 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003693 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003694 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003695 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003696 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003697 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3698 SetRawInputAt(0, current_method);
3699 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003700
3701 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003702
3703 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3704 return other->AsLoadClass()->type_index_ == type_index_;
3705 }
3706
3707 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3708
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003709 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003710 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003711 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003712 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003713
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003714 bool NeedsEnvironment() const OVERRIDE {
3715 // Will call runtime and load the class if the class is not loaded yet.
3716 // TODO: finer grain decision.
3717 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003718 }
3719
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003720 bool MustGenerateClinitCheck() const {
3721 return generate_clinit_check_;
3722 }
3723
Calin Juravle0ba218d2015-05-19 18:46:01 +01003724 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3725 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003726 }
3727
3728 bool CanCallRuntime() const {
3729 return MustGenerateClinitCheck() || !is_referrers_class_;
3730 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003731
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003732 bool CanThrow() const OVERRIDE {
3733 // May call runtime and and therefore can throw.
3734 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003735 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003736 }
3737
Calin Juravleacf735c2015-02-12 15:25:22 +00003738 ReferenceTypeInfo GetLoadedClassRTI() {
3739 return loaded_class_rti_;
3740 }
3741
3742 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3743 // Make sure we only set exact types (the loaded class should never be merged).
3744 DCHECK(rti.IsExact());
3745 loaded_class_rti_ = rti;
3746 }
3747
3748 bool IsResolved() {
3749 return loaded_class_rti_.IsExact();
3750 }
3751
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003752 const DexFile& GetDexFile() { return dex_file_; }
3753
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003754 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3755
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003756 DECLARE_INSTRUCTION(LoadClass);
3757
3758 private:
3759 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003760 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003761 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003762 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003763 // Whether this instruction must generate the initialization check.
3764 // Used for code generation.
3765 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003766
Calin Juravleacf735c2015-02-12 15:25:22 +00003767 ReferenceTypeInfo loaded_class_rti_;
3768
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003769 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3770};
3771
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003772class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003773 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003774 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003775 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3776 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003777 dex_pc_(dex_pc) {
3778 SetRawInputAt(0, current_method);
3779 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003780
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003781 bool CanBeMoved() const OVERRIDE { return true; }
3782
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003783 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3784 return other->AsLoadString()->string_index_ == string_index_;
3785 }
3786
3787 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3788
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003789 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003790 uint32_t GetStringIndex() const { return string_index_; }
3791
3792 // TODO: Can we deopt or debug when we resolve a string?
3793 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003794 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003795
3796 DECLARE_INSTRUCTION(LoadString);
3797
3798 private:
3799 const uint32_t string_index_;
3800 const uint32_t dex_pc_;
3801
3802 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3803};
3804
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003805/**
3806 * Performs an initialization check on its Class object input.
3807 */
3808class HClinitCheck : public HExpression<1> {
3809 public:
3810 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003811 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003812 dex_pc_(dex_pc) {
3813 SetRawInputAt(0, constant);
3814 }
3815
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003816 bool CanBeMoved() const OVERRIDE { return true; }
3817 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3818 UNUSED(other);
3819 return true;
3820 }
3821
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003822 bool NeedsEnvironment() const OVERRIDE {
3823 // May call runtime to initialize the class.
3824 return true;
3825 }
3826
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003827 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003828
3829 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3830
3831 DECLARE_INSTRUCTION(ClinitCheck);
3832
3833 private:
3834 const uint32_t dex_pc_;
3835
3836 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3837};
3838
3839class HStaticFieldGet : public HExpression<1> {
3840 public:
3841 HStaticFieldGet(HInstruction* cls,
3842 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003843 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003844 bool is_volatile,
3845 uint32_t field_idx,
3846 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003847 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003848 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003849 SetRawInputAt(0, cls);
3850 }
3851
Calin Juravle52c48962014-12-16 17:02:57 +00003852
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003853 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003854
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003855 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003856 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3857 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003858 }
3859
3860 size_t ComputeHashCode() const OVERRIDE {
3861 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3862 }
3863
Calin Juravle52c48962014-12-16 17:02:57 +00003864 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003865 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3866 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003867 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003868
3869 DECLARE_INSTRUCTION(StaticFieldGet);
3870
3871 private:
3872 const FieldInfo field_info_;
3873
3874 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3875};
3876
3877class HStaticFieldSet : public HTemplateInstruction<2> {
3878 public:
3879 HStaticFieldSet(HInstruction* cls,
3880 HInstruction* value,
3881 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003882 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003883 bool is_volatile,
3884 uint32_t field_idx,
3885 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003886 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003887 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003888 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003889 SetRawInputAt(0, cls);
3890 SetRawInputAt(1, value);
3891 }
3892
Calin Juravle52c48962014-12-16 17:02:57 +00003893 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003894 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3895 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003896 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003897
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003898 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003899 bool GetValueCanBeNull() const { return value_can_be_null_; }
3900 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003901
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003902 DECLARE_INSTRUCTION(StaticFieldSet);
3903
3904 private:
3905 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003906 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003907
3908 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3909};
3910
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003911// Implement the move-exception DEX instruction.
3912class HLoadException : public HExpression<0> {
3913 public:
3914 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3915
3916 DECLARE_INSTRUCTION(LoadException);
3917
3918 private:
3919 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3920};
3921
3922class HThrow : public HTemplateInstruction<1> {
3923 public:
3924 HThrow(HInstruction* exception, uint32_t dex_pc)
3925 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3926 SetRawInputAt(0, exception);
3927 }
3928
3929 bool IsControlFlow() const OVERRIDE { return true; }
3930
3931 bool NeedsEnvironment() const OVERRIDE { return true; }
3932
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003933 bool CanThrow() const OVERRIDE { return true; }
3934
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003935 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003936
3937 DECLARE_INSTRUCTION(Throw);
3938
3939 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003940 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003941
3942 DISALLOW_COPY_AND_ASSIGN(HThrow);
3943};
3944
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003945class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003946 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003947 HInstanceOf(HInstruction* object,
3948 HLoadClass* constant,
3949 bool class_is_final,
3950 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003951 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3952 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003953 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003954 dex_pc_(dex_pc) {
3955 SetRawInputAt(0, object);
3956 SetRawInputAt(1, constant);
3957 }
3958
3959 bool CanBeMoved() const OVERRIDE { return true; }
3960
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003961 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003962 return true;
3963 }
3964
3965 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003966 return false;
3967 }
3968
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003969 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003970
3971 bool IsClassFinal() const { return class_is_final_; }
3972
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003973 // Used only in code generation.
3974 bool MustDoNullCheck() const { return must_do_null_check_; }
3975 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3976
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003977 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003978
3979 private:
3980 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003981 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003982 const uint32_t dex_pc_;
3983
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003984 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3985};
3986
Calin Juravleb1498f62015-02-16 13:13:29 +00003987class HBoundType : public HExpression<1> {
3988 public:
3989 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3990 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3991 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003992 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003993 SetRawInputAt(0, input);
3994 }
3995
3996 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3997
3998 bool CanBeNull() const OVERRIDE {
3999 // `null instanceof ClassX` always return false so we can't be null.
4000 return false;
4001 }
4002
4003 DECLARE_INSTRUCTION(BoundType);
4004
4005 private:
4006 // Encodes the most upper class that this instruction can have. In other words
4007 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
4008 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
4009 const ReferenceTypeInfo bound_type_;
4010
4011 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4012};
4013
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004014class HCheckCast : public HTemplateInstruction<2> {
4015 public:
4016 HCheckCast(HInstruction* object,
4017 HLoadClass* constant,
4018 bool class_is_final,
4019 uint32_t dex_pc)
4020 : HTemplateInstruction(SideEffects::None()),
4021 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004022 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004023 dex_pc_(dex_pc) {
4024 SetRawInputAt(0, object);
4025 SetRawInputAt(1, constant);
4026 }
4027
4028 bool CanBeMoved() const OVERRIDE { return true; }
4029
4030 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4031 return true;
4032 }
4033
4034 bool NeedsEnvironment() const OVERRIDE {
4035 // Instruction may throw a CheckCastError.
4036 return true;
4037 }
4038
4039 bool CanThrow() const OVERRIDE { return true; }
4040
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004041 bool MustDoNullCheck() const { return must_do_null_check_; }
4042 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4043
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004044 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004045
4046 bool IsClassFinal() const { return class_is_final_; }
4047
4048 DECLARE_INSTRUCTION(CheckCast);
4049
4050 private:
4051 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004052 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004053 const uint32_t dex_pc_;
4054
4055 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004056};
4057
Calin Juravle27df7582015-04-17 19:12:31 +01004058class HMemoryBarrier : public HTemplateInstruction<0> {
4059 public:
4060 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
4061 : HTemplateInstruction(SideEffects::None()),
4062 barrier_kind_(barrier_kind) {}
4063
4064 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4065
4066 DECLARE_INSTRUCTION(MemoryBarrier);
4067
4068 private:
4069 const MemBarrierKind barrier_kind_;
4070
4071 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4072};
4073
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004074class HMonitorOperation : public HTemplateInstruction<1> {
4075 public:
4076 enum OperationKind {
4077 kEnter,
4078 kExit,
4079 };
4080
4081 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
4082 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
4083 SetRawInputAt(0, object);
4084 }
4085
4086 // Instruction may throw a Java exception, so we need an environment.
4087 bool NeedsEnvironment() const OVERRIDE { return true; }
4088 bool CanThrow() const OVERRIDE { return true; }
4089
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004090 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004091
4092 bool IsEnter() const { return kind_ == kEnter; }
4093
4094 DECLARE_INSTRUCTION(MonitorOperation);
4095
Calin Juravle52c48962014-12-16 17:02:57 +00004096 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004097 const OperationKind kind_;
4098 const uint32_t dex_pc_;
4099
4100 private:
4101 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4102};
4103
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004104class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004105 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004106 MoveOperands(Location source,
4107 Location destination,
4108 Primitive::Type type,
4109 HInstruction* instruction)
4110 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004111
4112 Location GetSource() const { return source_; }
4113 Location GetDestination() const { return destination_; }
4114
4115 void SetSource(Location value) { source_ = value; }
4116 void SetDestination(Location value) { destination_ = value; }
4117
4118 // The parallel move resolver marks moves as "in-progress" by clearing the
4119 // destination (but not the source).
4120 Location MarkPending() {
4121 DCHECK(!IsPending());
4122 Location dest = destination_;
4123 destination_ = Location::NoLocation();
4124 return dest;
4125 }
4126
4127 void ClearPending(Location dest) {
4128 DCHECK(IsPending());
4129 destination_ = dest;
4130 }
4131
4132 bool IsPending() const {
4133 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4134 return destination_.IsInvalid() && !source_.IsInvalid();
4135 }
4136
4137 // True if this blocks a move from the given location.
4138 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004139 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004140 }
4141
4142 // A move is redundant if it's been eliminated, if its source and
4143 // destination are the same, or if its destination is unneeded.
4144 bool IsRedundant() const {
4145 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4146 }
4147
4148 // We clear both operands to indicate move that's been eliminated.
4149 void Eliminate() {
4150 source_ = destination_ = Location::NoLocation();
4151 }
4152
4153 bool IsEliminated() const {
4154 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4155 return source_.IsInvalid();
4156 }
4157
Alexey Frunze4dda3372015-06-01 18:31:49 -07004158 Primitive::Type GetType() const { return type_; }
4159
Nicolas Geoffray90218252015-04-15 11:56:51 +01004160 bool Is64BitMove() const {
4161 return Primitive::Is64BitType(type_);
4162 }
4163
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004164 HInstruction* GetInstruction() const { return instruction_; }
4165
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004166 private:
4167 Location source_;
4168 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004169 // The type this move is for.
4170 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004171 // The instruction this move is assocatied with. Null when this move is
4172 // for moving an input in the expected locations of user (including a phi user).
4173 // This is only used in debug mode, to ensure we do not connect interval siblings
4174 // in the same parallel move.
4175 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004176};
4177
4178static constexpr size_t kDefaultNumberOfMoves = 4;
4179
4180class HParallelMove : public HTemplateInstruction<0> {
4181 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004182 explicit HParallelMove(ArenaAllocator* arena)
4183 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004184
Nicolas Geoffray90218252015-04-15 11:56:51 +01004185 void AddMove(Location source,
4186 Location destination,
4187 Primitive::Type type,
4188 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004189 DCHECK(source.IsValid());
4190 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004191 if (kIsDebugBuild) {
4192 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004193 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004194 if (moves_.Get(i).GetInstruction() == instruction) {
4195 // Special case the situation where the move is for the spill slot
4196 // of the instruction.
4197 if ((GetPrevious() == instruction)
4198 || ((GetPrevious() == nullptr)
4199 && instruction->IsPhi()
4200 && instruction->GetBlock() == GetBlock())) {
4201 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4202 << "Doing parallel moves for the same instruction.";
4203 } else {
4204 DCHECK(false) << "Doing parallel moves for the same instruction.";
4205 }
4206 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004207 }
4208 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004209 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004210 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004211 << "Overlapped destination for two moves in a parallel move: "
4212 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4213 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004214 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004215 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004216 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004217 }
4218
4219 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004220 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004221 }
4222
4223 size_t NumMoves() const { return moves_.Size(); }
4224
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004225 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004226
4227 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004228 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004229
4230 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4231};
4232
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004233class HGraphVisitor : public ValueObject {
4234 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004235 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4236 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004237
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004238 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004239 virtual void VisitBasicBlock(HBasicBlock* block);
4240
Roland Levillain633021e2014-10-01 14:12:25 +01004241 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004242 void VisitInsertionOrder();
4243
Roland Levillain633021e2014-10-01 14:12:25 +01004244 // Visit the graph following dominator tree reverse post-order.
4245 void VisitReversePostOrder();
4246
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004247 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004248
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004249 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004250#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004251 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4252
4253 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4254
4255#undef DECLARE_VISIT_INSTRUCTION
4256
4257 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004258 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004259
4260 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4261};
4262
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004263class HGraphDelegateVisitor : public HGraphVisitor {
4264 public:
4265 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4266 virtual ~HGraphDelegateVisitor() {}
4267
4268 // Visit functions that delegate to to super class.
4269#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004270 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004271
4272 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4273
4274#undef DECLARE_VISIT_INSTRUCTION
4275
4276 private:
4277 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4278};
4279
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004280class HInsertionOrderIterator : public ValueObject {
4281 public:
4282 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4283
4284 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4285 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4286 void Advance() { ++index_; }
4287
4288 private:
4289 const HGraph& graph_;
4290 size_t index_;
4291
4292 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4293};
4294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004295class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004296 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004297 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4298 // Check that reverse post order of the graph has been built.
4299 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4300 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004301
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004302 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4303 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004304 void Advance() { ++index_; }
4305
4306 private:
4307 const HGraph& graph_;
4308 size_t index_;
4309
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004310 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004311};
4312
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004313class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004314 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004315 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004316 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4317 // Check that reverse post order of the graph has been built.
4318 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4319 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004320
4321 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004322 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004323 void Advance() { --index_; }
4324
4325 private:
4326 const HGraph& graph_;
4327 size_t index_;
4328
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004329 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004330};
4331
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004332class HLinearPostOrderIterator : public ValueObject {
4333 public:
4334 explicit HLinearPostOrderIterator(const HGraph& graph)
4335 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4336
4337 bool Done() const { return index_ == 0; }
4338
4339 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4340
4341 void Advance() {
4342 --index_;
4343 DCHECK_GE(index_, 0U);
4344 }
4345
4346 private:
4347 const GrowableArray<HBasicBlock*>& order_;
4348 size_t index_;
4349
4350 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4351};
4352
4353class HLinearOrderIterator : public ValueObject {
4354 public:
4355 explicit HLinearOrderIterator(const HGraph& graph)
4356 : order_(graph.GetLinearOrder()), index_(0) {}
4357
4358 bool Done() const { return index_ == order_.Size(); }
4359 HBasicBlock* Current() const { return order_.Get(index_); }
4360 void Advance() { ++index_; }
4361
4362 private:
4363 const GrowableArray<HBasicBlock*>& order_;
4364 size_t index_;
4365
4366 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4367};
4368
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004369// Iterator over the blocks that art part of the loop. Includes blocks part
4370// of an inner loop. The order in which the blocks are iterated is on their
4371// block id.
4372class HBlocksInLoopIterator : public ValueObject {
4373 public:
4374 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4375 : blocks_in_loop_(info.GetBlocks()),
4376 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4377 index_(0) {
4378 if (!blocks_in_loop_.IsBitSet(index_)) {
4379 Advance();
4380 }
4381 }
4382
4383 bool Done() const { return index_ == blocks_.Size(); }
4384 HBasicBlock* Current() const { return blocks_.Get(index_); }
4385 void Advance() {
4386 ++index_;
4387 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4388 if (blocks_in_loop_.IsBitSet(index_)) {
4389 break;
4390 }
4391 }
4392 }
4393
4394 private:
4395 const BitVector& blocks_in_loop_;
4396 const GrowableArray<HBasicBlock*>& blocks_;
4397 size_t index_;
4398
4399 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4400};
4401
Mingyao Yang3584bce2015-05-19 16:01:59 -07004402// Iterator over the blocks that art part of the loop. Includes blocks part
4403// of an inner loop. The order in which the blocks are iterated is reverse
4404// post order.
4405class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4406 public:
4407 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4408 : blocks_in_loop_(info.GetBlocks()),
4409 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4410 index_(0) {
4411 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4412 Advance();
4413 }
4414 }
4415
4416 bool Done() const { return index_ == blocks_.Size(); }
4417 HBasicBlock* Current() const { return blocks_.Get(index_); }
4418 void Advance() {
4419 ++index_;
4420 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4421 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4422 break;
4423 }
4424 }
4425 }
4426
4427 private:
4428 const BitVector& blocks_in_loop_;
4429 const GrowableArray<HBasicBlock*>& blocks_;
4430 size_t index_;
4431
4432 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4433};
4434
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004435inline int64_t Int64FromConstant(HConstant* constant) {
4436 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4437 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4438 : constant->AsLongConstant()->GetValue();
4439}
4440
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004441} // namespace art
4442
4443#endif // ART_COMPILER_OPTIMIZING_NODES_H_