blob: 9443653db7e0d1bfc41d501be14118c86d9d77b9 [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 Geoffrayec7e4722014-06-06 11:24:33 +0100628 void RemovePredecessor(HBasicBlock* block) {
629 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100630 }
631
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000632 void RemoveSuccessor(HBasicBlock* block) {
633 successors_.Delete(block);
634 }
635
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100636 void ClearAllPredecessors() {
637 predecessors_.Reset();
638 }
639
640 void AddPredecessor(HBasicBlock* block) {
641 predecessors_.Add(block);
642 block->successors_.Add(this);
643 }
644
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100645 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100646 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100647 HBasicBlock* temp = predecessors_.Get(0);
648 predecessors_.Put(0, predecessors_.Get(1));
649 predecessors_.Put(1, temp);
650 }
651
David Brazdil769c9e52015-04-27 13:54:09 +0100652 void SwapSuccessors() {
653 DCHECK_EQ(successors_.Size(), 2u);
654 HBasicBlock* temp = successors_.Get(0);
655 successors_.Put(0, successors_.Get(1));
656 successors_.Put(1, temp);
657 }
658
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100659 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
660 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
661 if (predecessors_.Get(i) == predecessor) {
662 return i;
663 }
664 }
665 return -1;
666 }
667
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100668 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
669 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
670 if (successors_.Get(i) == successor) {
671 return i;
672 }
673 }
674 return -1;
675 }
676
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000677 // Split the block into two blocks just after `cursor`. Returns the newly
678 // created block. Note that this method just updates raw block information,
679 // like predecessors, successors, dominators, and instruction list. It does not
680 // update the graph, reverse post order, loop information, nor make sure the
681 // blocks are consistent (for example ending with a control flow instruction).
682 HBasicBlock* SplitAfter(HInstruction* cursor);
683
684 // Merge `other` at the end of `this`. Successors and dominated blocks of
685 // `other` are changed to be successors and dominated blocks of `this`. Note
686 // that this method does not update the graph, reverse post order, loop
687 // information, nor make sure the blocks are consistent (for example ending
688 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100689 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000690
691 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
692 // of `this` are moved to `other`.
693 // Note that this method does not update the graph, reverse post order, loop
694 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000695 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000696 void ReplaceWith(HBasicBlock* other);
697
David Brazdil2d7352b2015-04-20 14:52:42 +0100698 // Merge `other` at the end of `this`. This method updates loops, reverse post
699 // order, links to predecessors, successors, dominators and deletes the block
700 // from the graph. The two blocks must be successive, i.e. `this` the only
701 // predecessor of `other` and vice versa.
702 void MergeWith(HBasicBlock* other);
703
704 // Disconnects `this` from all its predecessors, successors and dominator,
705 // removes it from all loops it is included in and eventually from the graph.
706 // The block must not dominate any other block. Predecessors and successors
707 // are safely updated.
708 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000709
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000710 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100711 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100712 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100713 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100714 // Replace instruction `initial` with `replacement` within this block.
715 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
716 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100718 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000719 // RemoveInstruction and RemovePhi delete a given instruction from the respective
720 // instruction list. With 'ensure_safety' set to true, it verifies that the
721 // instruction is not in use and removes it from the use lists of its inputs.
722 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
723 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100724 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725
726 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100727 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100728 }
729
Roland Levillain6b879dd2014-09-22 17:13:44 +0100730 bool IsLoopPreHeaderFirstPredecessor() const {
731 DCHECK(IsLoopHeader());
732 DCHECK(!GetPredecessors().IsEmpty());
733 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
734 }
735
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100736 HLoopInformation* GetLoopInformation() const {
737 return loop_information_;
738 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000739
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000740 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100741 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000742 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100743 void SetInLoop(HLoopInformation* info) {
744 if (IsLoopHeader()) {
745 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100746 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747 loop_information_ = info;
748 } else if (loop_information_->Contains(*info->GetHeader())) {
749 // Block is currently part of an outer loop. Make it part of this inner loop.
750 // Note that a non loop header having a loop information means this loop information
751 // has already been populated
752 loop_information_ = info;
753 } else {
754 // Block is part of an inner loop. Do not update the loop information.
755 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
756 // at this point, because this method is being called while populating `info`.
757 }
758 }
759
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000760 // Raw update of the loop information.
761 void SetLoopInformation(HLoopInformation* info) {
762 loop_information_ = info;
763 }
764
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100765 bool IsInLoop() const { return loop_information_ != nullptr; }
766
David Brazdila4b8c212015-05-07 09:59:30 +0100767 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100768 bool Dominates(HBasicBlock* block) const;
769
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100770 size_t GetLifetimeStart() const { return lifetime_start_; }
771 size_t GetLifetimeEnd() const { return lifetime_end_; }
772
773 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
774 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
775
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100776 uint32_t GetDexPc() const { return dex_pc_; }
777
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000778 bool IsCatchBlock() const { return is_catch_block_; }
779 void SetIsCatchBlock() { is_catch_block_ = true; }
780
David Brazdil8d5b8b22015-03-24 10:51:52 +0000781 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000782 bool EndsWithIf() const;
783 bool HasSinglePhi() const;
784
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000785 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000786 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787 GrowableArray<HBasicBlock*> predecessors_;
788 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100789 HInstructionList instructions_;
790 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000791 HLoopInformation* loop_information_;
792 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100793 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000794 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100795 // The dex program counter of the first instruction of this block.
796 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100797 size_t lifetime_start_;
798 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000799 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000801 friend class HGraph;
802 friend class HInstruction;
803
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000804 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
805};
806
David Brazdilb2bd1c52015-03-25 11:17:37 +0000807// Iterates over the LoopInformation of all loops which contain 'block'
808// from the innermost to the outermost.
809class HLoopInformationOutwardIterator : public ValueObject {
810 public:
811 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
812 : current_(block.GetLoopInformation()) {}
813
814 bool Done() const { return current_ == nullptr; }
815
816 void Advance() {
817 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100818 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000819 }
820
821 HLoopInformation* Current() const {
822 DCHECK(!Done());
823 return current_;
824 }
825
826 private:
827 HLoopInformation* current_;
828
829 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
830};
831
Alexandre Ramesef20f712015-06-09 10:29:30 +0100832#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100833 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000834 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000835 M(ArrayGet, Instruction) \
836 M(ArrayLength, Instruction) \
837 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100838 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000839 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000840 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000841 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100842 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000843 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100844 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100845 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700846 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000847 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000848 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000849 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100850 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000851 M(Exit, Instruction) \
852 M(FloatConstant, Constant) \
853 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100854 M(GreaterThan, Condition) \
855 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100856 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000857 M(InstanceFieldGet, Instruction) \
858 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000859 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100860 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000861 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000862 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100863 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000864 M(LessThan, Condition) \
865 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000866 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000867 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100868 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000869 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100870 M(Local, Instruction) \
871 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100872 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000873 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000874 M(Mul, BinaryOperation) \
875 M(Neg, UnaryOperation) \
876 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100877 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100878 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000879 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000880 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000881 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000882 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100883 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000884 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100885 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000886 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100887 M(Return, Instruction) \
888 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000889 M(Shl, BinaryOperation) \
890 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100891 M(StaticFieldGet, Instruction) \
892 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100893 M(StoreLocal, Instruction) \
894 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100895 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000896 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000897 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000898 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000899 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000900 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000901
Alexandre Ramesef20f712015-06-09 10:29:30 +0100902#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
903
904#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
905
906#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
907
908#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
909
910#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
911 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
912 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
913 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
914 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
915 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
916
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100917#define FOR_EACH_INSTRUCTION(M) \
918 FOR_EACH_CONCRETE_INSTRUCTION(M) \
919 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100920 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100921 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100922 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700923
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100924#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000925FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
926#undef FORWARD_DECLARATION
927
Roland Levillainccc07a92014-09-16 14:48:16 +0100928#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000929 InstructionKind GetKind() const OVERRIDE { return k##type; } \
930 const char* DebugName() const OVERRIDE { return #type; } \
931 const H##type* As##type() const OVERRIDE { return this; } \
932 H##type* As##type() OVERRIDE { return this; } \
933 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100934 return other->Is##type(); \
935 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000936 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000937
David Brazdiled596192015-01-23 10:39:45 +0000938template <typename T> class HUseList;
939
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100940template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700941class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000942 public:
David Brazdiled596192015-01-23 10:39:45 +0000943 HUseListNode* GetPrevious() const { return prev_; }
944 HUseListNode* GetNext() const { return next_; }
945 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100946 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100947 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100948
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000949 private:
David Brazdiled596192015-01-23 10:39:45 +0000950 HUseListNode(T user, size_t index)
951 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
952
953 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100954 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000955 HUseListNode<T>* prev_;
956 HUseListNode<T>* next_;
957
958 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000959
960 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
961};
962
David Brazdiled596192015-01-23 10:39:45 +0000963template <typename T>
964class HUseList : public ValueObject {
965 public:
966 HUseList() : first_(nullptr) {}
967
968 void Clear() {
969 first_ = nullptr;
970 }
971
972 // Adds a new entry at the beginning of the use list and returns
973 // the newly created node.
974 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000975 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000976 if (IsEmpty()) {
977 first_ = new_node;
978 } else {
979 first_->prev_ = new_node;
980 new_node->next_ = first_;
981 first_ = new_node;
982 }
983 return new_node;
984 }
985
986 HUseListNode<T>* GetFirst() const {
987 return first_;
988 }
989
990 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000991 DCHECK(node != nullptr);
992 DCHECK(Contains(node));
993
David Brazdiled596192015-01-23 10:39:45 +0000994 if (node->prev_ != nullptr) {
995 node->prev_->next_ = node->next_;
996 }
997 if (node->next_ != nullptr) {
998 node->next_->prev_ = node->prev_;
999 }
1000 if (node == first_) {
1001 first_ = node->next_;
1002 }
1003 }
1004
David Brazdil1abb4192015-02-17 18:33:36 +00001005 bool Contains(const HUseListNode<T>* node) const {
1006 if (node == nullptr) {
1007 return false;
1008 }
1009 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1010 if (current == node) {
1011 return true;
1012 }
1013 }
1014 return false;
1015 }
1016
David Brazdiled596192015-01-23 10:39:45 +00001017 bool IsEmpty() const {
1018 return first_ == nullptr;
1019 }
1020
1021 bool HasOnlyOneUse() const {
1022 return first_ != nullptr && first_->next_ == nullptr;
1023 }
1024
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001025 size_t SizeSlow() const {
1026 size_t count = 0;
1027 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1028 ++count;
1029 }
1030 return count;
1031 }
1032
David Brazdiled596192015-01-23 10:39:45 +00001033 private:
1034 HUseListNode<T>* first_;
1035};
1036
1037template<typename T>
1038class HUseIterator : public ValueObject {
1039 public:
1040 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1041
1042 bool Done() const { return current_ == nullptr; }
1043
1044 void Advance() {
1045 DCHECK(!Done());
1046 current_ = current_->GetNext();
1047 }
1048
1049 HUseListNode<T>* Current() const {
1050 DCHECK(!Done());
1051 return current_;
1052 }
1053
1054 private:
1055 HUseListNode<T>* current_;
1056
1057 friend class HValue;
1058};
1059
David Brazdil1abb4192015-02-17 18:33:36 +00001060// This class is used by HEnvironment and HInstruction classes to record the
1061// instructions they use and pointers to the corresponding HUseListNodes kept
1062// by the used instructions.
1063template <typename T>
1064class HUserRecord : public ValueObject {
1065 public:
1066 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1067 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1068
1069 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1070 : instruction_(old_record.instruction_), use_node_(use_node) {
1071 DCHECK(instruction_ != nullptr);
1072 DCHECK(use_node_ != nullptr);
1073 DCHECK(old_record.use_node_ == nullptr);
1074 }
1075
1076 HInstruction* GetInstruction() const { return instruction_; }
1077 HUseListNode<T>* GetUseNode() const { return use_node_; }
1078
1079 private:
1080 // Instruction used by the user.
1081 HInstruction* instruction_;
1082
1083 // Corresponding entry in the use list kept by 'instruction_'.
1084 HUseListNode<T>* use_node_;
1085};
1086
Calin Juravle27df7582015-04-17 19:12:31 +01001087// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1088// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1089// flag is consider.
1090// - DependsOn suggests that there is a real dependency between side effects but it only
1091// checks DependendsOnSomething flag.
1092//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001093// Represents the side effects an instruction may have.
1094class SideEffects : public ValueObject {
1095 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001096 SideEffects() : flags_(0) {}
1097
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001098 static SideEffects None() {
1099 return SideEffects(0);
1100 }
1101
1102 static SideEffects All() {
1103 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1104 }
1105
1106 static SideEffects ChangesSomething() {
1107 return SideEffects((1 << kFlagChangesCount) - 1);
1108 }
1109
1110 static SideEffects DependsOnSomething() {
1111 int count = kFlagDependsOnCount - kFlagChangesCount;
1112 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1113 }
1114
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001115 SideEffects Union(SideEffects other) const {
1116 return SideEffects(flags_ | other.flags_);
1117 }
1118
Roland Levillain72bceff2014-09-15 18:29:00 +01001119 bool HasSideEffects() const {
1120 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1121 return (flags_ & all_bits_set) != 0;
1122 }
1123
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001124 bool HasAllSideEffects() const {
1125 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1126 return all_bits_set == (flags_ & all_bits_set);
1127 }
1128
1129 bool DependsOn(SideEffects other) const {
1130 size_t depends_flags = other.ComputeDependsFlags();
1131 return (flags_ & depends_flags) != 0;
1132 }
1133
1134 bool HasDependencies() const {
1135 int count = kFlagDependsOnCount - kFlagChangesCount;
1136 size_t all_bits_set = (1 << count) - 1;
1137 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1138 }
1139
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001140 private:
1141 static constexpr int kFlagChangesSomething = 0;
1142 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1143
1144 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1145 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1146
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001147 explicit SideEffects(size_t flags) : flags_(flags) {}
1148
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001149 size_t ComputeDependsFlags() const {
1150 return flags_ << kFlagChangesCount;
1151 }
1152
1153 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001154};
1155
David Brazdiled596192015-01-23 10:39:45 +00001156// A HEnvironment object contains the values of virtual registers at a given location.
1157class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1158 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001159 HEnvironment(ArenaAllocator* arena,
1160 size_t number_of_vregs,
1161 const DexFile& dex_file,
1162 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001163 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001164 InvokeType invoke_type,
1165 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001166 : vregs_(arena, number_of_vregs),
1167 locations_(arena, number_of_vregs),
1168 parent_(nullptr),
1169 dex_file_(dex_file),
1170 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001171 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001172 invoke_type_(invoke_type),
1173 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001174 vregs_.SetSize(number_of_vregs);
1175 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001176 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001177 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001178
1179 locations_.SetSize(number_of_vregs);
1180 for (size_t i = 0; i < number_of_vregs; ++i) {
1181 locations_.Put(i, Location());
1182 }
1183 }
1184
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001185 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001186 : HEnvironment(arena,
1187 to_copy.Size(),
1188 to_copy.GetDexFile(),
1189 to_copy.GetMethodIdx(),
1190 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001191 to_copy.GetInvokeType(),
1192 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001193
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001194 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001195 if (parent_ != nullptr) {
1196 parent_->SetAndCopyParentChain(allocator, parent);
1197 } else {
1198 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1199 parent_->CopyFrom(parent);
1200 if (parent->GetParent() != nullptr) {
1201 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1202 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001203 }
David Brazdiled596192015-01-23 10:39:45 +00001204 }
1205
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001206 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1207 void CopyFrom(HEnvironment* environment);
1208
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001209 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1210 // input to the loop phi instead. This is for inserting instructions that
1211 // require an environment (like HDeoptimization) in the loop pre-header.
1212 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001213
1214 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001215 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001216 }
1217
1218 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001219 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001220 }
1221
David Brazdil1abb4192015-02-17 18:33:36 +00001222 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001223
1224 size_t Size() const { return vregs_.Size(); }
1225
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001226 HEnvironment* GetParent() const { return parent_; }
1227
1228 void SetLocationAt(size_t index, Location location) {
1229 locations_.Put(index, location);
1230 }
1231
1232 Location GetLocationAt(size_t index) const {
1233 return locations_.Get(index);
1234 }
1235
1236 uint32_t GetDexPc() const {
1237 return dex_pc_;
1238 }
1239
1240 uint32_t GetMethodIdx() const {
1241 return method_idx_;
1242 }
1243
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001244 InvokeType GetInvokeType() const {
1245 return invoke_type_;
1246 }
1247
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001248 const DexFile& GetDexFile() const {
1249 return dex_file_;
1250 }
1251
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001252 HInstruction* GetHolder() const {
1253 return holder_;
1254 }
1255
David Brazdiled596192015-01-23 10:39:45 +00001256 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001257 // Record instructions' use entries of this environment for constant-time removal.
1258 // It should only be called by HInstruction when a new environment use is added.
1259 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1260 DCHECK(env_use->GetUser() == this);
1261 size_t index = env_use->GetIndex();
1262 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1263 }
David Brazdiled596192015-01-23 10:39:45 +00001264
David Brazdil1abb4192015-02-17 18:33:36 +00001265 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001266 GrowableArray<Location> locations_;
1267 HEnvironment* parent_;
1268 const DexFile& dex_file_;
1269 const uint32_t method_idx_;
1270 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001271 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001272
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001273 // The instruction that holds this environment. Only used in debug mode
1274 // to ensure the graph is consistent.
1275 HInstruction* const holder_;
1276
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001277 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001278
1279 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1280};
1281
Calin Juravleacf735c2015-02-12 15:25:22 +00001282class ReferenceTypeInfo : ValueObject {
1283 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001284 typedef Handle<mirror::Class> TypeHandle;
1285
1286 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1287 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1288 if (type_handle->IsObjectClass()) {
1289 // Override the type handle to be consistent with the case when we get to
1290 // Top but don't have the Object class available. It avoids having to guess
1291 // what value the type_handle has when it's Top.
1292 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1293 } else {
1294 return ReferenceTypeInfo(type_handle, is_exact, false);
1295 }
1296 }
1297
1298 static ReferenceTypeInfo CreateTop(bool is_exact) {
1299 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001300 }
1301
1302 bool IsExact() const { return is_exact_; }
1303 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001304 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1305 return !IsTop() && GetTypeHandle()->IsInterface();
1306 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001307
1308 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1309
Calin Juravleb1498f62015-02-16 13:13:29 +00001310 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001311 if (IsTop()) {
1312 // Top (equivalent for java.lang.Object) is supertype of anything.
1313 return true;
1314 }
1315 if (rti.IsTop()) {
1316 // If we get here `this` is not Top() so it can't be a supertype.
1317 return false;
1318 }
1319 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1320 }
1321
1322 // Returns true if the type information provide the same amount of details.
1323 // Note that it does not mean that the instructions have the same actual type
1324 // (e.g. tops are equal but they can be the result of a merge).
1325 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1326 if (IsExact() != rti.IsExact()) {
1327 return false;
1328 }
1329 if (IsTop() && rti.IsTop()) {
1330 // `Top` means java.lang.Object, so the types are equivalent.
1331 return true;
1332 }
1333 if (IsTop() || rti.IsTop()) {
1334 // If only one is top or object than they are not equivalent.
1335 // NB: We need this extra check because the type_handle of `Top` is invalid
1336 // and we cannot inspect its reference.
1337 return false;
1338 }
1339
1340 // Finally check the types.
1341 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1342 }
1343
1344 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001345 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1346 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1347 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1348
Calin Juravleacf735c2015-02-12 15:25:22 +00001349 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001350 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001351 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001352 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001353 bool is_exact_;
1354 // A true value here means that the object type should be java.lang.Object.
1355 // We don't have access to the corresponding mirror object every time so this
1356 // flag acts as a substitute. When true, the TypeHandle refers to a null
1357 // pointer and should not be used.
1358 bool is_top_;
1359};
1360
1361std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1362
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001363class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001364 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001365 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001366 : previous_(nullptr),
1367 next_(nullptr),
1368 block_(nullptr),
1369 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001370 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001371 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001372 locations_(nullptr),
1373 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001374 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001375 side_effects_(side_effects),
1376 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001377
Dave Allison20dfc792014-06-16 20:44:29 -07001378 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001379
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001380#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001381 enum InstructionKind {
1382 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1383 };
1384#undef DECLARE_KIND
1385
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001386 HInstruction* GetNext() const { return next_; }
1387 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001388
Calin Juravle77520bc2015-01-12 18:45:46 +00001389 HInstruction* GetNextDisregardingMoves() const;
1390 HInstruction* GetPreviousDisregardingMoves() const;
1391
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001392 HBasicBlock* GetBlock() const { return block_; }
1393 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001394 bool IsInBlock() const { return block_ != nullptr; }
1395 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001396 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001397
Roland Levillain6b879dd2014-09-22 17:13:44 +01001398 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001399 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001400
1401 virtual void Accept(HGraphVisitor* visitor) = 0;
1402 virtual const char* DebugName() const = 0;
1403
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001404 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001405 void SetRawInputAt(size_t index, HInstruction* input) {
1406 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1407 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001408
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001409 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001410 virtual uint32_t GetDexPc() const {
1411 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1412 " does not need an environment";
1413 UNREACHABLE();
1414 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001415 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001416 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001417 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001418
Calin Juravle10e244f2015-01-26 18:54:32 +00001419 // Does not apply for all instructions, but having this at top level greatly
1420 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001421 virtual bool CanBeNull() const {
1422 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1423 return true;
1424 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001425
Calin Juravle641547a2015-04-21 22:08:51 +01001426 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1427 UNUSED(obj);
1428 return false;
1429 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001430
Calin Juravleacf735c2015-02-12 15:25:22 +00001431 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001432 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001433 reference_type_info_ = reference_type_info;
1434 }
1435
Calin Juravle61d544b2015-02-23 16:46:57 +00001436 ReferenceTypeInfo GetReferenceTypeInfo() const {
1437 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1438 return reference_type_info_;
1439 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001440
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001441 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001442 DCHECK(user != nullptr);
1443 HUseListNode<HInstruction*>* use =
1444 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1445 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001446 }
1447
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001448 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001449 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001450 HUseListNode<HEnvironment*>* env_use =
1451 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1452 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001453 }
1454
David Brazdil1abb4192015-02-17 18:33:36 +00001455 void RemoveAsUserOfInput(size_t input) {
1456 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1457 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1458 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001459
David Brazdil1abb4192015-02-17 18:33:36 +00001460 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1461 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001462
David Brazdiled596192015-01-23 10:39:45 +00001463 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1464 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001465 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001466 bool HasOnlyOneNonEnvironmentUse() const {
1467 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1468 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001469
Roland Levillain6c82d402014-10-13 16:10:27 +01001470 // Does this instruction strictly dominate `other_instruction`?
1471 // Returns false if this instruction and `other_instruction` are the same.
1472 // Aborts if this instruction and `other_instruction` are both phis.
1473 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001474
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001475 int GetId() const { return id_; }
1476 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001477
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001478 int GetSsaIndex() const { return ssa_index_; }
1479 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1480 bool HasSsaIndex() const { return ssa_index_ != -1; }
1481
1482 bool HasEnvironment() const { return environment_ != nullptr; }
1483 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001484 // Set the `environment_` field. Raw because this method does not
1485 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001486 void SetRawEnvironment(HEnvironment* environment) {
1487 DCHECK(environment_ == nullptr);
1488 DCHECK_EQ(environment->GetHolder(), this);
1489 environment_ = environment;
1490 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001491
1492 // Set the environment of this instruction, copying it from `environment`. While
1493 // copying, the uses lists are being updated.
1494 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001495 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001496 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001497 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001498 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001499 if (environment->GetParent() != nullptr) {
1500 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1501 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001502 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001503
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001504 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1505 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001506 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001507 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001508 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001509 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001510 if (environment->GetParent() != nullptr) {
1511 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1512 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001513 }
1514
Nicolas Geoffray39468442014-09-02 15:17:15 +01001515 // Returns the number of entries in the environment. Typically, that is the
1516 // number of dex registers in a method. It could be more in case of inlining.
1517 size_t EnvironmentSize() const;
1518
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001519 LocationSummary* GetLocations() const { return locations_; }
1520 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001521
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001522 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001523 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001524
Alexandre Rames188d4312015-04-09 18:30:21 +01001525 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1526 // uses of this instruction by `other` are *not* updated.
1527 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1528 ReplaceWith(other);
1529 other->ReplaceInput(this, use_index);
1530 }
1531
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001532 // Move `this` instruction before `cursor`.
1533 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001534
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001535#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001536 bool Is##type() const { return (As##type() != nullptr); } \
1537 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001538 virtual H##type* As##type() { return nullptr; }
1539
1540 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1541#undef INSTRUCTION_TYPE_CHECK
1542
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001543 // Returns whether the instruction can be moved within the graph.
1544 virtual bool CanBeMoved() const { return false; }
1545
1546 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001547 virtual bool InstructionTypeEquals(HInstruction* other) const {
1548 UNUSED(other);
1549 return false;
1550 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001551
1552 // Returns whether any data encoded in the two instructions is equal.
1553 // This method does not look at the inputs. Both instructions must be
1554 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001555 virtual bool InstructionDataEquals(HInstruction* other) const {
1556 UNUSED(other);
1557 return false;
1558 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001559
1560 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001561 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001562 // 2) Their inputs are identical.
1563 bool Equals(HInstruction* other) const;
1564
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001565 virtual InstructionKind GetKind() const = 0;
1566
1567 virtual size_t ComputeHashCode() const {
1568 size_t result = GetKind();
1569 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1570 result = (result * 31) + InputAt(i)->GetId();
1571 }
1572 return result;
1573 }
1574
1575 SideEffects GetSideEffects() const { return side_effects_; }
1576
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001577 size_t GetLifetimePosition() const { return lifetime_position_; }
1578 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1579 LiveInterval* GetLiveInterval() const { return live_interval_; }
1580 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1581 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1582
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001583 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1584
1585 // Returns whether the code generation of the instruction will require to have access
1586 // to the current method. Such instructions are:
1587 // (1): Instructions that require an environment, as calling the runtime requires
1588 // to walk the stack and have the current method stored at a specific stack address.
1589 // (2): Object literals like classes and strings, that are loaded from the dex cache
1590 // fields of the current method.
1591 bool NeedsCurrentMethod() const {
1592 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1593 }
1594
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001595 virtual bool NeedsDexCache() const { return false; }
1596
David Brazdil1abb4192015-02-17 18:33:36 +00001597 protected:
1598 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1599 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1600
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001601 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001602 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1603
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001604 HInstruction* previous_;
1605 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001606 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001607
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001608 // An instruction gets an id when it is added to the graph.
1609 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001610 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001611 int id_;
1612
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001613 // When doing liveness analysis, instructions that have uses get an SSA index.
1614 int ssa_index_;
1615
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001616 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001617 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001618
1619 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001620 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001621
Nicolas Geoffray39468442014-09-02 15:17:15 +01001622 // The environment associated with this instruction. Not null if the instruction
1623 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001624 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001625
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001626 // Set by the code generator.
1627 LocationSummary* locations_;
1628
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001629 // Set by the liveness analysis.
1630 LiveInterval* live_interval_;
1631
1632 // Set by the liveness analysis, this is the position in a linear
1633 // order of blocks where this instruction's live interval start.
1634 size_t lifetime_position_;
1635
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001636 const SideEffects side_effects_;
1637
Calin Juravleacf735c2015-02-12 15:25:22 +00001638 // TODO: for primitive types this should be marked as invalid.
1639 ReferenceTypeInfo reference_type_info_;
1640
David Brazdil1abb4192015-02-17 18:33:36 +00001641 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001642 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001643 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001644 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001645 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001646
1647 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1648};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001649std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001650
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001651class HInputIterator : public ValueObject {
1652 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001653 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001654
1655 bool Done() const { return index_ == instruction_->InputCount(); }
1656 HInstruction* Current() const { return instruction_->InputAt(index_); }
1657 void Advance() { index_++; }
1658
1659 private:
1660 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001661 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001662
1663 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1664};
1665
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001666class HInstructionIterator : public ValueObject {
1667 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001668 explicit HInstructionIterator(const HInstructionList& instructions)
1669 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001670 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001671 }
1672
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001673 bool Done() const { return instruction_ == nullptr; }
1674 HInstruction* Current() const { return instruction_; }
1675 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001676 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001677 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001678 }
1679
1680 private:
1681 HInstruction* instruction_;
1682 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001683
1684 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001685};
1686
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001687class HBackwardInstructionIterator : public ValueObject {
1688 public:
1689 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1690 : instruction_(instructions.last_instruction_) {
1691 next_ = Done() ? nullptr : instruction_->GetPrevious();
1692 }
1693
1694 bool Done() const { return instruction_ == nullptr; }
1695 HInstruction* Current() const { return instruction_; }
1696 void Advance() {
1697 instruction_ = next_;
1698 next_ = Done() ? nullptr : instruction_->GetPrevious();
1699 }
1700
1701 private:
1702 HInstruction* instruction_;
1703 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001704
1705 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001706};
1707
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001708// An embedded container with N elements of type T. Used (with partial
1709// specialization for N=0) because embedded arrays cannot have size 0.
1710template<typename T, intptr_t N>
1711class EmbeddedArray {
1712 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001713 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001714
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001715 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001716
1717 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001718 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001719 return elements_[i];
1720 }
1721
1722 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001723 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001724 return elements_[i];
1725 }
1726
1727 const T& At(intptr_t i) const {
1728 return (*this)[i];
1729 }
1730
1731 void SetAt(intptr_t i, const T& val) {
1732 (*this)[i] = val;
1733 }
1734
1735 private:
1736 T elements_[N];
1737};
1738
1739template<typename T>
1740class EmbeddedArray<T, 0> {
1741 public:
1742 intptr_t length() const { return 0; }
1743 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001744 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001745 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001746 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001747 }
1748 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001749 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001750 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001751 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001752 }
1753};
1754
1755template<intptr_t N>
1756class HTemplateInstruction: public HInstruction {
1757 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001758 HTemplateInstruction<N>(SideEffects side_effects)
1759 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001760 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001762 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001763
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001764 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001765 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1766
1767 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1768 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001769 }
1770
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001771 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001772 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001773
1774 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001775};
1776
Dave Allison20dfc792014-06-16 20:44:29 -07001777template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001778class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001779 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001780 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1781 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001782 virtual ~HExpression() {}
1783
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001784 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001785
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001786 protected:
1787 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001788};
1789
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001790// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1791// instruction that branches to the exit block.
1792class HReturnVoid : public HTemplateInstruction<0> {
1793 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001794 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001796 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001797
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001798 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001799
1800 private:
1801 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1802};
1803
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804// Represents dex's RETURN opcodes. A HReturn is a control flow
1805// instruction that branches to the exit block.
1806class HReturn : public HTemplateInstruction<1> {
1807 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001808 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001809 SetRawInputAt(0, value);
1810 }
1811
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001812 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001813
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001814 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001815
1816 private:
1817 DISALLOW_COPY_AND_ASSIGN(HReturn);
1818};
1819
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001820// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001821// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001822// exit block.
1823class HExit : public HTemplateInstruction<0> {
1824 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001825 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001826
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001827 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001828
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001829 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001830
1831 private:
1832 DISALLOW_COPY_AND_ASSIGN(HExit);
1833};
1834
1835// Jumps from one block to another.
1836class HGoto : public HTemplateInstruction<0> {
1837 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001838 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1839
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001840 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001841
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001842 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001843 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001844 }
1845
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001846 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001847
1848 private:
1849 DISALLOW_COPY_AND_ASSIGN(HGoto);
1850};
1851
Dave Allison20dfc792014-06-16 20:44:29 -07001852
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001853// Conditional branch. A block ending with an HIf instruction must have
1854// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001855class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001856 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001857 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858 SetRawInputAt(0, input);
1859 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001860
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001861 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001862
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001863 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001864 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001865 }
1866
1867 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001868 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001869 }
1870
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001871 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001872
1873 private:
1874 DISALLOW_COPY_AND_ASSIGN(HIf);
1875};
1876
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001877// Deoptimize to interpreter, upon checking a condition.
1878class HDeoptimize : public HTemplateInstruction<1> {
1879 public:
1880 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1881 : HTemplateInstruction(SideEffects::None()),
1882 dex_pc_(dex_pc) {
1883 SetRawInputAt(0, cond);
1884 }
1885
1886 bool NeedsEnvironment() const OVERRIDE { return true; }
1887 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001888 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001889
1890 DECLARE_INSTRUCTION(Deoptimize);
1891
1892 private:
1893 uint32_t dex_pc_;
1894
1895 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1896};
1897
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001898// Represents the ArtMethod that was passed as a first argument to
1899// the method. It is used by instructions that depend on it, like
1900// instructions that work with the dex cache.
1901class HCurrentMethod : public HExpression<0> {
1902 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001903 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001904
1905 DECLARE_INSTRUCTION(CurrentMethod);
1906
1907 private:
1908 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
1909};
1910
Roland Levillain88cb1752014-10-20 16:36:47 +01001911class HUnaryOperation : public HExpression<1> {
1912 public:
1913 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1914 : HExpression(result_type, SideEffects::None()) {
1915 SetRawInputAt(0, input);
1916 }
1917
1918 HInstruction* GetInput() const { return InputAt(0); }
1919 Primitive::Type GetResultType() const { return GetType(); }
1920
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001921 bool CanBeMoved() const OVERRIDE { return true; }
1922 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001923 UNUSED(other);
1924 return true;
1925 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001926
Roland Levillain9240d6a2014-10-20 16:47:04 +01001927 // Try to statically evaluate `operation` and return a HConstant
1928 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001929 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001930 HConstant* TryStaticEvaluation() const;
1931
1932 // Apply this operation to `x`.
1933 virtual int32_t Evaluate(int32_t x) const = 0;
1934 virtual int64_t Evaluate(int64_t x) const = 0;
1935
Roland Levillain88cb1752014-10-20 16:36:47 +01001936 DECLARE_INSTRUCTION(UnaryOperation);
1937
1938 private:
1939 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1940};
1941
Dave Allison20dfc792014-06-16 20:44:29 -07001942class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001944 HBinaryOperation(Primitive::Type result_type,
1945 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001947 SetRawInputAt(0, left);
1948 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001949 }
1950
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001951 HInstruction* GetLeft() const { return InputAt(0); }
1952 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001953 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001954
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001955 virtual bool IsCommutative() const { return false; }
1956
1957 // Put constant on the right.
1958 // Returns whether order is changed.
1959 bool OrderInputsWithConstantOnTheRight() {
1960 HInstruction* left = InputAt(0);
1961 HInstruction* right = InputAt(1);
1962 if (left->IsConstant() && !right->IsConstant()) {
1963 ReplaceInput(right, 0);
1964 ReplaceInput(left, 1);
1965 return true;
1966 }
1967 return false;
1968 }
1969
1970 // Order inputs by instruction id, but favor constant on the right side.
1971 // This helps GVN for commutative ops.
1972 void OrderInputs() {
1973 DCHECK(IsCommutative());
1974 HInstruction* left = InputAt(0);
1975 HInstruction* right = InputAt(1);
1976 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1977 return;
1978 }
1979 if (OrderInputsWithConstantOnTheRight()) {
1980 return;
1981 }
1982 // Order according to instruction id.
1983 if (left->GetId() > right->GetId()) {
1984 ReplaceInput(right, 0);
1985 ReplaceInput(left, 1);
1986 }
1987 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001988
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001989 bool CanBeMoved() const OVERRIDE { return true; }
1990 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001991 UNUSED(other);
1992 return true;
1993 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001994
Roland Levillain9240d6a2014-10-20 16:47:04 +01001995 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001996 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001997 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001998 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001999
2000 // Apply this operation to `x` and `y`.
2001 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2002 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2003
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002004 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002005 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002006 HConstant* GetConstantRight() const;
2007
2008 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002009 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002010 HInstruction* GetLeastConstantLeft() const;
2011
Roland Levillainccc07a92014-09-16 14:48:16 +01002012 DECLARE_INSTRUCTION(BinaryOperation);
2013
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002014 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002015 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2016};
2017
Dave Allison20dfc792014-06-16 20:44:29 -07002018class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002019 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002020 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002021 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
2022 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002023
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002024 bool NeedsMaterialization() const { return needs_materialization_; }
2025 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002026
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002027 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002028 // `instruction`, and disregard moves in between.
2029 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002030
Dave Allison20dfc792014-06-16 20:44:29 -07002031 DECLARE_INSTRUCTION(Condition);
2032
2033 virtual IfCondition GetCondition() const = 0;
2034
2035 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002036 // For register allocation purposes, returns whether this instruction needs to be
2037 // materialized (that is, not just be in the processor flags).
2038 bool needs_materialization_;
2039
Dave Allison20dfc792014-06-16 20:44:29 -07002040 DISALLOW_COPY_AND_ASSIGN(HCondition);
2041};
2042
2043// Instruction to check if two inputs are equal to each other.
2044class HEqual : public HCondition {
2045 public:
2046 HEqual(HInstruction* first, HInstruction* second)
2047 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002048
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002049 bool IsCommutative() const OVERRIDE { return true; }
2050
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002051 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002052 return x == y ? 1 : 0;
2053 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002054 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002055 return x == y ? 1 : 0;
2056 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002057
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002058 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002060 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002061 return kCondEQ;
2062 }
2063
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002064 private:
2065 DISALLOW_COPY_AND_ASSIGN(HEqual);
2066};
2067
Dave Allison20dfc792014-06-16 20:44:29 -07002068class HNotEqual : public HCondition {
2069 public:
2070 HNotEqual(HInstruction* first, HInstruction* second)
2071 : HCondition(first, second) {}
2072
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002073 bool IsCommutative() const OVERRIDE { return true; }
2074
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002075 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002076 return x != y ? 1 : 0;
2077 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002078 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002079 return x != y ? 1 : 0;
2080 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002081
Dave Allison20dfc792014-06-16 20:44:29 -07002082 DECLARE_INSTRUCTION(NotEqual);
2083
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002084 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002085 return kCondNE;
2086 }
2087
2088 private:
2089 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2090};
2091
2092class HLessThan : public HCondition {
2093 public:
2094 HLessThan(HInstruction* first, HInstruction* second)
2095 : HCondition(first, second) {}
2096
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002097 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002098 return x < y ? 1 : 0;
2099 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002100 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002101 return x < y ? 1 : 0;
2102 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002103
Dave Allison20dfc792014-06-16 20:44:29 -07002104 DECLARE_INSTRUCTION(LessThan);
2105
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002106 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002107 return kCondLT;
2108 }
2109
2110 private:
2111 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2112};
2113
2114class HLessThanOrEqual : public HCondition {
2115 public:
2116 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2117 : HCondition(first, second) {}
2118
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002119 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002120 return x <= y ? 1 : 0;
2121 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002122 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002123 return x <= y ? 1 : 0;
2124 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002125
Dave Allison20dfc792014-06-16 20:44:29 -07002126 DECLARE_INSTRUCTION(LessThanOrEqual);
2127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002128 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002129 return kCondLE;
2130 }
2131
2132 private:
2133 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2134};
2135
2136class HGreaterThan : public HCondition {
2137 public:
2138 HGreaterThan(HInstruction* first, HInstruction* second)
2139 : HCondition(first, second) {}
2140
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002141 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002142 return x > y ? 1 : 0;
2143 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002144 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002145 return x > y ? 1 : 0;
2146 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002147
Dave Allison20dfc792014-06-16 20:44:29 -07002148 DECLARE_INSTRUCTION(GreaterThan);
2149
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002150 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002151 return kCondGT;
2152 }
2153
2154 private:
2155 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2156};
2157
2158class HGreaterThanOrEqual : public HCondition {
2159 public:
2160 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2161 : HCondition(first, second) {}
2162
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002163 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002164 return x >= y ? 1 : 0;
2165 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002166 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002167 return x >= y ? 1 : 0;
2168 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002169
Dave Allison20dfc792014-06-16 20:44:29 -07002170 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2171
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002172 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002173 return kCondGE;
2174 }
2175
2176 private:
2177 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2178};
2179
2180
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002181// Instruction to check how two inputs compare to each other.
2182// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2183class HCompare : public HBinaryOperation {
2184 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002185 // The bias applies for floating point operations and indicates how NaN
2186 // comparisons are treated:
2187 enum Bias {
2188 kNoBias, // bias is not applicable (i.e. for long operation)
2189 kGtBias, // return 1 for NaN comparisons
2190 kLtBias, // return -1 for NaN comparisons
2191 };
2192
2193 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
2194 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002195 DCHECK_EQ(type, first->GetType());
2196 DCHECK_EQ(type, second->GetType());
2197 }
2198
Calin Juravleddb7df22014-11-25 20:56:51 +00002199 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002200 return
2201 x == y ? 0 :
2202 x > y ? 1 :
2203 -1;
2204 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002205
2206 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002207 return
2208 x == y ? 0 :
2209 x > y ? 1 :
2210 -1;
2211 }
2212
Calin Juravleddb7df22014-11-25 20:56:51 +00002213 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2214 return bias_ == other->AsCompare()->bias_;
2215 }
2216
2217 bool IsGtBias() { return bias_ == kGtBias; }
2218
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002219 DECLARE_INSTRUCTION(Compare);
2220
2221 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002222 const Bias bias_;
2223
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002224 DISALLOW_COPY_AND_ASSIGN(HCompare);
2225};
2226
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002227// A local in the graph. Corresponds to a Dex register.
2228class HLocal : public HTemplateInstruction<0> {
2229 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002230 explicit HLocal(uint16_t reg_number)
2231 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002232
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002233 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002234
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002235 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002236
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002237 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002238 // The Dex register number.
2239 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002240
2241 DISALLOW_COPY_AND_ASSIGN(HLocal);
2242};
2243
2244// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002245class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002246 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002247 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002248 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002249 SetRawInputAt(0, local);
2250 }
2251
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002252 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2253
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002254 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002255
2256 private:
2257 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2258};
2259
2260// Store a value in a given local. This instruction has two inputs: the value
2261// and the local.
2262class HStoreLocal : public HTemplateInstruction<2> {
2263 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002264 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002265 SetRawInputAt(0, local);
2266 SetRawInputAt(1, value);
2267 }
2268
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002269 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2270
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002271 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002272
2273 private:
2274 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2275};
2276
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002277class HConstant : public HExpression<0> {
2278 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2280
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002281 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002282
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002283 virtual bool IsMinusOne() const { return false; }
2284 virtual bool IsZero() const { return false; }
2285 virtual bool IsOne() const { return false; }
2286
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002287 DECLARE_INSTRUCTION(Constant);
2288
2289 private:
2290 DISALLOW_COPY_AND_ASSIGN(HConstant);
2291};
2292
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002293class HFloatConstant : public HConstant {
2294 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002295 float GetValue() const { return value_; }
2296
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002297 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002298 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2299 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002300 }
2301
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002302 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002303
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002304 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002305 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002306 }
2307 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002308 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002309 }
2310 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002311 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2312 }
2313 bool IsNaN() const {
2314 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002315 }
2316
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002317 DECLARE_INSTRUCTION(FloatConstant);
2318
2319 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002320 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002321 explicit HFloatConstant(int32_t value)
2322 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002323
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002324 const float value_;
2325
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002326 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002327 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002328 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002329 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2330};
2331
2332class HDoubleConstant : public HConstant {
2333 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002334 double GetValue() const { return value_; }
2335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002336 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002337 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2338 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002339 }
2340
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002341 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002342
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002343 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002344 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002345 }
2346 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002347 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002348 }
2349 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002350 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2351 }
2352 bool IsNaN() const {
2353 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002354 }
2355
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002356 DECLARE_INSTRUCTION(DoubleConstant);
2357
2358 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002359 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002360 explicit HDoubleConstant(int64_t value)
2361 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002362
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002363 const double value_;
2364
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002365 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002366 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002367 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002368 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2369};
2370
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002371class HNullConstant : public HConstant {
2372 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002373 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2374 return true;
2375 }
2376
2377 size_t ComputeHashCode() const OVERRIDE { return 0; }
2378
2379 DECLARE_INSTRUCTION(NullConstant);
2380
2381 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002382 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2383
2384 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002385 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2386};
2387
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002388// Constants of the type int. Those can be from Dex instructions, or
2389// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002390class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002391 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002392 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002393
Calin Juravle61d544b2015-02-23 16:46:57 +00002394 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002395 return other->AsIntConstant()->value_ == value_;
2396 }
2397
Calin Juravle61d544b2015-02-23 16:46:57 +00002398 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2399
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002400 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2401 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2402 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2403
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002404 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002405
2406 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002407 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2408
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002409 const int32_t value_;
2410
David Brazdil8d5b8b22015-03-24 10:51:52 +00002411 friend class HGraph;
2412 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002413 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002414 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2415};
2416
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002417class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002418 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002419 int64_t GetValue() const { return value_; }
2420
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002421 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002422 return other->AsLongConstant()->value_ == value_;
2423 }
2424
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002425 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002426
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002427 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2428 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2429 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2430
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002431 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002432
2433 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002434 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2435
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002436 const int64_t value_;
2437
David Brazdil8d5b8b22015-03-24 10:51:52 +00002438 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002439 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2440};
2441
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002442enum class Intrinsics {
2443#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2444#include "intrinsics_list.h"
2445 kNone,
2446 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2447#undef INTRINSICS_LIST
2448#undef OPTIMIZING_INTRINSICS
2449};
2450std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2451
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002452class HInvoke : public HInstruction {
2453 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002454 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002455
2456 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2457 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002458 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002459
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002460 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002461 SetRawInputAt(index, argument);
2462 }
2463
Roland Levillain3e3d7332015-04-28 11:00:54 +01002464 // Return the number of arguments. This number can be lower than
2465 // the number of inputs returned by InputCount(), as some invoke
2466 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2467 // inputs at the end of their list of inputs.
2468 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2469
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002470 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002471
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002472 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002473
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002474 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002475 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002476
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002477 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2478
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002479 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002480 return intrinsic_;
2481 }
2482
2483 void SetIntrinsic(Intrinsics intrinsic) {
2484 intrinsic_ = intrinsic;
2485 }
2486
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002487 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002488 return GetEnvironment()->GetParent() != nullptr;
2489 }
2490
2491 bool CanThrow() const OVERRIDE { return true; }
2492
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002493 DECLARE_INSTRUCTION(Invoke);
2494
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002495 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002496 HInvoke(ArenaAllocator* arena,
2497 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002498 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002499 Primitive::Type return_type,
2500 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002501 uint32_t dex_method_index,
2502 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002503 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002504 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002505 inputs_(arena, number_of_arguments),
2506 return_type_(return_type),
2507 dex_pc_(dex_pc),
2508 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002509 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002510 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002511 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2512 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002513 }
2514
David Brazdil1abb4192015-02-17 18:33:36 +00002515 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2516 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2517 inputs_.Put(index, input);
2518 }
2519
Roland Levillain3e3d7332015-04-28 11:00:54 +01002520 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002521 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002522 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002523 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002524 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002525 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002526 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002527
2528 private:
2529 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2530};
2531
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002532class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002533 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002534 // Requirements of this method call regarding the class
2535 // initialization (clinit) check of its declaring class.
2536 enum class ClinitCheckRequirement {
2537 kNone, // Class already initialized.
2538 kExplicit, // Static call having explicit clinit check as last input.
2539 kImplicit, // Static call implicitly requiring a clinit check.
2540 };
2541
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002542 HInvokeStaticOrDirect(ArenaAllocator* arena,
2543 uint32_t number_of_arguments,
2544 Primitive::Type return_type,
2545 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002546 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002547 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002548 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002549 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002550 InvokeType invoke_type,
2551 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002552 : HInvoke(arena,
2553 number_of_arguments,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002554 // There is one extra argument for the HCurrentMethod node, and
2555 // potentially one other if the clinit check is explicit.
2556 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002557 return_type,
2558 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002559 dex_method_index,
2560 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002561 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002562 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002563 clinit_check_requirement_(clinit_check_requirement),
2564 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002565
Calin Juravle641547a2015-04-21 22:08:51 +01002566 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2567 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002568 // We access the method via the dex cache so we can't do an implicit null check.
2569 // TODO: for intrinsics we can generate implicit null checks.
2570 return false;
2571 }
2572
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002573 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002574 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002575 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002576 bool IsStringInit() const { return string_init_offset_ != 0; }
2577 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002578 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002579
Roland Levillain4c0eb422015-04-24 16:43:49 +01002580 // Is this instruction a call to a static method?
2581 bool IsStatic() const {
2582 return GetInvokeType() == kStatic;
2583 }
2584
Roland Levillain3e3d7332015-04-28 11:00:54 +01002585 // Remove the art::HLoadClass instruction set as last input by
2586 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2587 // the initial art::HClinitCheck instruction (only relevant for
2588 // static calls with explicit clinit check).
2589 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002590 DCHECK(IsStaticWithExplicitClinitCheck());
2591 size_t last_input_index = InputCount() - 1;
2592 HInstruction* last_input = InputAt(last_input_index);
2593 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002594 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002595 RemoveAsUserOfInput(last_input_index);
2596 inputs_.DeleteAt(last_input_index);
2597 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2598 DCHECK(IsStaticWithImplicitClinitCheck());
2599 }
2600
2601 // Is this a call to a static method whose declaring class has an
2602 // explicit intialization check in the graph?
2603 bool IsStaticWithExplicitClinitCheck() const {
2604 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2605 }
2606
2607 // Is this a call to a static method whose declaring class has an
2608 // implicit intialization check requirement?
2609 bool IsStaticWithImplicitClinitCheck() const {
2610 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2611 }
2612
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002613 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002614
Roland Levillain4c0eb422015-04-24 16:43:49 +01002615 protected:
2616 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2617 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2618 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2619 HInstruction* input = input_record.GetInstruction();
2620 // `input` is the last input of a static invoke marked as having
2621 // an explicit clinit check. It must either be:
2622 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2623 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2624 DCHECK(input != nullptr);
2625 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2626 }
2627 return input_record;
2628 }
2629
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002630 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002631 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002632 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002633 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002634 // Thread entrypoint offset for string init method if this is a string init invoke.
2635 // Note that there are multiple string init methods, each having its own offset.
2636 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002637
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002638 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002639};
2640
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002641class HInvokeVirtual : public HInvoke {
2642 public:
2643 HInvokeVirtual(ArenaAllocator* arena,
2644 uint32_t number_of_arguments,
2645 Primitive::Type return_type,
2646 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002647 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002648 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002649 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002650 vtable_index_(vtable_index) {}
2651
Calin Juravle641547a2015-04-21 22:08:51 +01002652 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002653 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002654 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002655 }
2656
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002657 uint32_t GetVTableIndex() const { return vtable_index_; }
2658
2659 DECLARE_INSTRUCTION(InvokeVirtual);
2660
2661 private:
2662 const uint32_t vtable_index_;
2663
2664 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2665};
2666
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002667class HInvokeInterface : public HInvoke {
2668 public:
2669 HInvokeInterface(ArenaAllocator* arena,
2670 uint32_t number_of_arguments,
2671 Primitive::Type return_type,
2672 uint32_t dex_pc,
2673 uint32_t dex_method_index,
2674 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002675 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002676 imt_index_(imt_index) {}
2677
Calin Juravle641547a2015-04-21 22:08:51 +01002678 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002679 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002680 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002681 }
2682
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002683 uint32_t GetImtIndex() const { return imt_index_; }
2684 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2685
2686 DECLARE_INSTRUCTION(InvokeInterface);
2687
2688 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002689 const uint32_t imt_index_;
2690
2691 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2692};
2693
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002694class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002695 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002696 HNewInstance(HCurrentMethod* current_method,
2697 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002698 uint16_t type_index,
2699 const DexFile& dex_file,
2700 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002701 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2702 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002703 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002704 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002705 entrypoint_(entrypoint) {
2706 SetRawInputAt(0, current_method);
2707 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002708
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002709 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002710 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002711 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002712
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002713 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002714 bool NeedsEnvironment() const OVERRIDE { return true; }
2715 // It may throw when called on:
2716 // - interfaces
2717 // - abstract/innaccessible/unknown classes
2718 // TODO: optimize when possible.
2719 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002720
Calin Juravle10e244f2015-01-26 18:54:32 +00002721 bool CanBeNull() const OVERRIDE { return false; }
2722
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002723 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2724
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002725 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002726
2727 private:
2728 const uint32_t dex_pc_;
2729 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002730 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002731 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002732
2733 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2734};
2735
Roland Levillain88cb1752014-10-20 16:36:47 +01002736class HNeg : public HUnaryOperation {
2737 public:
2738 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2739 : HUnaryOperation(result_type, input) {}
2740
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002741 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2742 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002743
Roland Levillain88cb1752014-10-20 16:36:47 +01002744 DECLARE_INSTRUCTION(Neg);
2745
2746 private:
2747 DISALLOW_COPY_AND_ASSIGN(HNeg);
2748};
2749
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002750class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002751 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002752 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002753 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002754 uint32_t dex_pc,
2755 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002756 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002757 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002758 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2759 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002760 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002761 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002762 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002763 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002764 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002765 }
2766
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002767 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002768 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002769 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002770
2771 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002772 bool NeedsEnvironment() const OVERRIDE { return true; }
2773
Mingyao Yang0c365e62015-03-31 15:09:29 -07002774 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2775 bool CanThrow() const OVERRIDE { return true; }
2776
Calin Juravle10e244f2015-01-26 18:54:32 +00002777 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002778
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002779 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2780
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002781 DECLARE_INSTRUCTION(NewArray);
2782
2783 private:
2784 const uint32_t dex_pc_;
2785 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002786 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002787 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002788
2789 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2790};
2791
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002792class HAdd : public HBinaryOperation {
2793 public:
2794 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2795 : HBinaryOperation(result_type, left, right) {}
2796
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002797 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002798
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002799 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002800 return x + y;
2801 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002802 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002803 return x + y;
2804 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002805
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002806 DECLARE_INSTRUCTION(Add);
2807
2808 private:
2809 DISALLOW_COPY_AND_ASSIGN(HAdd);
2810};
2811
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002812class HSub : public HBinaryOperation {
2813 public:
2814 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2815 : HBinaryOperation(result_type, left, right) {}
2816
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002817 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002818 return x - y;
2819 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002820 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002821 return x - y;
2822 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002823
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002824 DECLARE_INSTRUCTION(Sub);
2825
2826 private:
2827 DISALLOW_COPY_AND_ASSIGN(HSub);
2828};
2829
Calin Juravle34bacdf2014-10-07 20:23:36 +01002830class HMul : public HBinaryOperation {
2831 public:
2832 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2833 : HBinaryOperation(result_type, left, right) {}
2834
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002835 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002836
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002837 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2838 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002839
2840 DECLARE_INSTRUCTION(Mul);
2841
2842 private:
2843 DISALLOW_COPY_AND_ASSIGN(HMul);
2844};
2845
Calin Juravle7c4954d2014-10-28 16:57:40 +00002846class HDiv : public HBinaryOperation {
2847 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002848 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2849 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002850
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002851 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002852 // Our graph structure ensures we never have 0 for `y` during constant folding.
2853 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002854 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002855 return (y == -1) ? -x : x / y;
2856 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002857
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002858 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002859 DCHECK_NE(y, 0);
2860 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2861 return (y == -1) ? -x : x / y;
2862 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002863
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002864 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002865
Calin Juravle7c4954d2014-10-28 16:57:40 +00002866 DECLARE_INSTRUCTION(Div);
2867
2868 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002869 const uint32_t dex_pc_;
2870
Calin Juravle7c4954d2014-10-28 16:57:40 +00002871 DISALLOW_COPY_AND_ASSIGN(HDiv);
2872};
2873
Calin Juravlebacfec32014-11-14 15:54:36 +00002874class HRem : public HBinaryOperation {
2875 public:
2876 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2877 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2878
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002879 int32_t Evaluate(int32_t x, int32_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) ? 0 : x % y;
2883 }
2884
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002885 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002886 DCHECK_NE(y, 0);
2887 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2888 return (y == -1) ? 0 : x % y;
2889 }
2890
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002891 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002892
2893 DECLARE_INSTRUCTION(Rem);
2894
2895 private:
2896 const uint32_t dex_pc_;
2897
2898 DISALLOW_COPY_AND_ASSIGN(HRem);
2899};
2900
Calin Juravled0d48522014-11-04 16:40:20 +00002901class HDivZeroCheck : public HExpression<1> {
2902 public:
2903 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2904 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2905 SetRawInputAt(0, value);
2906 }
2907
2908 bool CanBeMoved() const OVERRIDE { return true; }
2909
2910 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2911 UNUSED(other);
2912 return true;
2913 }
2914
2915 bool NeedsEnvironment() const OVERRIDE { return true; }
2916 bool CanThrow() const OVERRIDE { return true; }
2917
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002918 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002919
2920 DECLARE_INSTRUCTION(DivZeroCheck);
2921
2922 private:
2923 const uint32_t dex_pc_;
2924
2925 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2926};
2927
Calin Juravle9aec02f2014-11-18 23:06:35 +00002928class HShl : public HBinaryOperation {
2929 public:
2930 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2931 : HBinaryOperation(result_type, left, right) {}
2932
2933 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2934 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2935
2936 DECLARE_INSTRUCTION(Shl);
2937
2938 private:
2939 DISALLOW_COPY_AND_ASSIGN(HShl);
2940};
2941
2942class HShr : public HBinaryOperation {
2943 public:
2944 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2945 : HBinaryOperation(result_type, left, right) {}
2946
2947 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2948 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2949
2950 DECLARE_INSTRUCTION(Shr);
2951
2952 private:
2953 DISALLOW_COPY_AND_ASSIGN(HShr);
2954};
2955
2956class HUShr : public HBinaryOperation {
2957 public:
2958 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2959 : HBinaryOperation(result_type, left, right) {}
2960
2961 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2962 uint32_t ux = static_cast<uint32_t>(x);
2963 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2964 return static_cast<int32_t>(ux >> uy);
2965 }
2966
2967 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2968 uint64_t ux = static_cast<uint64_t>(x);
2969 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2970 return static_cast<int64_t>(ux >> uy);
2971 }
2972
2973 DECLARE_INSTRUCTION(UShr);
2974
2975 private:
2976 DISALLOW_COPY_AND_ASSIGN(HUShr);
2977};
2978
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002979class HAnd : public HBinaryOperation {
2980 public:
2981 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2982 : HBinaryOperation(result_type, left, right) {}
2983
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002984 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002985
2986 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2987 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2988
2989 DECLARE_INSTRUCTION(And);
2990
2991 private:
2992 DISALLOW_COPY_AND_ASSIGN(HAnd);
2993};
2994
2995class HOr : public HBinaryOperation {
2996 public:
2997 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2998 : HBinaryOperation(result_type, left, right) {}
2999
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003000 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003001
3002 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3003 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3004
3005 DECLARE_INSTRUCTION(Or);
3006
3007 private:
3008 DISALLOW_COPY_AND_ASSIGN(HOr);
3009};
3010
3011class HXor : public HBinaryOperation {
3012 public:
3013 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3014 : HBinaryOperation(result_type, left, right) {}
3015
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003016 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003017
3018 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3019 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3020
3021 DECLARE_INSTRUCTION(Xor);
3022
3023 private:
3024 DISALLOW_COPY_AND_ASSIGN(HXor);
3025};
3026
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003027// The value of a parameter in this method. Its location depends on
3028// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003029class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003030 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003031 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3032 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003033
3034 uint8_t GetIndex() const { return index_; }
3035
Calin Juravle10e244f2015-01-26 18:54:32 +00003036 bool CanBeNull() const OVERRIDE { return !is_this_; }
3037
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003038 bool IsThis() const { return is_this_; }
3039
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003040 DECLARE_INSTRUCTION(ParameterValue);
3041
3042 private:
3043 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003044 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003045 const uint8_t index_;
3046
Calin Juravle10e244f2015-01-26 18:54:32 +00003047 // Whether or not the parameter value corresponds to 'this' argument.
3048 const bool is_this_;
3049
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003050 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3051};
3052
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003053class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003054 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003055 explicit HNot(Primitive::Type result_type, HInstruction* input)
3056 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003057
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003058 bool CanBeMoved() const OVERRIDE { return true; }
3059 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003060 UNUSED(other);
3061 return true;
3062 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003063
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003064 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3065 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003066
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003067 DECLARE_INSTRUCTION(Not);
3068
3069 private:
3070 DISALLOW_COPY_AND_ASSIGN(HNot);
3071};
3072
David Brazdil66d126e2015-04-03 16:02:44 +01003073class HBooleanNot : public HUnaryOperation {
3074 public:
3075 explicit HBooleanNot(HInstruction* input)
3076 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3077
3078 bool CanBeMoved() const OVERRIDE { return true; }
3079 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3080 UNUSED(other);
3081 return true;
3082 }
3083
3084 int32_t Evaluate(int32_t x) const OVERRIDE {
3085 DCHECK(IsUint<1>(x));
3086 return !x;
3087 }
3088
3089 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3090 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3091 UNREACHABLE();
3092 }
3093
3094 DECLARE_INSTRUCTION(BooleanNot);
3095
3096 private:
3097 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3098};
3099
Roland Levillaindff1f282014-11-05 14:15:05 +00003100class HTypeConversion : public HExpression<1> {
3101 public:
3102 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003103 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3104 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003105 SetRawInputAt(0, input);
3106 DCHECK_NE(input->GetType(), result_type);
3107 }
3108
3109 HInstruction* GetInput() const { return InputAt(0); }
3110 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3111 Primitive::Type GetResultType() const { return GetType(); }
3112
Roland Levillain624279f2014-12-04 11:54:28 +00003113 // Required by the x86 and ARM code generators when producing calls
3114 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003115 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003116
Roland Levillaindff1f282014-11-05 14:15:05 +00003117 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003118 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003119
Mark Mendelle82549b2015-05-06 10:55:34 -04003120 // Try to statically evaluate the conversion and return a HConstant
3121 // containing the result. If the input cannot be converted, return nullptr.
3122 HConstant* TryStaticEvaluation() const;
3123
Roland Levillaindff1f282014-11-05 14:15:05 +00003124 DECLARE_INSTRUCTION(TypeConversion);
3125
3126 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003127 const uint32_t dex_pc_;
3128
Roland Levillaindff1f282014-11-05 14:15:05 +00003129 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3130};
3131
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003132static constexpr uint32_t kNoRegNumber = -1;
3133
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003134class HPhi : public HInstruction {
3135 public:
3136 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003137 : HInstruction(SideEffects::None()),
3138 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003139 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003140 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003141 is_live_(false),
3142 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003143 inputs_.SetSize(number_of_inputs);
3144 }
3145
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003146 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3147 static Primitive::Type ToPhiType(Primitive::Type type) {
3148 switch (type) {
3149 case Primitive::kPrimBoolean:
3150 case Primitive::kPrimByte:
3151 case Primitive::kPrimShort:
3152 case Primitive::kPrimChar:
3153 return Primitive::kPrimInt;
3154 default:
3155 return type;
3156 }
3157 }
3158
Calin Juravle10e244f2015-01-26 18:54:32 +00003159 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003160
3161 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003162 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003163
Calin Juravle10e244f2015-01-26 18:54:32 +00003164 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003165 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003166
Calin Juravle10e244f2015-01-26 18:54:32 +00003167 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3168 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3169
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003170 uint32_t GetRegNumber() const { return reg_number_; }
3171
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003172 void SetDead() { is_live_ = false; }
3173 void SetLive() { is_live_ = true; }
3174 bool IsDead() const { return !is_live_; }
3175 bool IsLive() const { return is_live_; }
3176
Calin Juravlea4f88312015-04-16 12:57:19 +01003177 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3178 // An equivalent phi is a phi having the same dex register and type.
3179 // It assumes that phis with the same dex register are adjacent.
3180 HPhi* GetNextEquivalentPhiWithSameType() {
3181 HInstruction* next = GetNext();
3182 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3183 if (next->GetType() == GetType()) {
3184 return next->AsPhi();
3185 }
3186 next = next->GetNext();
3187 }
3188 return nullptr;
3189 }
3190
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003191 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003192
David Brazdil1abb4192015-02-17 18:33:36 +00003193 protected:
3194 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3195
3196 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3197 inputs_.Put(index, input);
3198 }
3199
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003200 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003201 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003202 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003203 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003204 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003205 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003206
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003207 DISALLOW_COPY_AND_ASSIGN(HPhi);
3208};
3209
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003210class HNullCheck : public HExpression<1> {
3211 public:
3212 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003213 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003214 SetRawInputAt(0, value);
3215 }
3216
Calin Juravle10e244f2015-01-26 18:54:32 +00003217 bool CanBeMoved() const OVERRIDE { return true; }
3218 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003219 UNUSED(other);
3220 return true;
3221 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003222
Calin Juravle10e244f2015-01-26 18:54:32 +00003223 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003224
Calin Juravle10e244f2015-01-26 18:54:32 +00003225 bool CanThrow() const OVERRIDE { return true; }
3226
3227 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003228
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003229 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003230
3231 DECLARE_INSTRUCTION(NullCheck);
3232
3233 private:
3234 const uint32_t dex_pc_;
3235
3236 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3237};
3238
3239class FieldInfo : public ValueObject {
3240 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003241 FieldInfo(MemberOffset field_offset,
3242 Primitive::Type field_type,
3243 bool is_volatile,
3244 uint32_t index,
3245 const DexFile& dex_file)
3246 : field_offset_(field_offset),
3247 field_type_(field_type),
3248 is_volatile_(is_volatile),
3249 index_(index),
3250 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003251
3252 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003253 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003254 uint32_t GetFieldIndex() const { return index_; }
3255 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003256 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003257
3258 private:
3259 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003260 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003261 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003262 uint32_t index_;
3263 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003264};
3265
3266class HInstanceFieldGet : public HExpression<1> {
3267 public:
3268 HInstanceFieldGet(HInstruction* value,
3269 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003270 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003271 bool is_volatile,
3272 uint32_t field_idx,
3273 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003274 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003275 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003276 SetRawInputAt(0, value);
3277 }
3278
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003279 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003280
3281 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3282 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3283 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003284 }
3285
Calin Juravle641547a2015-04-21 22:08:51 +01003286 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3287 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003288 }
3289
3290 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003291 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3292 }
3293
Calin Juravle52c48962014-12-16 17:02:57 +00003294 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003295 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003296 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003297 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003298
3299 DECLARE_INSTRUCTION(InstanceFieldGet);
3300
3301 private:
3302 const FieldInfo field_info_;
3303
3304 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3305};
3306
3307class HInstanceFieldSet : public HTemplateInstruction<2> {
3308 public:
3309 HInstanceFieldSet(HInstruction* object,
3310 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003311 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003312 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003313 bool is_volatile,
3314 uint32_t field_idx,
3315 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003316 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003317 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003318 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003319 SetRawInputAt(0, object);
3320 SetRawInputAt(1, value);
3321 }
3322
Calin Juravle641547a2015-04-21 22:08:51 +01003323 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3324 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003325 }
3326
Calin Juravle52c48962014-12-16 17:02:57 +00003327 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003328 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003329 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003330 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003331 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003332 bool GetValueCanBeNull() const { return value_can_be_null_; }
3333 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003334
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003335 DECLARE_INSTRUCTION(InstanceFieldSet);
3336
3337 private:
3338 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003339 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003340
3341 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3342};
3343
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003344class HArrayGet : public HExpression<2> {
3345 public:
3346 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003347 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003348 SetRawInputAt(0, array);
3349 SetRawInputAt(1, index);
3350 }
3351
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003352 bool CanBeMoved() const OVERRIDE { return true; }
3353 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003354 UNUSED(other);
3355 return true;
3356 }
Calin Juravle641547a2015-04-21 22:08:51 +01003357 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3358 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003359 // TODO: We can be smarter here.
3360 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3361 // which generates the implicit null check. There are cases when these can be removed
3362 // to produce better code. If we ever add optimizations to do so we should allow an
3363 // implicit check here (as long as the address falls in the first page).
3364 return false;
3365 }
3366
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003367 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003368
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003369 HInstruction* GetArray() const { return InputAt(0); }
3370 HInstruction* GetIndex() const { return InputAt(1); }
3371
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003372 DECLARE_INSTRUCTION(ArrayGet);
3373
3374 private:
3375 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3376};
3377
3378class HArraySet : public HTemplateInstruction<3> {
3379 public:
3380 HArraySet(HInstruction* array,
3381 HInstruction* index,
3382 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003383 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003384 uint32_t dex_pc)
3385 : HTemplateInstruction(SideEffects::ChangesSomething()),
3386 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003387 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003388 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3389 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003390 SetRawInputAt(0, array);
3391 SetRawInputAt(1, index);
3392 SetRawInputAt(2, value);
3393 }
3394
Calin Juravle77520bc2015-01-12 18:45:46 +00003395 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003396 // We currently always call a runtime method to catch array store
3397 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003398 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003399 }
3400
Mingyao Yang81014cb2015-06-02 03:16:27 -07003401 // Can throw ArrayStoreException.
3402 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3403
Calin Juravle641547a2015-04-21 22:08:51 +01003404 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3405 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003406 // TODO: Same as for ArrayGet.
3407 return false;
3408 }
3409
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003410 void ClearNeedsTypeCheck() {
3411 needs_type_check_ = false;
3412 }
3413
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003414 void ClearValueCanBeNull() {
3415 value_can_be_null_ = false;
3416 }
3417
3418 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003419 bool NeedsTypeCheck() const { return needs_type_check_; }
3420
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003421 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003422
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003423 HInstruction* GetArray() const { return InputAt(0); }
3424 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003425 HInstruction* GetValue() const { return InputAt(2); }
3426
3427 Primitive::Type GetComponentType() const {
3428 // The Dex format does not type floating point index operations. Since the
3429 // `expected_component_type_` is set during building and can therefore not
3430 // be correct, we also check what is the value type. If it is a floating
3431 // point type, we must use that type.
3432 Primitive::Type value_type = GetValue()->GetType();
3433 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3434 ? value_type
3435 : expected_component_type_;
3436 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003437
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003438 DECLARE_INSTRUCTION(ArraySet);
3439
3440 private:
3441 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003442 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003443 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003444 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003445
3446 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3447};
3448
3449class HArrayLength : public HExpression<1> {
3450 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003451 explicit HArrayLength(HInstruction* array)
3452 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3453 // Note that arrays do not change length, so the instruction does not
3454 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003455 SetRawInputAt(0, array);
3456 }
3457
Calin Juravle77520bc2015-01-12 18:45:46 +00003458 bool CanBeMoved() const OVERRIDE { return true; }
3459 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003460 UNUSED(other);
3461 return true;
3462 }
Calin Juravle641547a2015-04-21 22:08:51 +01003463 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3464 return obj == InputAt(0);
3465 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003466
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003467 DECLARE_INSTRUCTION(ArrayLength);
3468
3469 private:
3470 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3471};
3472
3473class HBoundsCheck : public HExpression<2> {
3474 public:
3475 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003476 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003477 DCHECK(index->GetType() == Primitive::kPrimInt);
3478 SetRawInputAt(0, index);
3479 SetRawInputAt(1, length);
3480 }
3481
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003482 bool CanBeMoved() const OVERRIDE { return true; }
3483 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003484 UNUSED(other);
3485 return true;
3486 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003487
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003488 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003489
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003490 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003491
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003492 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003493
3494 DECLARE_INSTRUCTION(BoundsCheck);
3495
3496 private:
3497 const uint32_t dex_pc_;
3498
3499 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3500};
3501
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003502/**
3503 * Some DEX instructions are folded into multiple HInstructions that need
3504 * to stay live until the last HInstruction. This class
3505 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003506 * HInstruction stays live. `index` represents the stack location index of the
3507 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003508 */
3509class HTemporary : public HTemplateInstruction<0> {
3510 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003511 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003512
3513 size_t GetIndex() const { return index_; }
3514
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003515 Primitive::Type GetType() const OVERRIDE {
3516 // The previous instruction is the one that will be stored in the temporary location.
3517 DCHECK(GetPrevious() != nullptr);
3518 return GetPrevious()->GetType();
3519 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003520
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003521 DECLARE_INSTRUCTION(Temporary);
3522
3523 private:
3524 const size_t index_;
3525
3526 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3527};
3528
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003529class HSuspendCheck : public HTemplateInstruction<0> {
3530 public:
3531 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003532 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003533
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003534 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003535 return true;
3536 }
3537
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003538 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003539 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3540 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003541
3542 DECLARE_INSTRUCTION(SuspendCheck);
3543
3544 private:
3545 const uint32_t dex_pc_;
3546
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003547 // Only used for code generation, in order to share the same slow path between back edges
3548 // of a same loop.
3549 SlowPathCode* slow_path_;
3550
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003551 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3552};
3553
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003554/**
3555 * Instruction to load a Class object.
3556 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003557class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003558 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003559 HLoadClass(HCurrentMethod* current_method,
3560 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003561 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003562 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003563 uint32_t dex_pc)
3564 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3565 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003566 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003567 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003568 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003569 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003570 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3571 SetRawInputAt(0, current_method);
3572 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003573
3574 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003575
3576 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3577 return other->AsLoadClass()->type_index_ == type_index_;
3578 }
3579
3580 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3581
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003582 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003583 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003584 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003585
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003586 bool NeedsEnvironment() const OVERRIDE {
3587 // Will call runtime and load the class if the class is not loaded yet.
3588 // TODO: finer grain decision.
3589 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003590 }
3591
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003592 bool MustGenerateClinitCheck() const {
3593 return generate_clinit_check_;
3594 }
3595
Calin Juravle0ba218d2015-05-19 18:46:01 +01003596 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3597 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003598 }
3599
3600 bool CanCallRuntime() const {
3601 return MustGenerateClinitCheck() || !is_referrers_class_;
3602 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003603
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003604 bool CanThrow() const OVERRIDE {
3605 // May call runtime and and therefore can throw.
3606 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003607 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003608 }
3609
Calin Juravleacf735c2015-02-12 15:25:22 +00003610 ReferenceTypeInfo GetLoadedClassRTI() {
3611 return loaded_class_rti_;
3612 }
3613
3614 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3615 // Make sure we only set exact types (the loaded class should never be merged).
3616 DCHECK(rti.IsExact());
3617 loaded_class_rti_ = rti;
3618 }
3619
3620 bool IsResolved() {
3621 return loaded_class_rti_.IsExact();
3622 }
3623
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003624 const DexFile& GetDexFile() { return dex_file_; }
3625
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003626 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3627
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003628 DECLARE_INSTRUCTION(LoadClass);
3629
3630 private:
3631 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003632 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003633 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003634 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003635 // Whether this instruction must generate the initialization check.
3636 // Used for code generation.
3637 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003638
Calin Juravleacf735c2015-02-12 15:25:22 +00003639 ReferenceTypeInfo loaded_class_rti_;
3640
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003641 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3642};
3643
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003644class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003645 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003646 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003647 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3648 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003649 dex_pc_(dex_pc) {
3650 SetRawInputAt(0, current_method);
3651 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003652
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003653 bool CanBeMoved() const OVERRIDE { return true; }
3654
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003655 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3656 return other->AsLoadString()->string_index_ == string_index_;
3657 }
3658
3659 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3660
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003661 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003662 uint32_t GetStringIndex() const { return string_index_; }
3663
3664 // TODO: Can we deopt or debug when we resolve a string?
3665 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003666 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003667
3668 DECLARE_INSTRUCTION(LoadString);
3669
3670 private:
3671 const uint32_t string_index_;
3672 const uint32_t dex_pc_;
3673
3674 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3675};
3676
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003677/**
3678 * Performs an initialization check on its Class object input.
3679 */
3680class HClinitCheck : public HExpression<1> {
3681 public:
3682 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003683 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003684 dex_pc_(dex_pc) {
3685 SetRawInputAt(0, constant);
3686 }
3687
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003688 bool CanBeMoved() const OVERRIDE { return true; }
3689 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3690 UNUSED(other);
3691 return true;
3692 }
3693
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003694 bool NeedsEnvironment() const OVERRIDE {
3695 // May call runtime to initialize the class.
3696 return true;
3697 }
3698
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003699 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003700
3701 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3702
3703 DECLARE_INSTRUCTION(ClinitCheck);
3704
3705 private:
3706 const uint32_t dex_pc_;
3707
3708 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3709};
3710
3711class HStaticFieldGet : public HExpression<1> {
3712 public:
3713 HStaticFieldGet(HInstruction* cls,
3714 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003715 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003716 bool is_volatile,
3717 uint32_t field_idx,
3718 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003719 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003720 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003721 SetRawInputAt(0, cls);
3722 }
3723
Calin Juravle52c48962014-12-16 17:02:57 +00003724
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003725 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003726
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003727 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003728 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3729 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003730 }
3731
3732 size_t ComputeHashCode() const OVERRIDE {
3733 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3734 }
3735
Calin Juravle52c48962014-12-16 17:02:57 +00003736 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003737 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3738 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003739 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003740
3741 DECLARE_INSTRUCTION(StaticFieldGet);
3742
3743 private:
3744 const FieldInfo field_info_;
3745
3746 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3747};
3748
3749class HStaticFieldSet : public HTemplateInstruction<2> {
3750 public:
3751 HStaticFieldSet(HInstruction* cls,
3752 HInstruction* value,
3753 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003754 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003755 bool is_volatile,
3756 uint32_t field_idx,
3757 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003758 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003759 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003760 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003761 SetRawInputAt(0, cls);
3762 SetRawInputAt(1, value);
3763 }
3764
Calin Juravle52c48962014-12-16 17:02:57 +00003765 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003766 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3767 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003768 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003769
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003770 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003771 bool GetValueCanBeNull() const { return value_can_be_null_; }
3772 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003773
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003774 DECLARE_INSTRUCTION(StaticFieldSet);
3775
3776 private:
3777 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003778 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003779
3780 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3781};
3782
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003783// Implement the move-exception DEX instruction.
3784class HLoadException : public HExpression<0> {
3785 public:
3786 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3787
3788 DECLARE_INSTRUCTION(LoadException);
3789
3790 private:
3791 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3792};
3793
3794class HThrow : public HTemplateInstruction<1> {
3795 public:
3796 HThrow(HInstruction* exception, uint32_t dex_pc)
3797 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3798 SetRawInputAt(0, exception);
3799 }
3800
3801 bool IsControlFlow() const OVERRIDE { return true; }
3802
3803 bool NeedsEnvironment() const OVERRIDE { return true; }
3804
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003805 bool CanThrow() const OVERRIDE { return true; }
3806
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003807 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003808
3809 DECLARE_INSTRUCTION(Throw);
3810
3811 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003812 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003813
3814 DISALLOW_COPY_AND_ASSIGN(HThrow);
3815};
3816
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003817class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003818 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003819 HInstanceOf(HInstruction* object,
3820 HLoadClass* constant,
3821 bool class_is_final,
3822 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003823 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3824 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003825 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003826 dex_pc_(dex_pc) {
3827 SetRawInputAt(0, object);
3828 SetRawInputAt(1, constant);
3829 }
3830
3831 bool CanBeMoved() const OVERRIDE { return true; }
3832
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003833 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003834 return true;
3835 }
3836
3837 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003838 return false;
3839 }
3840
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003841 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003842
3843 bool IsClassFinal() const { return class_is_final_; }
3844
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003845 // Used only in code generation.
3846 bool MustDoNullCheck() const { return must_do_null_check_; }
3847 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3848
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003849 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003850
3851 private:
3852 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003853 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003854 const uint32_t dex_pc_;
3855
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003856 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3857};
3858
Calin Juravleb1498f62015-02-16 13:13:29 +00003859class HBoundType : public HExpression<1> {
3860 public:
3861 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3862 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3863 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003864 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003865 SetRawInputAt(0, input);
3866 }
3867
3868 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3869
3870 bool CanBeNull() const OVERRIDE {
3871 // `null instanceof ClassX` always return false so we can't be null.
3872 return false;
3873 }
3874
3875 DECLARE_INSTRUCTION(BoundType);
3876
3877 private:
3878 // Encodes the most upper class that this instruction can have. In other words
3879 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3880 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3881 const ReferenceTypeInfo bound_type_;
3882
3883 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3884};
3885
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003886class HCheckCast : public HTemplateInstruction<2> {
3887 public:
3888 HCheckCast(HInstruction* object,
3889 HLoadClass* constant,
3890 bool class_is_final,
3891 uint32_t dex_pc)
3892 : HTemplateInstruction(SideEffects::None()),
3893 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003894 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003895 dex_pc_(dex_pc) {
3896 SetRawInputAt(0, object);
3897 SetRawInputAt(1, constant);
3898 }
3899
3900 bool CanBeMoved() const OVERRIDE { return true; }
3901
3902 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3903 return true;
3904 }
3905
3906 bool NeedsEnvironment() const OVERRIDE {
3907 // Instruction may throw a CheckCastError.
3908 return true;
3909 }
3910
3911 bool CanThrow() const OVERRIDE { return true; }
3912
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003913 bool MustDoNullCheck() const { return must_do_null_check_; }
3914 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3915
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003916 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003917
3918 bool IsClassFinal() const { return class_is_final_; }
3919
3920 DECLARE_INSTRUCTION(CheckCast);
3921
3922 private:
3923 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003924 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003925 const uint32_t dex_pc_;
3926
3927 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003928};
3929
Calin Juravle27df7582015-04-17 19:12:31 +01003930class HMemoryBarrier : public HTemplateInstruction<0> {
3931 public:
3932 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3933 : HTemplateInstruction(SideEffects::None()),
3934 barrier_kind_(barrier_kind) {}
3935
3936 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3937
3938 DECLARE_INSTRUCTION(MemoryBarrier);
3939
3940 private:
3941 const MemBarrierKind barrier_kind_;
3942
3943 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3944};
3945
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003946class HMonitorOperation : public HTemplateInstruction<1> {
3947 public:
3948 enum OperationKind {
3949 kEnter,
3950 kExit,
3951 };
3952
3953 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3954 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3955 SetRawInputAt(0, object);
3956 }
3957
3958 // Instruction may throw a Java exception, so we need an environment.
3959 bool NeedsEnvironment() const OVERRIDE { return true; }
3960 bool CanThrow() const OVERRIDE { return true; }
3961
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003962 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003963
3964 bool IsEnter() const { return kind_ == kEnter; }
3965
3966 DECLARE_INSTRUCTION(MonitorOperation);
3967
Calin Juravle52c48962014-12-16 17:02:57 +00003968 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003969 const OperationKind kind_;
3970 const uint32_t dex_pc_;
3971
3972 private:
3973 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3974};
3975
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003976class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003977 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003978 MoveOperands(Location source,
3979 Location destination,
3980 Primitive::Type type,
3981 HInstruction* instruction)
3982 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003983
3984 Location GetSource() const { return source_; }
3985 Location GetDestination() const { return destination_; }
3986
3987 void SetSource(Location value) { source_ = value; }
3988 void SetDestination(Location value) { destination_ = value; }
3989
3990 // The parallel move resolver marks moves as "in-progress" by clearing the
3991 // destination (but not the source).
3992 Location MarkPending() {
3993 DCHECK(!IsPending());
3994 Location dest = destination_;
3995 destination_ = Location::NoLocation();
3996 return dest;
3997 }
3998
3999 void ClearPending(Location dest) {
4000 DCHECK(IsPending());
4001 destination_ = dest;
4002 }
4003
4004 bool IsPending() const {
4005 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4006 return destination_.IsInvalid() && !source_.IsInvalid();
4007 }
4008
4009 // True if this blocks a move from the given location.
4010 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004011 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004012 }
4013
4014 // A move is redundant if it's been eliminated, if its source and
4015 // destination are the same, or if its destination is unneeded.
4016 bool IsRedundant() const {
4017 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4018 }
4019
4020 // We clear both operands to indicate move that's been eliminated.
4021 void Eliminate() {
4022 source_ = destination_ = Location::NoLocation();
4023 }
4024
4025 bool IsEliminated() const {
4026 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4027 return source_.IsInvalid();
4028 }
4029
Nicolas Geoffray90218252015-04-15 11:56:51 +01004030 bool Is64BitMove() const {
4031 return Primitive::Is64BitType(type_);
4032 }
4033
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004034 HInstruction* GetInstruction() const { return instruction_; }
4035
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004036 private:
4037 Location source_;
4038 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004039 // The type this move is for.
4040 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004041 // The instruction this move is assocatied with. Null when this move is
4042 // for moving an input in the expected locations of user (including a phi user).
4043 // This is only used in debug mode, to ensure we do not connect interval siblings
4044 // in the same parallel move.
4045 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004046};
4047
4048static constexpr size_t kDefaultNumberOfMoves = 4;
4049
4050class HParallelMove : public HTemplateInstruction<0> {
4051 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004052 explicit HParallelMove(ArenaAllocator* arena)
4053 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004054
Nicolas Geoffray90218252015-04-15 11:56:51 +01004055 void AddMove(Location source,
4056 Location destination,
4057 Primitive::Type type,
4058 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004059 DCHECK(source.IsValid());
4060 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004061 if (kIsDebugBuild) {
4062 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004063 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004064 if (moves_.Get(i).GetInstruction() == instruction) {
4065 // Special case the situation where the move is for the spill slot
4066 // of the instruction.
4067 if ((GetPrevious() == instruction)
4068 || ((GetPrevious() == nullptr)
4069 && instruction->IsPhi()
4070 && instruction->GetBlock() == GetBlock())) {
4071 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4072 << "Doing parallel moves for the same instruction.";
4073 } else {
4074 DCHECK(false) << "Doing parallel moves for the same instruction.";
4075 }
4076 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004077 }
4078 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004079 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004080 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004081 << "Overlapped destination for two moves in a parallel move: "
4082 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4083 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004084 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004085 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004086 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004087 }
4088
4089 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004090 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004091 }
4092
4093 size_t NumMoves() const { return moves_.Size(); }
4094
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004095 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004096
4097 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004098 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004099
4100 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4101};
4102
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004103class HGraphVisitor : public ValueObject {
4104 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004105 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4106 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004107
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004108 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004109 virtual void VisitBasicBlock(HBasicBlock* block);
4110
Roland Levillain633021e2014-10-01 14:12:25 +01004111 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004112 void VisitInsertionOrder();
4113
Roland Levillain633021e2014-10-01 14:12:25 +01004114 // Visit the graph following dominator tree reverse post-order.
4115 void VisitReversePostOrder();
4116
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004117 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004118
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004119 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004120#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004121 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4122
4123 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4124
4125#undef DECLARE_VISIT_INSTRUCTION
4126
4127 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004128 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004129
4130 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4131};
4132
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004133class HGraphDelegateVisitor : public HGraphVisitor {
4134 public:
4135 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4136 virtual ~HGraphDelegateVisitor() {}
4137
4138 // Visit functions that delegate to to super class.
4139#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004140 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004141
4142 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4143
4144#undef DECLARE_VISIT_INSTRUCTION
4145
4146 private:
4147 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4148};
4149
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004150class HInsertionOrderIterator : public ValueObject {
4151 public:
4152 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4153
4154 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4155 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4156 void Advance() { ++index_; }
4157
4158 private:
4159 const HGraph& graph_;
4160 size_t index_;
4161
4162 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4163};
4164
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004165class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004166 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004167 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4168 // Check that reverse post order of the graph has been built.
4169 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4170 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004171
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004172 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4173 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004174 void Advance() { ++index_; }
4175
4176 private:
4177 const HGraph& graph_;
4178 size_t index_;
4179
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004180 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004181};
4182
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004183class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004184 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004185 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004186 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4187 // Check that reverse post order of the graph has been built.
4188 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4189 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004190
4191 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004192 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004193 void Advance() { --index_; }
4194
4195 private:
4196 const HGraph& graph_;
4197 size_t index_;
4198
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004199 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004200};
4201
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004202class HLinearPostOrderIterator : public ValueObject {
4203 public:
4204 explicit HLinearPostOrderIterator(const HGraph& graph)
4205 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4206
4207 bool Done() const { return index_ == 0; }
4208
4209 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4210
4211 void Advance() {
4212 --index_;
4213 DCHECK_GE(index_, 0U);
4214 }
4215
4216 private:
4217 const GrowableArray<HBasicBlock*>& order_;
4218 size_t index_;
4219
4220 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4221};
4222
4223class HLinearOrderIterator : public ValueObject {
4224 public:
4225 explicit HLinearOrderIterator(const HGraph& graph)
4226 : order_(graph.GetLinearOrder()), index_(0) {}
4227
4228 bool Done() const { return index_ == order_.Size(); }
4229 HBasicBlock* Current() const { return order_.Get(index_); }
4230 void Advance() { ++index_; }
4231
4232 private:
4233 const GrowableArray<HBasicBlock*>& order_;
4234 size_t index_;
4235
4236 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4237};
4238
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004239// Iterator over the blocks that art part of the loop. Includes blocks part
4240// of an inner loop. The order in which the blocks are iterated is on their
4241// block id.
4242class HBlocksInLoopIterator : public ValueObject {
4243 public:
4244 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4245 : blocks_in_loop_(info.GetBlocks()),
4246 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4247 index_(0) {
4248 if (!blocks_in_loop_.IsBitSet(index_)) {
4249 Advance();
4250 }
4251 }
4252
4253 bool Done() const { return index_ == blocks_.Size(); }
4254 HBasicBlock* Current() const { return blocks_.Get(index_); }
4255 void Advance() {
4256 ++index_;
4257 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4258 if (blocks_in_loop_.IsBitSet(index_)) {
4259 break;
4260 }
4261 }
4262 }
4263
4264 private:
4265 const BitVector& blocks_in_loop_;
4266 const GrowableArray<HBasicBlock*>& blocks_;
4267 size_t index_;
4268
4269 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4270};
4271
Mingyao Yang3584bce2015-05-19 16:01:59 -07004272// Iterator over the blocks that art part of the loop. Includes blocks part
4273// of an inner loop. The order in which the blocks are iterated is reverse
4274// post order.
4275class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4276 public:
4277 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4278 : blocks_in_loop_(info.GetBlocks()),
4279 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4280 index_(0) {
4281 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4282 Advance();
4283 }
4284 }
4285
4286 bool Done() const { return index_ == blocks_.Size(); }
4287 HBasicBlock* Current() const { return blocks_.Get(index_); }
4288 void Advance() {
4289 ++index_;
4290 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4291 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4292 break;
4293 }
4294 }
4295 }
4296
4297 private:
4298 const BitVector& blocks_in_loop_;
4299 const GrowableArray<HBasicBlock*>& blocks_;
4300 size_t index_;
4301
4302 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4303};
4304
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004305inline int64_t Int64FromConstant(HConstant* constant) {
4306 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4307 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4308 : constant->AsLongConstant()->GetValue();
4309}
4310
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004311} // namespace art
4312
4313#endif // ART_COMPILER_OPTIMIZING_NODES_H_