blob: 78ef13e039109689fdccae58ca41afadfd42a661 [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
Alexey Frunze4dda3372015-06-01 18:31:49 -07002193 HCompare(Primitive::Type type,
2194 HInstruction* first,
2195 HInstruction* second,
2196 Bias bias,
2197 uint32_t dex_pc)
2198 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002199 DCHECK_EQ(type, first->GetType());
2200 DCHECK_EQ(type, second->GetType());
2201 }
2202
Calin Juravleddb7df22014-11-25 20:56:51 +00002203 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002204 return
2205 x == y ? 0 :
2206 x > y ? 1 :
2207 -1;
2208 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002209
2210 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002211 return
2212 x == y ? 0 :
2213 x > y ? 1 :
2214 -1;
2215 }
2216
Calin Juravleddb7df22014-11-25 20:56:51 +00002217 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2218 return bias_ == other->AsCompare()->bias_;
2219 }
2220
2221 bool IsGtBias() { return bias_ == kGtBias; }
2222
Alexey Frunze4dda3372015-06-01 18:31:49 -07002223 uint32_t GetDexPc() const { return dex_pc_; }
2224
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002225 DECLARE_INSTRUCTION(Compare);
2226
2227 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002228 const Bias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002229 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002230
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002231 DISALLOW_COPY_AND_ASSIGN(HCompare);
2232};
2233
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002234// A local in the graph. Corresponds to a Dex register.
2235class HLocal : public HTemplateInstruction<0> {
2236 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002237 explicit HLocal(uint16_t reg_number)
2238 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002239
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002240 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002241
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002242 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002243
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002244 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002245 // The Dex register number.
2246 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002247
2248 DISALLOW_COPY_AND_ASSIGN(HLocal);
2249};
2250
2251// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002252class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002253 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002254 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002255 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002256 SetRawInputAt(0, local);
2257 }
2258
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002259 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2260
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002261 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002262
2263 private:
2264 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2265};
2266
2267// Store a value in a given local. This instruction has two inputs: the value
2268// and the local.
2269class HStoreLocal : public HTemplateInstruction<2> {
2270 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002271 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002272 SetRawInputAt(0, local);
2273 SetRawInputAt(1, value);
2274 }
2275
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002276 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2277
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002278 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002279
2280 private:
2281 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2282};
2283
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002284class HConstant : public HExpression<0> {
2285 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002286 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2287
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002288 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002289
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002290 virtual bool IsMinusOne() const { return false; }
2291 virtual bool IsZero() const { return false; }
2292 virtual bool IsOne() const { return false; }
2293
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002294 DECLARE_INSTRUCTION(Constant);
2295
2296 private:
2297 DISALLOW_COPY_AND_ASSIGN(HConstant);
2298};
2299
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002300class HFloatConstant : public HConstant {
2301 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002302 float GetValue() const { return value_; }
2303
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002304 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002305 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2306 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002307 }
2308
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002309 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002310
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002311 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002312 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002313 }
2314 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002315 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002316 }
2317 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002318 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2319 }
2320 bool IsNaN() const {
2321 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002322 }
2323
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002324 DECLARE_INSTRUCTION(FloatConstant);
2325
2326 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002327 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002328 explicit HFloatConstant(int32_t value)
2329 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002330
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002331 const float value_;
2332
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002333 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002334 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002335 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002336 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2337};
2338
2339class HDoubleConstant : public HConstant {
2340 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002341 double GetValue() const { return value_; }
2342
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002343 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002344 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2345 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002346 }
2347
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002348 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002349
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002350 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002351 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002352 }
2353 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002354 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002355 }
2356 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002357 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2358 }
2359 bool IsNaN() const {
2360 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002361 }
2362
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002363 DECLARE_INSTRUCTION(DoubleConstant);
2364
2365 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002366 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002367 explicit HDoubleConstant(int64_t value)
2368 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002369
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002370 const double value_;
2371
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002372 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002373 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002374 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002375 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2376};
2377
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002378class HNullConstant : public HConstant {
2379 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002380 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2381 return true;
2382 }
2383
2384 size_t ComputeHashCode() const OVERRIDE { return 0; }
2385
2386 DECLARE_INSTRUCTION(NullConstant);
2387
2388 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002389 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2390
2391 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002392 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2393};
2394
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002395// Constants of the type int. Those can be from Dex instructions, or
2396// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002397class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002398 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002399 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002400
Calin Juravle61d544b2015-02-23 16:46:57 +00002401 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002402 return other->AsIntConstant()->value_ == value_;
2403 }
2404
Calin Juravle61d544b2015-02-23 16:46:57 +00002405 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2406
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002407 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2408 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2409 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2410
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002411 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002412
2413 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002414 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2415
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002416 const int32_t value_;
2417
David Brazdil8d5b8b22015-03-24 10:51:52 +00002418 friend class HGraph;
2419 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002420 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002421 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2422};
2423
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002424class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002425 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002426 int64_t GetValue() const { return value_; }
2427
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002428 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002429 return other->AsLongConstant()->value_ == value_;
2430 }
2431
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002432 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002433
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002434 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2435 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2436 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2437
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002438 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002439
2440 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002441 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2442
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002443 const int64_t value_;
2444
David Brazdil8d5b8b22015-03-24 10:51:52 +00002445 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002446 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2447};
2448
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002449enum class Intrinsics {
2450#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2451#include "intrinsics_list.h"
2452 kNone,
2453 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2454#undef INTRINSICS_LIST
2455#undef OPTIMIZING_INTRINSICS
2456};
2457std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2458
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002459class HInvoke : public HInstruction {
2460 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002461 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002462
2463 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2464 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002465 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002466
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002467 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002468 SetRawInputAt(index, argument);
2469 }
2470
Roland Levillain3e3d7332015-04-28 11:00:54 +01002471 // Return the number of arguments. This number can be lower than
2472 // the number of inputs returned by InputCount(), as some invoke
2473 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2474 // inputs at the end of their list of inputs.
2475 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2476
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002477 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002478
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002479 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002480
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002481 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002482 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002483
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002484 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2485
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002486 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002487 return intrinsic_;
2488 }
2489
2490 void SetIntrinsic(Intrinsics intrinsic) {
2491 intrinsic_ = intrinsic;
2492 }
2493
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002494 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002495 return GetEnvironment()->GetParent() != nullptr;
2496 }
2497
2498 bool CanThrow() const OVERRIDE { return true; }
2499
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002500 DECLARE_INSTRUCTION(Invoke);
2501
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002502 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002503 HInvoke(ArenaAllocator* arena,
2504 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002505 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002506 Primitive::Type return_type,
2507 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002508 uint32_t dex_method_index,
2509 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002510 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002511 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002512 inputs_(arena, number_of_arguments),
2513 return_type_(return_type),
2514 dex_pc_(dex_pc),
2515 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002516 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002517 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002518 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2519 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002520 }
2521
David Brazdil1abb4192015-02-17 18:33:36 +00002522 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2523 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2524 inputs_.Put(index, input);
2525 }
2526
Roland Levillain3e3d7332015-04-28 11:00:54 +01002527 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002528 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002529 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002530 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002531 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002532 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002533 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002534
2535 private:
2536 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2537};
2538
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002539class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002540 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002541 // Requirements of this method call regarding the class
2542 // initialization (clinit) check of its declaring class.
2543 enum class ClinitCheckRequirement {
2544 kNone, // Class already initialized.
2545 kExplicit, // Static call having explicit clinit check as last input.
2546 kImplicit, // Static call implicitly requiring a clinit check.
2547 };
2548
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002549 HInvokeStaticOrDirect(ArenaAllocator* arena,
2550 uint32_t number_of_arguments,
2551 Primitive::Type return_type,
2552 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002553 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002554 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002555 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002556 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002557 InvokeType invoke_type,
2558 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002559 : HInvoke(arena,
2560 number_of_arguments,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002561 // There is one extra argument for the HCurrentMethod node, and
2562 // potentially one other if the clinit check is explicit.
2563 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002564 return_type,
2565 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002566 dex_method_index,
2567 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002568 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002569 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002570 clinit_check_requirement_(clinit_check_requirement),
2571 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002572
Calin Juravle641547a2015-04-21 22:08:51 +01002573 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2574 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002575 // We access the method via the dex cache so we can't do an implicit null check.
2576 // TODO: for intrinsics we can generate implicit null checks.
2577 return false;
2578 }
2579
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002580 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002581 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002582 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002583 bool IsStringInit() const { return string_init_offset_ != 0; }
2584 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002585 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002586
Roland Levillain4c0eb422015-04-24 16:43:49 +01002587 // Is this instruction a call to a static method?
2588 bool IsStatic() const {
2589 return GetInvokeType() == kStatic;
2590 }
2591
Roland Levillain3e3d7332015-04-28 11:00:54 +01002592 // Remove the art::HLoadClass instruction set as last input by
2593 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2594 // the initial art::HClinitCheck instruction (only relevant for
2595 // static calls with explicit clinit check).
2596 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002597 DCHECK(IsStaticWithExplicitClinitCheck());
2598 size_t last_input_index = InputCount() - 1;
2599 HInstruction* last_input = InputAt(last_input_index);
2600 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002601 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002602 RemoveAsUserOfInput(last_input_index);
2603 inputs_.DeleteAt(last_input_index);
2604 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2605 DCHECK(IsStaticWithImplicitClinitCheck());
2606 }
2607
2608 // Is this a call to a static method whose declaring class has an
2609 // explicit intialization check in the graph?
2610 bool IsStaticWithExplicitClinitCheck() const {
2611 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2612 }
2613
2614 // Is this a call to a static method whose declaring class has an
2615 // implicit intialization check requirement?
2616 bool IsStaticWithImplicitClinitCheck() const {
2617 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2618 }
2619
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002620 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002621
Roland Levillain4c0eb422015-04-24 16:43:49 +01002622 protected:
2623 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2624 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2625 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2626 HInstruction* input = input_record.GetInstruction();
2627 // `input` is the last input of a static invoke marked as having
2628 // an explicit clinit check. It must either be:
2629 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2630 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2631 DCHECK(input != nullptr);
2632 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2633 }
2634 return input_record;
2635 }
2636
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002637 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002638 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002639 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002640 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002641 // Thread entrypoint offset for string init method if this is a string init invoke.
2642 // Note that there are multiple string init methods, each having its own offset.
2643 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002644
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002645 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002646};
2647
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002648class HInvokeVirtual : public HInvoke {
2649 public:
2650 HInvokeVirtual(ArenaAllocator* arena,
2651 uint32_t number_of_arguments,
2652 Primitive::Type return_type,
2653 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002654 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002655 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002656 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002657 vtable_index_(vtable_index) {}
2658
Calin Juravle641547a2015-04-21 22:08:51 +01002659 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002660 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002661 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002662 }
2663
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002664 uint32_t GetVTableIndex() const { return vtable_index_; }
2665
2666 DECLARE_INSTRUCTION(InvokeVirtual);
2667
2668 private:
2669 const uint32_t vtable_index_;
2670
2671 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2672};
2673
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002674class HInvokeInterface : public HInvoke {
2675 public:
2676 HInvokeInterface(ArenaAllocator* arena,
2677 uint32_t number_of_arguments,
2678 Primitive::Type return_type,
2679 uint32_t dex_pc,
2680 uint32_t dex_method_index,
2681 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002682 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002683 imt_index_(imt_index) {}
2684
Calin Juravle641547a2015-04-21 22:08:51 +01002685 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002686 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002687 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002688 }
2689
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002690 uint32_t GetImtIndex() const { return imt_index_; }
2691 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2692
2693 DECLARE_INSTRUCTION(InvokeInterface);
2694
2695 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002696 const uint32_t imt_index_;
2697
2698 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2699};
2700
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002701class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002702 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002703 HNewInstance(HCurrentMethod* current_method,
2704 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002705 uint16_t type_index,
2706 const DexFile& dex_file,
2707 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002708 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2709 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002710 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002711 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002712 entrypoint_(entrypoint) {
2713 SetRawInputAt(0, current_method);
2714 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002715
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002716 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002717 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002718 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002719
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002720 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002721 bool NeedsEnvironment() const OVERRIDE { return true; }
2722 // It may throw when called on:
2723 // - interfaces
2724 // - abstract/innaccessible/unknown classes
2725 // TODO: optimize when possible.
2726 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002727
Calin Juravle10e244f2015-01-26 18:54:32 +00002728 bool CanBeNull() const OVERRIDE { return false; }
2729
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002730 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2731
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002732 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002733
2734 private:
2735 const uint32_t dex_pc_;
2736 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002737 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002738 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002739
2740 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2741};
2742
Roland Levillain88cb1752014-10-20 16:36:47 +01002743class HNeg : public HUnaryOperation {
2744 public:
2745 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2746 : HUnaryOperation(result_type, input) {}
2747
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002748 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2749 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002750
Roland Levillain88cb1752014-10-20 16:36:47 +01002751 DECLARE_INSTRUCTION(Neg);
2752
2753 private:
2754 DISALLOW_COPY_AND_ASSIGN(HNeg);
2755};
2756
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002757class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002758 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002759 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002760 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002761 uint32_t dex_pc,
2762 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002763 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002764 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002765 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2766 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002767 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002768 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002769 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002770 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002771 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002772 }
2773
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002774 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002775 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002776 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002777
2778 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002779 bool NeedsEnvironment() const OVERRIDE { return true; }
2780
Mingyao Yang0c365e62015-03-31 15:09:29 -07002781 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2782 bool CanThrow() const OVERRIDE { return true; }
2783
Calin Juravle10e244f2015-01-26 18:54:32 +00002784 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002785
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002786 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2787
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002788 DECLARE_INSTRUCTION(NewArray);
2789
2790 private:
2791 const uint32_t dex_pc_;
2792 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002793 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002794 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002795
2796 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2797};
2798
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002799class HAdd : public HBinaryOperation {
2800 public:
2801 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2802 : HBinaryOperation(result_type, left, right) {}
2803
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002804 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002805
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002806 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002807 return x + y;
2808 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002809 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002810 return x + y;
2811 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002812
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002813 DECLARE_INSTRUCTION(Add);
2814
2815 private:
2816 DISALLOW_COPY_AND_ASSIGN(HAdd);
2817};
2818
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002819class HSub : public HBinaryOperation {
2820 public:
2821 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2822 : HBinaryOperation(result_type, left, right) {}
2823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002824 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002825 return x - y;
2826 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002827 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002828 return x - y;
2829 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002830
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002831 DECLARE_INSTRUCTION(Sub);
2832
2833 private:
2834 DISALLOW_COPY_AND_ASSIGN(HSub);
2835};
2836
Calin Juravle34bacdf2014-10-07 20:23:36 +01002837class HMul : public HBinaryOperation {
2838 public:
2839 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2840 : HBinaryOperation(result_type, left, right) {}
2841
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002842 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002843
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002844 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2845 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002846
2847 DECLARE_INSTRUCTION(Mul);
2848
2849 private:
2850 DISALLOW_COPY_AND_ASSIGN(HMul);
2851};
2852
Calin Juravle7c4954d2014-10-28 16:57:40 +00002853class HDiv : public HBinaryOperation {
2854 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002855 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2856 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002857
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002858 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002859 // Our graph structure ensures we never have 0 for `y` during constant folding.
2860 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002861 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002862 return (y == -1) ? -x : x / y;
2863 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002864
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002865 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002866 DCHECK_NE(y, 0);
2867 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2868 return (y == -1) ? -x : x / y;
2869 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002870
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002871 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002872
Calin Juravle7c4954d2014-10-28 16:57:40 +00002873 DECLARE_INSTRUCTION(Div);
2874
2875 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002876 const uint32_t dex_pc_;
2877
Calin Juravle7c4954d2014-10-28 16:57:40 +00002878 DISALLOW_COPY_AND_ASSIGN(HDiv);
2879};
2880
Calin Juravlebacfec32014-11-14 15:54:36 +00002881class HRem : public HBinaryOperation {
2882 public:
2883 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2884 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002886 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002887 DCHECK_NE(y, 0);
2888 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2889 return (y == -1) ? 0 : x % y;
2890 }
2891
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002892 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002893 DCHECK_NE(y, 0);
2894 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2895 return (y == -1) ? 0 : x % y;
2896 }
2897
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002898 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002899
2900 DECLARE_INSTRUCTION(Rem);
2901
2902 private:
2903 const uint32_t dex_pc_;
2904
2905 DISALLOW_COPY_AND_ASSIGN(HRem);
2906};
2907
Calin Juravled0d48522014-11-04 16:40:20 +00002908class HDivZeroCheck : public HExpression<1> {
2909 public:
2910 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2911 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2912 SetRawInputAt(0, value);
2913 }
2914
2915 bool CanBeMoved() const OVERRIDE { return true; }
2916
2917 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2918 UNUSED(other);
2919 return true;
2920 }
2921
2922 bool NeedsEnvironment() const OVERRIDE { return true; }
2923 bool CanThrow() const OVERRIDE { return true; }
2924
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002925 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002926
2927 DECLARE_INSTRUCTION(DivZeroCheck);
2928
2929 private:
2930 const uint32_t dex_pc_;
2931
2932 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2933};
2934
Calin Juravle9aec02f2014-11-18 23:06:35 +00002935class HShl : public HBinaryOperation {
2936 public:
2937 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2938 : HBinaryOperation(result_type, left, right) {}
2939
2940 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2941 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2942
2943 DECLARE_INSTRUCTION(Shl);
2944
2945 private:
2946 DISALLOW_COPY_AND_ASSIGN(HShl);
2947};
2948
2949class HShr : public HBinaryOperation {
2950 public:
2951 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2952 : HBinaryOperation(result_type, left, right) {}
2953
2954 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2955 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2956
2957 DECLARE_INSTRUCTION(Shr);
2958
2959 private:
2960 DISALLOW_COPY_AND_ASSIGN(HShr);
2961};
2962
2963class HUShr : public HBinaryOperation {
2964 public:
2965 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2966 : HBinaryOperation(result_type, left, right) {}
2967
2968 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2969 uint32_t ux = static_cast<uint32_t>(x);
2970 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2971 return static_cast<int32_t>(ux >> uy);
2972 }
2973
2974 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2975 uint64_t ux = static_cast<uint64_t>(x);
2976 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2977 return static_cast<int64_t>(ux >> uy);
2978 }
2979
2980 DECLARE_INSTRUCTION(UShr);
2981
2982 private:
2983 DISALLOW_COPY_AND_ASSIGN(HUShr);
2984};
2985
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002986class HAnd : public HBinaryOperation {
2987 public:
2988 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2989 : HBinaryOperation(result_type, left, right) {}
2990
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002991 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002992
2993 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2994 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2995
2996 DECLARE_INSTRUCTION(And);
2997
2998 private:
2999 DISALLOW_COPY_AND_ASSIGN(HAnd);
3000};
3001
3002class HOr : public HBinaryOperation {
3003 public:
3004 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3005 : HBinaryOperation(result_type, left, right) {}
3006
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003007 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003008
3009 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3010 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3011
3012 DECLARE_INSTRUCTION(Or);
3013
3014 private:
3015 DISALLOW_COPY_AND_ASSIGN(HOr);
3016};
3017
3018class HXor : public HBinaryOperation {
3019 public:
3020 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3021 : HBinaryOperation(result_type, left, right) {}
3022
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003023 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003024
3025 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3026 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3027
3028 DECLARE_INSTRUCTION(Xor);
3029
3030 private:
3031 DISALLOW_COPY_AND_ASSIGN(HXor);
3032};
3033
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003034// The value of a parameter in this method. Its location depends on
3035// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003036class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003037 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003038 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3039 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003040
3041 uint8_t GetIndex() const { return index_; }
3042
Calin Juravle10e244f2015-01-26 18:54:32 +00003043 bool CanBeNull() const OVERRIDE { return !is_this_; }
3044
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003045 bool IsThis() const { return is_this_; }
3046
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003047 DECLARE_INSTRUCTION(ParameterValue);
3048
3049 private:
3050 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003051 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003052 const uint8_t index_;
3053
Calin Juravle10e244f2015-01-26 18:54:32 +00003054 // Whether or not the parameter value corresponds to 'this' argument.
3055 const bool is_this_;
3056
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003057 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3058};
3059
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003060class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003061 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003062 explicit HNot(Primitive::Type result_type, HInstruction* input)
3063 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003064
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003065 bool CanBeMoved() const OVERRIDE { return true; }
3066 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003067 UNUSED(other);
3068 return true;
3069 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003070
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003071 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3072 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003073
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003074 DECLARE_INSTRUCTION(Not);
3075
3076 private:
3077 DISALLOW_COPY_AND_ASSIGN(HNot);
3078};
3079
David Brazdil66d126e2015-04-03 16:02:44 +01003080class HBooleanNot : public HUnaryOperation {
3081 public:
3082 explicit HBooleanNot(HInstruction* input)
3083 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3084
3085 bool CanBeMoved() const OVERRIDE { return true; }
3086 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3087 UNUSED(other);
3088 return true;
3089 }
3090
3091 int32_t Evaluate(int32_t x) const OVERRIDE {
3092 DCHECK(IsUint<1>(x));
3093 return !x;
3094 }
3095
3096 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3097 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3098 UNREACHABLE();
3099 }
3100
3101 DECLARE_INSTRUCTION(BooleanNot);
3102
3103 private:
3104 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3105};
3106
Roland Levillaindff1f282014-11-05 14:15:05 +00003107class HTypeConversion : public HExpression<1> {
3108 public:
3109 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003110 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3111 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003112 SetRawInputAt(0, input);
3113 DCHECK_NE(input->GetType(), result_type);
3114 }
3115
3116 HInstruction* GetInput() const { return InputAt(0); }
3117 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3118 Primitive::Type GetResultType() const { return GetType(); }
3119
Roland Levillain624279f2014-12-04 11:54:28 +00003120 // Required by the x86 and ARM code generators when producing calls
3121 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003122 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003123
Roland Levillaindff1f282014-11-05 14:15:05 +00003124 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003125 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003126
Mark Mendelle82549b2015-05-06 10:55:34 -04003127 // Try to statically evaluate the conversion and return a HConstant
3128 // containing the result. If the input cannot be converted, return nullptr.
3129 HConstant* TryStaticEvaluation() const;
3130
Roland Levillaindff1f282014-11-05 14:15:05 +00003131 DECLARE_INSTRUCTION(TypeConversion);
3132
3133 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003134 const uint32_t dex_pc_;
3135
Roland Levillaindff1f282014-11-05 14:15:05 +00003136 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3137};
3138
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003139static constexpr uint32_t kNoRegNumber = -1;
3140
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003141class HPhi : public HInstruction {
3142 public:
3143 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003144 : HInstruction(SideEffects::None()),
3145 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003146 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003147 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003148 is_live_(false),
3149 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003150 inputs_.SetSize(number_of_inputs);
3151 }
3152
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003153 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3154 static Primitive::Type ToPhiType(Primitive::Type type) {
3155 switch (type) {
3156 case Primitive::kPrimBoolean:
3157 case Primitive::kPrimByte:
3158 case Primitive::kPrimShort:
3159 case Primitive::kPrimChar:
3160 return Primitive::kPrimInt;
3161 default:
3162 return type;
3163 }
3164 }
3165
Calin Juravle10e244f2015-01-26 18:54:32 +00003166 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003167
3168 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003169 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003170
Calin Juravle10e244f2015-01-26 18:54:32 +00003171 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003172 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003173
Calin Juravle10e244f2015-01-26 18:54:32 +00003174 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3175 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3176
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003177 uint32_t GetRegNumber() const { return reg_number_; }
3178
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003179 void SetDead() { is_live_ = false; }
3180 void SetLive() { is_live_ = true; }
3181 bool IsDead() const { return !is_live_; }
3182 bool IsLive() const { return is_live_; }
3183
Calin Juravlea4f88312015-04-16 12:57:19 +01003184 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3185 // An equivalent phi is a phi having the same dex register and type.
3186 // It assumes that phis with the same dex register are adjacent.
3187 HPhi* GetNextEquivalentPhiWithSameType() {
3188 HInstruction* next = GetNext();
3189 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3190 if (next->GetType() == GetType()) {
3191 return next->AsPhi();
3192 }
3193 next = next->GetNext();
3194 }
3195 return nullptr;
3196 }
3197
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003198 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003199
David Brazdil1abb4192015-02-17 18:33:36 +00003200 protected:
3201 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3202
3203 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3204 inputs_.Put(index, input);
3205 }
3206
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003207 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003208 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003209 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003210 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003211 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003212 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003213
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003214 DISALLOW_COPY_AND_ASSIGN(HPhi);
3215};
3216
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003217class HNullCheck : public HExpression<1> {
3218 public:
3219 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003220 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003221 SetRawInputAt(0, value);
3222 }
3223
Calin Juravle10e244f2015-01-26 18:54:32 +00003224 bool CanBeMoved() const OVERRIDE { return true; }
3225 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003226 UNUSED(other);
3227 return true;
3228 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003229
Calin Juravle10e244f2015-01-26 18:54:32 +00003230 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003231
Calin Juravle10e244f2015-01-26 18:54:32 +00003232 bool CanThrow() const OVERRIDE { return true; }
3233
3234 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003235
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003236 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003237
3238 DECLARE_INSTRUCTION(NullCheck);
3239
3240 private:
3241 const uint32_t dex_pc_;
3242
3243 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3244};
3245
3246class FieldInfo : public ValueObject {
3247 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003248 FieldInfo(MemberOffset field_offset,
3249 Primitive::Type field_type,
3250 bool is_volatile,
3251 uint32_t index,
3252 const DexFile& dex_file)
3253 : field_offset_(field_offset),
3254 field_type_(field_type),
3255 is_volatile_(is_volatile),
3256 index_(index),
3257 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003258
3259 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003260 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003261 uint32_t GetFieldIndex() const { return index_; }
3262 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003263 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003264
3265 private:
3266 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003267 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003268 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003269 uint32_t index_;
3270 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003271};
3272
3273class HInstanceFieldGet : public HExpression<1> {
3274 public:
3275 HInstanceFieldGet(HInstruction* value,
3276 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003277 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003278 bool is_volatile,
3279 uint32_t field_idx,
3280 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003281 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003282 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003283 SetRawInputAt(0, value);
3284 }
3285
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003286 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003287
3288 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3289 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3290 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003291 }
3292
Calin Juravle641547a2015-04-21 22:08:51 +01003293 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3294 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003295 }
3296
3297 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003298 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3299 }
3300
Calin Juravle52c48962014-12-16 17:02:57 +00003301 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003302 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003303 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003304 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003305
3306 DECLARE_INSTRUCTION(InstanceFieldGet);
3307
3308 private:
3309 const FieldInfo field_info_;
3310
3311 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3312};
3313
3314class HInstanceFieldSet : public HTemplateInstruction<2> {
3315 public:
3316 HInstanceFieldSet(HInstruction* object,
3317 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003318 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003319 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003320 bool is_volatile,
3321 uint32_t field_idx,
3322 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003323 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003324 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003325 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003326 SetRawInputAt(0, object);
3327 SetRawInputAt(1, value);
3328 }
3329
Calin Juravle641547a2015-04-21 22:08:51 +01003330 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3331 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003332 }
3333
Calin Juravle52c48962014-12-16 17:02:57 +00003334 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003335 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003336 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003337 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003338 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003339 bool GetValueCanBeNull() const { return value_can_be_null_; }
3340 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003341
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003342 DECLARE_INSTRUCTION(InstanceFieldSet);
3343
3344 private:
3345 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003346 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003347
3348 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3349};
3350
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003351class HArrayGet : public HExpression<2> {
3352 public:
3353 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003354 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003355 SetRawInputAt(0, array);
3356 SetRawInputAt(1, index);
3357 }
3358
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003359 bool CanBeMoved() const OVERRIDE { return true; }
3360 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003361 UNUSED(other);
3362 return true;
3363 }
Calin Juravle641547a2015-04-21 22:08:51 +01003364 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3365 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003366 // TODO: We can be smarter here.
3367 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3368 // which generates the implicit null check. There are cases when these can be removed
3369 // to produce better code. If we ever add optimizations to do so we should allow an
3370 // implicit check here (as long as the address falls in the first page).
3371 return false;
3372 }
3373
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003374 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003375
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003376 HInstruction* GetArray() const { return InputAt(0); }
3377 HInstruction* GetIndex() const { return InputAt(1); }
3378
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003379 DECLARE_INSTRUCTION(ArrayGet);
3380
3381 private:
3382 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3383};
3384
3385class HArraySet : public HTemplateInstruction<3> {
3386 public:
3387 HArraySet(HInstruction* array,
3388 HInstruction* index,
3389 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003390 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003391 uint32_t dex_pc)
3392 : HTemplateInstruction(SideEffects::ChangesSomething()),
3393 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003394 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003395 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3396 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003397 SetRawInputAt(0, array);
3398 SetRawInputAt(1, index);
3399 SetRawInputAt(2, value);
3400 }
3401
Calin Juravle77520bc2015-01-12 18:45:46 +00003402 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003403 // We currently always call a runtime method to catch array store
3404 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003405 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003406 }
3407
Mingyao Yang81014cb2015-06-02 03:16:27 -07003408 // Can throw ArrayStoreException.
3409 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3410
Calin Juravle641547a2015-04-21 22:08:51 +01003411 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3412 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003413 // TODO: Same as for ArrayGet.
3414 return false;
3415 }
3416
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003417 void ClearNeedsTypeCheck() {
3418 needs_type_check_ = false;
3419 }
3420
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003421 void ClearValueCanBeNull() {
3422 value_can_be_null_ = false;
3423 }
3424
3425 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003426 bool NeedsTypeCheck() const { return needs_type_check_; }
3427
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003428 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003429
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003430 HInstruction* GetArray() const { return InputAt(0); }
3431 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003432 HInstruction* GetValue() const { return InputAt(2); }
3433
3434 Primitive::Type GetComponentType() const {
3435 // The Dex format does not type floating point index operations. Since the
3436 // `expected_component_type_` is set during building and can therefore not
3437 // be correct, we also check what is the value type. If it is a floating
3438 // point type, we must use that type.
3439 Primitive::Type value_type = GetValue()->GetType();
3440 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3441 ? value_type
3442 : expected_component_type_;
3443 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003444
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003445 DECLARE_INSTRUCTION(ArraySet);
3446
3447 private:
3448 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003449 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003450 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003451 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003452
3453 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3454};
3455
3456class HArrayLength : public HExpression<1> {
3457 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003458 explicit HArrayLength(HInstruction* array)
3459 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3460 // Note that arrays do not change length, so the instruction does not
3461 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003462 SetRawInputAt(0, array);
3463 }
3464
Calin Juravle77520bc2015-01-12 18:45:46 +00003465 bool CanBeMoved() const OVERRIDE { return true; }
3466 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003467 UNUSED(other);
3468 return true;
3469 }
Calin Juravle641547a2015-04-21 22:08:51 +01003470 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3471 return obj == InputAt(0);
3472 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003473
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003474 DECLARE_INSTRUCTION(ArrayLength);
3475
3476 private:
3477 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3478};
3479
3480class HBoundsCheck : public HExpression<2> {
3481 public:
3482 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003483 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003484 DCHECK(index->GetType() == Primitive::kPrimInt);
3485 SetRawInputAt(0, index);
3486 SetRawInputAt(1, length);
3487 }
3488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003489 bool CanBeMoved() const OVERRIDE { return true; }
3490 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003491 UNUSED(other);
3492 return true;
3493 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003494
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003495 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003496
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003497 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003498
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003499 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003500
3501 DECLARE_INSTRUCTION(BoundsCheck);
3502
3503 private:
3504 const uint32_t dex_pc_;
3505
3506 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3507};
3508
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003509/**
3510 * Some DEX instructions are folded into multiple HInstructions that need
3511 * to stay live until the last HInstruction. This class
3512 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003513 * HInstruction stays live. `index` represents the stack location index of the
3514 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003515 */
3516class HTemporary : public HTemplateInstruction<0> {
3517 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003518 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003519
3520 size_t GetIndex() const { return index_; }
3521
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003522 Primitive::Type GetType() const OVERRIDE {
3523 // The previous instruction is the one that will be stored in the temporary location.
3524 DCHECK(GetPrevious() != nullptr);
3525 return GetPrevious()->GetType();
3526 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003527
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003528 DECLARE_INSTRUCTION(Temporary);
3529
3530 private:
3531 const size_t index_;
3532
3533 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3534};
3535
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003536class HSuspendCheck : public HTemplateInstruction<0> {
3537 public:
3538 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003539 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003540
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003541 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003542 return true;
3543 }
3544
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003545 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003546 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3547 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003548
3549 DECLARE_INSTRUCTION(SuspendCheck);
3550
3551 private:
3552 const uint32_t dex_pc_;
3553
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003554 // Only used for code generation, in order to share the same slow path between back edges
3555 // of a same loop.
3556 SlowPathCode* slow_path_;
3557
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003558 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3559};
3560
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003561/**
3562 * Instruction to load a Class object.
3563 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003564class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003565 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003566 HLoadClass(HCurrentMethod* current_method,
3567 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003568 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003569 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003570 uint32_t dex_pc)
3571 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3572 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003573 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003574 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003575 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003576 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003577 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3578 SetRawInputAt(0, current_method);
3579 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003580
3581 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003582
3583 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3584 return other->AsLoadClass()->type_index_ == type_index_;
3585 }
3586
3587 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3588
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003589 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003590 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003591 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003592
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003593 bool NeedsEnvironment() const OVERRIDE {
3594 // Will call runtime and load the class if the class is not loaded yet.
3595 // TODO: finer grain decision.
3596 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003597 }
3598
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003599 bool MustGenerateClinitCheck() const {
3600 return generate_clinit_check_;
3601 }
3602
Calin Juravle0ba218d2015-05-19 18:46:01 +01003603 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3604 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003605 }
3606
3607 bool CanCallRuntime() const {
3608 return MustGenerateClinitCheck() || !is_referrers_class_;
3609 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003610
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003611 bool CanThrow() const OVERRIDE {
3612 // May call runtime and and therefore can throw.
3613 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003614 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003615 }
3616
Calin Juravleacf735c2015-02-12 15:25:22 +00003617 ReferenceTypeInfo GetLoadedClassRTI() {
3618 return loaded_class_rti_;
3619 }
3620
3621 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3622 // Make sure we only set exact types (the loaded class should never be merged).
3623 DCHECK(rti.IsExact());
3624 loaded_class_rti_ = rti;
3625 }
3626
3627 bool IsResolved() {
3628 return loaded_class_rti_.IsExact();
3629 }
3630
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003631 const DexFile& GetDexFile() { return dex_file_; }
3632
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003633 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3634
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003635 DECLARE_INSTRUCTION(LoadClass);
3636
3637 private:
3638 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003639 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003640 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003641 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003642 // Whether this instruction must generate the initialization check.
3643 // Used for code generation.
3644 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003645
Calin Juravleacf735c2015-02-12 15:25:22 +00003646 ReferenceTypeInfo loaded_class_rti_;
3647
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003648 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3649};
3650
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003651class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003652 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003653 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003654 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3655 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003656 dex_pc_(dex_pc) {
3657 SetRawInputAt(0, current_method);
3658 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003659
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003660 bool CanBeMoved() const OVERRIDE { return true; }
3661
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003662 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3663 return other->AsLoadString()->string_index_ == string_index_;
3664 }
3665
3666 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3667
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003668 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003669 uint32_t GetStringIndex() const { return string_index_; }
3670
3671 // TODO: Can we deopt or debug when we resolve a string?
3672 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003673 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003674
3675 DECLARE_INSTRUCTION(LoadString);
3676
3677 private:
3678 const uint32_t string_index_;
3679 const uint32_t dex_pc_;
3680
3681 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3682};
3683
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003684/**
3685 * Performs an initialization check on its Class object input.
3686 */
3687class HClinitCheck : public HExpression<1> {
3688 public:
3689 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003690 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003691 dex_pc_(dex_pc) {
3692 SetRawInputAt(0, constant);
3693 }
3694
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003695 bool CanBeMoved() const OVERRIDE { return true; }
3696 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3697 UNUSED(other);
3698 return true;
3699 }
3700
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003701 bool NeedsEnvironment() const OVERRIDE {
3702 // May call runtime to initialize the class.
3703 return true;
3704 }
3705
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003706 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003707
3708 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3709
3710 DECLARE_INSTRUCTION(ClinitCheck);
3711
3712 private:
3713 const uint32_t dex_pc_;
3714
3715 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3716};
3717
3718class HStaticFieldGet : public HExpression<1> {
3719 public:
3720 HStaticFieldGet(HInstruction* cls,
3721 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003722 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003723 bool is_volatile,
3724 uint32_t field_idx,
3725 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003726 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003727 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003728 SetRawInputAt(0, cls);
3729 }
3730
Calin Juravle52c48962014-12-16 17:02:57 +00003731
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003732 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003733
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003734 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003735 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3736 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003737 }
3738
3739 size_t ComputeHashCode() const OVERRIDE {
3740 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3741 }
3742
Calin Juravle52c48962014-12-16 17:02:57 +00003743 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003744 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3745 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003746 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003747
3748 DECLARE_INSTRUCTION(StaticFieldGet);
3749
3750 private:
3751 const FieldInfo field_info_;
3752
3753 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3754};
3755
3756class HStaticFieldSet : public HTemplateInstruction<2> {
3757 public:
3758 HStaticFieldSet(HInstruction* cls,
3759 HInstruction* value,
3760 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003761 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003762 bool is_volatile,
3763 uint32_t field_idx,
3764 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003765 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003766 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003767 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003768 SetRawInputAt(0, cls);
3769 SetRawInputAt(1, value);
3770 }
3771
Calin Juravle52c48962014-12-16 17:02:57 +00003772 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003773 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3774 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003775 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003776
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003777 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003778 bool GetValueCanBeNull() const { return value_can_be_null_; }
3779 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003780
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003781 DECLARE_INSTRUCTION(StaticFieldSet);
3782
3783 private:
3784 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003785 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003786
3787 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3788};
3789
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003790// Implement the move-exception DEX instruction.
3791class HLoadException : public HExpression<0> {
3792 public:
3793 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3794
3795 DECLARE_INSTRUCTION(LoadException);
3796
3797 private:
3798 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3799};
3800
3801class HThrow : public HTemplateInstruction<1> {
3802 public:
3803 HThrow(HInstruction* exception, uint32_t dex_pc)
3804 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3805 SetRawInputAt(0, exception);
3806 }
3807
3808 bool IsControlFlow() const OVERRIDE { return true; }
3809
3810 bool NeedsEnvironment() const OVERRIDE { return true; }
3811
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003812 bool CanThrow() const OVERRIDE { return true; }
3813
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003814 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003815
3816 DECLARE_INSTRUCTION(Throw);
3817
3818 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003819 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003820
3821 DISALLOW_COPY_AND_ASSIGN(HThrow);
3822};
3823
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003824class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003825 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003826 HInstanceOf(HInstruction* object,
3827 HLoadClass* constant,
3828 bool class_is_final,
3829 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003830 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3831 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003832 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003833 dex_pc_(dex_pc) {
3834 SetRawInputAt(0, object);
3835 SetRawInputAt(1, constant);
3836 }
3837
3838 bool CanBeMoved() const OVERRIDE { return true; }
3839
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003840 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003841 return true;
3842 }
3843
3844 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003845 return false;
3846 }
3847
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003848 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003849
3850 bool IsClassFinal() const { return class_is_final_; }
3851
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003852 // Used only in code generation.
3853 bool MustDoNullCheck() const { return must_do_null_check_; }
3854 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3855
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003856 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003857
3858 private:
3859 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003860 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003861 const uint32_t dex_pc_;
3862
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003863 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3864};
3865
Calin Juravleb1498f62015-02-16 13:13:29 +00003866class HBoundType : public HExpression<1> {
3867 public:
3868 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3869 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3870 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003871 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003872 SetRawInputAt(0, input);
3873 }
3874
3875 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3876
3877 bool CanBeNull() const OVERRIDE {
3878 // `null instanceof ClassX` always return false so we can't be null.
3879 return false;
3880 }
3881
3882 DECLARE_INSTRUCTION(BoundType);
3883
3884 private:
3885 // Encodes the most upper class that this instruction can have. In other words
3886 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3887 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3888 const ReferenceTypeInfo bound_type_;
3889
3890 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3891};
3892
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003893class HCheckCast : public HTemplateInstruction<2> {
3894 public:
3895 HCheckCast(HInstruction* object,
3896 HLoadClass* constant,
3897 bool class_is_final,
3898 uint32_t dex_pc)
3899 : HTemplateInstruction(SideEffects::None()),
3900 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003901 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003902 dex_pc_(dex_pc) {
3903 SetRawInputAt(0, object);
3904 SetRawInputAt(1, constant);
3905 }
3906
3907 bool CanBeMoved() const OVERRIDE { return true; }
3908
3909 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3910 return true;
3911 }
3912
3913 bool NeedsEnvironment() const OVERRIDE {
3914 // Instruction may throw a CheckCastError.
3915 return true;
3916 }
3917
3918 bool CanThrow() const OVERRIDE { return true; }
3919
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003920 bool MustDoNullCheck() const { return must_do_null_check_; }
3921 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3922
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003923 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003924
3925 bool IsClassFinal() const { return class_is_final_; }
3926
3927 DECLARE_INSTRUCTION(CheckCast);
3928
3929 private:
3930 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003931 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003932 const uint32_t dex_pc_;
3933
3934 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003935};
3936
Calin Juravle27df7582015-04-17 19:12:31 +01003937class HMemoryBarrier : public HTemplateInstruction<0> {
3938 public:
3939 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3940 : HTemplateInstruction(SideEffects::None()),
3941 barrier_kind_(barrier_kind) {}
3942
3943 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3944
3945 DECLARE_INSTRUCTION(MemoryBarrier);
3946
3947 private:
3948 const MemBarrierKind barrier_kind_;
3949
3950 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3951};
3952
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003953class HMonitorOperation : public HTemplateInstruction<1> {
3954 public:
3955 enum OperationKind {
3956 kEnter,
3957 kExit,
3958 };
3959
3960 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3961 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3962 SetRawInputAt(0, object);
3963 }
3964
3965 // Instruction may throw a Java exception, so we need an environment.
3966 bool NeedsEnvironment() const OVERRIDE { return true; }
3967 bool CanThrow() const OVERRIDE { return true; }
3968
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003969 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003970
3971 bool IsEnter() const { return kind_ == kEnter; }
3972
3973 DECLARE_INSTRUCTION(MonitorOperation);
3974
Calin Juravle52c48962014-12-16 17:02:57 +00003975 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003976 const OperationKind kind_;
3977 const uint32_t dex_pc_;
3978
3979 private:
3980 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3981};
3982
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003983class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003984 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003985 MoveOperands(Location source,
3986 Location destination,
3987 Primitive::Type type,
3988 HInstruction* instruction)
3989 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003990
3991 Location GetSource() const { return source_; }
3992 Location GetDestination() const { return destination_; }
3993
3994 void SetSource(Location value) { source_ = value; }
3995 void SetDestination(Location value) { destination_ = value; }
3996
3997 // The parallel move resolver marks moves as "in-progress" by clearing the
3998 // destination (but not the source).
3999 Location MarkPending() {
4000 DCHECK(!IsPending());
4001 Location dest = destination_;
4002 destination_ = Location::NoLocation();
4003 return dest;
4004 }
4005
4006 void ClearPending(Location dest) {
4007 DCHECK(IsPending());
4008 destination_ = dest;
4009 }
4010
4011 bool IsPending() const {
4012 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4013 return destination_.IsInvalid() && !source_.IsInvalid();
4014 }
4015
4016 // True if this blocks a move from the given location.
4017 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004018 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004019 }
4020
4021 // A move is redundant if it's been eliminated, if its source and
4022 // destination are the same, or if its destination is unneeded.
4023 bool IsRedundant() const {
4024 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4025 }
4026
4027 // We clear both operands to indicate move that's been eliminated.
4028 void Eliminate() {
4029 source_ = destination_ = Location::NoLocation();
4030 }
4031
4032 bool IsEliminated() const {
4033 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4034 return source_.IsInvalid();
4035 }
4036
Alexey Frunze4dda3372015-06-01 18:31:49 -07004037 Primitive::Type GetType() const { return type_; }
4038
Nicolas Geoffray90218252015-04-15 11:56:51 +01004039 bool Is64BitMove() const {
4040 return Primitive::Is64BitType(type_);
4041 }
4042
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004043 HInstruction* GetInstruction() const { return instruction_; }
4044
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004045 private:
4046 Location source_;
4047 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004048 // The type this move is for.
4049 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004050 // The instruction this move is assocatied with. Null when this move is
4051 // for moving an input in the expected locations of user (including a phi user).
4052 // This is only used in debug mode, to ensure we do not connect interval siblings
4053 // in the same parallel move.
4054 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004055};
4056
4057static constexpr size_t kDefaultNumberOfMoves = 4;
4058
4059class HParallelMove : public HTemplateInstruction<0> {
4060 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004061 explicit HParallelMove(ArenaAllocator* arena)
4062 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004063
Nicolas Geoffray90218252015-04-15 11:56:51 +01004064 void AddMove(Location source,
4065 Location destination,
4066 Primitive::Type type,
4067 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004068 DCHECK(source.IsValid());
4069 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004070 if (kIsDebugBuild) {
4071 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004072 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004073 if (moves_.Get(i).GetInstruction() == instruction) {
4074 // Special case the situation where the move is for the spill slot
4075 // of the instruction.
4076 if ((GetPrevious() == instruction)
4077 || ((GetPrevious() == nullptr)
4078 && instruction->IsPhi()
4079 && instruction->GetBlock() == GetBlock())) {
4080 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4081 << "Doing parallel moves for the same instruction.";
4082 } else {
4083 DCHECK(false) << "Doing parallel moves for the same instruction.";
4084 }
4085 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004086 }
4087 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004088 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004089 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004090 << "Overlapped destination for two moves in a parallel move: "
4091 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4092 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004093 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004094 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004095 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004096 }
4097
4098 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004099 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004100 }
4101
4102 size_t NumMoves() const { return moves_.Size(); }
4103
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004104 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004105
4106 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004107 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004108
4109 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4110};
4111
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004112class HGraphVisitor : public ValueObject {
4113 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004114 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4115 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004116
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004117 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004118 virtual void VisitBasicBlock(HBasicBlock* block);
4119
Roland Levillain633021e2014-10-01 14:12:25 +01004120 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004121 void VisitInsertionOrder();
4122
Roland Levillain633021e2014-10-01 14:12:25 +01004123 // Visit the graph following dominator tree reverse post-order.
4124 void VisitReversePostOrder();
4125
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004126 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004127
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004128 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004129#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004130 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4131
4132 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4133
4134#undef DECLARE_VISIT_INSTRUCTION
4135
4136 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004137 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004138
4139 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4140};
4141
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004142class HGraphDelegateVisitor : public HGraphVisitor {
4143 public:
4144 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4145 virtual ~HGraphDelegateVisitor() {}
4146
4147 // Visit functions that delegate to to super class.
4148#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004149 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004150
4151 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4152
4153#undef DECLARE_VISIT_INSTRUCTION
4154
4155 private:
4156 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4157};
4158
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004159class HInsertionOrderIterator : public ValueObject {
4160 public:
4161 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4162
4163 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4164 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4165 void Advance() { ++index_; }
4166
4167 private:
4168 const HGraph& graph_;
4169 size_t index_;
4170
4171 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4172};
4173
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004174class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004175 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004176 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4177 // Check that reverse post order of the graph has been built.
4178 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4179 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004180
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004181 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4182 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004183 void Advance() { ++index_; }
4184
4185 private:
4186 const HGraph& graph_;
4187 size_t index_;
4188
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004189 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004190};
4191
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004192class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004193 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004194 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004195 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4196 // Check that reverse post order of the graph has been built.
4197 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4198 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004199
4200 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004201 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004202 void Advance() { --index_; }
4203
4204 private:
4205 const HGraph& graph_;
4206 size_t index_;
4207
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004208 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004209};
4210
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004211class HLinearPostOrderIterator : public ValueObject {
4212 public:
4213 explicit HLinearPostOrderIterator(const HGraph& graph)
4214 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4215
4216 bool Done() const { return index_ == 0; }
4217
4218 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4219
4220 void Advance() {
4221 --index_;
4222 DCHECK_GE(index_, 0U);
4223 }
4224
4225 private:
4226 const GrowableArray<HBasicBlock*>& order_;
4227 size_t index_;
4228
4229 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4230};
4231
4232class HLinearOrderIterator : public ValueObject {
4233 public:
4234 explicit HLinearOrderIterator(const HGraph& graph)
4235 : order_(graph.GetLinearOrder()), index_(0) {}
4236
4237 bool Done() const { return index_ == order_.Size(); }
4238 HBasicBlock* Current() const { return order_.Get(index_); }
4239 void Advance() { ++index_; }
4240
4241 private:
4242 const GrowableArray<HBasicBlock*>& order_;
4243 size_t index_;
4244
4245 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4246};
4247
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004248// Iterator over the blocks that art part of the loop. Includes blocks part
4249// of an inner loop. The order in which the blocks are iterated is on their
4250// block id.
4251class HBlocksInLoopIterator : public ValueObject {
4252 public:
4253 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4254 : blocks_in_loop_(info.GetBlocks()),
4255 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4256 index_(0) {
4257 if (!blocks_in_loop_.IsBitSet(index_)) {
4258 Advance();
4259 }
4260 }
4261
4262 bool Done() const { return index_ == blocks_.Size(); }
4263 HBasicBlock* Current() const { return blocks_.Get(index_); }
4264 void Advance() {
4265 ++index_;
4266 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4267 if (blocks_in_loop_.IsBitSet(index_)) {
4268 break;
4269 }
4270 }
4271 }
4272
4273 private:
4274 const BitVector& blocks_in_loop_;
4275 const GrowableArray<HBasicBlock*>& blocks_;
4276 size_t index_;
4277
4278 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4279};
4280
Mingyao Yang3584bce2015-05-19 16:01:59 -07004281// Iterator over the blocks that art part of the loop. Includes blocks part
4282// of an inner loop. The order in which the blocks are iterated is reverse
4283// post order.
4284class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4285 public:
4286 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4287 : blocks_in_loop_(info.GetBlocks()),
4288 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4289 index_(0) {
4290 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4291 Advance();
4292 }
4293 }
4294
4295 bool Done() const { return index_ == blocks_.Size(); }
4296 HBasicBlock* Current() const { return blocks_.Get(index_); }
4297 void Advance() {
4298 ++index_;
4299 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4300 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4301 break;
4302 }
4303 }
4304 }
4305
4306 private:
4307 const BitVector& blocks_in_loop_;
4308 const GrowableArray<HBasicBlock*>& blocks_;
4309 size_t index_;
4310
4311 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4312};
4313
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004314inline int64_t Int64FromConstant(HConstant* constant) {
4315 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4316 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4317 : constant->AsLongConstant()->GetValue();
4318}
4319
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004320} // namespace art
4321
4322#endif // ART_COMPILER_OPTIMIZING_NODES_H_