blob: f2e9e22057448136907c7360f20bb9d5b65897d2 [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
732 // created, latter block. Note that this method will create a Goto at the end
733 // of the former block and will create an edge between them. It will not,
734 // however, update the graph, reverse post order or loop information.
735 HBasicBlock* SplitBefore(HInstruction* cursor);
736
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000737 // Split the block into two blocks just after `cursor`. Returns the newly
738 // created block. Note that this method just updates raw block information,
739 // like predecessors, successors, dominators, and instruction list. It does not
740 // update the graph, reverse post order, loop information, nor make sure the
741 // blocks are consistent (for example ending with a control flow instruction).
742 HBasicBlock* SplitAfter(HInstruction* cursor);
743
744 // Merge `other` at the end of `this`. Successors and dominated blocks of
745 // `other` are changed to be successors and dominated blocks of `this`. Note
746 // that this method does not update the graph, reverse post order, loop
747 // information, nor make sure the blocks are consistent (for example ending
748 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100749 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000750
751 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
752 // of `this` are moved to `other`.
753 // Note that this method does not update the graph, reverse post order, loop
754 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000755 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 void ReplaceWith(HBasicBlock* other);
757
David Brazdil2d7352b2015-04-20 14:52:42 +0100758 // Merge `other` at the end of `this`. This method updates loops, reverse post
759 // order, links to predecessors, successors, dominators and deletes the block
760 // from the graph. The two blocks must be successive, i.e. `this` the only
761 // predecessor of `other` and vice versa.
762 void MergeWith(HBasicBlock* other);
763
764 // Disconnects `this` from all its predecessors, successors and dominator,
765 // removes it from all loops it is included in and eventually from the graph.
766 // The block must not dominate any other block. Predecessors and successors
767 // are safely updated.
768 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000769
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000770 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100771 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100772 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100773 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100774 // Replace instruction `initial` with `replacement` within this block.
775 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
776 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100777 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100778 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000779 // RemoveInstruction and RemovePhi delete a given instruction from the respective
780 // instruction list. With 'ensure_safety' set to true, it verifies that the
781 // instruction is not in use and removes it from the use lists of its inputs.
782 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
783 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100784 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100785
786 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100787 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100788 }
789
Roland Levillain6b879dd2014-09-22 17:13:44 +0100790 bool IsLoopPreHeaderFirstPredecessor() const {
791 DCHECK(IsLoopHeader());
792 DCHECK(!GetPredecessors().IsEmpty());
793 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
794 }
795
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100796 HLoopInformation* GetLoopInformation() const {
797 return loop_information_;
798 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000800 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100801 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000802 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100803 void SetInLoop(HLoopInformation* info) {
804 if (IsLoopHeader()) {
805 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100806 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 loop_information_ = info;
808 } else if (loop_information_->Contains(*info->GetHeader())) {
809 // Block is currently part of an outer loop. Make it part of this inner loop.
810 // Note that a non loop header having a loop information means this loop information
811 // has already been populated
812 loop_information_ = info;
813 } else {
814 // Block is part of an inner loop. Do not update the loop information.
815 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
816 // at this point, because this method is being called while populating `info`.
817 }
818 }
819
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000820 // Raw update of the loop information.
821 void SetLoopInformation(HLoopInformation* info) {
822 loop_information_ = info;
823 }
824
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100825 bool IsInLoop() const { return loop_information_ != nullptr; }
826
David Brazdila4b8c212015-05-07 09:59:30 +0100827 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100828 bool Dominates(HBasicBlock* block) const;
829
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100830 size_t GetLifetimeStart() const { return lifetime_start_; }
831 size_t GetLifetimeEnd() const { return lifetime_end_; }
832
833 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
834 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
835
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100836 uint32_t GetDexPc() const { return dex_pc_; }
837
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000838 bool IsCatchBlock() const { return is_catch_block_; }
839 void SetIsCatchBlock() { is_catch_block_ = true; }
840
David Brazdil8d5b8b22015-03-24 10:51:52 +0000841 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000842 bool EndsWithIf() const;
843 bool HasSinglePhi() const;
844
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000845 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000847 GrowableArray<HBasicBlock*> predecessors_;
848 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100849 HInstructionList instructions_;
850 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000851 HLoopInformation* loop_information_;
852 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100853 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000854 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100855 // The dex program counter of the first instruction of this block.
856 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100857 size_t lifetime_start_;
858 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000859 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000861 friend class HGraph;
862 friend class HInstruction;
863
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000864 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
865};
866
David Brazdilb2bd1c52015-03-25 11:17:37 +0000867// Iterates over the LoopInformation of all loops which contain 'block'
868// from the innermost to the outermost.
869class HLoopInformationOutwardIterator : public ValueObject {
870 public:
871 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
872 : current_(block.GetLoopInformation()) {}
873
874 bool Done() const { return current_ == nullptr; }
875
876 void Advance() {
877 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100878 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000879 }
880
881 HLoopInformation* Current() const {
882 DCHECK(!Done());
883 return current_;
884 }
885
886 private:
887 HLoopInformation* current_;
888
889 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
890};
891
Alexandre Ramesef20f712015-06-09 10:29:30 +0100892#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100893 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000894 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000895 M(ArrayGet, Instruction) \
896 M(ArrayLength, Instruction) \
897 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100898 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000899 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000900 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000901 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100902 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000903 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100904 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100905 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700906 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000907 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000908 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000909 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100910 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000911 M(Exit, Instruction) \
912 M(FloatConstant, Constant) \
913 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100914 M(GreaterThan, Condition) \
915 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100916 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000917 M(InstanceFieldGet, Instruction) \
918 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000919 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100920 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000921 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000922 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100923 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000924 M(LessThan, Condition) \
925 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000926 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000927 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100928 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000929 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100930 M(Local, Instruction) \
931 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100932 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000933 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000934 M(Mul, BinaryOperation) \
935 M(Neg, UnaryOperation) \
936 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100937 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100938 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000939 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000940 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000941 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000942 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100943 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000944 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100945 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000946 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100947 M(Return, Instruction) \
948 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000949 M(Shl, BinaryOperation) \
950 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100951 M(StaticFieldGet, Instruction) \
952 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100953 M(StoreLocal, Instruction) \
954 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100955 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000956 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000957 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000959 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000960 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000961 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962
Alexandre Ramesef20f712015-06-09 10:29:30 +0100963#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
964
965#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
966
967#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
968
969#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
970
971#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
972 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
973 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
974 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
975 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
976 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
977
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100978#define FOR_EACH_INSTRUCTION(M) \
979 FOR_EACH_CONCRETE_INSTRUCTION(M) \
980 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100981 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100982 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100983 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700984
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100985#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000986FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
987#undef FORWARD_DECLARATION
988
Roland Levillainccc07a92014-09-16 14:48:16 +0100989#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000990 InstructionKind GetKind() const OVERRIDE { return k##type; } \
991 const char* DebugName() const OVERRIDE { return #type; } \
992 const H##type* As##type() const OVERRIDE { return this; } \
993 H##type* As##type() OVERRIDE { return this; } \
994 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100995 return other->Is##type(); \
996 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000997 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000998
David Brazdiled596192015-01-23 10:39:45 +0000999template <typename T> class HUseList;
1000
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001001template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001002class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001003 public:
David Brazdiled596192015-01-23 10:39:45 +00001004 HUseListNode* GetPrevious() const { return prev_; }
1005 HUseListNode* GetNext() const { return next_; }
1006 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001007 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001008 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001009
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001010 private:
David Brazdiled596192015-01-23 10:39:45 +00001011 HUseListNode(T user, size_t index)
1012 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1013
1014 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001015 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001016 HUseListNode<T>* prev_;
1017 HUseListNode<T>* next_;
1018
1019 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001020
1021 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1022};
1023
David Brazdiled596192015-01-23 10:39:45 +00001024template <typename T>
1025class HUseList : public ValueObject {
1026 public:
1027 HUseList() : first_(nullptr) {}
1028
1029 void Clear() {
1030 first_ = nullptr;
1031 }
1032
1033 // Adds a new entry at the beginning of the use list and returns
1034 // the newly created node.
1035 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001036 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001037 if (IsEmpty()) {
1038 first_ = new_node;
1039 } else {
1040 first_->prev_ = new_node;
1041 new_node->next_ = first_;
1042 first_ = new_node;
1043 }
1044 return new_node;
1045 }
1046
1047 HUseListNode<T>* GetFirst() const {
1048 return first_;
1049 }
1050
1051 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001052 DCHECK(node != nullptr);
1053 DCHECK(Contains(node));
1054
David Brazdiled596192015-01-23 10:39:45 +00001055 if (node->prev_ != nullptr) {
1056 node->prev_->next_ = node->next_;
1057 }
1058 if (node->next_ != nullptr) {
1059 node->next_->prev_ = node->prev_;
1060 }
1061 if (node == first_) {
1062 first_ = node->next_;
1063 }
1064 }
1065
David Brazdil1abb4192015-02-17 18:33:36 +00001066 bool Contains(const HUseListNode<T>* node) const {
1067 if (node == nullptr) {
1068 return false;
1069 }
1070 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1071 if (current == node) {
1072 return true;
1073 }
1074 }
1075 return false;
1076 }
1077
David Brazdiled596192015-01-23 10:39:45 +00001078 bool IsEmpty() const {
1079 return first_ == nullptr;
1080 }
1081
1082 bool HasOnlyOneUse() const {
1083 return first_ != nullptr && first_->next_ == nullptr;
1084 }
1085
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001086 size_t SizeSlow() const {
1087 size_t count = 0;
1088 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1089 ++count;
1090 }
1091 return count;
1092 }
1093
David Brazdiled596192015-01-23 10:39:45 +00001094 private:
1095 HUseListNode<T>* first_;
1096};
1097
1098template<typename T>
1099class HUseIterator : public ValueObject {
1100 public:
1101 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1102
1103 bool Done() const { return current_ == nullptr; }
1104
1105 void Advance() {
1106 DCHECK(!Done());
1107 current_ = current_->GetNext();
1108 }
1109
1110 HUseListNode<T>* Current() const {
1111 DCHECK(!Done());
1112 return current_;
1113 }
1114
1115 private:
1116 HUseListNode<T>* current_;
1117
1118 friend class HValue;
1119};
1120
David Brazdil1abb4192015-02-17 18:33:36 +00001121// This class is used by HEnvironment and HInstruction classes to record the
1122// instructions they use and pointers to the corresponding HUseListNodes kept
1123// by the used instructions.
1124template <typename T>
1125class HUserRecord : public ValueObject {
1126 public:
1127 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1128 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1129
1130 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1131 : instruction_(old_record.instruction_), use_node_(use_node) {
1132 DCHECK(instruction_ != nullptr);
1133 DCHECK(use_node_ != nullptr);
1134 DCHECK(old_record.use_node_ == nullptr);
1135 }
1136
1137 HInstruction* GetInstruction() const { return instruction_; }
1138 HUseListNode<T>* GetUseNode() const { return use_node_; }
1139
1140 private:
1141 // Instruction used by the user.
1142 HInstruction* instruction_;
1143
1144 // Corresponding entry in the use list kept by 'instruction_'.
1145 HUseListNode<T>* use_node_;
1146};
1147
Calin Juravle27df7582015-04-17 19:12:31 +01001148// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1149// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1150// flag is consider.
1151// - DependsOn suggests that there is a real dependency between side effects but it only
1152// checks DependendsOnSomething flag.
1153//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001154// Represents the side effects an instruction may have.
1155class SideEffects : public ValueObject {
1156 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001157 SideEffects() : flags_(0) {}
1158
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001159 static SideEffects None() {
1160 return SideEffects(0);
1161 }
1162
1163 static SideEffects All() {
1164 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1165 }
1166
1167 static SideEffects ChangesSomething() {
1168 return SideEffects((1 << kFlagChangesCount) - 1);
1169 }
1170
1171 static SideEffects DependsOnSomething() {
1172 int count = kFlagDependsOnCount - kFlagChangesCount;
1173 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1174 }
1175
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001176 SideEffects Union(SideEffects other) const {
1177 return SideEffects(flags_ | other.flags_);
1178 }
1179
Roland Levillain72bceff2014-09-15 18:29:00 +01001180 bool HasSideEffects() const {
1181 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1182 return (flags_ & all_bits_set) != 0;
1183 }
1184
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001185 bool HasAllSideEffects() const {
1186 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1187 return all_bits_set == (flags_ & all_bits_set);
1188 }
1189
1190 bool DependsOn(SideEffects other) const {
1191 size_t depends_flags = other.ComputeDependsFlags();
1192 return (flags_ & depends_flags) != 0;
1193 }
1194
1195 bool HasDependencies() const {
1196 int count = kFlagDependsOnCount - kFlagChangesCount;
1197 size_t all_bits_set = (1 << count) - 1;
1198 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1199 }
1200
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001201 private:
1202 static constexpr int kFlagChangesSomething = 0;
1203 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1204
1205 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1206 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1207
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001208 explicit SideEffects(size_t flags) : flags_(flags) {}
1209
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001210 size_t ComputeDependsFlags() const {
1211 return flags_ << kFlagChangesCount;
1212 }
1213
1214 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001215};
1216
David Brazdiled596192015-01-23 10:39:45 +00001217// A HEnvironment object contains the values of virtual registers at a given location.
1218class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1219 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001220 HEnvironment(ArenaAllocator* arena,
1221 size_t number_of_vregs,
1222 const DexFile& dex_file,
1223 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001224 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001225 InvokeType invoke_type,
1226 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001227 : vregs_(arena, number_of_vregs),
1228 locations_(arena, number_of_vregs),
1229 parent_(nullptr),
1230 dex_file_(dex_file),
1231 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001232 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001233 invoke_type_(invoke_type),
1234 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001235 vregs_.SetSize(number_of_vregs);
1236 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001237 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001238 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001239
1240 locations_.SetSize(number_of_vregs);
1241 for (size_t i = 0; i < number_of_vregs; ++i) {
1242 locations_.Put(i, Location());
1243 }
1244 }
1245
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001246 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001247 : HEnvironment(arena,
1248 to_copy.Size(),
1249 to_copy.GetDexFile(),
1250 to_copy.GetMethodIdx(),
1251 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001252 to_copy.GetInvokeType(),
1253 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001254
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001255 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001256 if (parent_ != nullptr) {
1257 parent_->SetAndCopyParentChain(allocator, parent);
1258 } else {
1259 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1260 parent_->CopyFrom(parent);
1261 if (parent->GetParent() != nullptr) {
1262 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1263 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001264 }
David Brazdiled596192015-01-23 10:39:45 +00001265 }
1266
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001267 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1268 void CopyFrom(HEnvironment* environment);
1269
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001270 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1271 // input to the loop phi instead. This is for inserting instructions that
1272 // require an environment (like HDeoptimization) in the loop pre-header.
1273 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001274
1275 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001276 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001277 }
1278
1279 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001280 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001281 }
1282
David Brazdil1abb4192015-02-17 18:33:36 +00001283 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001284
1285 size_t Size() const { return vregs_.Size(); }
1286
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001287 HEnvironment* GetParent() const { return parent_; }
1288
1289 void SetLocationAt(size_t index, Location location) {
1290 locations_.Put(index, location);
1291 }
1292
1293 Location GetLocationAt(size_t index) const {
1294 return locations_.Get(index);
1295 }
1296
1297 uint32_t GetDexPc() const {
1298 return dex_pc_;
1299 }
1300
1301 uint32_t GetMethodIdx() const {
1302 return method_idx_;
1303 }
1304
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001305 InvokeType GetInvokeType() const {
1306 return invoke_type_;
1307 }
1308
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001309 const DexFile& GetDexFile() const {
1310 return dex_file_;
1311 }
1312
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001313 HInstruction* GetHolder() const {
1314 return holder_;
1315 }
1316
David Brazdiled596192015-01-23 10:39:45 +00001317 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001318 // Record instructions' use entries of this environment for constant-time removal.
1319 // It should only be called by HInstruction when a new environment use is added.
1320 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1321 DCHECK(env_use->GetUser() == this);
1322 size_t index = env_use->GetIndex();
1323 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1324 }
David Brazdiled596192015-01-23 10:39:45 +00001325
David Brazdil1abb4192015-02-17 18:33:36 +00001326 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001327 GrowableArray<Location> locations_;
1328 HEnvironment* parent_;
1329 const DexFile& dex_file_;
1330 const uint32_t method_idx_;
1331 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001332 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001333
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001334 // The instruction that holds this environment. Only used in debug mode
1335 // to ensure the graph is consistent.
1336 HInstruction* const holder_;
1337
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001338 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001339
1340 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1341};
1342
Calin Juravleacf735c2015-02-12 15:25:22 +00001343class ReferenceTypeInfo : ValueObject {
1344 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001345 typedef Handle<mirror::Class> TypeHandle;
1346
1347 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1349 if (type_handle->IsObjectClass()) {
1350 // Override the type handle to be consistent with the case when we get to
1351 // Top but don't have the Object class available. It avoids having to guess
1352 // what value the type_handle has when it's Top.
1353 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1354 } else {
1355 return ReferenceTypeInfo(type_handle, is_exact, false);
1356 }
1357 }
1358
1359 static ReferenceTypeInfo CreateTop(bool is_exact) {
1360 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001361 }
1362
1363 bool IsExact() const { return is_exact_; }
1364 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001365 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1366 return !IsTop() && GetTypeHandle()->IsInterface();
1367 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001368
1369 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1370
Calin Juravleb1498f62015-02-16 13:13:29 +00001371 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001372 if (IsTop()) {
1373 // Top (equivalent for java.lang.Object) is supertype of anything.
1374 return true;
1375 }
1376 if (rti.IsTop()) {
1377 // If we get here `this` is not Top() so it can't be a supertype.
1378 return false;
1379 }
1380 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1381 }
1382
1383 // Returns true if the type information provide the same amount of details.
1384 // Note that it does not mean that the instructions have the same actual type
1385 // (e.g. tops are equal but they can be the result of a merge).
1386 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1387 if (IsExact() != rti.IsExact()) {
1388 return false;
1389 }
1390 if (IsTop() && rti.IsTop()) {
1391 // `Top` means java.lang.Object, so the types are equivalent.
1392 return true;
1393 }
1394 if (IsTop() || rti.IsTop()) {
1395 // If only one is top or object than they are not equivalent.
1396 // NB: We need this extra check because the type_handle of `Top` is invalid
1397 // and we cannot inspect its reference.
1398 return false;
1399 }
1400
1401 // Finally check the types.
1402 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1403 }
1404
1405 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001406 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1407 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1408 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1409
Calin Juravleacf735c2015-02-12 15:25:22 +00001410 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001411 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001412 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001413 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001414 bool is_exact_;
1415 // A true value here means that the object type should be java.lang.Object.
1416 // We don't have access to the corresponding mirror object every time so this
1417 // flag acts as a substitute. When true, the TypeHandle refers to a null
1418 // pointer and should not be used.
1419 bool is_top_;
1420};
1421
1422std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1423
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001424class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001425 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001426 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001427 : previous_(nullptr),
1428 next_(nullptr),
1429 block_(nullptr),
1430 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001431 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001432 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001433 locations_(nullptr),
1434 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001435 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001436 side_effects_(side_effects),
1437 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001438
Dave Allison20dfc792014-06-16 20:44:29 -07001439 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001440
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001441#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001442 enum InstructionKind {
1443 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1444 };
1445#undef DECLARE_KIND
1446
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001447 HInstruction* GetNext() const { return next_; }
1448 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001449
Calin Juravle77520bc2015-01-12 18:45:46 +00001450 HInstruction* GetNextDisregardingMoves() const;
1451 HInstruction* GetPreviousDisregardingMoves() const;
1452
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001453 HBasicBlock* GetBlock() const { return block_; }
1454 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001455 bool IsInBlock() const { return block_ != nullptr; }
1456 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001457 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001458
Roland Levillain6b879dd2014-09-22 17:13:44 +01001459 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001460 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001461
1462 virtual void Accept(HGraphVisitor* visitor) = 0;
1463 virtual const char* DebugName() const = 0;
1464
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001465 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001466 void SetRawInputAt(size_t index, HInstruction* input) {
1467 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1468 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001469
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001470 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001471 virtual uint32_t GetDexPc() const {
1472 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1473 " does not need an environment";
1474 UNREACHABLE();
1475 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001476 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001477 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001478 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001479
Calin Juravle10e244f2015-01-26 18:54:32 +00001480 // Does not apply for all instructions, but having this at top level greatly
1481 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001482 virtual bool CanBeNull() const {
1483 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1484 return true;
1485 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001486
Calin Juravle641547a2015-04-21 22:08:51 +01001487 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1488 UNUSED(obj);
1489 return false;
1490 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001491
Calin Juravleacf735c2015-02-12 15:25:22 +00001492 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001493 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001494 reference_type_info_ = reference_type_info;
1495 }
1496
Calin Juravle61d544b2015-02-23 16:46:57 +00001497 ReferenceTypeInfo GetReferenceTypeInfo() const {
1498 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1499 return reference_type_info_;
1500 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001501
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001502 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001503 DCHECK(user != nullptr);
1504 HUseListNode<HInstruction*>* use =
1505 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1506 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001507 }
1508
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001509 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001510 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001511 HUseListNode<HEnvironment*>* env_use =
1512 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1513 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001514 }
1515
David Brazdil1abb4192015-02-17 18:33:36 +00001516 void RemoveAsUserOfInput(size_t input) {
1517 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1518 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1519 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001520
David Brazdil1abb4192015-02-17 18:33:36 +00001521 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1522 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001523
David Brazdiled596192015-01-23 10:39:45 +00001524 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1525 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001526 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001527 bool HasOnlyOneNonEnvironmentUse() const {
1528 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1529 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001530
Roland Levillain6c82d402014-10-13 16:10:27 +01001531 // Does this instruction strictly dominate `other_instruction`?
1532 // Returns false if this instruction and `other_instruction` are the same.
1533 // Aborts if this instruction and `other_instruction` are both phis.
1534 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001535
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001536 int GetId() const { return id_; }
1537 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001538
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001539 int GetSsaIndex() const { return ssa_index_; }
1540 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1541 bool HasSsaIndex() const { return ssa_index_ != -1; }
1542
1543 bool HasEnvironment() const { return environment_ != nullptr; }
1544 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001545 // Set the `environment_` field. Raw because this method does not
1546 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001547 void SetRawEnvironment(HEnvironment* environment) {
1548 DCHECK(environment_ == nullptr);
1549 DCHECK_EQ(environment->GetHolder(), this);
1550 environment_ = environment;
1551 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001552
1553 // Set the environment of this instruction, copying it from `environment`. While
1554 // copying, the uses lists are being updated.
1555 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001556 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001557 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001558 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001559 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001560 if (environment->GetParent() != nullptr) {
1561 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1562 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001563 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001564
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001565 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1566 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001567 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001568 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001569 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001570 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001571 if (environment->GetParent() != nullptr) {
1572 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1573 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001574 }
1575
Nicolas Geoffray39468442014-09-02 15:17:15 +01001576 // Returns the number of entries in the environment. Typically, that is the
1577 // number of dex registers in a method. It could be more in case of inlining.
1578 size_t EnvironmentSize() const;
1579
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001580 LocationSummary* GetLocations() const { return locations_; }
1581 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001582
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001583 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001584 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001585
Alexandre Rames188d4312015-04-09 18:30:21 +01001586 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1587 // uses of this instruction by `other` are *not* updated.
1588 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1589 ReplaceWith(other);
1590 other->ReplaceInput(this, use_index);
1591 }
1592
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001593 // Move `this` instruction before `cursor`.
1594 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001595
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001596#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001597 bool Is##type() const { return (As##type() != nullptr); } \
1598 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001599 virtual H##type* As##type() { return nullptr; }
1600
1601 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1602#undef INSTRUCTION_TYPE_CHECK
1603
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001604 // Returns whether the instruction can be moved within the graph.
1605 virtual bool CanBeMoved() const { return false; }
1606
1607 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001608 virtual bool InstructionTypeEquals(HInstruction* other) const {
1609 UNUSED(other);
1610 return false;
1611 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001612
1613 // Returns whether any data encoded in the two instructions is equal.
1614 // This method does not look at the inputs. Both instructions must be
1615 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001616 virtual bool InstructionDataEquals(HInstruction* other) const {
1617 UNUSED(other);
1618 return false;
1619 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001620
1621 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001622 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001623 // 2) Their inputs are identical.
1624 bool Equals(HInstruction* other) const;
1625
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001626 virtual InstructionKind GetKind() const = 0;
1627
1628 virtual size_t ComputeHashCode() const {
1629 size_t result = GetKind();
1630 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1631 result = (result * 31) + InputAt(i)->GetId();
1632 }
1633 return result;
1634 }
1635
1636 SideEffects GetSideEffects() const { return side_effects_; }
1637
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001638 size_t GetLifetimePosition() const { return lifetime_position_; }
1639 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1640 LiveInterval* GetLiveInterval() const { return live_interval_; }
1641 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1642 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1643
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001644 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1645
1646 // Returns whether the code generation of the instruction will require to have access
1647 // to the current method. Such instructions are:
1648 // (1): Instructions that require an environment, as calling the runtime requires
1649 // to walk the stack and have the current method stored at a specific stack address.
1650 // (2): Object literals like classes and strings, that are loaded from the dex cache
1651 // fields of the current method.
1652 bool NeedsCurrentMethod() const {
1653 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1654 }
1655
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001656 virtual bool NeedsDexCache() const { return false; }
1657
David Brazdil1abb4192015-02-17 18:33:36 +00001658 protected:
1659 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1660 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1661
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001662 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001663 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1664
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001665 HInstruction* previous_;
1666 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001667 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001668
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001669 // An instruction gets an id when it is added to the graph.
1670 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001671 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001672 int id_;
1673
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001674 // When doing liveness analysis, instructions that have uses get an SSA index.
1675 int ssa_index_;
1676
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001677 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001678 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001679
1680 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001681 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001682
Nicolas Geoffray39468442014-09-02 15:17:15 +01001683 // The environment associated with this instruction. Not null if the instruction
1684 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001685 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001686
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001687 // Set by the code generator.
1688 LocationSummary* locations_;
1689
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001690 // Set by the liveness analysis.
1691 LiveInterval* live_interval_;
1692
1693 // Set by the liveness analysis, this is the position in a linear
1694 // order of blocks where this instruction's live interval start.
1695 size_t lifetime_position_;
1696
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001697 const SideEffects side_effects_;
1698
Calin Juravleacf735c2015-02-12 15:25:22 +00001699 // TODO: for primitive types this should be marked as invalid.
1700 ReferenceTypeInfo reference_type_info_;
1701
David Brazdil1abb4192015-02-17 18:33:36 +00001702 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001703 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001704 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001705 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001706 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001707
1708 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1709};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001710std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001711
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001712class HInputIterator : public ValueObject {
1713 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001714 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001715
1716 bool Done() const { return index_ == instruction_->InputCount(); }
1717 HInstruction* Current() const { return instruction_->InputAt(index_); }
1718 void Advance() { index_++; }
1719
1720 private:
1721 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001722 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001723
1724 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1725};
1726
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001727class HInstructionIterator : public ValueObject {
1728 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001729 explicit HInstructionIterator(const HInstructionList& instructions)
1730 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001731 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001732 }
1733
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001734 bool Done() const { return instruction_ == nullptr; }
1735 HInstruction* Current() const { return instruction_; }
1736 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001737 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001738 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001739 }
1740
1741 private:
1742 HInstruction* instruction_;
1743 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001744
1745 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001746};
1747
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001748class HBackwardInstructionIterator : public ValueObject {
1749 public:
1750 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1751 : instruction_(instructions.last_instruction_) {
1752 next_ = Done() ? nullptr : instruction_->GetPrevious();
1753 }
1754
1755 bool Done() const { return instruction_ == nullptr; }
1756 HInstruction* Current() const { return instruction_; }
1757 void Advance() {
1758 instruction_ = next_;
1759 next_ = Done() ? nullptr : instruction_->GetPrevious();
1760 }
1761
1762 private:
1763 HInstruction* instruction_;
1764 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001765
1766 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001767};
1768
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001769// An embedded container with N elements of type T. Used (with partial
1770// specialization for N=0) because embedded arrays cannot have size 0.
1771template<typename T, intptr_t N>
1772class EmbeddedArray {
1773 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001774 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001775
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001776 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777
1778 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001779 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001780 return elements_[i];
1781 }
1782
1783 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001784 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001785 return elements_[i];
1786 }
1787
1788 const T& At(intptr_t i) const {
1789 return (*this)[i];
1790 }
1791
1792 void SetAt(intptr_t i, const T& val) {
1793 (*this)[i] = val;
1794 }
1795
1796 private:
1797 T elements_[N];
1798};
1799
1800template<typename T>
1801class EmbeddedArray<T, 0> {
1802 public:
1803 intptr_t length() const { return 0; }
1804 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001805 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001806 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001807 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001808 }
1809 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001810 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001811 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001812 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001813 }
1814};
1815
1816template<intptr_t N>
1817class HTemplateInstruction: public HInstruction {
1818 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001819 HTemplateInstruction<N>(SideEffects side_effects)
1820 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001821 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001822
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001823 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001824
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001825 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001826 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1827
1828 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1829 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001830 }
1831
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001832 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001833 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001834
1835 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001836};
1837
Dave Allison20dfc792014-06-16 20:44:29 -07001838template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001839class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001840 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001841 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1842 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001843 virtual ~HExpression() {}
1844
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001845 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001846
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001847 protected:
1848 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001849};
1850
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001851// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1852// instruction that branches to the exit block.
1853class HReturnVoid : public HTemplateInstruction<0> {
1854 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001855 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001857 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001858
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001859 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001860
1861 private:
1862 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1863};
1864
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001865// Represents dex's RETURN opcodes. A HReturn is a control flow
1866// instruction that branches to the exit block.
1867class HReturn : public HTemplateInstruction<1> {
1868 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001869 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870 SetRawInputAt(0, value);
1871 }
1872
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001873 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001874
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001875 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001876
1877 private:
1878 DISALLOW_COPY_AND_ASSIGN(HReturn);
1879};
1880
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001881// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001882// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001883// exit block.
1884class HExit : public HTemplateInstruction<0> {
1885 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001886 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001887
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001888 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001889
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001890 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001891
1892 private:
1893 DISALLOW_COPY_AND_ASSIGN(HExit);
1894};
1895
1896// Jumps from one block to another.
1897class HGoto : public HTemplateInstruction<0> {
1898 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001899 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1900
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001901 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001902
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001903 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001904 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001905 }
1906
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001907 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001908
1909 private:
1910 DISALLOW_COPY_AND_ASSIGN(HGoto);
1911};
1912
Dave Allison20dfc792014-06-16 20:44:29 -07001913
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001914// Conditional branch. A block ending with an HIf instruction must have
1915// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001916class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001917 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001918 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001919 SetRawInputAt(0, input);
1920 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001921
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001922 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001923
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001924 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001925 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001926 }
1927
1928 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001929 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001930 }
1931
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001932 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001933
1934 private:
1935 DISALLOW_COPY_AND_ASSIGN(HIf);
1936};
1937
David Brazdilfc6a86a2015-06-26 10:33:45 +00001938
1939// Abstract instruction which marks the beginning and/or end of a try block and
1940// links it to the respective exception handlers. Behaves the same as a Goto in
1941// non-exceptional control flow.
1942// Normal-flow successor is stored at index zero, exception handlers under
1943// higher indices in no particular order.
1944class HTryBoundary : public HTemplateInstruction<0> {
1945 public:
1946 HTryBoundary(bool is_entry, bool is_exit)
1947 : HTemplateInstruction(SideEffects::None()), is_entry_(is_entry), is_exit_(is_exit) {}
1948
1949 bool IsControlFlow() const OVERRIDE { return true; }
1950
1951 // Returns the block's non-exceptional successor (index zero).
1952 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
1953
1954 // Returns whether `handler` is among its exception handlers (non-zero index
1955 // successors).
1956 bool HasExceptionHandler(HBasicBlock* handler) const {
1957 DCHECK(handler->IsCatchBlock());
1958 return GetBlock()->GetSuccessors().Contains(handler, /* start_from */ 1);
1959 }
1960
1961 // Returns whether successor at index `idx` is an exception handler.
1962 bool IsExceptionalSuccessor(size_t idx) const {
1963 DCHECK_LT(idx, GetBlock()->GetSuccessors().Size());
1964 bool is_handler = (idx != 0);
1965 DCHECK(!is_handler || GetBlock()->GetSuccessors().Get(idx)->IsCatchBlock());
1966 return is_handler;
1967 }
1968
1969 // If not present already, adds `handler` to its block's list of exception
1970 // handlers.
1971 void AddExceptionHandler(HBasicBlock* handler) {
1972 if (!HasExceptionHandler(handler)) {
1973 GetBlock()->AddSuccessor(handler);
1974 }
1975 }
1976
1977 bool IsTryEntry() const { return is_entry_; }
1978 bool IsTryExit() const { return is_exit_; }
1979
1980 DECLARE_INSTRUCTION(TryBoundary);
1981
1982 private:
1983 // Only for debugging purposes.
1984 bool is_entry_;
1985 bool is_exit_;
1986
1987 // Only set by HGraphBuilder.
1988 void SetIsTryEntry() { is_entry_ = true; }
1989 void SetIsTryExit() { is_exit_ = true; }
1990
1991 friend HGraphBuilder;
1992
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 Geoffray19a19cf2014-10-22 16:07:05 +01003712
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003713 bool NeedsEnvironment() const OVERRIDE {
3714 // Will call runtime and load the class if the class is not loaded yet.
3715 // TODO: finer grain decision.
3716 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003717 }
3718
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003719 bool MustGenerateClinitCheck() const {
3720 return generate_clinit_check_;
3721 }
3722
Calin Juravle0ba218d2015-05-19 18:46:01 +01003723 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3724 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003725 }
3726
3727 bool CanCallRuntime() const {
3728 return MustGenerateClinitCheck() || !is_referrers_class_;
3729 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003730
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003731 bool CanThrow() const OVERRIDE {
3732 // May call runtime and and therefore can throw.
3733 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003734 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003735 }
3736
Calin Juravleacf735c2015-02-12 15:25:22 +00003737 ReferenceTypeInfo GetLoadedClassRTI() {
3738 return loaded_class_rti_;
3739 }
3740
3741 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3742 // Make sure we only set exact types (the loaded class should never be merged).
3743 DCHECK(rti.IsExact());
3744 loaded_class_rti_ = rti;
3745 }
3746
3747 bool IsResolved() {
3748 return loaded_class_rti_.IsExact();
3749 }
3750
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003751 const DexFile& GetDexFile() { return dex_file_; }
3752
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003753 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3754
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003755 DECLARE_INSTRUCTION(LoadClass);
3756
3757 private:
3758 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003759 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003760 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003761 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003762 // Whether this instruction must generate the initialization check.
3763 // Used for code generation.
3764 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003765
Calin Juravleacf735c2015-02-12 15:25:22 +00003766 ReferenceTypeInfo loaded_class_rti_;
3767
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003768 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3769};
3770
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003771class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003772 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003773 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003774 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3775 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003776 dex_pc_(dex_pc) {
3777 SetRawInputAt(0, current_method);
3778 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003779
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003780 bool CanBeMoved() const OVERRIDE { return true; }
3781
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003782 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3783 return other->AsLoadString()->string_index_ == string_index_;
3784 }
3785
3786 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3787
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003788 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003789 uint32_t GetStringIndex() const { return string_index_; }
3790
3791 // TODO: Can we deopt or debug when we resolve a string?
3792 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003793 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003794
3795 DECLARE_INSTRUCTION(LoadString);
3796
3797 private:
3798 const uint32_t string_index_;
3799 const uint32_t dex_pc_;
3800
3801 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3802};
3803
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003804/**
3805 * Performs an initialization check on its Class object input.
3806 */
3807class HClinitCheck : public HExpression<1> {
3808 public:
3809 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003810 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003811 dex_pc_(dex_pc) {
3812 SetRawInputAt(0, constant);
3813 }
3814
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003815 bool CanBeMoved() const OVERRIDE { return true; }
3816 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3817 UNUSED(other);
3818 return true;
3819 }
3820
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003821 bool NeedsEnvironment() const OVERRIDE {
3822 // May call runtime to initialize the class.
3823 return true;
3824 }
3825
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003826 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003827
3828 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3829
3830 DECLARE_INSTRUCTION(ClinitCheck);
3831
3832 private:
3833 const uint32_t dex_pc_;
3834
3835 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3836};
3837
3838class HStaticFieldGet : public HExpression<1> {
3839 public:
3840 HStaticFieldGet(HInstruction* cls,
3841 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003842 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003843 bool is_volatile,
3844 uint32_t field_idx,
3845 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003846 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003847 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003848 SetRawInputAt(0, cls);
3849 }
3850
Calin Juravle52c48962014-12-16 17:02:57 +00003851
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003852 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003853
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003854 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003855 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3856 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003857 }
3858
3859 size_t ComputeHashCode() const OVERRIDE {
3860 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3861 }
3862
Calin Juravle52c48962014-12-16 17:02:57 +00003863 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003864 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3865 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003866 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003867
3868 DECLARE_INSTRUCTION(StaticFieldGet);
3869
3870 private:
3871 const FieldInfo field_info_;
3872
3873 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3874};
3875
3876class HStaticFieldSet : public HTemplateInstruction<2> {
3877 public:
3878 HStaticFieldSet(HInstruction* cls,
3879 HInstruction* value,
3880 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003881 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003882 bool is_volatile,
3883 uint32_t field_idx,
3884 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003885 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003886 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003887 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003888 SetRawInputAt(0, cls);
3889 SetRawInputAt(1, value);
3890 }
3891
Calin Juravle52c48962014-12-16 17:02:57 +00003892 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003893 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3894 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003895 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003896
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003897 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003898 bool GetValueCanBeNull() const { return value_can_be_null_; }
3899 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003900
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003901 DECLARE_INSTRUCTION(StaticFieldSet);
3902
3903 private:
3904 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003905 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003906
3907 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3908};
3909
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003910// Implement the move-exception DEX instruction.
3911class HLoadException : public HExpression<0> {
3912 public:
3913 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3914
3915 DECLARE_INSTRUCTION(LoadException);
3916
3917 private:
3918 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3919};
3920
3921class HThrow : public HTemplateInstruction<1> {
3922 public:
3923 HThrow(HInstruction* exception, uint32_t dex_pc)
3924 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3925 SetRawInputAt(0, exception);
3926 }
3927
3928 bool IsControlFlow() const OVERRIDE { return true; }
3929
3930 bool NeedsEnvironment() const OVERRIDE { return true; }
3931
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003932 bool CanThrow() const OVERRIDE { return true; }
3933
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003934 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003935
3936 DECLARE_INSTRUCTION(Throw);
3937
3938 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003939 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003940
3941 DISALLOW_COPY_AND_ASSIGN(HThrow);
3942};
3943
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003944class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003945 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003946 HInstanceOf(HInstruction* object,
3947 HLoadClass* constant,
3948 bool class_is_final,
3949 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003950 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3951 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003952 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003953 dex_pc_(dex_pc) {
3954 SetRawInputAt(0, object);
3955 SetRawInputAt(1, constant);
3956 }
3957
3958 bool CanBeMoved() const OVERRIDE { return true; }
3959
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003960 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003961 return true;
3962 }
3963
3964 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003965 return false;
3966 }
3967
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003968 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003969
3970 bool IsClassFinal() const { return class_is_final_; }
3971
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003972 // Used only in code generation.
3973 bool MustDoNullCheck() const { return must_do_null_check_; }
3974 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3975
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003976 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003977
3978 private:
3979 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003980 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003981 const uint32_t dex_pc_;
3982
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003983 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3984};
3985
Calin Juravleb1498f62015-02-16 13:13:29 +00003986class HBoundType : public HExpression<1> {
3987 public:
3988 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3989 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3990 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003991 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003992 SetRawInputAt(0, input);
3993 }
3994
3995 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3996
3997 bool CanBeNull() const OVERRIDE {
3998 // `null instanceof ClassX` always return false so we can't be null.
3999 return false;
4000 }
4001
4002 DECLARE_INSTRUCTION(BoundType);
4003
4004 private:
4005 // Encodes the most upper class that this instruction can have. In other words
4006 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
4007 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
4008 const ReferenceTypeInfo bound_type_;
4009
4010 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4011};
4012
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004013class HCheckCast : public HTemplateInstruction<2> {
4014 public:
4015 HCheckCast(HInstruction* object,
4016 HLoadClass* constant,
4017 bool class_is_final,
4018 uint32_t dex_pc)
4019 : HTemplateInstruction(SideEffects::None()),
4020 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004021 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004022 dex_pc_(dex_pc) {
4023 SetRawInputAt(0, object);
4024 SetRawInputAt(1, constant);
4025 }
4026
4027 bool CanBeMoved() const OVERRIDE { return true; }
4028
4029 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4030 return true;
4031 }
4032
4033 bool NeedsEnvironment() const OVERRIDE {
4034 // Instruction may throw a CheckCastError.
4035 return true;
4036 }
4037
4038 bool CanThrow() const OVERRIDE { return true; }
4039
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004040 bool MustDoNullCheck() const { return must_do_null_check_; }
4041 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4042
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004043 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004044
4045 bool IsClassFinal() const { return class_is_final_; }
4046
4047 DECLARE_INSTRUCTION(CheckCast);
4048
4049 private:
4050 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004051 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004052 const uint32_t dex_pc_;
4053
4054 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004055};
4056
Calin Juravle27df7582015-04-17 19:12:31 +01004057class HMemoryBarrier : public HTemplateInstruction<0> {
4058 public:
4059 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
4060 : HTemplateInstruction(SideEffects::None()),
4061 barrier_kind_(barrier_kind) {}
4062
4063 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4064
4065 DECLARE_INSTRUCTION(MemoryBarrier);
4066
4067 private:
4068 const MemBarrierKind barrier_kind_;
4069
4070 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4071};
4072
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004073class HMonitorOperation : public HTemplateInstruction<1> {
4074 public:
4075 enum OperationKind {
4076 kEnter,
4077 kExit,
4078 };
4079
4080 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
4081 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
4082 SetRawInputAt(0, object);
4083 }
4084
4085 // Instruction may throw a Java exception, so we need an environment.
4086 bool NeedsEnvironment() const OVERRIDE { return true; }
4087 bool CanThrow() const OVERRIDE { return true; }
4088
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004089 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004090
4091 bool IsEnter() const { return kind_ == kEnter; }
4092
4093 DECLARE_INSTRUCTION(MonitorOperation);
4094
Calin Juravle52c48962014-12-16 17:02:57 +00004095 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004096 const OperationKind kind_;
4097 const uint32_t dex_pc_;
4098
4099 private:
4100 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4101};
4102
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004103class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004104 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004105 MoveOperands(Location source,
4106 Location destination,
4107 Primitive::Type type,
4108 HInstruction* instruction)
4109 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004110
4111 Location GetSource() const { return source_; }
4112 Location GetDestination() const { return destination_; }
4113
4114 void SetSource(Location value) { source_ = value; }
4115 void SetDestination(Location value) { destination_ = value; }
4116
4117 // The parallel move resolver marks moves as "in-progress" by clearing the
4118 // destination (but not the source).
4119 Location MarkPending() {
4120 DCHECK(!IsPending());
4121 Location dest = destination_;
4122 destination_ = Location::NoLocation();
4123 return dest;
4124 }
4125
4126 void ClearPending(Location dest) {
4127 DCHECK(IsPending());
4128 destination_ = dest;
4129 }
4130
4131 bool IsPending() const {
4132 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4133 return destination_.IsInvalid() && !source_.IsInvalid();
4134 }
4135
4136 // True if this blocks a move from the given location.
4137 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004138 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004139 }
4140
4141 // A move is redundant if it's been eliminated, if its source and
4142 // destination are the same, or if its destination is unneeded.
4143 bool IsRedundant() const {
4144 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4145 }
4146
4147 // We clear both operands to indicate move that's been eliminated.
4148 void Eliminate() {
4149 source_ = destination_ = Location::NoLocation();
4150 }
4151
4152 bool IsEliminated() const {
4153 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4154 return source_.IsInvalid();
4155 }
4156
Alexey Frunze4dda3372015-06-01 18:31:49 -07004157 Primitive::Type GetType() const { return type_; }
4158
Nicolas Geoffray90218252015-04-15 11:56:51 +01004159 bool Is64BitMove() const {
4160 return Primitive::Is64BitType(type_);
4161 }
4162
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004163 HInstruction* GetInstruction() const { return instruction_; }
4164
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004165 private:
4166 Location source_;
4167 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004168 // The type this move is for.
4169 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004170 // The instruction this move is assocatied with. Null when this move is
4171 // for moving an input in the expected locations of user (including a phi user).
4172 // This is only used in debug mode, to ensure we do not connect interval siblings
4173 // in the same parallel move.
4174 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004175};
4176
4177static constexpr size_t kDefaultNumberOfMoves = 4;
4178
4179class HParallelMove : public HTemplateInstruction<0> {
4180 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004181 explicit HParallelMove(ArenaAllocator* arena)
4182 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004183
Nicolas Geoffray90218252015-04-15 11:56:51 +01004184 void AddMove(Location source,
4185 Location destination,
4186 Primitive::Type type,
4187 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004188 DCHECK(source.IsValid());
4189 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004190 if (kIsDebugBuild) {
4191 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004192 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004193 if (moves_.Get(i).GetInstruction() == instruction) {
4194 // Special case the situation where the move is for the spill slot
4195 // of the instruction.
4196 if ((GetPrevious() == instruction)
4197 || ((GetPrevious() == nullptr)
4198 && instruction->IsPhi()
4199 && instruction->GetBlock() == GetBlock())) {
4200 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4201 << "Doing parallel moves for the same instruction.";
4202 } else {
4203 DCHECK(false) << "Doing parallel moves for the same instruction.";
4204 }
4205 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004206 }
4207 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004208 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004209 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004210 << "Overlapped destination for two moves in a parallel move: "
4211 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4212 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004213 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004214 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004215 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004216 }
4217
4218 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004219 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004220 }
4221
4222 size_t NumMoves() const { return moves_.Size(); }
4223
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004224 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004225
4226 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004227 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004228
4229 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4230};
4231
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004232class HGraphVisitor : public ValueObject {
4233 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004234 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4235 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004236
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004237 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004238 virtual void VisitBasicBlock(HBasicBlock* block);
4239
Roland Levillain633021e2014-10-01 14:12:25 +01004240 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004241 void VisitInsertionOrder();
4242
Roland Levillain633021e2014-10-01 14:12:25 +01004243 // Visit the graph following dominator tree reverse post-order.
4244 void VisitReversePostOrder();
4245
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004246 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004247
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004248 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004249#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004250 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4251
4252 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4253
4254#undef DECLARE_VISIT_INSTRUCTION
4255
4256 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004257 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004258
4259 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4260};
4261
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004262class HGraphDelegateVisitor : public HGraphVisitor {
4263 public:
4264 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4265 virtual ~HGraphDelegateVisitor() {}
4266
4267 // Visit functions that delegate to to super class.
4268#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004269 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004270
4271 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4272
4273#undef DECLARE_VISIT_INSTRUCTION
4274
4275 private:
4276 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4277};
4278
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004279class HInsertionOrderIterator : public ValueObject {
4280 public:
4281 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4282
4283 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4284 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4285 void Advance() { ++index_; }
4286
4287 private:
4288 const HGraph& graph_;
4289 size_t index_;
4290
4291 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4292};
4293
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004294class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004295 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004296 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4297 // Check that reverse post order of the graph has been built.
4298 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4299 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004300
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004301 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4302 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004303 void Advance() { ++index_; }
4304
4305 private:
4306 const HGraph& graph_;
4307 size_t index_;
4308
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004309 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004310};
4311
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004312class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004313 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004314 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004315 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4316 // Check that reverse post order of the graph has been built.
4317 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4318 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004319
4320 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004321 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004322 void Advance() { --index_; }
4323
4324 private:
4325 const HGraph& graph_;
4326 size_t index_;
4327
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004328 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004329};
4330
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004331class HLinearPostOrderIterator : public ValueObject {
4332 public:
4333 explicit HLinearPostOrderIterator(const HGraph& graph)
4334 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4335
4336 bool Done() const { return index_ == 0; }
4337
4338 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4339
4340 void Advance() {
4341 --index_;
4342 DCHECK_GE(index_, 0U);
4343 }
4344
4345 private:
4346 const GrowableArray<HBasicBlock*>& order_;
4347 size_t index_;
4348
4349 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4350};
4351
4352class HLinearOrderIterator : public ValueObject {
4353 public:
4354 explicit HLinearOrderIterator(const HGraph& graph)
4355 : order_(graph.GetLinearOrder()), index_(0) {}
4356
4357 bool Done() const { return index_ == order_.Size(); }
4358 HBasicBlock* Current() const { return order_.Get(index_); }
4359 void Advance() { ++index_; }
4360
4361 private:
4362 const GrowableArray<HBasicBlock*>& order_;
4363 size_t index_;
4364
4365 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4366};
4367
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004368// Iterator over the blocks that art part of the loop. Includes blocks part
4369// of an inner loop. The order in which the blocks are iterated is on their
4370// block id.
4371class HBlocksInLoopIterator : public ValueObject {
4372 public:
4373 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4374 : blocks_in_loop_(info.GetBlocks()),
4375 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4376 index_(0) {
4377 if (!blocks_in_loop_.IsBitSet(index_)) {
4378 Advance();
4379 }
4380 }
4381
4382 bool Done() const { return index_ == blocks_.Size(); }
4383 HBasicBlock* Current() const { return blocks_.Get(index_); }
4384 void Advance() {
4385 ++index_;
4386 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4387 if (blocks_in_loop_.IsBitSet(index_)) {
4388 break;
4389 }
4390 }
4391 }
4392
4393 private:
4394 const BitVector& blocks_in_loop_;
4395 const GrowableArray<HBasicBlock*>& blocks_;
4396 size_t index_;
4397
4398 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4399};
4400
Mingyao Yang3584bce2015-05-19 16:01:59 -07004401// Iterator over the blocks that art part of the loop. Includes blocks part
4402// of an inner loop. The order in which the blocks are iterated is reverse
4403// post order.
4404class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4405 public:
4406 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4407 : blocks_in_loop_(info.GetBlocks()),
4408 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4409 index_(0) {
4410 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4411 Advance();
4412 }
4413 }
4414
4415 bool Done() const { return index_ == blocks_.Size(); }
4416 HBasicBlock* Current() const { return blocks_.Get(index_); }
4417 void Advance() {
4418 ++index_;
4419 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4420 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4421 break;
4422 }
4423 }
4424 }
4425
4426 private:
4427 const BitVector& blocks_in_loop_;
4428 const GrowableArray<HBasicBlock*>& blocks_;
4429 size_t index_;
4430
4431 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4432};
4433
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004434inline int64_t Int64FromConstant(HConstant* constant) {
4435 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4436 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4437 : constant->AsLongConstant()->GetValue();
4438}
4439
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004440} // namespace art
4441
4442#endif // ART_COMPILER_OPTIMIZING_NODES_H_