blob: 26eee1c52e2f2962048d15ca9def516fd609af36 [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;
42class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000045class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000047class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010048class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010049class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010050class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000051class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010052class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000054
55static const int kDefaultNumberOfBlocks = 8;
56static const int kDefaultNumberOfSuccessors = 2;
57static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010058static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000059static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060
Calin Juravle9aec02f2014-11-18 23:06:35 +000061static constexpr uint32_t kMaxIntShiftValue = 0x1f;
62static constexpr uint64_t kMaxLongShiftValue = 0x3f;
63
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010064static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
65
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010066static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
67
Dave Allison20dfc792014-06-16 20:44:29 -070068enum IfCondition {
69 kCondEQ,
70 kCondNE,
71 kCondLT,
72 kCondLE,
73 kCondGT,
74 kCondGE,
75};
76
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077class HInstructionList {
78 public:
79 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
80
81 void AddInstruction(HInstruction* instruction);
82 void RemoveInstruction(HInstruction* instruction);
83
David Brazdilc3d743f2015-04-22 13:40:50 +010084 // Insert `instruction` before/after an existing instruction `cursor`.
85 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
86 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
87
Roland Levillain6b469232014-09-25 10:10:38 +010088 // Return true if this list contains `instruction`.
89 bool Contains(HInstruction* instruction) const;
90
Roland Levillainccc07a92014-09-16 14:48:16 +010091 // Return true if `instruction1` is found before `instruction2` in
92 // this instruction list and false otherwise. Abort if none
93 // of these instructions is found.
94 bool FoundBefore(const HInstruction* instruction1,
95 const HInstruction* instruction2) const;
96
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000097 bool IsEmpty() const { return first_instruction_ == nullptr; }
98 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
99
100 // Update the block of all instructions to be `block`.
101 void SetBlockOfInstructions(HBasicBlock* block) const;
102
103 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
104 void Add(const HInstructionList& instruction_list);
105
David Brazdil2d7352b2015-04-20 14:52:42 +0100106 // Return the number of instructions in the list. This is an expensive operation.
107 size_t CountSize() const;
108
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 private:
110 HInstruction* first_instruction_;
111 HInstruction* last_instruction_;
112
113 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000114 friend class HGraph;
115 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100116 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100117 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100118
119 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
120};
121
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000122// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700123class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000124 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100125 HGraph(ArenaAllocator* arena,
126 const DexFile& dex_file,
127 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100128 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100130 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100131 bool debuggable = false,
132 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000133 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100135 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100136 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700137 entry_block_(nullptr),
138 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100139 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140 number_of_vregs_(0),
141 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000142 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400143 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000144 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000145 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100146 dex_file_(dex_file),
147 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100148 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100149 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100150 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000152 cached_null_constant_(nullptr),
153 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000154 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
155 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100156 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
157 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000158
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000159 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100160 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100161 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000163 HBasicBlock* GetEntryBlock() const { return entry_block_; }
164 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100165 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000166
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000167 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
168 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000169
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000170 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100171
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000172 // Try building the SSA form of this graph, with dominance computation and loop
173 // recognition. Returns whether it was successful in doing all these steps.
174 bool TryBuildingSsa() {
175 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000176 // The SSA builder requires loops to all be natural. Specifically, the dead phi
177 // elimination phase checks the consistency of the graph when doing a post-order
178 // visit for eliminating dead phis: a dead phi can only have loop header phi
179 // users remaining when being visited.
180 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100182 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000183 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000184 }
185
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000186 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100188 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000189
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000190 // Analyze all natural loops in this graph. Returns false if one
191 // loop is not natural, that is the header does not dominate the
192 // back edge.
193 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100194
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000195 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
196 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
197
Mingyao Yang3584bce2015-05-19 16:01:59 -0700198 // Need to add a couple of blocks to test if the loop body is entered and
199 // put deoptimization instructions, etc.
200 void TransformLoopHeaderForBCE(HBasicBlock* header);
201
David Brazdil2d7352b2015-04-20 14:52:42 +0100202 // Removes `block` from the graph.
203 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
206 void SimplifyLoop(HBasicBlock* header);
207
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000208 int32_t GetNextInstructionId() {
209 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000210 return current_instruction_id_++;
211 }
212
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 int32_t GetCurrentInstructionId() const {
214 return current_instruction_id_;
215 }
216
217 void SetCurrentInstructionId(int32_t id) {
218 current_instruction_id_ = id;
219 }
220
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100221 uint16_t GetMaximumNumberOfOutVRegs() const {
222 return maximum_number_of_out_vregs_;
223 }
224
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
226 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100227 }
228
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100229 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
230 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
231 }
232
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000233 void UpdateTemporariesVRegSlots(size_t slots) {
234 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100235 }
236
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000237 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100238 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000239 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100240 }
241
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100242 void SetNumberOfVRegs(uint16_t number_of_vregs) {
243 number_of_vregs_ = number_of_vregs;
244 }
245
246 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100247 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100248 return number_of_vregs_;
249 }
250
251 void SetNumberOfInVRegs(uint16_t value) {
252 number_of_in_vregs_ = value;
253 }
254
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100255 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100256 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100257 return number_of_vregs_ - number_of_in_vregs_;
258 }
259
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100260 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
261 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100262 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100263
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100264 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
265 return linear_order_;
266 }
267
Mark Mendell1152c922015-04-24 17:06:35 -0400268 bool HasBoundsChecks() const {
269 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800270 }
271
Mark Mendell1152c922015-04-24 17:06:35 -0400272 void SetHasBoundsChecks(bool value) {
273 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800274 }
275
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100276 bool ShouldGenerateConstructorBarrier() const {
277 return should_generate_constructor_barrier_;
278 }
279
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000280 bool IsDebuggable() const { return debuggable_; }
281
David Brazdil8d5b8b22015-03-24 10:51:52 +0000282 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000283 // already, it is created and inserted into the graph. This method is only for
284 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000285 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000286 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000287 HIntConstant* GetIntConstant(int32_t value) {
288 return CreateConstant(value, &cached_int_constants_);
289 }
290 HLongConstant* GetLongConstant(int64_t value) {
291 return CreateConstant(value, &cached_long_constants_);
292 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000293 HFloatConstant* GetFloatConstant(float value) {
294 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
295 }
296 HDoubleConstant* GetDoubleConstant(double value) {
297 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
298 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000299
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100300 HCurrentMethod* GetCurrentMethod();
301
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000302 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100303
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100304 const DexFile& GetDexFile() const {
305 return dex_file_;
306 }
307
308 uint32_t GetMethodIdx() const {
309 return method_idx_;
310 }
311
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100312 InvokeType GetInvokeType() const {
313 return invoke_type_;
314 }
315
David Brazdil2d7352b2015-04-20 14:52:42 +0100316 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000317 void VisitBlockForDominatorTree(HBasicBlock* block,
318 HBasicBlock* predecessor,
319 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100320 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 void VisitBlockForBackEdges(HBasicBlock* block,
322 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100323 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000324 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100325 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000327 template <class InstructionType, typename ValueType>
328 InstructionType* CreateConstant(ValueType value,
329 ArenaSafeMap<ValueType, InstructionType*>* cache) {
330 // Try to find an existing constant of the given value.
331 InstructionType* constant = nullptr;
332 auto cached_constant = cache->find(value);
333 if (cached_constant != cache->end()) {
334 constant = cached_constant->second;
335 }
336
337 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100338 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000339 if (constant == nullptr || constant->GetBlock() == nullptr) {
340 constant = new (arena_) InstructionType(value);
341 cache->Overwrite(value, constant);
342 InsertConstant(constant);
343 }
344 return constant;
345 }
346
David Brazdil8d5b8b22015-03-24 10:51:52 +0000347 void InsertConstant(HConstant* instruction);
348
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000349 // Cache a float constant into the graph. This method should only be
350 // called by the SsaBuilder when creating "equivalent" instructions.
351 void CacheFloatConstant(HFloatConstant* constant);
352
353 // See CacheFloatConstant comment.
354 void CacheDoubleConstant(HDoubleConstant* constant);
355
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000357
358 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000359 GrowableArray<HBasicBlock*> blocks_;
360
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100361 // List of blocks to perform a reverse post order tree traversal.
362 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000363
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100364 // List of blocks to perform a linear order tree traversal.
365 GrowableArray<HBasicBlock*> linear_order_;
366
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000367 HBasicBlock* entry_block_;
368 HBasicBlock* exit_block_;
369
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100370 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100371 uint16_t maximum_number_of_out_vregs_;
372
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100373 // The number of virtual registers in this method. Contains the parameters.
374 uint16_t number_of_vregs_;
375
376 // The number of virtual registers used by parameters of this method.
377 uint16_t number_of_in_vregs_;
378
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000379 // Number of vreg size slots that the temporaries use (used in baseline compiler).
380 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100381
Mark Mendell1152c922015-04-24 17:06:35 -0400382 // Has bounds checks. We can totally skip BCE if it's false.
383 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800384
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000385 // Indicates whether the graph should be compiled in a way that
386 // ensures full debuggability. If false, we can apply more
387 // aggressive optimizations that may limit the level of debugging.
388 const bool debuggable_;
389
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000390 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000392
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100393 // The dex file from which the method is from.
394 const DexFile& dex_file_;
395
396 // The method index in the dex file.
397 const uint32_t method_idx_;
398
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100399 // If inlined, this encodes how the callee is being invoked.
400 const InvokeType invoke_type_;
401
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100402 // Whether the graph has been transformed to SSA form. Only used
403 // in debug mode to ensure we are not using properties only valid
404 // for non-SSA form (like the number of temporaries).
405 bool in_ssa_form_;
406
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100407 const bool should_generate_constructor_barrier_;
408
Mathieu Chartiere401d142015-04-22 13:56:20 -0700409 const InstructionSet instruction_set_;
410
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000411 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000412 HNullConstant* cached_null_constant_;
413 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000414 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000415 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000416 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000417
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100418 HCurrentMethod* cached_current_method_;
419
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000420 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100421 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000422 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000423 DISALLOW_COPY_AND_ASSIGN(HGraph);
424};
425
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700426class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000427 public:
428 HLoopInformation(HBasicBlock* header, HGraph* graph)
429 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100430 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100431 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100432 // Make bit vector growable, as the number of blocks may change.
433 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100434
435 HBasicBlock* GetHeader() const {
436 return header_;
437 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000438
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000439 void SetHeader(HBasicBlock* block) {
440 header_ = block;
441 }
442
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100443 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
444 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
445 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
446
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000447 void AddBackEdge(HBasicBlock* back_edge) {
448 back_edges_.Add(back_edge);
449 }
450
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100451 void RemoveBackEdge(HBasicBlock* back_edge) {
452 back_edges_.Delete(back_edge);
453 }
454
David Brazdil46e2a392015-03-16 17:31:52 +0000455 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100456 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000457 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100458 }
459 return false;
460 }
461
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000462 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000463 return back_edges_.Size();
464 }
465
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100466 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100467
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
469 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100470 }
471
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100472 // Returns the lifetime position of the back edge that has the
473 // greatest lifetime position.
474 size_t GetLifetimeEnd() const;
475
476 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
477 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
478 if (back_edges_.Get(i) == existing) {
479 back_edges_.Put(i, new_back_edge);
480 return;
481 }
482 }
483 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100484 }
485
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100486 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100487 // that is the header dominates the back edge.
488 bool Populate();
489
David Brazdila4b8c212015-05-07 09:59:30 +0100490 // Reanalyzes the loop by removing loop info from its blocks and re-running
491 // Populate(). If there are no back edges left, the loop info is completely
492 // removed as well as its SuspendCheck instruction. It must be run on nested
493 // inner loops first.
494 void Update();
495
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100496 // Returns whether this loop information contains `block`.
497 // Note that this loop information *must* be populated before entering this function.
498 bool Contains(const HBasicBlock& block) const;
499
500 // Returns whether this loop information is an inner loop of `other`.
501 // Note that `other` *must* be populated before entering this function.
502 bool IsIn(const HLoopInformation& other) const;
503
504 const ArenaBitVector& GetBlocks() const { return blocks_; }
505
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000506 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000507 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000508
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000509 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 // Internal recursive implementation of `Populate`.
511 void PopulateRecursive(HBasicBlock* block);
512
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000513 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100514 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000515 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100516 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000517
518 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
519};
520
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100521static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100522static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100523
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000524// A block in a method. Contains the list of instructions represented
525// as a double linked list. Each block knows its predecessors and
526// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100527
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700528class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100530 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000531 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000532 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
533 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000534 loop_information_(nullptr),
535 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100536 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100537 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100538 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100539 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000540 lifetime_end_(kNoLifetime),
541 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000542
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100543 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
544 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000545 }
546
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100547 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
548 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000549 }
550
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100551 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
552 return dominated_blocks_;
553 }
554
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100555 bool IsEntryBlock() const {
556 return graph_->GetEntryBlock() == this;
557 }
558
559 bool IsExitBlock() const {
560 return graph_->GetExitBlock() == this;
561 }
562
David Brazdil46e2a392015-03-16 17:31:52 +0000563 bool IsSingleGoto() const;
564
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000565 void AddBackEdge(HBasicBlock* back_edge) {
566 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000567 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100569 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000570 loop_information_->AddBackEdge(back_edge);
571 }
572
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000573 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000574 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000575
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000576 int GetBlockId() const { return block_id_; }
577 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000578
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000579 HBasicBlock* GetDominator() const { return dominator_; }
580 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100581 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100582 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000583 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
584 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
585 if (dominated_blocks_.Get(i) == existing) {
586 dominated_blocks_.Put(i, new_block);
587 return;
588 }
589 }
590 LOG(FATAL) << "Unreachable";
591 UNREACHABLE();
592 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000593
594 int NumberOfBackEdges() const {
595 return loop_information_ == nullptr
596 ? 0
597 : loop_information_->NumberOfBackEdges();
598 }
599
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100600 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
601 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100602 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100603 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100604 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
605 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000606
607 void AddSuccessor(HBasicBlock* block) {
608 successors_.Add(block);
609 block->predecessors_.Add(this);
610 }
611
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100612 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
613 size_t successor_index = GetSuccessorIndexOf(existing);
614 DCHECK_NE(successor_index, static_cast<size_t>(-1));
615 existing->RemovePredecessor(this);
616 new_block->predecessors_.Add(this);
617 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618 }
619
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000620 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
621 size_t predecessor_index = GetPredecessorIndexOf(existing);
622 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
623 existing->RemoveSuccessor(this);
624 new_block->successors_.Add(this);
625 predecessors_.Put(predecessor_index, new_block);
626 }
627
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100628 // Insert `this` between `predecessor` and `successor. This method
629 // preserves the indicies, and will update the first edge found between
630 // `predecessor` and `successor`.
631 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
632 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
633 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
634 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
635 DCHECK_NE(successor_index, static_cast<size_t>(-1));
636 successor->predecessors_.Put(predecessor_index, this);
637 predecessor->successors_.Put(successor_index, this);
638 successors_.Add(successor);
639 predecessors_.Add(predecessor);
640 }
641
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100642 void RemovePredecessor(HBasicBlock* block) {
643 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100644 }
645
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000646 void RemoveSuccessor(HBasicBlock* block) {
647 successors_.Delete(block);
648 }
649
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100650 void ClearAllPredecessors() {
651 predecessors_.Reset();
652 }
653
654 void AddPredecessor(HBasicBlock* block) {
655 predecessors_.Add(block);
656 block->successors_.Add(this);
657 }
658
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100659 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100660 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100661 HBasicBlock* temp = predecessors_.Get(0);
662 predecessors_.Put(0, predecessors_.Get(1));
663 predecessors_.Put(1, temp);
664 }
665
David Brazdil769c9e52015-04-27 13:54:09 +0100666 void SwapSuccessors() {
667 DCHECK_EQ(successors_.Size(), 2u);
668 HBasicBlock* temp = successors_.Get(0);
669 successors_.Put(0, successors_.Get(1));
670 successors_.Put(1, temp);
671 }
672
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100673 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
674 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
675 if (predecessors_.Get(i) == predecessor) {
676 return i;
677 }
678 }
679 return -1;
680 }
681
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100682 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
683 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
684 if (successors_.Get(i) == successor) {
685 return i;
686 }
687 }
688 return -1;
689 }
690
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000691 // Split the block into two blocks just after `cursor`. Returns the newly
692 // created block. Note that this method just updates raw block information,
693 // like predecessors, successors, dominators, and instruction list. It does not
694 // update the graph, reverse post order, loop information, nor make sure the
695 // blocks are consistent (for example ending with a control flow instruction).
696 HBasicBlock* SplitAfter(HInstruction* cursor);
697
698 // Merge `other` at the end of `this`. Successors and dominated blocks of
699 // `other` are changed to be successors and dominated blocks of `this`. Note
700 // that this method does not update the graph, reverse post order, loop
701 // information, nor make sure the blocks are consistent (for example ending
702 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100703 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000704
705 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
706 // of `this` are moved to `other`.
707 // Note that this method does not update the graph, reverse post order, loop
708 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000709 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000710 void ReplaceWith(HBasicBlock* other);
711
David Brazdil2d7352b2015-04-20 14:52:42 +0100712 // Merge `other` at the end of `this`. This method updates loops, reverse post
713 // order, links to predecessors, successors, dominators and deletes the block
714 // from the graph. The two blocks must be successive, i.e. `this` the only
715 // predecessor of `other` and vice versa.
716 void MergeWith(HBasicBlock* other);
717
718 // Disconnects `this` from all its predecessors, successors and dominator,
719 // removes it from all loops it is included in and eventually from the graph.
720 // The block must not dominate any other block. Predecessors and successors
721 // are safely updated.
722 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000723
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000724 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100725 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100726 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100727 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100728 // Replace instruction `initial` with `replacement` within this block.
729 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
730 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100731 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100732 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000733 // RemoveInstruction and RemovePhi delete a given instruction from the respective
734 // instruction list. With 'ensure_safety' set to true, it verifies that the
735 // instruction is not in use and removes it from the use lists of its inputs.
736 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
737 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100738 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100739
740 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100741 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100742 }
743
Roland Levillain6b879dd2014-09-22 17:13:44 +0100744 bool IsLoopPreHeaderFirstPredecessor() const {
745 DCHECK(IsLoopHeader());
746 DCHECK(!GetPredecessors().IsEmpty());
747 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
748 }
749
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100750 HLoopInformation* GetLoopInformation() const {
751 return loop_information_;
752 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000753
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000754 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100755 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757 void SetInLoop(HLoopInformation* info) {
758 if (IsLoopHeader()) {
759 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100760 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100761 loop_information_ = info;
762 } else if (loop_information_->Contains(*info->GetHeader())) {
763 // Block is currently part of an outer loop. Make it part of this inner loop.
764 // Note that a non loop header having a loop information means this loop information
765 // has already been populated
766 loop_information_ = info;
767 } else {
768 // Block is part of an inner loop. Do not update the loop information.
769 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
770 // at this point, because this method is being called while populating `info`.
771 }
772 }
773
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000774 // Raw update of the loop information.
775 void SetLoopInformation(HLoopInformation* info) {
776 loop_information_ = info;
777 }
778
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100779 bool IsInLoop() const { return loop_information_ != nullptr; }
780
David Brazdila4b8c212015-05-07 09:59:30 +0100781 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100782 bool Dominates(HBasicBlock* block) const;
783
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100784 size_t GetLifetimeStart() const { return lifetime_start_; }
785 size_t GetLifetimeEnd() const { return lifetime_end_; }
786
787 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
788 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
789
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100790 uint32_t GetDexPc() const { return dex_pc_; }
791
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000792 bool IsCatchBlock() const { return is_catch_block_; }
793 void SetIsCatchBlock() { is_catch_block_ = true; }
794
David Brazdil8d5b8b22015-03-24 10:51:52 +0000795 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000796 bool EndsWithIf() const;
797 bool HasSinglePhi() const;
798
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000800 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000801 GrowableArray<HBasicBlock*> predecessors_;
802 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100803 HInstructionList instructions_;
804 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000805 HLoopInformation* loop_information_;
806 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100807 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000808 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100809 // The dex program counter of the first instruction of this block.
810 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100811 size_t lifetime_start_;
812 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000813 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000815 friend class HGraph;
816 friend class HInstruction;
817
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
819};
820
David Brazdilb2bd1c52015-03-25 11:17:37 +0000821// Iterates over the LoopInformation of all loops which contain 'block'
822// from the innermost to the outermost.
823class HLoopInformationOutwardIterator : public ValueObject {
824 public:
825 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
826 : current_(block.GetLoopInformation()) {}
827
828 bool Done() const { return current_ == nullptr; }
829
830 void Advance() {
831 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100832 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000833 }
834
835 HLoopInformation* Current() const {
836 DCHECK(!Done());
837 return current_;
838 }
839
840 private:
841 HLoopInformation* current_;
842
843 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
844};
845
Alexandre Ramesef20f712015-06-09 10:29:30 +0100846#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100847 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000848 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000849 M(ArrayGet, Instruction) \
850 M(ArrayLength, Instruction) \
851 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100852 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000853 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000854 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000855 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100856 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000857 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100858 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100859 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700860 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000861 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000862 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000863 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100864 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000865 M(Exit, Instruction) \
866 M(FloatConstant, Constant) \
867 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100868 M(GreaterThan, Condition) \
869 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100870 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000871 M(InstanceFieldGet, Instruction) \
872 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000873 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100874 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000875 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000876 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100877 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000878 M(LessThan, Condition) \
879 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000880 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000881 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100882 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000883 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100884 M(Local, Instruction) \
885 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100886 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000887 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000888 M(Mul, BinaryOperation) \
889 M(Neg, UnaryOperation) \
890 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100891 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100892 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000893 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000894 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000895 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000896 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100897 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000898 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100899 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000900 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100901 M(Return, Instruction) \
902 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000903 M(Shl, BinaryOperation) \
904 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100905 M(StaticFieldGet, Instruction) \
906 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100907 M(StoreLocal, Instruction) \
908 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100909 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000910 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000911 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000912 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000913 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000914 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000915
Alexandre Ramesef20f712015-06-09 10:29:30 +0100916#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
917
918#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
919
920#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
921
922#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
923
924#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
925 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
926 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
927 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
928 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
929 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
930
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100931#define FOR_EACH_INSTRUCTION(M) \
932 FOR_EACH_CONCRETE_INSTRUCTION(M) \
933 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100934 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100935 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100936 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700937
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100938#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000939FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
940#undef FORWARD_DECLARATION
941
Roland Levillainccc07a92014-09-16 14:48:16 +0100942#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000943 InstructionKind GetKind() const OVERRIDE { return k##type; } \
944 const char* DebugName() const OVERRIDE { return #type; } \
945 const H##type* As##type() const OVERRIDE { return this; } \
946 H##type* As##type() OVERRIDE { return this; } \
947 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100948 return other->Is##type(); \
949 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000950 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951
David Brazdiled596192015-01-23 10:39:45 +0000952template <typename T> class HUseList;
953
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100954template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700955class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000956 public:
David Brazdiled596192015-01-23 10:39:45 +0000957 HUseListNode* GetPrevious() const { return prev_; }
958 HUseListNode* GetNext() const { return next_; }
959 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100960 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100961 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100962
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000963 private:
David Brazdiled596192015-01-23 10:39:45 +0000964 HUseListNode(T user, size_t index)
965 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
966
967 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100968 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000969 HUseListNode<T>* prev_;
970 HUseListNode<T>* next_;
971
972 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000973
974 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
975};
976
David Brazdiled596192015-01-23 10:39:45 +0000977template <typename T>
978class HUseList : public ValueObject {
979 public:
980 HUseList() : first_(nullptr) {}
981
982 void Clear() {
983 first_ = nullptr;
984 }
985
986 // Adds a new entry at the beginning of the use list and returns
987 // the newly created node.
988 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000989 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000990 if (IsEmpty()) {
991 first_ = new_node;
992 } else {
993 first_->prev_ = new_node;
994 new_node->next_ = first_;
995 first_ = new_node;
996 }
997 return new_node;
998 }
999
1000 HUseListNode<T>* GetFirst() const {
1001 return first_;
1002 }
1003
1004 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001005 DCHECK(node != nullptr);
1006 DCHECK(Contains(node));
1007
David Brazdiled596192015-01-23 10:39:45 +00001008 if (node->prev_ != nullptr) {
1009 node->prev_->next_ = node->next_;
1010 }
1011 if (node->next_ != nullptr) {
1012 node->next_->prev_ = node->prev_;
1013 }
1014 if (node == first_) {
1015 first_ = node->next_;
1016 }
1017 }
1018
David Brazdil1abb4192015-02-17 18:33:36 +00001019 bool Contains(const HUseListNode<T>* node) const {
1020 if (node == nullptr) {
1021 return false;
1022 }
1023 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1024 if (current == node) {
1025 return true;
1026 }
1027 }
1028 return false;
1029 }
1030
David Brazdiled596192015-01-23 10:39:45 +00001031 bool IsEmpty() const {
1032 return first_ == nullptr;
1033 }
1034
1035 bool HasOnlyOneUse() const {
1036 return first_ != nullptr && first_->next_ == nullptr;
1037 }
1038
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001039 size_t SizeSlow() const {
1040 size_t count = 0;
1041 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1042 ++count;
1043 }
1044 return count;
1045 }
1046
David Brazdiled596192015-01-23 10:39:45 +00001047 private:
1048 HUseListNode<T>* first_;
1049};
1050
1051template<typename T>
1052class HUseIterator : public ValueObject {
1053 public:
1054 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1055
1056 bool Done() const { return current_ == nullptr; }
1057
1058 void Advance() {
1059 DCHECK(!Done());
1060 current_ = current_->GetNext();
1061 }
1062
1063 HUseListNode<T>* Current() const {
1064 DCHECK(!Done());
1065 return current_;
1066 }
1067
1068 private:
1069 HUseListNode<T>* current_;
1070
1071 friend class HValue;
1072};
1073
David Brazdil1abb4192015-02-17 18:33:36 +00001074// This class is used by HEnvironment and HInstruction classes to record the
1075// instructions they use and pointers to the corresponding HUseListNodes kept
1076// by the used instructions.
1077template <typename T>
1078class HUserRecord : public ValueObject {
1079 public:
1080 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1081 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1082
1083 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1084 : instruction_(old_record.instruction_), use_node_(use_node) {
1085 DCHECK(instruction_ != nullptr);
1086 DCHECK(use_node_ != nullptr);
1087 DCHECK(old_record.use_node_ == nullptr);
1088 }
1089
1090 HInstruction* GetInstruction() const { return instruction_; }
1091 HUseListNode<T>* GetUseNode() const { return use_node_; }
1092
1093 private:
1094 // Instruction used by the user.
1095 HInstruction* instruction_;
1096
1097 // Corresponding entry in the use list kept by 'instruction_'.
1098 HUseListNode<T>* use_node_;
1099};
1100
Calin Juravle27df7582015-04-17 19:12:31 +01001101// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1102// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1103// flag is consider.
1104// - DependsOn suggests that there is a real dependency between side effects but it only
1105// checks DependendsOnSomething flag.
1106//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001107// Represents the side effects an instruction may have.
1108class SideEffects : public ValueObject {
1109 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001110 SideEffects() : flags_(0) {}
1111
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001112 static SideEffects None() {
1113 return SideEffects(0);
1114 }
1115
1116 static SideEffects All() {
1117 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1118 }
1119
1120 static SideEffects ChangesSomething() {
1121 return SideEffects((1 << kFlagChangesCount) - 1);
1122 }
1123
1124 static SideEffects DependsOnSomething() {
1125 int count = kFlagDependsOnCount - kFlagChangesCount;
1126 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1127 }
1128
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001129 SideEffects Union(SideEffects other) const {
1130 return SideEffects(flags_ | other.flags_);
1131 }
1132
Roland Levillain72bceff2014-09-15 18:29:00 +01001133 bool HasSideEffects() const {
1134 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1135 return (flags_ & all_bits_set) != 0;
1136 }
1137
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001138 bool HasAllSideEffects() const {
1139 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1140 return all_bits_set == (flags_ & all_bits_set);
1141 }
1142
1143 bool DependsOn(SideEffects other) const {
1144 size_t depends_flags = other.ComputeDependsFlags();
1145 return (flags_ & depends_flags) != 0;
1146 }
1147
1148 bool HasDependencies() const {
1149 int count = kFlagDependsOnCount - kFlagChangesCount;
1150 size_t all_bits_set = (1 << count) - 1;
1151 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1152 }
1153
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001154 private:
1155 static constexpr int kFlagChangesSomething = 0;
1156 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1157
1158 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1159 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1160
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001161 explicit SideEffects(size_t flags) : flags_(flags) {}
1162
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001163 size_t ComputeDependsFlags() const {
1164 return flags_ << kFlagChangesCount;
1165 }
1166
1167 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001168};
1169
David Brazdiled596192015-01-23 10:39:45 +00001170// A HEnvironment object contains the values of virtual registers at a given location.
1171class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1172 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001173 HEnvironment(ArenaAllocator* arena,
1174 size_t number_of_vregs,
1175 const DexFile& dex_file,
1176 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001177 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001178 InvokeType invoke_type,
1179 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001180 : vregs_(arena, number_of_vregs),
1181 locations_(arena, number_of_vregs),
1182 parent_(nullptr),
1183 dex_file_(dex_file),
1184 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001185 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001186 invoke_type_(invoke_type),
1187 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001188 vregs_.SetSize(number_of_vregs);
1189 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001190 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001191 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001192
1193 locations_.SetSize(number_of_vregs);
1194 for (size_t i = 0; i < number_of_vregs; ++i) {
1195 locations_.Put(i, Location());
1196 }
1197 }
1198
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001199 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001200 : HEnvironment(arena,
1201 to_copy.Size(),
1202 to_copy.GetDexFile(),
1203 to_copy.GetMethodIdx(),
1204 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001205 to_copy.GetInvokeType(),
1206 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001207
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001208 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001209 if (parent_ != nullptr) {
1210 parent_->SetAndCopyParentChain(allocator, parent);
1211 } else {
1212 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1213 parent_->CopyFrom(parent);
1214 if (parent->GetParent() != nullptr) {
1215 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1216 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001217 }
David Brazdiled596192015-01-23 10:39:45 +00001218 }
1219
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001220 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1221 void CopyFrom(HEnvironment* environment);
1222
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001223 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1224 // input to the loop phi instead. This is for inserting instructions that
1225 // require an environment (like HDeoptimization) in the loop pre-header.
1226 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001227
1228 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001229 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001230 }
1231
1232 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001233 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001234 }
1235
David Brazdil1abb4192015-02-17 18:33:36 +00001236 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001237
1238 size_t Size() const { return vregs_.Size(); }
1239
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001240 HEnvironment* GetParent() const { return parent_; }
1241
1242 void SetLocationAt(size_t index, Location location) {
1243 locations_.Put(index, location);
1244 }
1245
1246 Location GetLocationAt(size_t index) const {
1247 return locations_.Get(index);
1248 }
1249
1250 uint32_t GetDexPc() const {
1251 return dex_pc_;
1252 }
1253
1254 uint32_t GetMethodIdx() const {
1255 return method_idx_;
1256 }
1257
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001258 InvokeType GetInvokeType() const {
1259 return invoke_type_;
1260 }
1261
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001262 const DexFile& GetDexFile() const {
1263 return dex_file_;
1264 }
1265
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001266 HInstruction* GetHolder() const {
1267 return holder_;
1268 }
1269
David Brazdiled596192015-01-23 10:39:45 +00001270 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001271 // Record instructions' use entries of this environment for constant-time removal.
1272 // It should only be called by HInstruction when a new environment use is added.
1273 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1274 DCHECK(env_use->GetUser() == this);
1275 size_t index = env_use->GetIndex();
1276 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1277 }
David Brazdiled596192015-01-23 10:39:45 +00001278
David Brazdil1abb4192015-02-17 18:33:36 +00001279 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001280 GrowableArray<Location> locations_;
1281 HEnvironment* parent_;
1282 const DexFile& dex_file_;
1283 const uint32_t method_idx_;
1284 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001285 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001286
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001287 // The instruction that holds this environment. Only used in debug mode
1288 // to ensure the graph is consistent.
1289 HInstruction* const holder_;
1290
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001291 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001292
1293 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1294};
1295
Calin Juravleacf735c2015-02-12 15:25:22 +00001296class ReferenceTypeInfo : ValueObject {
1297 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001298 typedef Handle<mirror::Class> TypeHandle;
1299
1300 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1302 if (type_handle->IsObjectClass()) {
1303 // Override the type handle to be consistent with the case when we get to
1304 // Top but don't have the Object class available. It avoids having to guess
1305 // what value the type_handle has when it's Top.
1306 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1307 } else {
1308 return ReferenceTypeInfo(type_handle, is_exact, false);
1309 }
1310 }
1311
1312 static ReferenceTypeInfo CreateTop(bool is_exact) {
1313 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001314 }
1315
1316 bool IsExact() const { return is_exact_; }
1317 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001318 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1319 return !IsTop() && GetTypeHandle()->IsInterface();
1320 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001321
1322 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1323
Calin Juravleb1498f62015-02-16 13:13:29 +00001324 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001325 if (IsTop()) {
1326 // Top (equivalent for java.lang.Object) is supertype of anything.
1327 return true;
1328 }
1329 if (rti.IsTop()) {
1330 // If we get here `this` is not Top() so it can't be a supertype.
1331 return false;
1332 }
1333 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1334 }
1335
1336 // Returns true if the type information provide the same amount of details.
1337 // Note that it does not mean that the instructions have the same actual type
1338 // (e.g. tops are equal but they can be the result of a merge).
1339 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1340 if (IsExact() != rti.IsExact()) {
1341 return false;
1342 }
1343 if (IsTop() && rti.IsTop()) {
1344 // `Top` means java.lang.Object, so the types are equivalent.
1345 return true;
1346 }
1347 if (IsTop() || rti.IsTop()) {
1348 // If only one is top or object than they are not equivalent.
1349 // NB: We need this extra check because the type_handle of `Top` is invalid
1350 // and we cannot inspect its reference.
1351 return false;
1352 }
1353
1354 // Finally check the types.
1355 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1356 }
1357
1358 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001359 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1360 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1361 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1362
Calin Juravleacf735c2015-02-12 15:25:22 +00001363 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001364 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001365 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001366 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001367 bool is_exact_;
1368 // A true value here means that the object type should be java.lang.Object.
1369 // We don't have access to the corresponding mirror object every time so this
1370 // flag acts as a substitute. When true, the TypeHandle refers to a null
1371 // pointer and should not be used.
1372 bool is_top_;
1373};
1374
1375std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1376
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001377class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001378 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001379 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001380 : previous_(nullptr),
1381 next_(nullptr),
1382 block_(nullptr),
1383 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001384 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001385 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001386 locations_(nullptr),
1387 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001388 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001389 side_effects_(side_effects),
1390 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001391
Dave Allison20dfc792014-06-16 20:44:29 -07001392 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001393
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001394#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001395 enum InstructionKind {
1396 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1397 };
1398#undef DECLARE_KIND
1399
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001400 HInstruction* GetNext() const { return next_; }
1401 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001402
Calin Juravle77520bc2015-01-12 18:45:46 +00001403 HInstruction* GetNextDisregardingMoves() const;
1404 HInstruction* GetPreviousDisregardingMoves() const;
1405
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001406 HBasicBlock* GetBlock() const { return block_; }
1407 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001408 bool IsInBlock() const { return block_ != nullptr; }
1409 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001410 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001411
Roland Levillain6b879dd2014-09-22 17:13:44 +01001412 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001413 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001414
1415 virtual void Accept(HGraphVisitor* visitor) = 0;
1416 virtual const char* DebugName() const = 0;
1417
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001418 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001419 void SetRawInputAt(size_t index, HInstruction* input) {
1420 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1421 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001422
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001423 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001424 virtual uint32_t GetDexPc() const {
1425 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1426 " does not need an environment";
1427 UNREACHABLE();
1428 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001429 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001430 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001431 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001432
Calin Juravle10e244f2015-01-26 18:54:32 +00001433 // Does not apply for all instructions, but having this at top level greatly
1434 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001435 virtual bool CanBeNull() const {
1436 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1437 return true;
1438 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001439
Calin Juravle641547a2015-04-21 22:08:51 +01001440 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1441 UNUSED(obj);
1442 return false;
1443 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001444
Calin Juravleacf735c2015-02-12 15:25:22 +00001445 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001446 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001447 reference_type_info_ = reference_type_info;
1448 }
1449
Calin Juravle61d544b2015-02-23 16:46:57 +00001450 ReferenceTypeInfo GetReferenceTypeInfo() const {
1451 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1452 return reference_type_info_;
1453 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001454
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001455 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001456 DCHECK(user != nullptr);
1457 HUseListNode<HInstruction*>* use =
1458 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1459 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001460 }
1461
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001462 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001463 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001464 HUseListNode<HEnvironment*>* env_use =
1465 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1466 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001467 }
1468
David Brazdil1abb4192015-02-17 18:33:36 +00001469 void RemoveAsUserOfInput(size_t input) {
1470 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1471 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1472 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001473
David Brazdil1abb4192015-02-17 18:33:36 +00001474 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1475 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001476
David Brazdiled596192015-01-23 10:39:45 +00001477 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1478 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001479 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001480 bool HasOnlyOneNonEnvironmentUse() const {
1481 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1482 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001483
Roland Levillain6c82d402014-10-13 16:10:27 +01001484 // Does this instruction strictly dominate `other_instruction`?
1485 // Returns false if this instruction and `other_instruction` are the same.
1486 // Aborts if this instruction and `other_instruction` are both phis.
1487 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001488
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001489 int GetId() const { return id_; }
1490 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001491
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001492 int GetSsaIndex() const { return ssa_index_; }
1493 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1494 bool HasSsaIndex() const { return ssa_index_ != -1; }
1495
1496 bool HasEnvironment() const { return environment_ != nullptr; }
1497 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001498 // Set the `environment_` field. Raw because this method does not
1499 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001500 void SetRawEnvironment(HEnvironment* environment) {
1501 DCHECK(environment_ == nullptr);
1502 DCHECK_EQ(environment->GetHolder(), this);
1503 environment_ = environment;
1504 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001505
1506 // Set the environment of this instruction, copying it from `environment`. While
1507 // copying, the uses lists are being updated.
1508 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001509 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001510 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001511 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001512 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001513 if (environment->GetParent() != nullptr) {
1514 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1515 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001516 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001517
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001518 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1519 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001520 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001521 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001522 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001523 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001524 if (environment->GetParent() != nullptr) {
1525 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1526 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001527 }
1528
Nicolas Geoffray39468442014-09-02 15:17:15 +01001529 // Returns the number of entries in the environment. Typically, that is the
1530 // number of dex registers in a method. It could be more in case of inlining.
1531 size_t EnvironmentSize() const;
1532
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001533 LocationSummary* GetLocations() const { return locations_; }
1534 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001535
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001536 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001537 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001538
Alexandre Rames188d4312015-04-09 18:30:21 +01001539 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1540 // uses of this instruction by `other` are *not* updated.
1541 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1542 ReplaceWith(other);
1543 other->ReplaceInput(this, use_index);
1544 }
1545
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001546 // Move `this` instruction before `cursor`.
1547 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001548
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001549#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001550 bool Is##type() const { return (As##type() != nullptr); } \
1551 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001552 virtual H##type* As##type() { return nullptr; }
1553
1554 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1555#undef INSTRUCTION_TYPE_CHECK
1556
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001557 // Returns whether the instruction can be moved within the graph.
1558 virtual bool CanBeMoved() const { return false; }
1559
1560 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001561 virtual bool InstructionTypeEquals(HInstruction* other) const {
1562 UNUSED(other);
1563 return false;
1564 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001565
1566 // Returns whether any data encoded in the two instructions is equal.
1567 // This method does not look at the inputs. Both instructions must be
1568 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001569 virtual bool InstructionDataEquals(HInstruction* other) const {
1570 UNUSED(other);
1571 return false;
1572 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001573
1574 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001575 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001576 // 2) Their inputs are identical.
1577 bool Equals(HInstruction* other) const;
1578
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001579 virtual InstructionKind GetKind() const = 0;
1580
1581 virtual size_t ComputeHashCode() const {
1582 size_t result = GetKind();
1583 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1584 result = (result * 31) + InputAt(i)->GetId();
1585 }
1586 return result;
1587 }
1588
1589 SideEffects GetSideEffects() const { return side_effects_; }
1590
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001591 size_t GetLifetimePosition() const { return lifetime_position_; }
1592 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1593 LiveInterval* GetLiveInterval() const { return live_interval_; }
1594 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1595 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1596
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001597 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1598
1599 // Returns whether the code generation of the instruction will require to have access
1600 // to the current method. Such instructions are:
1601 // (1): Instructions that require an environment, as calling the runtime requires
1602 // to walk the stack and have the current method stored at a specific stack address.
1603 // (2): Object literals like classes and strings, that are loaded from the dex cache
1604 // fields of the current method.
1605 bool NeedsCurrentMethod() const {
1606 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1607 }
1608
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001609 virtual bool NeedsDexCache() const { return false; }
1610
David Brazdil1abb4192015-02-17 18:33:36 +00001611 protected:
1612 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1613 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1614
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001615 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001616 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1617
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001618 HInstruction* previous_;
1619 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001620 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001621
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001622 // An instruction gets an id when it is added to the graph.
1623 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001624 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001625 int id_;
1626
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001627 // When doing liveness analysis, instructions that have uses get an SSA index.
1628 int ssa_index_;
1629
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001630 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001631 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001632
1633 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001634 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001635
Nicolas Geoffray39468442014-09-02 15:17:15 +01001636 // The environment associated with this instruction. Not null if the instruction
1637 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001638 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001639
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001640 // Set by the code generator.
1641 LocationSummary* locations_;
1642
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001643 // Set by the liveness analysis.
1644 LiveInterval* live_interval_;
1645
1646 // Set by the liveness analysis, this is the position in a linear
1647 // order of blocks where this instruction's live interval start.
1648 size_t lifetime_position_;
1649
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001650 const SideEffects side_effects_;
1651
Calin Juravleacf735c2015-02-12 15:25:22 +00001652 // TODO: for primitive types this should be marked as invalid.
1653 ReferenceTypeInfo reference_type_info_;
1654
David Brazdil1abb4192015-02-17 18:33:36 +00001655 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001656 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001657 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001658 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001659 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001660
1661 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1662};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001663std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001664
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001665class HInputIterator : public ValueObject {
1666 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001667 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001668
1669 bool Done() const { return index_ == instruction_->InputCount(); }
1670 HInstruction* Current() const { return instruction_->InputAt(index_); }
1671 void Advance() { index_++; }
1672
1673 private:
1674 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001675 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001676
1677 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1678};
1679
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001680class HInstructionIterator : public ValueObject {
1681 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001682 explicit HInstructionIterator(const HInstructionList& instructions)
1683 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001684 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001685 }
1686
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001687 bool Done() const { return instruction_ == nullptr; }
1688 HInstruction* Current() const { return instruction_; }
1689 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001690 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001691 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001692 }
1693
1694 private:
1695 HInstruction* instruction_;
1696 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001697
1698 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001699};
1700
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001701class HBackwardInstructionIterator : public ValueObject {
1702 public:
1703 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1704 : instruction_(instructions.last_instruction_) {
1705 next_ = Done() ? nullptr : instruction_->GetPrevious();
1706 }
1707
1708 bool Done() const { return instruction_ == nullptr; }
1709 HInstruction* Current() const { return instruction_; }
1710 void Advance() {
1711 instruction_ = next_;
1712 next_ = Done() ? nullptr : instruction_->GetPrevious();
1713 }
1714
1715 private:
1716 HInstruction* instruction_;
1717 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001718
1719 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001720};
1721
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001722// An embedded container with N elements of type T. Used (with partial
1723// specialization for N=0) because embedded arrays cannot have size 0.
1724template<typename T, intptr_t N>
1725class EmbeddedArray {
1726 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001727 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001728
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001729 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001730
1731 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001732 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001733 return elements_[i];
1734 }
1735
1736 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001737 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001738 return elements_[i];
1739 }
1740
1741 const T& At(intptr_t i) const {
1742 return (*this)[i];
1743 }
1744
1745 void SetAt(intptr_t i, const T& val) {
1746 (*this)[i] = val;
1747 }
1748
1749 private:
1750 T elements_[N];
1751};
1752
1753template<typename T>
1754class EmbeddedArray<T, 0> {
1755 public:
1756 intptr_t length() const { return 0; }
1757 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001758 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001759 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001760 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001761 }
1762 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001763 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001764 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001765 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001766 }
1767};
1768
1769template<intptr_t N>
1770class HTemplateInstruction: public HInstruction {
1771 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001772 HTemplateInstruction<N>(SideEffects side_effects)
1773 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001774 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001775
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001776 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001778 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001779 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1780
1781 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1782 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001783 }
1784
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001785 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001786 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787
1788 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001789};
1790
Dave Allison20dfc792014-06-16 20:44:29 -07001791template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001792class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001793 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001794 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1795 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001796 virtual ~HExpression() {}
1797
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001798 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001799
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001800 protected:
1801 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001802};
1803
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001804// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1805// instruction that branches to the exit block.
1806class HReturnVoid : public HTemplateInstruction<0> {
1807 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001808 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001809
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001810 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001811
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001812 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001813
1814 private:
1815 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1816};
1817
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001818// Represents dex's RETURN opcodes. A HReturn is a control flow
1819// instruction that branches to the exit block.
1820class HReturn : public HTemplateInstruction<1> {
1821 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001822 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001823 SetRawInputAt(0, value);
1824 }
1825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001826 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001827
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001828 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001829
1830 private:
1831 DISALLOW_COPY_AND_ASSIGN(HReturn);
1832};
1833
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001834// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001835// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001836// exit block.
1837class HExit : public HTemplateInstruction<0> {
1838 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001839 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001840
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001841 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001842
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001843 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001844
1845 private:
1846 DISALLOW_COPY_AND_ASSIGN(HExit);
1847};
1848
1849// Jumps from one block to another.
1850class HGoto : public HTemplateInstruction<0> {
1851 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001852 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1853
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001854 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001855
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001856 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001857 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001858 }
1859
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001860 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001861
1862 private:
1863 DISALLOW_COPY_AND_ASSIGN(HGoto);
1864};
1865
Dave Allison20dfc792014-06-16 20:44:29 -07001866
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001867// Conditional branch. A block ending with an HIf instruction must have
1868// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001869class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001870 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001871 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001872 SetRawInputAt(0, input);
1873 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001874
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001875 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001876
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001877 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001878 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001879 }
1880
1881 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001882 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001883 }
1884
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001885 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001886
1887 private:
1888 DISALLOW_COPY_AND_ASSIGN(HIf);
1889};
1890
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001891// Deoptimize to interpreter, upon checking a condition.
1892class HDeoptimize : public HTemplateInstruction<1> {
1893 public:
1894 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1895 : HTemplateInstruction(SideEffects::None()),
1896 dex_pc_(dex_pc) {
1897 SetRawInputAt(0, cond);
1898 }
1899
1900 bool NeedsEnvironment() const OVERRIDE { return true; }
1901 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001902 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001903
1904 DECLARE_INSTRUCTION(Deoptimize);
1905
1906 private:
1907 uint32_t dex_pc_;
1908
1909 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1910};
1911
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001912// Represents the ArtMethod that was passed as a first argument to
1913// the method. It is used by instructions that depend on it, like
1914// instructions that work with the dex cache.
1915class HCurrentMethod : public HExpression<0> {
1916 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001917 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001918
1919 DECLARE_INSTRUCTION(CurrentMethod);
1920
1921 private:
1922 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
1923};
1924
Roland Levillain88cb1752014-10-20 16:36:47 +01001925class HUnaryOperation : public HExpression<1> {
1926 public:
1927 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1928 : HExpression(result_type, SideEffects::None()) {
1929 SetRawInputAt(0, input);
1930 }
1931
1932 HInstruction* GetInput() const { return InputAt(0); }
1933 Primitive::Type GetResultType() const { return GetType(); }
1934
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001935 bool CanBeMoved() const OVERRIDE { return true; }
1936 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001937 UNUSED(other);
1938 return true;
1939 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001940
Roland Levillain9240d6a2014-10-20 16:47:04 +01001941 // Try to statically evaluate `operation` and return a HConstant
1942 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001943 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001944 HConstant* TryStaticEvaluation() const;
1945
1946 // Apply this operation to `x`.
1947 virtual int32_t Evaluate(int32_t x) const = 0;
1948 virtual int64_t Evaluate(int64_t x) const = 0;
1949
Roland Levillain88cb1752014-10-20 16:36:47 +01001950 DECLARE_INSTRUCTION(UnaryOperation);
1951
1952 private:
1953 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1954};
1955
Dave Allison20dfc792014-06-16 20:44:29 -07001956class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001957 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001958 HBinaryOperation(Primitive::Type result_type,
1959 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001960 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001961 SetRawInputAt(0, left);
1962 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001963 }
1964
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001965 HInstruction* GetLeft() const { return InputAt(0); }
1966 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001967 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001968
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001969 virtual bool IsCommutative() const { return false; }
1970
1971 // Put constant on the right.
1972 // Returns whether order is changed.
1973 bool OrderInputsWithConstantOnTheRight() {
1974 HInstruction* left = InputAt(0);
1975 HInstruction* right = InputAt(1);
1976 if (left->IsConstant() && !right->IsConstant()) {
1977 ReplaceInput(right, 0);
1978 ReplaceInput(left, 1);
1979 return true;
1980 }
1981 return false;
1982 }
1983
1984 // Order inputs by instruction id, but favor constant on the right side.
1985 // This helps GVN for commutative ops.
1986 void OrderInputs() {
1987 DCHECK(IsCommutative());
1988 HInstruction* left = InputAt(0);
1989 HInstruction* right = InputAt(1);
1990 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1991 return;
1992 }
1993 if (OrderInputsWithConstantOnTheRight()) {
1994 return;
1995 }
1996 // Order according to instruction id.
1997 if (left->GetId() > right->GetId()) {
1998 ReplaceInput(right, 0);
1999 ReplaceInput(left, 1);
2000 }
2001 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002002
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002003 bool CanBeMoved() const OVERRIDE { return true; }
2004 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002005 UNUSED(other);
2006 return true;
2007 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002008
Roland Levillain9240d6a2014-10-20 16:47:04 +01002009 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002010 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002011 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002012 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002013
2014 // Apply this operation to `x` and `y`.
2015 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2016 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2017
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002018 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002019 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002020 HConstant* GetConstantRight() const;
2021
2022 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002023 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002024 HInstruction* GetLeastConstantLeft() const;
2025
Roland Levillainccc07a92014-09-16 14:48:16 +01002026 DECLARE_INSTRUCTION(BinaryOperation);
2027
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002028 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002029 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2030};
2031
Dave Allison20dfc792014-06-16 20:44:29 -07002032class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002033 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002034 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002035 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
2036 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002037
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002038 bool NeedsMaterialization() const { return needs_materialization_; }
2039 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002040
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002041 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002042 // `instruction`, and disregard moves in between.
2043 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002044
Dave Allison20dfc792014-06-16 20:44:29 -07002045 DECLARE_INSTRUCTION(Condition);
2046
2047 virtual IfCondition GetCondition() const = 0;
2048
2049 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002050 // For register allocation purposes, returns whether this instruction needs to be
2051 // materialized (that is, not just be in the processor flags).
2052 bool needs_materialization_;
2053
Dave Allison20dfc792014-06-16 20:44:29 -07002054 DISALLOW_COPY_AND_ASSIGN(HCondition);
2055};
2056
2057// Instruction to check if two inputs are equal to each other.
2058class HEqual : public HCondition {
2059 public:
2060 HEqual(HInstruction* first, HInstruction* second)
2061 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002063 bool IsCommutative() const OVERRIDE { return true; }
2064
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002065 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002066 return x == y ? 1 : 0;
2067 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002068 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002069 return x == y ? 1 : 0;
2070 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002071
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002072 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002073
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002074 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002075 return kCondEQ;
2076 }
2077
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002078 private:
2079 DISALLOW_COPY_AND_ASSIGN(HEqual);
2080};
2081
Dave Allison20dfc792014-06-16 20:44:29 -07002082class HNotEqual : public HCondition {
2083 public:
2084 HNotEqual(HInstruction* first, HInstruction* second)
2085 : HCondition(first, second) {}
2086
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002087 bool IsCommutative() const OVERRIDE { return true; }
2088
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002089 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002090 return x != y ? 1 : 0;
2091 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002092 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002093 return x != y ? 1 : 0;
2094 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002095
Dave Allison20dfc792014-06-16 20:44:29 -07002096 DECLARE_INSTRUCTION(NotEqual);
2097
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002098 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002099 return kCondNE;
2100 }
2101
2102 private:
2103 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2104};
2105
2106class HLessThan : public HCondition {
2107 public:
2108 HLessThan(HInstruction* first, HInstruction* second)
2109 : HCondition(first, second) {}
2110
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002111 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002112 return x < y ? 1 : 0;
2113 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002114 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002115 return x < y ? 1 : 0;
2116 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002117
Dave Allison20dfc792014-06-16 20:44:29 -07002118 DECLARE_INSTRUCTION(LessThan);
2119
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002120 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002121 return kCondLT;
2122 }
2123
2124 private:
2125 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2126};
2127
2128class HLessThanOrEqual : public HCondition {
2129 public:
2130 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2131 : HCondition(first, second) {}
2132
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002133 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002134 return x <= y ? 1 : 0;
2135 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002136 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002137 return x <= y ? 1 : 0;
2138 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002139
Dave Allison20dfc792014-06-16 20:44:29 -07002140 DECLARE_INSTRUCTION(LessThanOrEqual);
2141
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002142 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002143 return kCondLE;
2144 }
2145
2146 private:
2147 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2148};
2149
2150class HGreaterThan : public HCondition {
2151 public:
2152 HGreaterThan(HInstruction* first, HInstruction* second)
2153 : HCondition(first, second) {}
2154
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002155 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002156 return x > y ? 1 : 0;
2157 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002158 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002159 return x > y ? 1 : 0;
2160 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002161
Dave Allison20dfc792014-06-16 20:44:29 -07002162 DECLARE_INSTRUCTION(GreaterThan);
2163
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002164 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002165 return kCondGT;
2166 }
2167
2168 private:
2169 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2170};
2171
2172class HGreaterThanOrEqual : public HCondition {
2173 public:
2174 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2175 : HCondition(first, second) {}
2176
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002177 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002178 return x >= y ? 1 : 0;
2179 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002180 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002181 return x >= y ? 1 : 0;
2182 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002183
Dave Allison20dfc792014-06-16 20:44:29 -07002184 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2185
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002186 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002187 return kCondGE;
2188 }
2189
2190 private:
2191 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2192};
2193
2194
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002195// Instruction to check how two inputs compare to each other.
2196// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2197class HCompare : public HBinaryOperation {
2198 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002199 // The bias applies for floating point operations and indicates how NaN
2200 // comparisons are treated:
2201 enum Bias {
2202 kNoBias, // bias is not applicable (i.e. for long operation)
2203 kGtBias, // return 1 for NaN comparisons
2204 kLtBias, // return -1 for NaN comparisons
2205 };
2206
Alexey Frunze4dda3372015-06-01 18:31:49 -07002207 HCompare(Primitive::Type type,
2208 HInstruction* first,
2209 HInstruction* second,
2210 Bias bias,
2211 uint32_t dex_pc)
2212 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002213 DCHECK_EQ(type, first->GetType());
2214 DCHECK_EQ(type, second->GetType());
2215 }
2216
Calin Juravleddb7df22014-11-25 20:56:51 +00002217 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002218 return
2219 x == y ? 0 :
2220 x > y ? 1 :
2221 -1;
2222 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002223
2224 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002225 return
2226 x == y ? 0 :
2227 x > y ? 1 :
2228 -1;
2229 }
2230
Calin Juravleddb7df22014-11-25 20:56:51 +00002231 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2232 return bias_ == other->AsCompare()->bias_;
2233 }
2234
2235 bool IsGtBias() { return bias_ == kGtBias; }
2236
Alexey Frunze4dda3372015-06-01 18:31:49 -07002237 uint32_t GetDexPc() const { return dex_pc_; }
2238
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002239 DECLARE_INSTRUCTION(Compare);
2240
2241 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002242 const Bias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002243 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002244
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002245 DISALLOW_COPY_AND_ASSIGN(HCompare);
2246};
2247
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002248// A local in the graph. Corresponds to a Dex register.
2249class HLocal : public HTemplateInstruction<0> {
2250 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002251 explicit HLocal(uint16_t reg_number)
2252 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002253
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002254 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002255
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002256 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002257
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002258 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002259 // The Dex register number.
2260 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002261
2262 DISALLOW_COPY_AND_ASSIGN(HLocal);
2263};
2264
2265// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002266class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002267 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002268 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002269 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002270 SetRawInputAt(0, local);
2271 }
2272
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002273 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2274
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002275 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002276
2277 private:
2278 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2279};
2280
2281// Store a value in a given local. This instruction has two inputs: the value
2282// and the local.
2283class HStoreLocal : public HTemplateInstruction<2> {
2284 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002285 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002286 SetRawInputAt(0, local);
2287 SetRawInputAt(1, value);
2288 }
2289
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002290 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2291
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002292 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002293
2294 private:
2295 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2296};
2297
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002298class HConstant : public HExpression<0> {
2299 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002300 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2301
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002302 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002303
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002304 virtual bool IsMinusOne() const { return false; }
2305 virtual bool IsZero() const { return false; }
2306 virtual bool IsOne() const { return false; }
2307
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002308 DECLARE_INSTRUCTION(Constant);
2309
2310 private:
2311 DISALLOW_COPY_AND_ASSIGN(HConstant);
2312};
2313
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002314class HFloatConstant : public HConstant {
2315 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002316 float GetValue() const { return value_; }
2317
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002318 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002319 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2320 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002321 }
2322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002323 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002324
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002325 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002326 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002327 }
2328 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002329 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002330 }
2331 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002332 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2333 }
2334 bool IsNaN() const {
2335 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002336 }
2337
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002338 DECLARE_INSTRUCTION(FloatConstant);
2339
2340 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002341 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002342 explicit HFloatConstant(int32_t value)
2343 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002344
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002345 const float value_;
2346
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002347 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002348 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002349 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002350 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2351};
2352
2353class HDoubleConstant : public HConstant {
2354 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002355 double GetValue() const { return value_; }
2356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002357 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002358 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2359 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002360 }
2361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002362 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002363
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002364 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002365 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002366 }
2367 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002368 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002369 }
2370 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002371 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2372 }
2373 bool IsNaN() const {
2374 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002375 }
2376
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002377 DECLARE_INSTRUCTION(DoubleConstant);
2378
2379 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002380 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002381 explicit HDoubleConstant(int64_t value)
2382 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002383
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002384 const double value_;
2385
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002386 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002387 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002388 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002389 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2390};
2391
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002392class HNullConstant : public HConstant {
2393 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002394 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2395 return true;
2396 }
2397
2398 size_t ComputeHashCode() const OVERRIDE { return 0; }
2399
2400 DECLARE_INSTRUCTION(NullConstant);
2401
2402 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002403 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2404
2405 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002406 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2407};
2408
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002409// Constants of the type int. Those can be from Dex instructions, or
2410// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002411class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002412 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002413 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002414
Calin Juravle61d544b2015-02-23 16:46:57 +00002415 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002416 return other->AsIntConstant()->value_ == value_;
2417 }
2418
Calin Juravle61d544b2015-02-23 16:46:57 +00002419 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2420
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002421 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2422 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2423 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2424
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002425 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002426
2427 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002428 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2429
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002430 const int32_t value_;
2431
David Brazdil8d5b8b22015-03-24 10:51:52 +00002432 friend class HGraph;
2433 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002434 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002435 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2436};
2437
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002438class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002439 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002440 int64_t GetValue() const { return value_; }
2441
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002442 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002443 return other->AsLongConstant()->value_ == value_;
2444 }
2445
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002446 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002447
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002448 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2449 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2450 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2451
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002452 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002453
2454 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002455 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2456
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002457 const int64_t value_;
2458
David Brazdil8d5b8b22015-03-24 10:51:52 +00002459 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002460 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2461};
2462
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002463enum class Intrinsics {
2464#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2465#include "intrinsics_list.h"
2466 kNone,
2467 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2468#undef INTRINSICS_LIST
2469#undef OPTIMIZING_INTRINSICS
2470};
2471std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2472
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002473class HInvoke : public HInstruction {
2474 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002475 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002476
2477 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2478 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002479 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002480
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002481 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002482 SetRawInputAt(index, argument);
2483 }
2484
Roland Levillain3e3d7332015-04-28 11:00:54 +01002485 // Return the number of arguments. This number can be lower than
2486 // the number of inputs returned by InputCount(), as some invoke
2487 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2488 // inputs at the end of their list of inputs.
2489 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002491 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002492
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002493 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002494
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002495 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002496 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002497
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002498 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2499
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002500 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002501 return intrinsic_;
2502 }
2503
2504 void SetIntrinsic(Intrinsics intrinsic) {
2505 intrinsic_ = intrinsic;
2506 }
2507
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002508 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002509 return GetEnvironment()->GetParent() != nullptr;
2510 }
2511
2512 bool CanThrow() const OVERRIDE { return true; }
2513
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002514 DECLARE_INSTRUCTION(Invoke);
2515
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002516 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002517 HInvoke(ArenaAllocator* arena,
2518 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002519 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002520 Primitive::Type return_type,
2521 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002522 uint32_t dex_method_index,
2523 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002524 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002525 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002526 inputs_(arena, number_of_arguments),
2527 return_type_(return_type),
2528 dex_pc_(dex_pc),
2529 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002530 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002531 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002532 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2533 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002534 }
2535
David Brazdil1abb4192015-02-17 18:33:36 +00002536 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2537 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2538 inputs_.Put(index, input);
2539 }
2540
Roland Levillain3e3d7332015-04-28 11:00:54 +01002541 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002542 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002543 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002544 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002545 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002546 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002547 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002548
2549 private:
2550 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2551};
2552
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002553class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002554 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002555 // Requirements of this method call regarding the class
2556 // initialization (clinit) check of its declaring class.
2557 enum class ClinitCheckRequirement {
2558 kNone, // Class already initialized.
2559 kExplicit, // Static call having explicit clinit check as last input.
2560 kImplicit, // Static call implicitly requiring a clinit check.
2561 };
2562
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002563 HInvokeStaticOrDirect(ArenaAllocator* arena,
2564 uint32_t number_of_arguments,
2565 Primitive::Type return_type,
2566 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002567 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002568 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002569 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002570 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002571 InvokeType invoke_type,
2572 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002573 : HInvoke(arena,
2574 number_of_arguments,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002575 // There is one extra argument for the HCurrentMethod node, and
2576 // potentially one other if the clinit check is explicit.
2577 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002578 return_type,
2579 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002580 dex_method_index,
2581 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002582 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002583 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002584 clinit_check_requirement_(clinit_check_requirement),
2585 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002586
Calin Juravle641547a2015-04-21 22:08:51 +01002587 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2588 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002589 // We access the method via the dex cache so we can't do an implicit null check.
2590 // TODO: for intrinsics we can generate implicit null checks.
2591 return false;
2592 }
2593
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002594 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002595 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002596 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002597 bool IsStringInit() const { return string_init_offset_ != 0; }
2598 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002599 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002600
Roland Levillain4c0eb422015-04-24 16:43:49 +01002601 // Is this instruction a call to a static method?
2602 bool IsStatic() const {
2603 return GetInvokeType() == kStatic;
2604 }
2605
Roland Levillain3e3d7332015-04-28 11:00:54 +01002606 // Remove the art::HLoadClass instruction set as last input by
2607 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2608 // the initial art::HClinitCheck instruction (only relevant for
2609 // static calls with explicit clinit check).
2610 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002611 DCHECK(IsStaticWithExplicitClinitCheck());
2612 size_t last_input_index = InputCount() - 1;
2613 HInstruction* last_input = InputAt(last_input_index);
2614 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002615 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002616 RemoveAsUserOfInput(last_input_index);
2617 inputs_.DeleteAt(last_input_index);
2618 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2619 DCHECK(IsStaticWithImplicitClinitCheck());
2620 }
2621
2622 // Is this a call to a static method whose declaring class has an
2623 // explicit intialization check in the graph?
2624 bool IsStaticWithExplicitClinitCheck() const {
2625 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2626 }
2627
2628 // Is this a call to a static method whose declaring class has an
2629 // implicit intialization check requirement?
2630 bool IsStaticWithImplicitClinitCheck() const {
2631 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2632 }
2633
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002634 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002635
Roland Levillain4c0eb422015-04-24 16:43:49 +01002636 protected:
2637 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2638 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2639 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2640 HInstruction* input = input_record.GetInstruction();
2641 // `input` is the last input of a static invoke marked as having
2642 // an explicit clinit check. It must either be:
2643 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2644 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2645 DCHECK(input != nullptr);
2646 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2647 }
2648 return input_record;
2649 }
2650
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002651 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002652 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002653 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002654 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002655 // Thread entrypoint offset for string init method if this is a string init invoke.
2656 // Note that there are multiple string init methods, each having its own offset.
2657 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002658
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002659 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002660};
2661
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002662class HInvokeVirtual : public HInvoke {
2663 public:
2664 HInvokeVirtual(ArenaAllocator* arena,
2665 uint32_t number_of_arguments,
2666 Primitive::Type return_type,
2667 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002668 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002669 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002670 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002671 vtable_index_(vtable_index) {}
2672
Calin Juravle641547a2015-04-21 22:08:51 +01002673 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002674 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002675 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002676 }
2677
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002678 uint32_t GetVTableIndex() const { return vtable_index_; }
2679
2680 DECLARE_INSTRUCTION(InvokeVirtual);
2681
2682 private:
2683 const uint32_t vtable_index_;
2684
2685 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2686};
2687
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002688class HInvokeInterface : public HInvoke {
2689 public:
2690 HInvokeInterface(ArenaAllocator* arena,
2691 uint32_t number_of_arguments,
2692 Primitive::Type return_type,
2693 uint32_t dex_pc,
2694 uint32_t dex_method_index,
2695 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002696 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002697 imt_index_(imt_index) {}
2698
Calin Juravle641547a2015-04-21 22:08:51 +01002699 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002700 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002701 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002702 }
2703
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002704 uint32_t GetImtIndex() const { return imt_index_; }
2705 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2706
2707 DECLARE_INSTRUCTION(InvokeInterface);
2708
2709 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002710 const uint32_t imt_index_;
2711
2712 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2713};
2714
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002715class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002716 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002717 HNewInstance(HCurrentMethod* current_method,
2718 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002719 uint16_t type_index,
2720 const DexFile& dex_file,
2721 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002722 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2723 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002724 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002725 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002726 entrypoint_(entrypoint) {
2727 SetRawInputAt(0, current_method);
2728 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002729
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002730 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002731 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002732 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002733
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002734 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002735 bool NeedsEnvironment() const OVERRIDE { return true; }
2736 // It may throw when called on:
2737 // - interfaces
2738 // - abstract/innaccessible/unknown classes
2739 // TODO: optimize when possible.
2740 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002741
Calin Juravle10e244f2015-01-26 18:54:32 +00002742 bool CanBeNull() const OVERRIDE { return false; }
2743
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002744 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2745
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002746 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002747
2748 private:
2749 const uint32_t dex_pc_;
2750 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002751 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002752 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002753
2754 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2755};
2756
Roland Levillain88cb1752014-10-20 16:36:47 +01002757class HNeg : public HUnaryOperation {
2758 public:
2759 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2760 : HUnaryOperation(result_type, input) {}
2761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002762 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2763 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002764
Roland Levillain88cb1752014-10-20 16:36:47 +01002765 DECLARE_INSTRUCTION(Neg);
2766
2767 private:
2768 DISALLOW_COPY_AND_ASSIGN(HNeg);
2769};
2770
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002771class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002772 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002773 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002774 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002775 uint32_t dex_pc,
2776 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002777 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002778 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002779 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2780 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002781 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002782 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002783 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002784 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002785 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002786 }
2787
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002788 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002789 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002790 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002791
2792 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002793 bool NeedsEnvironment() const OVERRIDE { return true; }
2794
Mingyao Yang0c365e62015-03-31 15:09:29 -07002795 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2796 bool CanThrow() const OVERRIDE { return true; }
2797
Calin Juravle10e244f2015-01-26 18:54:32 +00002798 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002799
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002800 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2801
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002802 DECLARE_INSTRUCTION(NewArray);
2803
2804 private:
2805 const uint32_t dex_pc_;
2806 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002807 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002808 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002809
2810 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2811};
2812
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002813class HAdd : public HBinaryOperation {
2814 public:
2815 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2816 : HBinaryOperation(result_type, left, right) {}
2817
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002818 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002819
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002820 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002821 return x + y;
2822 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002823 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002824 return x + y;
2825 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002826
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002827 DECLARE_INSTRUCTION(Add);
2828
2829 private:
2830 DISALLOW_COPY_AND_ASSIGN(HAdd);
2831};
2832
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002833class HSub : public HBinaryOperation {
2834 public:
2835 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2836 : HBinaryOperation(result_type, left, right) {}
2837
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002838 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002839 return x - y;
2840 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002841 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002842 return x - y;
2843 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002844
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002845 DECLARE_INSTRUCTION(Sub);
2846
2847 private:
2848 DISALLOW_COPY_AND_ASSIGN(HSub);
2849};
2850
Calin Juravle34bacdf2014-10-07 20:23:36 +01002851class HMul : public HBinaryOperation {
2852 public:
2853 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2854 : HBinaryOperation(result_type, left, right) {}
2855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002856 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002857
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002858 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2859 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002860
2861 DECLARE_INSTRUCTION(Mul);
2862
2863 private:
2864 DISALLOW_COPY_AND_ASSIGN(HMul);
2865};
2866
Calin Juravle7c4954d2014-10-28 16:57:40 +00002867class HDiv : public HBinaryOperation {
2868 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002869 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2870 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002871
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002872 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002873 // Our graph structure ensures we never have 0 for `y` during constant folding.
2874 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002875 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002876 return (y == -1) ? -x : x / y;
2877 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002878
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002879 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002880 DCHECK_NE(y, 0);
2881 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2882 return (y == -1) ? -x : x / y;
2883 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002884
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002885 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002886
Calin Juravle7c4954d2014-10-28 16:57:40 +00002887 DECLARE_INSTRUCTION(Div);
2888
2889 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002890 const uint32_t dex_pc_;
2891
Calin Juravle7c4954d2014-10-28 16:57:40 +00002892 DISALLOW_COPY_AND_ASSIGN(HDiv);
2893};
2894
Calin Juravlebacfec32014-11-14 15:54:36 +00002895class HRem : public HBinaryOperation {
2896 public:
2897 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2898 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2899
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002900 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002901 DCHECK_NE(y, 0);
2902 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2903 return (y == -1) ? 0 : x % y;
2904 }
2905
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002906 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002907 DCHECK_NE(y, 0);
2908 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2909 return (y == -1) ? 0 : x % y;
2910 }
2911
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002912 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002913
2914 DECLARE_INSTRUCTION(Rem);
2915
2916 private:
2917 const uint32_t dex_pc_;
2918
2919 DISALLOW_COPY_AND_ASSIGN(HRem);
2920};
2921
Calin Juravled0d48522014-11-04 16:40:20 +00002922class HDivZeroCheck : public HExpression<1> {
2923 public:
2924 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2925 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2926 SetRawInputAt(0, value);
2927 }
2928
2929 bool CanBeMoved() const OVERRIDE { return true; }
2930
2931 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2932 UNUSED(other);
2933 return true;
2934 }
2935
2936 bool NeedsEnvironment() const OVERRIDE { return true; }
2937 bool CanThrow() const OVERRIDE { return true; }
2938
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002939 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002940
2941 DECLARE_INSTRUCTION(DivZeroCheck);
2942
2943 private:
2944 const uint32_t dex_pc_;
2945
2946 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2947};
2948
Calin Juravle9aec02f2014-11-18 23:06:35 +00002949class HShl : public HBinaryOperation {
2950 public:
2951 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2952 : HBinaryOperation(result_type, left, right) {}
2953
2954 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2955 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2956
2957 DECLARE_INSTRUCTION(Shl);
2958
2959 private:
2960 DISALLOW_COPY_AND_ASSIGN(HShl);
2961};
2962
2963class HShr : public HBinaryOperation {
2964 public:
2965 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2966 : HBinaryOperation(result_type, left, right) {}
2967
2968 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2969 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2970
2971 DECLARE_INSTRUCTION(Shr);
2972
2973 private:
2974 DISALLOW_COPY_AND_ASSIGN(HShr);
2975};
2976
2977class HUShr : public HBinaryOperation {
2978 public:
2979 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2980 : HBinaryOperation(result_type, left, right) {}
2981
2982 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2983 uint32_t ux = static_cast<uint32_t>(x);
2984 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2985 return static_cast<int32_t>(ux >> uy);
2986 }
2987
2988 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2989 uint64_t ux = static_cast<uint64_t>(x);
2990 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2991 return static_cast<int64_t>(ux >> uy);
2992 }
2993
2994 DECLARE_INSTRUCTION(UShr);
2995
2996 private:
2997 DISALLOW_COPY_AND_ASSIGN(HUShr);
2998};
2999
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003000class HAnd : public HBinaryOperation {
3001 public:
3002 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3003 : HBinaryOperation(result_type, left, right) {}
3004
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003005 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003006
3007 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3008 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3009
3010 DECLARE_INSTRUCTION(And);
3011
3012 private:
3013 DISALLOW_COPY_AND_ASSIGN(HAnd);
3014};
3015
3016class HOr : public HBinaryOperation {
3017 public:
3018 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3019 : HBinaryOperation(result_type, left, right) {}
3020
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003021 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003022
3023 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3024 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3025
3026 DECLARE_INSTRUCTION(Or);
3027
3028 private:
3029 DISALLOW_COPY_AND_ASSIGN(HOr);
3030};
3031
3032class HXor : public HBinaryOperation {
3033 public:
3034 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3035 : HBinaryOperation(result_type, left, right) {}
3036
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003037 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003038
3039 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3040 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3041
3042 DECLARE_INSTRUCTION(Xor);
3043
3044 private:
3045 DISALLOW_COPY_AND_ASSIGN(HXor);
3046};
3047
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003048// The value of a parameter in this method. Its location depends on
3049// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003050class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003051 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003052 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3053 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003054
3055 uint8_t GetIndex() const { return index_; }
3056
Calin Juravle10e244f2015-01-26 18:54:32 +00003057 bool CanBeNull() const OVERRIDE { return !is_this_; }
3058
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003059 bool IsThis() const { return is_this_; }
3060
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003061 DECLARE_INSTRUCTION(ParameterValue);
3062
3063 private:
3064 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003065 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003066 const uint8_t index_;
3067
Calin Juravle10e244f2015-01-26 18:54:32 +00003068 // Whether or not the parameter value corresponds to 'this' argument.
3069 const bool is_this_;
3070
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003071 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3072};
3073
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003074class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003075 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003076 explicit HNot(Primitive::Type result_type, HInstruction* input)
3077 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003078
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003079 bool CanBeMoved() const OVERRIDE { return true; }
3080 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003081 UNUSED(other);
3082 return true;
3083 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003084
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003085 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3086 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003087
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003088 DECLARE_INSTRUCTION(Not);
3089
3090 private:
3091 DISALLOW_COPY_AND_ASSIGN(HNot);
3092};
3093
David Brazdil66d126e2015-04-03 16:02:44 +01003094class HBooleanNot : public HUnaryOperation {
3095 public:
3096 explicit HBooleanNot(HInstruction* input)
3097 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3098
3099 bool CanBeMoved() const OVERRIDE { return true; }
3100 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3101 UNUSED(other);
3102 return true;
3103 }
3104
3105 int32_t Evaluate(int32_t x) const OVERRIDE {
3106 DCHECK(IsUint<1>(x));
3107 return !x;
3108 }
3109
3110 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3111 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3112 UNREACHABLE();
3113 }
3114
3115 DECLARE_INSTRUCTION(BooleanNot);
3116
3117 private:
3118 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3119};
3120
Roland Levillaindff1f282014-11-05 14:15:05 +00003121class HTypeConversion : public HExpression<1> {
3122 public:
3123 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003124 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3125 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003126 SetRawInputAt(0, input);
3127 DCHECK_NE(input->GetType(), result_type);
3128 }
3129
3130 HInstruction* GetInput() const { return InputAt(0); }
3131 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3132 Primitive::Type GetResultType() const { return GetType(); }
3133
Roland Levillain624279f2014-12-04 11:54:28 +00003134 // Required by the x86 and ARM code generators when producing calls
3135 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003136 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003137
Roland Levillaindff1f282014-11-05 14:15:05 +00003138 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003139 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003140
Mark Mendelle82549b2015-05-06 10:55:34 -04003141 // Try to statically evaluate the conversion and return a HConstant
3142 // containing the result. If the input cannot be converted, return nullptr.
3143 HConstant* TryStaticEvaluation() const;
3144
Roland Levillaindff1f282014-11-05 14:15:05 +00003145 DECLARE_INSTRUCTION(TypeConversion);
3146
3147 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003148 const uint32_t dex_pc_;
3149
Roland Levillaindff1f282014-11-05 14:15:05 +00003150 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3151};
3152
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003153static constexpr uint32_t kNoRegNumber = -1;
3154
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003155class HPhi : public HInstruction {
3156 public:
3157 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003158 : HInstruction(SideEffects::None()),
3159 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003160 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003161 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003162 is_live_(false),
3163 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003164 inputs_.SetSize(number_of_inputs);
3165 }
3166
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003167 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3168 static Primitive::Type ToPhiType(Primitive::Type type) {
3169 switch (type) {
3170 case Primitive::kPrimBoolean:
3171 case Primitive::kPrimByte:
3172 case Primitive::kPrimShort:
3173 case Primitive::kPrimChar:
3174 return Primitive::kPrimInt;
3175 default:
3176 return type;
3177 }
3178 }
3179
Calin Juravle10e244f2015-01-26 18:54:32 +00003180 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003181
3182 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003183 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003184
Calin Juravle10e244f2015-01-26 18:54:32 +00003185 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003186 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003187
Calin Juravle10e244f2015-01-26 18:54:32 +00003188 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3189 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3190
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003191 uint32_t GetRegNumber() const { return reg_number_; }
3192
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003193 void SetDead() { is_live_ = false; }
3194 void SetLive() { is_live_ = true; }
3195 bool IsDead() const { return !is_live_; }
3196 bool IsLive() const { return is_live_; }
3197
Calin Juravlea4f88312015-04-16 12:57:19 +01003198 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3199 // An equivalent phi is a phi having the same dex register and type.
3200 // It assumes that phis with the same dex register are adjacent.
3201 HPhi* GetNextEquivalentPhiWithSameType() {
3202 HInstruction* next = GetNext();
3203 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3204 if (next->GetType() == GetType()) {
3205 return next->AsPhi();
3206 }
3207 next = next->GetNext();
3208 }
3209 return nullptr;
3210 }
3211
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003212 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003213
David Brazdil1abb4192015-02-17 18:33:36 +00003214 protected:
3215 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3216
3217 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3218 inputs_.Put(index, input);
3219 }
3220
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003221 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003222 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003223 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003224 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003225 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003226 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003227
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003228 DISALLOW_COPY_AND_ASSIGN(HPhi);
3229};
3230
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003231class HNullCheck : public HExpression<1> {
3232 public:
3233 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003234 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003235 SetRawInputAt(0, value);
3236 }
3237
Calin Juravle10e244f2015-01-26 18:54:32 +00003238 bool CanBeMoved() const OVERRIDE { return true; }
3239 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003240 UNUSED(other);
3241 return true;
3242 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003243
Calin Juravle10e244f2015-01-26 18:54:32 +00003244 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003245
Calin Juravle10e244f2015-01-26 18:54:32 +00003246 bool CanThrow() const OVERRIDE { return true; }
3247
3248 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003249
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003250 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003251
3252 DECLARE_INSTRUCTION(NullCheck);
3253
3254 private:
3255 const uint32_t dex_pc_;
3256
3257 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3258};
3259
3260class FieldInfo : public ValueObject {
3261 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003262 FieldInfo(MemberOffset field_offset,
3263 Primitive::Type field_type,
3264 bool is_volatile,
3265 uint32_t index,
3266 const DexFile& dex_file)
3267 : field_offset_(field_offset),
3268 field_type_(field_type),
3269 is_volatile_(is_volatile),
3270 index_(index),
3271 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003272
3273 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003274 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003275 uint32_t GetFieldIndex() const { return index_; }
3276 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003277 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003278
3279 private:
3280 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003281 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003282 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003283 uint32_t index_;
3284 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003285};
3286
3287class HInstanceFieldGet : public HExpression<1> {
3288 public:
3289 HInstanceFieldGet(HInstruction* value,
3290 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003291 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003292 bool is_volatile,
3293 uint32_t field_idx,
3294 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003295 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003296 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003297 SetRawInputAt(0, value);
3298 }
3299
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003300 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003301
3302 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3303 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3304 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003305 }
3306
Calin Juravle641547a2015-04-21 22:08:51 +01003307 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3308 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003309 }
3310
3311 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003312 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3313 }
3314
Calin Juravle52c48962014-12-16 17:02:57 +00003315 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003316 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003317 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003318 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003319
3320 DECLARE_INSTRUCTION(InstanceFieldGet);
3321
3322 private:
3323 const FieldInfo field_info_;
3324
3325 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3326};
3327
3328class HInstanceFieldSet : public HTemplateInstruction<2> {
3329 public:
3330 HInstanceFieldSet(HInstruction* object,
3331 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003332 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003333 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003334 bool is_volatile,
3335 uint32_t field_idx,
3336 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003337 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003338 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003339 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003340 SetRawInputAt(0, object);
3341 SetRawInputAt(1, value);
3342 }
3343
Calin Juravle641547a2015-04-21 22:08:51 +01003344 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3345 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003346 }
3347
Calin Juravle52c48962014-12-16 17:02:57 +00003348 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003349 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003350 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003351 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003352 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003353 bool GetValueCanBeNull() const { return value_can_be_null_; }
3354 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003355
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003356 DECLARE_INSTRUCTION(InstanceFieldSet);
3357
3358 private:
3359 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003360 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003361
3362 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3363};
3364
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003365class HArrayGet : public HExpression<2> {
3366 public:
3367 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003368 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003369 SetRawInputAt(0, array);
3370 SetRawInputAt(1, index);
3371 }
3372
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003373 bool CanBeMoved() const OVERRIDE { return true; }
3374 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003375 UNUSED(other);
3376 return true;
3377 }
Calin Juravle641547a2015-04-21 22:08:51 +01003378 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3379 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003380 // TODO: We can be smarter here.
3381 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3382 // which generates the implicit null check. There are cases when these can be removed
3383 // to produce better code. If we ever add optimizations to do so we should allow an
3384 // implicit check here (as long as the address falls in the first page).
3385 return false;
3386 }
3387
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003388 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003389
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003390 HInstruction* GetArray() const { return InputAt(0); }
3391 HInstruction* GetIndex() const { return InputAt(1); }
3392
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003393 DECLARE_INSTRUCTION(ArrayGet);
3394
3395 private:
3396 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3397};
3398
3399class HArraySet : public HTemplateInstruction<3> {
3400 public:
3401 HArraySet(HInstruction* array,
3402 HInstruction* index,
3403 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003404 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003405 uint32_t dex_pc)
3406 : HTemplateInstruction(SideEffects::ChangesSomething()),
3407 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003408 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003409 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3410 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003411 SetRawInputAt(0, array);
3412 SetRawInputAt(1, index);
3413 SetRawInputAt(2, value);
3414 }
3415
Calin Juravle77520bc2015-01-12 18:45:46 +00003416 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003417 // We currently always call a runtime method to catch array store
3418 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003419 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003420 }
3421
Mingyao Yang81014cb2015-06-02 03:16:27 -07003422 // Can throw ArrayStoreException.
3423 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3424
Calin Juravle641547a2015-04-21 22:08:51 +01003425 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3426 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003427 // TODO: Same as for ArrayGet.
3428 return false;
3429 }
3430
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003431 void ClearNeedsTypeCheck() {
3432 needs_type_check_ = false;
3433 }
3434
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003435 void ClearValueCanBeNull() {
3436 value_can_be_null_ = false;
3437 }
3438
3439 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003440 bool NeedsTypeCheck() const { return needs_type_check_; }
3441
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003442 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003443
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003444 HInstruction* GetArray() const { return InputAt(0); }
3445 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003446 HInstruction* GetValue() const { return InputAt(2); }
3447
3448 Primitive::Type GetComponentType() const {
3449 // The Dex format does not type floating point index operations. Since the
3450 // `expected_component_type_` is set during building and can therefore not
3451 // be correct, we also check what is the value type. If it is a floating
3452 // point type, we must use that type.
3453 Primitive::Type value_type = GetValue()->GetType();
3454 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3455 ? value_type
3456 : expected_component_type_;
3457 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003458
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003459 DECLARE_INSTRUCTION(ArraySet);
3460
3461 private:
3462 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003463 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003464 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003465 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003466
3467 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3468};
3469
3470class HArrayLength : public HExpression<1> {
3471 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003472 explicit HArrayLength(HInstruction* array)
3473 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3474 // Note that arrays do not change length, so the instruction does not
3475 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003476 SetRawInputAt(0, array);
3477 }
3478
Calin Juravle77520bc2015-01-12 18:45:46 +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 return obj == InputAt(0);
3486 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003487
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003488 DECLARE_INSTRUCTION(ArrayLength);
3489
3490 private:
3491 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3492};
3493
3494class HBoundsCheck : public HExpression<2> {
3495 public:
3496 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003497 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003498 DCHECK(index->GetType() == Primitive::kPrimInt);
3499 SetRawInputAt(0, index);
3500 SetRawInputAt(1, length);
3501 }
3502
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003503 bool CanBeMoved() const OVERRIDE { return true; }
3504 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003505 UNUSED(other);
3506 return true;
3507 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003508
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003509 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003510
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003511 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003512
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003513 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003514
3515 DECLARE_INSTRUCTION(BoundsCheck);
3516
3517 private:
3518 const uint32_t dex_pc_;
3519
3520 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3521};
3522
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003523/**
3524 * Some DEX instructions are folded into multiple HInstructions that need
3525 * to stay live until the last HInstruction. This class
3526 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003527 * HInstruction stays live. `index` represents the stack location index of the
3528 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003529 */
3530class HTemporary : public HTemplateInstruction<0> {
3531 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003532 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003533
3534 size_t GetIndex() const { return index_; }
3535
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003536 Primitive::Type GetType() const OVERRIDE {
3537 // The previous instruction is the one that will be stored in the temporary location.
3538 DCHECK(GetPrevious() != nullptr);
3539 return GetPrevious()->GetType();
3540 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003541
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003542 DECLARE_INSTRUCTION(Temporary);
3543
3544 private:
3545 const size_t index_;
3546
3547 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3548};
3549
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003550class HSuspendCheck : public HTemplateInstruction<0> {
3551 public:
3552 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003553 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003554
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003555 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003556 return true;
3557 }
3558
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003559 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003560 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3561 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003562
3563 DECLARE_INSTRUCTION(SuspendCheck);
3564
3565 private:
3566 const uint32_t dex_pc_;
3567
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003568 // Only used for code generation, in order to share the same slow path between back edges
3569 // of a same loop.
3570 SlowPathCode* slow_path_;
3571
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003572 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3573};
3574
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003575/**
3576 * Instruction to load a Class object.
3577 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003578class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003579 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003580 HLoadClass(HCurrentMethod* current_method,
3581 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003582 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003583 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003584 uint32_t dex_pc)
3585 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3586 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003587 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003588 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003589 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003590 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003591 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3592 SetRawInputAt(0, current_method);
3593 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003594
3595 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003596
3597 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3598 return other->AsLoadClass()->type_index_ == type_index_;
3599 }
3600
3601 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3602
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003603 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003604 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003605 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003606
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003607 bool NeedsEnvironment() const OVERRIDE {
3608 // Will call runtime and load the class if the class is not loaded yet.
3609 // TODO: finer grain decision.
3610 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003611 }
3612
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003613 bool MustGenerateClinitCheck() const {
3614 return generate_clinit_check_;
3615 }
3616
Calin Juravle0ba218d2015-05-19 18:46:01 +01003617 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3618 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003619 }
3620
3621 bool CanCallRuntime() const {
3622 return MustGenerateClinitCheck() || !is_referrers_class_;
3623 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003624
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003625 bool CanThrow() const OVERRIDE {
3626 // May call runtime and and therefore can throw.
3627 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003628 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003629 }
3630
Calin Juravleacf735c2015-02-12 15:25:22 +00003631 ReferenceTypeInfo GetLoadedClassRTI() {
3632 return loaded_class_rti_;
3633 }
3634
3635 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3636 // Make sure we only set exact types (the loaded class should never be merged).
3637 DCHECK(rti.IsExact());
3638 loaded_class_rti_ = rti;
3639 }
3640
3641 bool IsResolved() {
3642 return loaded_class_rti_.IsExact();
3643 }
3644
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003645 const DexFile& GetDexFile() { return dex_file_; }
3646
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003647 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3648
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003649 DECLARE_INSTRUCTION(LoadClass);
3650
3651 private:
3652 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003653 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003654 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003655 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003656 // Whether this instruction must generate the initialization check.
3657 // Used for code generation.
3658 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003659
Calin Juravleacf735c2015-02-12 15:25:22 +00003660 ReferenceTypeInfo loaded_class_rti_;
3661
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003662 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3663};
3664
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003665class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003666 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003667 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003668 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3669 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003670 dex_pc_(dex_pc) {
3671 SetRawInputAt(0, current_method);
3672 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003673
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003674 bool CanBeMoved() const OVERRIDE { return true; }
3675
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003676 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3677 return other->AsLoadString()->string_index_ == string_index_;
3678 }
3679
3680 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3681
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003682 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003683 uint32_t GetStringIndex() const { return string_index_; }
3684
3685 // TODO: Can we deopt or debug when we resolve a string?
3686 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003687 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003688
3689 DECLARE_INSTRUCTION(LoadString);
3690
3691 private:
3692 const uint32_t string_index_;
3693 const uint32_t dex_pc_;
3694
3695 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3696};
3697
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003698/**
3699 * Performs an initialization check on its Class object input.
3700 */
3701class HClinitCheck : public HExpression<1> {
3702 public:
3703 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003704 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003705 dex_pc_(dex_pc) {
3706 SetRawInputAt(0, constant);
3707 }
3708
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003709 bool CanBeMoved() const OVERRIDE { return true; }
3710 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3711 UNUSED(other);
3712 return true;
3713 }
3714
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003715 bool NeedsEnvironment() const OVERRIDE {
3716 // May call runtime to initialize the class.
3717 return true;
3718 }
3719
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003720 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003721
3722 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3723
3724 DECLARE_INSTRUCTION(ClinitCheck);
3725
3726 private:
3727 const uint32_t dex_pc_;
3728
3729 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3730};
3731
3732class HStaticFieldGet : public HExpression<1> {
3733 public:
3734 HStaticFieldGet(HInstruction* cls,
3735 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003736 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003737 bool is_volatile,
3738 uint32_t field_idx,
3739 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003740 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003741 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003742 SetRawInputAt(0, cls);
3743 }
3744
Calin Juravle52c48962014-12-16 17:02:57 +00003745
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003746 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003747
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003748 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003749 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3750 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003751 }
3752
3753 size_t ComputeHashCode() const OVERRIDE {
3754 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3755 }
3756
Calin Juravle52c48962014-12-16 17:02:57 +00003757 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003758 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3759 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003760 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003761
3762 DECLARE_INSTRUCTION(StaticFieldGet);
3763
3764 private:
3765 const FieldInfo field_info_;
3766
3767 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3768};
3769
3770class HStaticFieldSet : public HTemplateInstruction<2> {
3771 public:
3772 HStaticFieldSet(HInstruction* cls,
3773 HInstruction* value,
3774 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003775 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003776 bool is_volatile,
3777 uint32_t field_idx,
3778 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003779 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003780 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003781 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003782 SetRawInputAt(0, cls);
3783 SetRawInputAt(1, value);
3784 }
3785
Calin Juravle52c48962014-12-16 17:02:57 +00003786 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003787 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3788 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003789 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003790
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003791 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003792 bool GetValueCanBeNull() const { return value_can_be_null_; }
3793 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003794
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003795 DECLARE_INSTRUCTION(StaticFieldSet);
3796
3797 private:
3798 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003799 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003800
3801 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3802};
3803
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003804// Implement the move-exception DEX instruction.
3805class HLoadException : public HExpression<0> {
3806 public:
3807 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3808
3809 DECLARE_INSTRUCTION(LoadException);
3810
3811 private:
3812 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3813};
3814
3815class HThrow : public HTemplateInstruction<1> {
3816 public:
3817 HThrow(HInstruction* exception, uint32_t dex_pc)
3818 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3819 SetRawInputAt(0, exception);
3820 }
3821
3822 bool IsControlFlow() const OVERRIDE { return true; }
3823
3824 bool NeedsEnvironment() const OVERRIDE { return true; }
3825
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003826 bool CanThrow() const OVERRIDE { return true; }
3827
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003828 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003829
3830 DECLARE_INSTRUCTION(Throw);
3831
3832 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003833 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003834
3835 DISALLOW_COPY_AND_ASSIGN(HThrow);
3836};
3837
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003838class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003839 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003840 HInstanceOf(HInstruction* object,
3841 HLoadClass* constant,
3842 bool class_is_final,
3843 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003844 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3845 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003846 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003847 dex_pc_(dex_pc) {
3848 SetRawInputAt(0, object);
3849 SetRawInputAt(1, constant);
3850 }
3851
3852 bool CanBeMoved() const OVERRIDE { return true; }
3853
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003854 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003855 return true;
3856 }
3857
3858 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003859 return false;
3860 }
3861
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003862 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003863
3864 bool IsClassFinal() const { return class_is_final_; }
3865
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003866 // Used only in code generation.
3867 bool MustDoNullCheck() const { return must_do_null_check_; }
3868 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3869
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003870 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003871
3872 private:
3873 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003874 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003875 const uint32_t dex_pc_;
3876
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003877 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3878};
3879
Calin Juravleb1498f62015-02-16 13:13:29 +00003880class HBoundType : public HExpression<1> {
3881 public:
3882 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3883 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3884 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003885 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003886 SetRawInputAt(0, input);
3887 }
3888
3889 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3890
3891 bool CanBeNull() const OVERRIDE {
3892 // `null instanceof ClassX` always return false so we can't be null.
3893 return false;
3894 }
3895
3896 DECLARE_INSTRUCTION(BoundType);
3897
3898 private:
3899 // Encodes the most upper class that this instruction can have. In other words
3900 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3901 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3902 const ReferenceTypeInfo bound_type_;
3903
3904 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3905};
3906
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003907class HCheckCast : public HTemplateInstruction<2> {
3908 public:
3909 HCheckCast(HInstruction* object,
3910 HLoadClass* constant,
3911 bool class_is_final,
3912 uint32_t dex_pc)
3913 : HTemplateInstruction(SideEffects::None()),
3914 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003915 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003916 dex_pc_(dex_pc) {
3917 SetRawInputAt(0, object);
3918 SetRawInputAt(1, constant);
3919 }
3920
3921 bool CanBeMoved() const OVERRIDE { return true; }
3922
3923 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3924 return true;
3925 }
3926
3927 bool NeedsEnvironment() const OVERRIDE {
3928 // Instruction may throw a CheckCastError.
3929 return true;
3930 }
3931
3932 bool CanThrow() const OVERRIDE { return true; }
3933
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003934 bool MustDoNullCheck() const { return must_do_null_check_; }
3935 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3936
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003937 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003938
3939 bool IsClassFinal() const { return class_is_final_; }
3940
3941 DECLARE_INSTRUCTION(CheckCast);
3942
3943 private:
3944 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003945 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003946 const uint32_t dex_pc_;
3947
3948 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003949};
3950
Calin Juravle27df7582015-04-17 19:12:31 +01003951class HMemoryBarrier : public HTemplateInstruction<0> {
3952 public:
3953 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3954 : HTemplateInstruction(SideEffects::None()),
3955 barrier_kind_(barrier_kind) {}
3956
3957 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3958
3959 DECLARE_INSTRUCTION(MemoryBarrier);
3960
3961 private:
3962 const MemBarrierKind barrier_kind_;
3963
3964 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3965};
3966
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003967class HMonitorOperation : public HTemplateInstruction<1> {
3968 public:
3969 enum OperationKind {
3970 kEnter,
3971 kExit,
3972 };
3973
3974 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3975 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3976 SetRawInputAt(0, object);
3977 }
3978
3979 // Instruction may throw a Java exception, so we need an environment.
3980 bool NeedsEnvironment() const OVERRIDE { return true; }
3981 bool CanThrow() const OVERRIDE { return true; }
3982
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003983 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003984
3985 bool IsEnter() const { return kind_ == kEnter; }
3986
3987 DECLARE_INSTRUCTION(MonitorOperation);
3988
Calin Juravle52c48962014-12-16 17:02:57 +00003989 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003990 const OperationKind kind_;
3991 const uint32_t dex_pc_;
3992
3993 private:
3994 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3995};
3996
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003997class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003998 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003999 MoveOperands(Location source,
4000 Location destination,
4001 Primitive::Type type,
4002 HInstruction* instruction)
4003 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004004
4005 Location GetSource() const { return source_; }
4006 Location GetDestination() const { return destination_; }
4007
4008 void SetSource(Location value) { source_ = value; }
4009 void SetDestination(Location value) { destination_ = value; }
4010
4011 // The parallel move resolver marks moves as "in-progress" by clearing the
4012 // destination (but not the source).
4013 Location MarkPending() {
4014 DCHECK(!IsPending());
4015 Location dest = destination_;
4016 destination_ = Location::NoLocation();
4017 return dest;
4018 }
4019
4020 void ClearPending(Location dest) {
4021 DCHECK(IsPending());
4022 destination_ = dest;
4023 }
4024
4025 bool IsPending() const {
4026 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4027 return destination_.IsInvalid() && !source_.IsInvalid();
4028 }
4029
4030 // True if this blocks a move from the given location.
4031 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004032 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004033 }
4034
4035 // A move is redundant if it's been eliminated, if its source and
4036 // destination are the same, or if its destination is unneeded.
4037 bool IsRedundant() const {
4038 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4039 }
4040
4041 // We clear both operands to indicate move that's been eliminated.
4042 void Eliminate() {
4043 source_ = destination_ = Location::NoLocation();
4044 }
4045
4046 bool IsEliminated() const {
4047 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4048 return source_.IsInvalid();
4049 }
4050
Alexey Frunze4dda3372015-06-01 18:31:49 -07004051 Primitive::Type GetType() const { return type_; }
4052
Nicolas Geoffray90218252015-04-15 11:56:51 +01004053 bool Is64BitMove() const {
4054 return Primitive::Is64BitType(type_);
4055 }
4056
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004057 HInstruction* GetInstruction() const { return instruction_; }
4058
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004059 private:
4060 Location source_;
4061 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004062 // The type this move is for.
4063 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004064 // The instruction this move is assocatied with. Null when this move is
4065 // for moving an input in the expected locations of user (including a phi user).
4066 // This is only used in debug mode, to ensure we do not connect interval siblings
4067 // in the same parallel move.
4068 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004069};
4070
4071static constexpr size_t kDefaultNumberOfMoves = 4;
4072
4073class HParallelMove : public HTemplateInstruction<0> {
4074 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004075 explicit HParallelMove(ArenaAllocator* arena)
4076 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004077
Nicolas Geoffray90218252015-04-15 11:56:51 +01004078 void AddMove(Location source,
4079 Location destination,
4080 Primitive::Type type,
4081 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004082 DCHECK(source.IsValid());
4083 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004084 if (kIsDebugBuild) {
4085 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004086 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004087 if (moves_.Get(i).GetInstruction() == instruction) {
4088 // Special case the situation where the move is for the spill slot
4089 // of the instruction.
4090 if ((GetPrevious() == instruction)
4091 || ((GetPrevious() == nullptr)
4092 && instruction->IsPhi()
4093 && instruction->GetBlock() == GetBlock())) {
4094 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4095 << "Doing parallel moves for the same instruction.";
4096 } else {
4097 DCHECK(false) << "Doing parallel moves for the same instruction.";
4098 }
4099 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004100 }
4101 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004102 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004103 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004104 << "Overlapped destination for two moves in a parallel move: "
4105 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4106 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004107 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004108 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004109 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004110 }
4111
4112 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004113 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004114 }
4115
4116 size_t NumMoves() const { return moves_.Size(); }
4117
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004118 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004119
4120 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004121 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004122
4123 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4124};
4125
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004126class HGraphVisitor : public ValueObject {
4127 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004128 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4129 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004130
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004131 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004132 virtual void VisitBasicBlock(HBasicBlock* block);
4133
Roland Levillain633021e2014-10-01 14:12:25 +01004134 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004135 void VisitInsertionOrder();
4136
Roland Levillain633021e2014-10-01 14:12:25 +01004137 // Visit the graph following dominator tree reverse post-order.
4138 void VisitReversePostOrder();
4139
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004140 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004141
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004142 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004143#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004144 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4145
4146 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4147
4148#undef DECLARE_VISIT_INSTRUCTION
4149
4150 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004151 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004152
4153 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4154};
4155
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004156class HGraphDelegateVisitor : public HGraphVisitor {
4157 public:
4158 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4159 virtual ~HGraphDelegateVisitor() {}
4160
4161 // Visit functions that delegate to to super class.
4162#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004163 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004164
4165 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4166
4167#undef DECLARE_VISIT_INSTRUCTION
4168
4169 private:
4170 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4171};
4172
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004173class HInsertionOrderIterator : public ValueObject {
4174 public:
4175 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4176
4177 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4178 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4179 void Advance() { ++index_; }
4180
4181 private:
4182 const HGraph& graph_;
4183 size_t index_;
4184
4185 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4186};
4187
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004188class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004189 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004190 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4191 // Check that reverse post order of the graph has been built.
4192 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4193 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004194
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004195 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4196 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004197 void Advance() { ++index_; }
4198
4199 private:
4200 const HGraph& graph_;
4201 size_t index_;
4202
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004203 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004204};
4205
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004206class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004207 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004208 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004209 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4210 // Check that reverse post order of the graph has been built.
4211 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4212 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004213
4214 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004215 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004216 void Advance() { --index_; }
4217
4218 private:
4219 const HGraph& graph_;
4220 size_t index_;
4221
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004222 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004223};
4224
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004225class HLinearPostOrderIterator : public ValueObject {
4226 public:
4227 explicit HLinearPostOrderIterator(const HGraph& graph)
4228 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4229
4230 bool Done() const { return index_ == 0; }
4231
4232 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4233
4234 void Advance() {
4235 --index_;
4236 DCHECK_GE(index_, 0U);
4237 }
4238
4239 private:
4240 const GrowableArray<HBasicBlock*>& order_;
4241 size_t index_;
4242
4243 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4244};
4245
4246class HLinearOrderIterator : public ValueObject {
4247 public:
4248 explicit HLinearOrderIterator(const HGraph& graph)
4249 : order_(graph.GetLinearOrder()), index_(0) {}
4250
4251 bool Done() const { return index_ == order_.Size(); }
4252 HBasicBlock* Current() const { return order_.Get(index_); }
4253 void Advance() { ++index_; }
4254
4255 private:
4256 const GrowableArray<HBasicBlock*>& order_;
4257 size_t index_;
4258
4259 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4260};
4261
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004262// Iterator over the blocks that art part of the loop. Includes blocks part
4263// of an inner loop. The order in which the blocks are iterated is on their
4264// block id.
4265class HBlocksInLoopIterator : public ValueObject {
4266 public:
4267 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4268 : blocks_in_loop_(info.GetBlocks()),
4269 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4270 index_(0) {
4271 if (!blocks_in_loop_.IsBitSet(index_)) {
4272 Advance();
4273 }
4274 }
4275
4276 bool Done() const { return index_ == blocks_.Size(); }
4277 HBasicBlock* Current() const { return blocks_.Get(index_); }
4278 void Advance() {
4279 ++index_;
4280 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4281 if (blocks_in_loop_.IsBitSet(index_)) {
4282 break;
4283 }
4284 }
4285 }
4286
4287 private:
4288 const BitVector& blocks_in_loop_;
4289 const GrowableArray<HBasicBlock*>& blocks_;
4290 size_t index_;
4291
4292 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4293};
4294
Mingyao Yang3584bce2015-05-19 16:01:59 -07004295// Iterator over the blocks that art part of the loop. Includes blocks part
4296// of an inner loop. The order in which the blocks are iterated is reverse
4297// post order.
4298class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4299 public:
4300 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4301 : blocks_in_loop_(info.GetBlocks()),
4302 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4303 index_(0) {
4304 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4305 Advance();
4306 }
4307 }
4308
4309 bool Done() const { return index_ == blocks_.Size(); }
4310 HBasicBlock* Current() const { return blocks_.Get(index_); }
4311 void Advance() {
4312 ++index_;
4313 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4314 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4315 break;
4316 }
4317 }
4318 }
4319
4320 private:
4321 const BitVector& blocks_in_loop_;
4322 const GrowableArray<HBasicBlock*>& blocks_;
4323 size_t index_;
4324
4325 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4326};
4327
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004328inline int64_t Int64FromConstant(HConstant* constant) {
4329 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4330 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4331 : constant->AsLongConstant()->GetValue();
4332}
4333
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004334} // namespace art
4335
4336#endif // ART_COMPILER_OPTIMIZING_NODES_H_