blob: 7ef69559de4d6d88e6e84c7d1facfc7e3b238869 [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.
338 if (constant == nullptr || constant->GetBlock() == nullptr) {
339 constant = new (arena_) InstructionType(value);
340 cache->Overwrite(value, constant);
341 InsertConstant(constant);
342 }
343 return constant;
344 }
345
David Brazdil8d5b8b22015-03-24 10:51:52 +0000346 void InsertConstant(HConstant* instruction);
347
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000348 // Cache a float constant into the graph. This method should only be
349 // called by the SsaBuilder when creating "equivalent" instructions.
350 void CacheFloatConstant(HFloatConstant* constant);
351
352 // See CacheFloatConstant comment.
353 void CacheDoubleConstant(HDoubleConstant* constant);
354
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000356
357 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000358 GrowableArray<HBasicBlock*> blocks_;
359
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100360 // List of blocks to perform a reverse post order tree traversal.
361 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000362
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100363 // List of blocks to perform a linear order tree traversal.
364 GrowableArray<HBasicBlock*> linear_order_;
365
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000366 HBasicBlock* entry_block_;
367 HBasicBlock* exit_block_;
368
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100369 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100370 uint16_t maximum_number_of_out_vregs_;
371
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100372 // The number of virtual registers in this method. Contains the parameters.
373 uint16_t number_of_vregs_;
374
375 // The number of virtual registers used by parameters of this method.
376 uint16_t number_of_in_vregs_;
377
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000378 // Number of vreg size slots that the temporaries use (used in baseline compiler).
379 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100380
Mark Mendell1152c922015-04-24 17:06:35 -0400381 // Has bounds checks. We can totally skip BCE if it's false.
382 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800383
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000384 // Indicates whether the graph should be compiled in a way that
385 // ensures full debuggability. If false, we can apply more
386 // aggressive optimizations that may limit the level of debugging.
387 const bool debuggable_;
388
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000389 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000390 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000391
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100392 // The dex file from which the method is from.
393 const DexFile& dex_file_;
394
395 // The method index in the dex file.
396 const uint32_t method_idx_;
397
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100398 // If inlined, this encodes how the callee is being invoked.
399 const InvokeType invoke_type_;
400
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100401 // Whether the graph has been transformed to SSA form. Only used
402 // in debug mode to ensure we are not using properties only valid
403 // for non-SSA form (like the number of temporaries).
404 bool in_ssa_form_;
405
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100406 const bool should_generate_constructor_barrier_;
407
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 const InstructionSet instruction_set_;
409
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000410 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000411 HNullConstant* cached_null_constant_;
412 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000413 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000414 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000415 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000416
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100417 HCurrentMethod* cached_current_method_;
418
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000419 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100420 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000421 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000422 DISALLOW_COPY_AND_ASSIGN(HGraph);
423};
424
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700425class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000426 public:
427 HLoopInformation(HBasicBlock* header, HGraph* graph)
428 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100429 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100430 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100431 // Make bit vector growable, as the number of blocks may change.
432 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100433
434 HBasicBlock* GetHeader() const {
435 return header_;
436 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000437
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000438 void SetHeader(HBasicBlock* block) {
439 header_ = block;
440 }
441
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100442 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
443 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
444 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
445
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000446 void AddBackEdge(HBasicBlock* back_edge) {
447 back_edges_.Add(back_edge);
448 }
449
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100450 void RemoveBackEdge(HBasicBlock* back_edge) {
451 back_edges_.Delete(back_edge);
452 }
453
David Brazdil46e2a392015-03-16 17:31:52 +0000454 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100455 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000456 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100457 }
458 return false;
459 }
460
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000461 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000462 return back_edges_.Size();
463 }
464
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100465 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100466
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100467 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
468 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100469 }
470
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100471 // Returns the lifetime position of the back edge that has the
472 // greatest lifetime position.
473 size_t GetLifetimeEnd() const;
474
475 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
476 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
477 if (back_edges_.Get(i) == existing) {
478 back_edges_.Put(i, new_back_edge);
479 return;
480 }
481 }
482 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100483 }
484
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100485 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100486 // that is the header dominates the back edge.
487 bool Populate();
488
David Brazdila4b8c212015-05-07 09:59:30 +0100489 // Reanalyzes the loop by removing loop info from its blocks and re-running
490 // Populate(). If there are no back edges left, the loop info is completely
491 // removed as well as its SuspendCheck instruction. It must be run on nested
492 // inner loops first.
493 void Update();
494
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100495 // Returns whether this loop information contains `block`.
496 // Note that this loop information *must* be populated before entering this function.
497 bool Contains(const HBasicBlock& block) const;
498
499 // Returns whether this loop information is an inner loop of `other`.
500 // Note that `other` *must* be populated before entering this function.
501 bool IsIn(const HLoopInformation& other) const;
502
503 const ArenaBitVector& GetBlocks() const { return blocks_; }
504
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000505 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000506 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000507
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000508 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 // Internal recursive implementation of `Populate`.
510 void PopulateRecursive(HBasicBlock* block);
511
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000512 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100513 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000514 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000516
517 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
518};
519
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100520static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100521static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100522
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000523// A block in a method. Contains the list of instructions represented
524// as a double linked list. Each block knows its predecessors and
525// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100526
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700527class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000528 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100529 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000530 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000531 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
532 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000533 loop_information_(nullptr),
534 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100535 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100536 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100537 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100538 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000539 lifetime_end_(kNoLifetime),
540 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000541
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
543 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000544 }
545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100546 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
547 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000548 }
549
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100550 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
551 return dominated_blocks_;
552 }
553
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100554 bool IsEntryBlock() const {
555 return graph_->GetEntryBlock() == this;
556 }
557
558 bool IsExitBlock() const {
559 return graph_->GetExitBlock() == this;
560 }
561
David Brazdil46e2a392015-03-16 17:31:52 +0000562 bool IsSingleGoto() const;
563
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000564 void AddBackEdge(HBasicBlock* back_edge) {
565 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000566 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100568 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000569 loop_information_->AddBackEdge(back_edge);
570 }
571
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000572 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000573 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000574
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000575 int GetBlockId() const { return block_id_; }
576 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000577
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000578 HBasicBlock* GetDominator() const { return dominator_; }
579 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100580 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100581 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000582 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
583 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
584 if (dominated_blocks_.Get(i) == existing) {
585 dominated_blocks_.Put(i, new_block);
586 return;
587 }
588 }
589 LOG(FATAL) << "Unreachable";
590 UNREACHABLE();
591 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000592
593 int NumberOfBackEdges() const {
594 return loop_information_ == nullptr
595 ? 0
596 : loop_information_->NumberOfBackEdges();
597 }
598
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100599 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
600 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100601 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100602 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100603 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
604 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000605
606 void AddSuccessor(HBasicBlock* block) {
607 successors_.Add(block);
608 block->predecessors_.Add(this);
609 }
610
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100611 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
612 size_t successor_index = GetSuccessorIndexOf(existing);
613 DCHECK_NE(successor_index, static_cast<size_t>(-1));
614 existing->RemovePredecessor(this);
615 new_block->predecessors_.Add(this);
616 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000617 }
618
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000619 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
620 size_t predecessor_index = GetPredecessorIndexOf(existing);
621 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
622 existing->RemoveSuccessor(this);
623 new_block->successors_.Add(this);
624 predecessors_.Put(predecessor_index, new_block);
625 }
626
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100627 void RemovePredecessor(HBasicBlock* block) {
628 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100629 }
630
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000631 void RemoveSuccessor(HBasicBlock* block) {
632 successors_.Delete(block);
633 }
634
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100635 void ClearAllPredecessors() {
636 predecessors_.Reset();
637 }
638
639 void AddPredecessor(HBasicBlock* block) {
640 predecessors_.Add(block);
641 block->successors_.Add(this);
642 }
643
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100644 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100645 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100646 HBasicBlock* temp = predecessors_.Get(0);
647 predecessors_.Put(0, predecessors_.Get(1));
648 predecessors_.Put(1, temp);
649 }
650
David Brazdil769c9e52015-04-27 13:54:09 +0100651 void SwapSuccessors() {
652 DCHECK_EQ(successors_.Size(), 2u);
653 HBasicBlock* temp = successors_.Get(0);
654 successors_.Put(0, successors_.Get(1));
655 successors_.Put(1, temp);
656 }
657
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100658 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
659 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
660 if (predecessors_.Get(i) == predecessor) {
661 return i;
662 }
663 }
664 return -1;
665 }
666
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100667 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
668 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
669 if (successors_.Get(i) == successor) {
670 return i;
671 }
672 }
673 return -1;
674 }
675
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000676 // Split the block into two blocks just after `cursor`. Returns the newly
677 // created block. Note that this method just updates raw block information,
678 // like predecessors, successors, dominators, and instruction list. It does not
679 // update the graph, reverse post order, loop information, nor make sure the
680 // blocks are consistent (for example ending with a control flow instruction).
681 HBasicBlock* SplitAfter(HInstruction* cursor);
682
683 // Merge `other` at the end of `this`. Successors and dominated blocks of
684 // `other` are changed to be successors and dominated blocks of `this`. Note
685 // that this method does not update the graph, reverse post order, loop
686 // information, nor make sure the blocks are consistent (for example ending
687 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100688 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689
690 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
691 // of `this` are moved to `other`.
692 // Note that this method does not update the graph, reverse post order, loop
693 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000694 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000695 void ReplaceWith(HBasicBlock* other);
696
David Brazdil2d7352b2015-04-20 14:52:42 +0100697 // Merge `other` at the end of `this`. This method updates loops, reverse post
698 // order, links to predecessors, successors, dominators and deletes the block
699 // from the graph. The two blocks must be successive, i.e. `this` the only
700 // predecessor of `other` and vice versa.
701 void MergeWith(HBasicBlock* other);
702
703 // Disconnects `this` from all its predecessors, successors and dominator,
704 // removes it from all loops it is included in and eventually from the graph.
705 // The block must not dominate any other block. Predecessors and successors
706 // are safely updated.
707 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000708
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000709 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100710 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100711 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100712 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100713 // Replace instruction `initial` with `replacement` within this block.
714 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
715 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100716 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100717 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000718 // RemoveInstruction and RemovePhi delete a given instruction from the respective
719 // instruction list. With 'ensure_safety' set to true, it verifies that the
720 // instruction is not in use and removes it from the use lists of its inputs.
721 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
722 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100723 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100724
725 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100726 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100727 }
728
Roland Levillain6b879dd2014-09-22 17:13:44 +0100729 bool IsLoopPreHeaderFirstPredecessor() const {
730 DCHECK(IsLoopHeader());
731 DCHECK(!GetPredecessors().IsEmpty());
732 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
733 }
734
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100735 HLoopInformation* GetLoopInformation() const {
736 return loop_information_;
737 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000738
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000739 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100740 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000741 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100742 void SetInLoop(HLoopInformation* info) {
743 if (IsLoopHeader()) {
744 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100745 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100746 loop_information_ = info;
747 } else if (loop_information_->Contains(*info->GetHeader())) {
748 // Block is currently part of an outer loop. Make it part of this inner loop.
749 // Note that a non loop header having a loop information means this loop information
750 // has already been populated
751 loop_information_ = info;
752 } else {
753 // Block is part of an inner loop. Do not update the loop information.
754 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
755 // at this point, because this method is being called while populating `info`.
756 }
757 }
758
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000759 // Raw update of the loop information.
760 void SetLoopInformation(HLoopInformation* info) {
761 loop_information_ = info;
762 }
763
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100764 bool IsInLoop() const { return loop_information_ != nullptr; }
765
David Brazdila4b8c212015-05-07 09:59:30 +0100766 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100767 bool Dominates(HBasicBlock* block) const;
768
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100769 size_t GetLifetimeStart() const { return lifetime_start_; }
770 size_t GetLifetimeEnd() const { return lifetime_end_; }
771
772 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
773 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
774
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100775 uint32_t GetDexPc() const { return dex_pc_; }
776
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000777 bool IsCatchBlock() const { return is_catch_block_; }
778 void SetIsCatchBlock() { is_catch_block_ = true; }
779
David Brazdil8d5b8b22015-03-24 10:51:52 +0000780 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000781 bool EndsWithIf() const;
782 bool HasSinglePhi() const;
783
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000784 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000785 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000786 GrowableArray<HBasicBlock*> predecessors_;
787 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100788 HInstructionList instructions_;
789 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000790 HLoopInformation* loop_information_;
791 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100792 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000793 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100794 // The dex program counter of the first instruction of this block.
795 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100796 size_t lifetime_start_;
797 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000798 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000800 friend class HGraph;
801 friend class HInstruction;
802
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
804};
805
David Brazdilb2bd1c52015-03-25 11:17:37 +0000806// Iterates over the LoopInformation of all loops which contain 'block'
807// from the innermost to the outermost.
808class HLoopInformationOutwardIterator : public ValueObject {
809 public:
810 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
811 : current_(block.GetLoopInformation()) {}
812
813 bool Done() const { return current_ == nullptr; }
814
815 void Advance() {
816 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100817 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000818 }
819
820 HLoopInformation* Current() const {
821 DCHECK(!Done());
822 return current_;
823 }
824
825 private:
826 HLoopInformation* current_;
827
828 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
829};
830
Alexandre Ramesef20f712015-06-09 10:29:30 +0100831#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100832 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000833 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000834 M(ArrayGet, Instruction) \
835 M(ArrayLength, Instruction) \
836 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100837 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000838 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000839 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000840 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100841 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000842 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100843 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100844 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700845 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000846 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000847 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000848 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100849 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000850 M(Exit, Instruction) \
851 M(FloatConstant, Constant) \
852 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100853 M(GreaterThan, Condition) \
854 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100855 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000856 M(InstanceFieldGet, Instruction) \
857 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000858 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100859 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000860 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000861 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100862 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000863 M(LessThan, Condition) \
864 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000865 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000866 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100867 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000868 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100869 M(Local, Instruction) \
870 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100871 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000872 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000873 M(Mul, BinaryOperation) \
874 M(Neg, UnaryOperation) \
875 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100876 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100877 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000878 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000879 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000880 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000881 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100882 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000883 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100884 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000885 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100886 M(Return, Instruction) \
887 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000888 M(Shl, BinaryOperation) \
889 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100890 M(StaticFieldGet, Instruction) \
891 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100892 M(StoreLocal, Instruction) \
893 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100894 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000895 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000896 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000897 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000898 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000899 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
Alexandre Ramesef20f712015-06-09 10:29:30 +0100901#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
902
903#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
904
905#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
906
907#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
908
909#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
910 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
911 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
912 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
913 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
914 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
915
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100916#define FOR_EACH_INSTRUCTION(M) \
917 FOR_EACH_CONCRETE_INSTRUCTION(M) \
918 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100919 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100920 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100921 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700922
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100923#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000924FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
925#undef FORWARD_DECLARATION
926
Roland Levillainccc07a92014-09-16 14:48:16 +0100927#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000928 InstructionKind GetKind() const OVERRIDE { return k##type; } \
929 const char* DebugName() const OVERRIDE { return #type; } \
930 const H##type* As##type() const OVERRIDE { return this; } \
931 H##type* As##type() OVERRIDE { return this; } \
932 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100933 return other->Is##type(); \
934 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000935 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000936
David Brazdiled596192015-01-23 10:39:45 +0000937template <typename T> class HUseList;
938
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100939template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700940class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000941 public:
David Brazdiled596192015-01-23 10:39:45 +0000942 HUseListNode* GetPrevious() const { return prev_; }
943 HUseListNode* GetNext() const { return next_; }
944 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100945 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100946 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100947
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000948 private:
David Brazdiled596192015-01-23 10:39:45 +0000949 HUseListNode(T user, size_t index)
950 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
951
952 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100953 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000954 HUseListNode<T>* prev_;
955 HUseListNode<T>* next_;
956
957 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000958
959 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
960};
961
David Brazdiled596192015-01-23 10:39:45 +0000962template <typename T>
963class HUseList : public ValueObject {
964 public:
965 HUseList() : first_(nullptr) {}
966
967 void Clear() {
968 first_ = nullptr;
969 }
970
971 // Adds a new entry at the beginning of the use list and returns
972 // the newly created node.
973 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000974 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000975 if (IsEmpty()) {
976 first_ = new_node;
977 } else {
978 first_->prev_ = new_node;
979 new_node->next_ = first_;
980 first_ = new_node;
981 }
982 return new_node;
983 }
984
985 HUseListNode<T>* GetFirst() const {
986 return first_;
987 }
988
989 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000990 DCHECK(node != nullptr);
991 DCHECK(Contains(node));
992
David Brazdiled596192015-01-23 10:39:45 +0000993 if (node->prev_ != nullptr) {
994 node->prev_->next_ = node->next_;
995 }
996 if (node->next_ != nullptr) {
997 node->next_->prev_ = node->prev_;
998 }
999 if (node == first_) {
1000 first_ = node->next_;
1001 }
1002 }
1003
David Brazdil1abb4192015-02-17 18:33:36 +00001004 bool Contains(const HUseListNode<T>* node) const {
1005 if (node == nullptr) {
1006 return false;
1007 }
1008 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1009 if (current == node) {
1010 return true;
1011 }
1012 }
1013 return false;
1014 }
1015
David Brazdiled596192015-01-23 10:39:45 +00001016 bool IsEmpty() const {
1017 return first_ == nullptr;
1018 }
1019
1020 bool HasOnlyOneUse() const {
1021 return first_ != nullptr && first_->next_ == nullptr;
1022 }
1023
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001024 size_t SizeSlow() const {
1025 size_t count = 0;
1026 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1027 ++count;
1028 }
1029 return count;
1030 }
1031
David Brazdiled596192015-01-23 10:39:45 +00001032 private:
1033 HUseListNode<T>* first_;
1034};
1035
1036template<typename T>
1037class HUseIterator : public ValueObject {
1038 public:
1039 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1040
1041 bool Done() const { return current_ == nullptr; }
1042
1043 void Advance() {
1044 DCHECK(!Done());
1045 current_ = current_->GetNext();
1046 }
1047
1048 HUseListNode<T>* Current() const {
1049 DCHECK(!Done());
1050 return current_;
1051 }
1052
1053 private:
1054 HUseListNode<T>* current_;
1055
1056 friend class HValue;
1057};
1058
David Brazdil1abb4192015-02-17 18:33:36 +00001059// This class is used by HEnvironment and HInstruction classes to record the
1060// instructions they use and pointers to the corresponding HUseListNodes kept
1061// by the used instructions.
1062template <typename T>
1063class HUserRecord : public ValueObject {
1064 public:
1065 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1066 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1067
1068 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1069 : instruction_(old_record.instruction_), use_node_(use_node) {
1070 DCHECK(instruction_ != nullptr);
1071 DCHECK(use_node_ != nullptr);
1072 DCHECK(old_record.use_node_ == nullptr);
1073 }
1074
1075 HInstruction* GetInstruction() const { return instruction_; }
1076 HUseListNode<T>* GetUseNode() const { return use_node_; }
1077
1078 private:
1079 // Instruction used by the user.
1080 HInstruction* instruction_;
1081
1082 // Corresponding entry in the use list kept by 'instruction_'.
1083 HUseListNode<T>* use_node_;
1084};
1085
Calin Juravle27df7582015-04-17 19:12:31 +01001086// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1087// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1088// flag is consider.
1089// - DependsOn suggests that there is a real dependency between side effects but it only
1090// checks DependendsOnSomething flag.
1091//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001092// Represents the side effects an instruction may have.
1093class SideEffects : public ValueObject {
1094 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001095 SideEffects() : flags_(0) {}
1096
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001097 static SideEffects None() {
1098 return SideEffects(0);
1099 }
1100
1101 static SideEffects All() {
1102 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1103 }
1104
1105 static SideEffects ChangesSomething() {
1106 return SideEffects((1 << kFlagChangesCount) - 1);
1107 }
1108
1109 static SideEffects DependsOnSomething() {
1110 int count = kFlagDependsOnCount - kFlagChangesCount;
1111 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1112 }
1113
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001114 SideEffects Union(SideEffects other) const {
1115 return SideEffects(flags_ | other.flags_);
1116 }
1117
Roland Levillain72bceff2014-09-15 18:29:00 +01001118 bool HasSideEffects() const {
1119 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1120 return (flags_ & all_bits_set) != 0;
1121 }
1122
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001123 bool HasAllSideEffects() const {
1124 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1125 return all_bits_set == (flags_ & all_bits_set);
1126 }
1127
1128 bool DependsOn(SideEffects other) const {
1129 size_t depends_flags = other.ComputeDependsFlags();
1130 return (flags_ & depends_flags) != 0;
1131 }
1132
1133 bool HasDependencies() const {
1134 int count = kFlagDependsOnCount - kFlagChangesCount;
1135 size_t all_bits_set = (1 << count) - 1;
1136 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1137 }
1138
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001139 private:
1140 static constexpr int kFlagChangesSomething = 0;
1141 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1142
1143 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1144 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1145
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001146 explicit SideEffects(size_t flags) : flags_(flags) {}
1147
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001148 size_t ComputeDependsFlags() const {
1149 return flags_ << kFlagChangesCount;
1150 }
1151
1152 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001153};
1154
David Brazdiled596192015-01-23 10:39:45 +00001155// A HEnvironment object contains the values of virtual registers at a given location.
1156class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1157 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001158 HEnvironment(ArenaAllocator* arena,
1159 size_t number_of_vregs,
1160 const DexFile& dex_file,
1161 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001162 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001163 InvokeType invoke_type,
1164 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001165 : vregs_(arena, number_of_vregs),
1166 locations_(arena, number_of_vregs),
1167 parent_(nullptr),
1168 dex_file_(dex_file),
1169 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001170 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001171 invoke_type_(invoke_type),
1172 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001173 vregs_.SetSize(number_of_vregs);
1174 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001175 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001176 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001177
1178 locations_.SetSize(number_of_vregs);
1179 for (size_t i = 0; i < number_of_vregs; ++i) {
1180 locations_.Put(i, Location());
1181 }
1182 }
1183
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001184 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001185 : HEnvironment(arena,
1186 to_copy.Size(),
1187 to_copy.GetDexFile(),
1188 to_copy.GetMethodIdx(),
1189 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001190 to_copy.GetInvokeType(),
1191 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001192
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001193 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001194 if (parent_ != nullptr) {
1195 parent_->SetAndCopyParentChain(allocator, parent);
1196 } else {
1197 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1198 parent_->CopyFrom(parent);
1199 if (parent->GetParent() != nullptr) {
1200 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1201 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001202 }
David Brazdiled596192015-01-23 10:39:45 +00001203 }
1204
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001205 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1206 void CopyFrom(HEnvironment* environment);
1207
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001208 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1209 // input to the loop phi instead. This is for inserting instructions that
1210 // require an environment (like HDeoptimization) in the loop pre-header.
1211 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001212
1213 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001214 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001215 }
1216
1217 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001218 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001219 }
1220
David Brazdil1abb4192015-02-17 18:33:36 +00001221 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001222
1223 size_t Size() const { return vregs_.Size(); }
1224
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001225 HEnvironment* GetParent() const { return parent_; }
1226
1227 void SetLocationAt(size_t index, Location location) {
1228 locations_.Put(index, location);
1229 }
1230
1231 Location GetLocationAt(size_t index) const {
1232 return locations_.Get(index);
1233 }
1234
1235 uint32_t GetDexPc() const {
1236 return dex_pc_;
1237 }
1238
1239 uint32_t GetMethodIdx() const {
1240 return method_idx_;
1241 }
1242
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001243 InvokeType GetInvokeType() const {
1244 return invoke_type_;
1245 }
1246
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001247 const DexFile& GetDexFile() const {
1248 return dex_file_;
1249 }
1250
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001251 HInstruction* GetHolder() const {
1252 return holder_;
1253 }
1254
David Brazdiled596192015-01-23 10:39:45 +00001255 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001256 // Record instructions' use entries of this environment for constant-time removal.
1257 // It should only be called by HInstruction when a new environment use is added.
1258 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1259 DCHECK(env_use->GetUser() == this);
1260 size_t index = env_use->GetIndex();
1261 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1262 }
David Brazdiled596192015-01-23 10:39:45 +00001263
David Brazdil1abb4192015-02-17 18:33:36 +00001264 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001265 GrowableArray<Location> locations_;
1266 HEnvironment* parent_;
1267 const DexFile& dex_file_;
1268 const uint32_t method_idx_;
1269 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001270 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001271
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001272 // The instruction that holds this environment. Only used in debug mode
1273 // to ensure the graph is consistent.
1274 HInstruction* const holder_;
1275
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001276 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001277
1278 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1279};
1280
Calin Juravleacf735c2015-02-12 15:25:22 +00001281class ReferenceTypeInfo : ValueObject {
1282 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001283 typedef Handle<mirror::Class> TypeHandle;
1284
1285 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1286 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1287 if (type_handle->IsObjectClass()) {
1288 // Override the type handle to be consistent with the case when we get to
1289 // Top but don't have the Object class available. It avoids having to guess
1290 // what value the type_handle has when it's Top.
1291 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1292 } else {
1293 return ReferenceTypeInfo(type_handle, is_exact, false);
1294 }
1295 }
1296
1297 static ReferenceTypeInfo CreateTop(bool is_exact) {
1298 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001299 }
1300
1301 bool IsExact() const { return is_exact_; }
1302 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001303 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1304 return !IsTop() && GetTypeHandle()->IsInterface();
1305 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001306
1307 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1308
Calin Juravleb1498f62015-02-16 13:13:29 +00001309 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001310 if (IsTop()) {
1311 // Top (equivalent for java.lang.Object) is supertype of anything.
1312 return true;
1313 }
1314 if (rti.IsTop()) {
1315 // If we get here `this` is not Top() so it can't be a supertype.
1316 return false;
1317 }
1318 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1319 }
1320
1321 // Returns true if the type information provide the same amount of details.
1322 // Note that it does not mean that the instructions have the same actual type
1323 // (e.g. tops are equal but they can be the result of a merge).
1324 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1325 if (IsExact() != rti.IsExact()) {
1326 return false;
1327 }
1328 if (IsTop() && rti.IsTop()) {
1329 // `Top` means java.lang.Object, so the types are equivalent.
1330 return true;
1331 }
1332 if (IsTop() || rti.IsTop()) {
1333 // If only one is top or object than they are not equivalent.
1334 // NB: We need this extra check because the type_handle of `Top` is invalid
1335 // and we cannot inspect its reference.
1336 return false;
1337 }
1338
1339 // Finally check the types.
1340 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1341 }
1342
1343 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001344 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1345 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1346 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1347
Calin Juravleacf735c2015-02-12 15:25:22 +00001348 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001349 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001350 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001351 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001352 bool is_exact_;
1353 // A true value here means that the object type should be java.lang.Object.
1354 // We don't have access to the corresponding mirror object every time so this
1355 // flag acts as a substitute. When true, the TypeHandle refers to a null
1356 // pointer and should not be used.
1357 bool is_top_;
1358};
1359
1360std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1361
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001362class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001363 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001364 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001365 : previous_(nullptr),
1366 next_(nullptr),
1367 block_(nullptr),
1368 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001369 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001370 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001371 locations_(nullptr),
1372 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001373 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001374 side_effects_(side_effects),
1375 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001376
Dave Allison20dfc792014-06-16 20:44:29 -07001377 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001378
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001379#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001380 enum InstructionKind {
1381 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1382 };
1383#undef DECLARE_KIND
1384
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001385 HInstruction* GetNext() const { return next_; }
1386 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001387
Calin Juravle77520bc2015-01-12 18:45:46 +00001388 HInstruction* GetNextDisregardingMoves() const;
1389 HInstruction* GetPreviousDisregardingMoves() const;
1390
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001391 HBasicBlock* GetBlock() const { return block_; }
1392 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001393 bool IsInBlock() const { return block_ != nullptr; }
1394 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001395 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001396
Roland Levillain6b879dd2014-09-22 17:13:44 +01001397 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001398 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001399
1400 virtual void Accept(HGraphVisitor* visitor) = 0;
1401 virtual const char* DebugName() const = 0;
1402
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001403 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001404 void SetRawInputAt(size_t index, HInstruction* input) {
1405 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1406 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001407
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001408 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001409 virtual uint32_t GetDexPc() const {
1410 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1411 " does not need an environment";
1412 UNREACHABLE();
1413 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001414 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001415 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001416 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001417
Calin Juravle10e244f2015-01-26 18:54:32 +00001418 // Does not apply for all instructions, but having this at top level greatly
1419 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001420 virtual bool CanBeNull() const {
1421 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1422 return true;
1423 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001424
Calin Juravle641547a2015-04-21 22:08:51 +01001425 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1426 UNUSED(obj);
1427 return false;
1428 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001429
Calin Juravleacf735c2015-02-12 15:25:22 +00001430 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001431 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001432 reference_type_info_ = reference_type_info;
1433 }
1434
Calin Juravle61d544b2015-02-23 16:46:57 +00001435 ReferenceTypeInfo GetReferenceTypeInfo() const {
1436 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1437 return reference_type_info_;
1438 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001439
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001440 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001441 DCHECK(user != nullptr);
1442 HUseListNode<HInstruction*>* use =
1443 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1444 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001445 }
1446
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001447 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001448 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001449 HUseListNode<HEnvironment*>* env_use =
1450 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1451 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001452 }
1453
David Brazdil1abb4192015-02-17 18:33:36 +00001454 void RemoveAsUserOfInput(size_t input) {
1455 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1456 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1457 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001458
David Brazdil1abb4192015-02-17 18:33:36 +00001459 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1460 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001461
David Brazdiled596192015-01-23 10:39:45 +00001462 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1463 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001464 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001465 bool HasOnlyOneNonEnvironmentUse() const {
1466 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1467 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001468
Roland Levillain6c82d402014-10-13 16:10:27 +01001469 // Does this instruction strictly dominate `other_instruction`?
1470 // Returns false if this instruction and `other_instruction` are the same.
1471 // Aborts if this instruction and `other_instruction` are both phis.
1472 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001473
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001474 int GetId() const { return id_; }
1475 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001476
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001477 int GetSsaIndex() const { return ssa_index_; }
1478 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1479 bool HasSsaIndex() const { return ssa_index_ != -1; }
1480
1481 bool HasEnvironment() const { return environment_ != nullptr; }
1482 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001483 // Set the `environment_` field. Raw because this method does not
1484 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001485 void SetRawEnvironment(HEnvironment* environment) {
1486 DCHECK(environment_ == nullptr);
1487 DCHECK_EQ(environment->GetHolder(), this);
1488 environment_ = environment;
1489 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001490
1491 // Set the environment of this instruction, copying it from `environment`. While
1492 // copying, the uses lists are being updated.
1493 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001494 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001495 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001496 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001497 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001498 if (environment->GetParent() != nullptr) {
1499 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1500 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001501 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001502
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001503 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1504 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001505 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001506 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001507 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001508 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001509 if (environment->GetParent() != nullptr) {
1510 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1511 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001512 }
1513
Nicolas Geoffray39468442014-09-02 15:17:15 +01001514 // Returns the number of entries in the environment. Typically, that is the
1515 // number of dex registers in a method. It could be more in case of inlining.
1516 size_t EnvironmentSize() const;
1517
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001518 LocationSummary* GetLocations() const { return locations_; }
1519 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001520
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001521 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001522 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001523
Alexandre Rames188d4312015-04-09 18:30:21 +01001524 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1525 // uses of this instruction by `other` are *not* updated.
1526 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1527 ReplaceWith(other);
1528 other->ReplaceInput(this, use_index);
1529 }
1530
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001531 // Move `this` instruction before `cursor`.
1532 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001533
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001534#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001535 bool Is##type() const { return (As##type() != nullptr); } \
1536 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001537 virtual H##type* As##type() { return nullptr; }
1538
1539 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1540#undef INSTRUCTION_TYPE_CHECK
1541
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001542 // Returns whether the instruction can be moved within the graph.
1543 virtual bool CanBeMoved() const { return false; }
1544
1545 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001546 virtual bool InstructionTypeEquals(HInstruction* other) const {
1547 UNUSED(other);
1548 return false;
1549 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001550
1551 // Returns whether any data encoded in the two instructions is equal.
1552 // This method does not look at the inputs. Both instructions must be
1553 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001554 virtual bool InstructionDataEquals(HInstruction* other) const {
1555 UNUSED(other);
1556 return false;
1557 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001558
1559 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001560 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001561 // 2) Their inputs are identical.
1562 bool Equals(HInstruction* other) const;
1563
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001564 virtual InstructionKind GetKind() const = 0;
1565
1566 virtual size_t ComputeHashCode() const {
1567 size_t result = GetKind();
1568 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1569 result = (result * 31) + InputAt(i)->GetId();
1570 }
1571 return result;
1572 }
1573
1574 SideEffects GetSideEffects() const { return side_effects_; }
1575
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001576 size_t GetLifetimePosition() const { return lifetime_position_; }
1577 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1578 LiveInterval* GetLiveInterval() const { return live_interval_; }
1579 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1580 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1581
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001582 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1583
1584 // Returns whether the code generation of the instruction will require to have access
1585 // to the current method. Such instructions are:
1586 // (1): Instructions that require an environment, as calling the runtime requires
1587 // to walk the stack and have the current method stored at a specific stack address.
1588 // (2): Object literals like classes and strings, that are loaded from the dex cache
1589 // fields of the current method.
1590 bool NeedsCurrentMethod() const {
1591 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1592 }
1593
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001594 virtual bool NeedsDexCache() const { return false; }
1595
David Brazdil1abb4192015-02-17 18:33:36 +00001596 protected:
1597 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1598 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1599
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001600 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001601 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1602
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001603 HInstruction* previous_;
1604 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001605 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001606
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001607 // An instruction gets an id when it is added to the graph.
1608 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001609 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001610 int id_;
1611
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001612 // When doing liveness analysis, instructions that have uses get an SSA index.
1613 int ssa_index_;
1614
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001615 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001616 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001617
1618 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001619 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001620
Nicolas Geoffray39468442014-09-02 15:17:15 +01001621 // The environment associated with this instruction. Not null if the instruction
1622 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001623 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001624
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001625 // Set by the code generator.
1626 LocationSummary* locations_;
1627
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001628 // Set by the liveness analysis.
1629 LiveInterval* live_interval_;
1630
1631 // Set by the liveness analysis, this is the position in a linear
1632 // order of blocks where this instruction's live interval start.
1633 size_t lifetime_position_;
1634
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001635 const SideEffects side_effects_;
1636
Calin Juravleacf735c2015-02-12 15:25:22 +00001637 // TODO: for primitive types this should be marked as invalid.
1638 ReferenceTypeInfo reference_type_info_;
1639
David Brazdil1abb4192015-02-17 18:33:36 +00001640 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001641 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001642 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001643 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001644 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001645
1646 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1647};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001648std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001649
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001650class HInputIterator : public ValueObject {
1651 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001652 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001653
1654 bool Done() const { return index_ == instruction_->InputCount(); }
1655 HInstruction* Current() const { return instruction_->InputAt(index_); }
1656 void Advance() { index_++; }
1657
1658 private:
1659 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001660 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001661
1662 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1663};
1664
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001665class HInstructionIterator : public ValueObject {
1666 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001667 explicit HInstructionIterator(const HInstructionList& instructions)
1668 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001669 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001670 }
1671
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001672 bool Done() const { return instruction_ == nullptr; }
1673 HInstruction* Current() const { return instruction_; }
1674 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001675 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001676 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001677 }
1678
1679 private:
1680 HInstruction* instruction_;
1681 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001682
1683 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001684};
1685
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001686class HBackwardInstructionIterator : public ValueObject {
1687 public:
1688 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1689 : instruction_(instructions.last_instruction_) {
1690 next_ = Done() ? nullptr : instruction_->GetPrevious();
1691 }
1692
1693 bool Done() const { return instruction_ == nullptr; }
1694 HInstruction* Current() const { return instruction_; }
1695 void Advance() {
1696 instruction_ = next_;
1697 next_ = Done() ? nullptr : instruction_->GetPrevious();
1698 }
1699
1700 private:
1701 HInstruction* instruction_;
1702 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001703
1704 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001705};
1706
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001707// An embedded container with N elements of type T. Used (with partial
1708// specialization for N=0) because embedded arrays cannot have size 0.
1709template<typename T, intptr_t N>
1710class EmbeddedArray {
1711 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001712 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001713
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001714 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001715
1716 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001717 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001718 return elements_[i];
1719 }
1720
1721 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001722 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001723 return elements_[i];
1724 }
1725
1726 const T& At(intptr_t i) const {
1727 return (*this)[i];
1728 }
1729
1730 void SetAt(intptr_t i, const T& val) {
1731 (*this)[i] = val;
1732 }
1733
1734 private:
1735 T elements_[N];
1736};
1737
1738template<typename T>
1739class EmbeddedArray<T, 0> {
1740 public:
1741 intptr_t length() const { return 0; }
1742 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001743 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001744 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001745 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001746 }
1747 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001748 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001749 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001750 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001751 }
1752};
1753
1754template<intptr_t N>
1755class HTemplateInstruction: public HInstruction {
1756 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001757 HTemplateInstruction<N>(SideEffects side_effects)
1758 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001759 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001760
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001761 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001762
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001763 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001764 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1765
1766 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1767 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001768 }
1769
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001770 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001771 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001772
1773 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001774};
1775
Dave Allison20dfc792014-06-16 20:44:29 -07001776template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001777class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001778 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001779 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1780 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001781 virtual ~HExpression() {}
1782
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001783 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001784
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001785 protected:
1786 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001787};
1788
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001789// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1790// instruction that branches to the exit block.
1791class HReturnVoid : public HTemplateInstruction<0> {
1792 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001793 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001794
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001795 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001796
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001797 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001798
1799 private:
1800 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1801};
1802
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001803// Represents dex's RETURN opcodes. A HReturn is a control flow
1804// instruction that branches to the exit block.
1805class HReturn : public HTemplateInstruction<1> {
1806 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001807 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001808 SetRawInputAt(0, value);
1809 }
1810
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001811 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001812
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001813 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001814
1815 private:
1816 DISALLOW_COPY_AND_ASSIGN(HReturn);
1817};
1818
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001819// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001820// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001821// exit block.
1822class HExit : public HTemplateInstruction<0> {
1823 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001824 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001826 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001827
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001828 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001829
1830 private:
1831 DISALLOW_COPY_AND_ASSIGN(HExit);
1832};
1833
1834// Jumps from one block to another.
1835class HGoto : public HTemplateInstruction<0> {
1836 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001837 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1838
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001839 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001841 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001842 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001843 }
1844
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001845 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001846
1847 private:
1848 DISALLOW_COPY_AND_ASSIGN(HGoto);
1849};
1850
Dave Allison20dfc792014-06-16 20:44:29 -07001851
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001852// Conditional branch. A block ending with an HIf instruction must have
1853// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001854class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001855 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001856 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001857 SetRawInputAt(0, input);
1858 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001859
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001860 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001861
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001862 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001863 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001864 }
1865
1866 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001867 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001868 }
1869
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001870 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001871
1872 private:
1873 DISALLOW_COPY_AND_ASSIGN(HIf);
1874};
1875
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001876// Deoptimize to interpreter, upon checking a condition.
1877class HDeoptimize : public HTemplateInstruction<1> {
1878 public:
1879 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1880 : HTemplateInstruction(SideEffects::None()),
1881 dex_pc_(dex_pc) {
1882 SetRawInputAt(0, cond);
1883 }
1884
1885 bool NeedsEnvironment() const OVERRIDE { return true; }
1886 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001887 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001888
1889 DECLARE_INSTRUCTION(Deoptimize);
1890
1891 private:
1892 uint32_t dex_pc_;
1893
1894 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1895};
1896
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001897// Represents the ArtMethod that was passed as a first argument to
1898// the method. It is used by instructions that depend on it, like
1899// instructions that work with the dex cache.
1900class HCurrentMethod : public HExpression<0> {
1901 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001902 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001903
1904 DECLARE_INSTRUCTION(CurrentMethod);
1905
1906 private:
1907 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
1908};
1909
Roland Levillain88cb1752014-10-20 16:36:47 +01001910class HUnaryOperation : public HExpression<1> {
1911 public:
1912 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1913 : HExpression(result_type, SideEffects::None()) {
1914 SetRawInputAt(0, input);
1915 }
1916
1917 HInstruction* GetInput() const { return InputAt(0); }
1918 Primitive::Type GetResultType() const { return GetType(); }
1919
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001920 bool CanBeMoved() const OVERRIDE { return true; }
1921 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001922 UNUSED(other);
1923 return true;
1924 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001925
Roland Levillain9240d6a2014-10-20 16:47:04 +01001926 // Try to statically evaluate `operation` and return a HConstant
1927 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001928 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001929 HConstant* TryStaticEvaluation() const;
1930
1931 // Apply this operation to `x`.
1932 virtual int32_t Evaluate(int32_t x) const = 0;
1933 virtual int64_t Evaluate(int64_t x) const = 0;
1934
Roland Levillain88cb1752014-10-20 16:36:47 +01001935 DECLARE_INSTRUCTION(UnaryOperation);
1936
1937 private:
1938 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1939};
1940
Dave Allison20dfc792014-06-16 20:44:29 -07001941class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001942 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001943 HBinaryOperation(Primitive::Type result_type,
1944 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001945 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001946 SetRawInputAt(0, left);
1947 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001948 }
1949
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001950 HInstruction* GetLeft() const { return InputAt(0); }
1951 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001952 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001953
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001954 virtual bool IsCommutative() const { return false; }
1955
1956 // Put constant on the right.
1957 // Returns whether order is changed.
1958 bool OrderInputsWithConstantOnTheRight() {
1959 HInstruction* left = InputAt(0);
1960 HInstruction* right = InputAt(1);
1961 if (left->IsConstant() && !right->IsConstant()) {
1962 ReplaceInput(right, 0);
1963 ReplaceInput(left, 1);
1964 return true;
1965 }
1966 return false;
1967 }
1968
1969 // Order inputs by instruction id, but favor constant on the right side.
1970 // This helps GVN for commutative ops.
1971 void OrderInputs() {
1972 DCHECK(IsCommutative());
1973 HInstruction* left = InputAt(0);
1974 HInstruction* right = InputAt(1);
1975 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1976 return;
1977 }
1978 if (OrderInputsWithConstantOnTheRight()) {
1979 return;
1980 }
1981 // Order according to instruction id.
1982 if (left->GetId() > right->GetId()) {
1983 ReplaceInput(right, 0);
1984 ReplaceInput(left, 1);
1985 }
1986 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001987
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001988 bool CanBeMoved() const OVERRIDE { return true; }
1989 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001990 UNUSED(other);
1991 return true;
1992 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001993
Roland Levillain9240d6a2014-10-20 16:47:04 +01001994 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001995 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001996 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001997 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001998
1999 // Apply this operation to `x` and `y`.
2000 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2001 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2002
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002003 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002004 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002005 HConstant* GetConstantRight() const;
2006
2007 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002008 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002009 HInstruction* GetLeastConstantLeft() const;
2010
Roland Levillainccc07a92014-09-16 14:48:16 +01002011 DECLARE_INSTRUCTION(BinaryOperation);
2012
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002013 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002014 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2015};
2016
Dave Allison20dfc792014-06-16 20:44:29 -07002017class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002018 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002019 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002020 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
2021 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002022
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002023 bool NeedsMaterialization() const { return needs_materialization_; }
2024 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002025
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002026 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002027 // `instruction`, and disregard moves in between.
2028 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002029
Dave Allison20dfc792014-06-16 20:44:29 -07002030 DECLARE_INSTRUCTION(Condition);
2031
2032 virtual IfCondition GetCondition() const = 0;
2033
2034 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002035 // For register allocation purposes, returns whether this instruction needs to be
2036 // materialized (that is, not just be in the processor flags).
2037 bool needs_materialization_;
2038
Dave Allison20dfc792014-06-16 20:44:29 -07002039 DISALLOW_COPY_AND_ASSIGN(HCondition);
2040};
2041
2042// Instruction to check if two inputs are equal to each other.
2043class HEqual : public HCondition {
2044 public:
2045 HEqual(HInstruction* first, HInstruction* second)
2046 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002048 bool IsCommutative() const OVERRIDE { return true; }
2049
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002050 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002051 return x == y ? 1 : 0;
2052 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002053 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002054 return x == y ? 1 : 0;
2055 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002056
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002057 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002058
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002059 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002060 return kCondEQ;
2061 }
2062
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002063 private:
2064 DISALLOW_COPY_AND_ASSIGN(HEqual);
2065};
2066
Dave Allison20dfc792014-06-16 20:44:29 -07002067class HNotEqual : public HCondition {
2068 public:
2069 HNotEqual(HInstruction* first, HInstruction* second)
2070 : HCondition(first, second) {}
2071
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002072 bool IsCommutative() const OVERRIDE { return true; }
2073
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002074 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002075 return x != y ? 1 : 0;
2076 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002077 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002078 return x != y ? 1 : 0;
2079 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002080
Dave Allison20dfc792014-06-16 20:44:29 -07002081 DECLARE_INSTRUCTION(NotEqual);
2082
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002083 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002084 return kCondNE;
2085 }
2086
2087 private:
2088 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2089};
2090
2091class HLessThan : public HCondition {
2092 public:
2093 HLessThan(HInstruction* first, HInstruction* second)
2094 : HCondition(first, second) {}
2095
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002096 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002097 return x < y ? 1 : 0;
2098 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002099 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002100 return x < y ? 1 : 0;
2101 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002102
Dave Allison20dfc792014-06-16 20:44:29 -07002103 DECLARE_INSTRUCTION(LessThan);
2104
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002105 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002106 return kCondLT;
2107 }
2108
2109 private:
2110 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2111};
2112
2113class HLessThanOrEqual : public HCondition {
2114 public:
2115 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2116 : HCondition(first, second) {}
2117
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002118 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002119 return x <= y ? 1 : 0;
2120 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002121 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002122 return x <= y ? 1 : 0;
2123 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002124
Dave Allison20dfc792014-06-16 20:44:29 -07002125 DECLARE_INSTRUCTION(LessThanOrEqual);
2126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002127 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002128 return kCondLE;
2129 }
2130
2131 private:
2132 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2133};
2134
2135class HGreaterThan : public HCondition {
2136 public:
2137 HGreaterThan(HInstruction* first, HInstruction* second)
2138 : HCondition(first, second) {}
2139
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002140 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002141 return x > y ? 1 : 0;
2142 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002143 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002144 return x > y ? 1 : 0;
2145 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002146
Dave Allison20dfc792014-06-16 20:44:29 -07002147 DECLARE_INSTRUCTION(GreaterThan);
2148
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002149 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002150 return kCondGT;
2151 }
2152
2153 private:
2154 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2155};
2156
2157class HGreaterThanOrEqual : public HCondition {
2158 public:
2159 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2160 : HCondition(first, second) {}
2161
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002162 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002163 return x >= y ? 1 : 0;
2164 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002165 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002166 return x >= y ? 1 : 0;
2167 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002168
Dave Allison20dfc792014-06-16 20:44:29 -07002169 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2170
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002171 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002172 return kCondGE;
2173 }
2174
2175 private:
2176 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2177};
2178
2179
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002180// Instruction to check how two inputs compare to each other.
2181// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2182class HCompare : public HBinaryOperation {
2183 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002184 // The bias applies for floating point operations and indicates how NaN
2185 // comparisons are treated:
2186 enum Bias {
2187 kNoBias, // bias is not applicable (i.e. for long operation)
2188 kGtBias, // return 1 for NaN comparisons
2189 kLtBias, // return -1 for NaN comparisons
2190 };
2191
2192 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
2193 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002194 DCHECK_EQ(type, first->GetType());
2195 DCHECK_EQ(type, second->GetType());
2196 }
2197
Calin Juravleddb7df22014-11-25 20:56:51 +00002198 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002199 return
2200 x == y ? 0 :
2201 x > y ? 1 :
2202 -1;
2203 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002204
2205 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002206 return
2207 x == y ? 0 :
2208 x > y ? 1 :
2209 -1;
2210 }
2211
Calin Juravleddb7df22014-11-25 20:56:51 +00002212 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2213 return bias_ == other->AsCompare()->bias_;
2214 }
2215
2216 bool IsGtBias() { return bias_ == kGtBias; }
2217
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002218 DECLARE_INSTRUCTION(Compare);
2219
2220 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002221 const Bias bias_;
2222
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002223 DISALLOW_COPY_AND_ASSIGN(HCompare);
2224};
2225
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226// A local in the graph. Corresponds to a Dex register.
2227class HLocal : public HTemplateInstruction<0> {
2228 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002229 explicit HLocal(uint16_t reg_number)
2230 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002231
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002232 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002233
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002234 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002235
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002236 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002237 // The Dex register number.
2238 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002239
2240 DISALLOW_COPY_AND_ASSIGN(HLocal);
2241};
2242
2243// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002244class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002245 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002246 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002247 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002248 SetRawInputAt(0, local);
2249 }
2250
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002251 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2252
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002253 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002254
2255 private:
2256 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2257};
2258
2259// Store a value in a given local. This instruction has two inputs: the value
2260// and the local.
2261class HStoreLocal : public HTemplateInstruction<2> {
2262 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002263 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002264 SetRawInputAt(0, local);
2265 SetRawInputAt(1, value);
2266 }
2267
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002268 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2269
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002270 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002271
2272 private:
2273 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2274};
2275
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002276class HConstant : public HExpression<0> {
2277 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002278 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2279
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002280 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002281
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002282 virtual bool IsMinusOne() const { return false; }
2283 virtual bool IsZero() const { return false; }
2284 virtual bool IsOne() const { return false; }
2285
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002286 DECLARE_INSTRUCTION(Constant);
2287
2288 private:
2289 DISALLOW_COPY_AND_ASSIGN(HConstant);
2290};
2291
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002292class HFloatConstant : public HConstant {
2293 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002294 float GetValue() const { return value_; }
2295
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002296 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002297 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2298 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002299 }
2300
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002301 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002302
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002303 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002304 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002305 }
2306 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002307 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002308 }
2309 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002310 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2311 }
2312 bool IsNaN() const {
2313 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002314 }
2315
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002316 DECLARE_INSTRUCTION(FloatConstant);
2317
2318 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002319 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002320 explicit HFloatConstant(int32_t value)
2321 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002322
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002323 const float value_;
2324
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002325 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002326 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002327 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002328 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2329};
2330
2331class HDoubleConstant : public HConstant {
2332 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002333 double GetValue() const { return value_; }
2334
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002335 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002336 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2337 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002338 }
2339
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002340 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002341
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002342 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002343 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002344 }
2345 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002346 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002347 }
2348 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002349 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2350 }
2351 bool IsNaN() const {
2352 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002353 }
2354
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002355 DECLARE_INSTRUCTION(DoubleConstant);
2356
2357 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002358 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002359 explicit HDoubleConstant(int64_t value)
2360 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002361
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002362 const double value_;
2363
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002364 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002365 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002366 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002367 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2368};
2369
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002370class HNullConstant : public HConstant {
2371 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002372 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2373 return true;
2374 }
2375
2376 size_t ComputeHashCode() const OVERRIDE { return 0; }
2377
2378 DECLARE_INSTRUCTION(NullConstant);
2379
2380 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002381 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2382
2383 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002384 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2385};
2386
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002387// Constants of the type int. Those can be from Dex instructions, or
2388// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002389class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002390 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002391 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002392
Calin Juravle61d544b2015-02-23 16:46:57 +00002393 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002394 return other->AsIntConstant()->value_ == value_;
2395 }
2396
Calin Juravle61d544b2015-02-23 16:46:57 +00002397 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2398
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002399 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2400 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2401 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2402
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002403 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002404
2405 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002406 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2407
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002408 const int32_t value_;
2409
David Brazdil8d5b8b22015-03-24 10:51:52 +00002410 friend class HGraph;
2411 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002412 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002413 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2414};
2415
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002416class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002417 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002418 int64_t GetValue() const { return value_; }
2419
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002420 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002421 return other->AsLongConstant()->value_ == value_;
2422 }
2423
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002424 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002425
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002426 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2427 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2428 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2429
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002430 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002431
2432 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002433 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2434
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002435 const int64_t value_;
2436
David Brazdil8d5b8b22015-03-24 10:51:52 +00002437 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002438 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2439};
2440
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002441enum class Intrinsics {
2442#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2443#include "intrinsics_list.h"
2444 kNone,
2445 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2446#undef INTRINSICS_LIST
2447#undef OPTIMIZING_INTRINSICS
2448};
2449std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2450
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002451class HInvoke : public HInstruction {
2452 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002453 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002454
2455 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2456 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002457 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002458
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002459 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002460 SetRawInputAt(index, argument);
2461 }
2462
Roland Levillain3e3d7332015-04-28 11:00:54 +01002463 // Return the number of arguments. This number can be lower than
2464 // the number of inputs returned by InputCount(), as some invoke
2465 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2466 // inputs at the end of their list of inputs.
2467 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2468
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002469 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002470
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002471 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002472
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002473 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002474 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002475
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002476 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2477
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002478 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002479 return intrinsic_;
2480 }
2481
2482 void SetIntrinsic(Intrinsics intrinsic) {
2483 intrinsic_ = intrinsic;
2484 }
2485
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002486 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002487 return GetEnvironment()->GetParent() != nullptr;
2488 }
2489
2490 bool CanThrow() const OVERRIDE { return true; }
2491
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002492 DECLARE_INSTRUCTION(Invoke);
2493
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002494 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002495 HInvoke(ArenaAllocator* arena,
2496 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002497 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002498 Primitive::Type return_type,
2499 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002500 uint32_t dex_method_index,
2501 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002502 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002503 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002504 inputs_(arena, number_of_arguments),
2505 return_type_(return_type),
2506 dex_pc_(dex_pc),
2507 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002508 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002509 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002510 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2511 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002512 }
2513
David Brazdil1abb4192015-02-17 18:33:36 +00002514 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2515 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2516 inputs_.Put(index, input);
2517 }
2518
Roland Levillain3e3d7332015-04-28 11:00:54 +01002519 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002520 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002521 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002522 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002523 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002524 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002525 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002526
2527 private:
2528 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2529};
2530
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002531class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002532 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002533 // Requirements of this method call regarding the class
2534 // initialization (clinit) check of its declaring class.
2535 enum class ClinitCheckRequirement {
2536 kNone, // Class already initialized.
2537 kExplicit, // Static call having explicit clinit check as last input.
2538 kImplicit, // Static call implicitly requiring a clinit check.
2539 };
2540
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002541 HInvokeStaticOrDirect(ArenaAllocator* arena,
2542 uint32_t number_of_arguments,
2543 Primitive::Type return_type,
2544 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002545 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002546 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002547 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002548 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002549 InvokeType invoke_type,
2550 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002551 : HInvoke(arena,
2552 number_of_arguments,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002553 // There is one extra argument for the HCurrentMethod node, and
2554 // potentially one other if the clinit check is explicit.
2555 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002556 return_type,
2557 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002558 dex_method_index,
2559 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002560 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002561 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002562 clinit_check_requirement_(clinit_check_requirement),
2563 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002564
Calin Juravle641547a2015-04-21 22:08:51 +01002565 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2566 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002567 // We access the method via the dex cache so we can't do an implicit null check.
2568 // TODO: for intrinsics we can generate implicit null checks.
2569 return false;
2570 }
2571
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002572 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002573 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002574 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002575 bool IsStringInit() const { return string_init_offset_ != 0; }
2576 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002577 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002578
Roland Levillain4c0eb422015-04-24 16:43:49 +01002579 // Is this instruction a call to a static method?
2580 bool IsStatic() const {
2581 return GetInvokeType() == kStatic;
2582 }
2583
Roland Levillain3e3d7332015-04-28 11:00:54 +01002584 // Remove the art::HLoadClass instruction set as last input by
2585 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2586 // the initial art::HClinitCheck instruction (only relevant for
2587 // static calls with explicit clinit check).
2588 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002589 DCHECK(IsStaticWithExplicitClinitCheck());
2590 size_t last_input_index = InputCount() - 1;
2591 HInstruction* last_input = InputAt(last_input_index);
2592 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002593 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002594 RemoveAsUserOfInput(last_input_index);
2595 inputs_.DeleteAt(last_input_index);
2596 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2597 DCHECK(IsStaticWithImplicitClinitCheck());
2598 }
2599
2600 // Is this a call to a static method whose declaring class has an
2601 // explicit intialization check in the graph?
2602 bool IsStaticWithExplicitClinitCheck() const {
2603 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2604 }
2605
2606 // Is this a call to a static method whose declaring class has an
2607 // implicit intialization check requirement?
2608 bool IsStaticWithImplicitClinitCheck() const {
2609 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2610 }
2611
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002612 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002613
Roland Levillain4c0eb422015-04-24 16:43:49 +01002614 protected:
2615 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2616 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2617 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2618 HInstruction* input = input_record.GetInstruction();
2619 // `input` is the last input of a static invoke marked as having
2620 // an explicit clinit check. It must either be:
2621 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2622 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2623 DCHECK(input != nullptr);
2624 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2625 }
2626 return input_record;
2627 }
2628
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002629 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002630 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002631 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002632 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002633 // Thread entrypoint offset for string init method if this is a string init invoke.
2634 // Note that there are multiple string init methods, each having its own offset.
2635 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002636
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002637 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002638};
2639
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002640class HInvokeVirtual : public HInvoke {
2641 public:
2642 HInvokeVirtual(ArenaAllocator* arena,
2643 uint32_t number_of_arguments,
2644 Primitive::Type return_type,
2645 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002646 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002647 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002648 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002649 vtable_index_(vtable_index) {}
2650
Calin Juravle641547a2015-04-21 22:08:51 +01002651 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002652 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002653 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002654 }
2655
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002656 uint32_t GetVTableIndex() const { return vtable_index_; }
2657
2658 DECLARE_INSTRUCTION(InvokeVirtual);
2659
2660 private:
2661 const uint32_t vtable_index_;
2662
2663 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2664};
2665
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002666class HInvokeInterface : public HInvoke {
2667 public:
2668 HInvokeInterface(ArenaAllocator* arena,
2669 uint32_t number_of_arguments,
2670 Primitive::Type return_type,
2671 uint32_t dex_pc,
2672 uint32_t dex_method_index,
2673 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002674 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002675 imt_index_(imt_index) {}
2676
Calin Juravle641547a2015-04-21 22:08:51 +01002677 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002678 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002679 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002680 }
2681
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002682 uint32_t GetImtIndex() const { return imt_index_; }
2683 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2684
2685 DECLARE_INSTRUCTION(InvokeInterface);
2686
2687 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002688 const uint32_t imt_index_;
2689
2690 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2691};
2692
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002693class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002694 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002695 HNewInstance(HCurrentMethod* current_method,
2696 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002697 uint16_t type_index,
2698 const DexFile& dex_file,
2699 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002700 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2701 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002702 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002703 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002704 entrypoint_(entrypoint) {
2705 SetRawInputAt(0, current_method);
2706 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002707
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002708 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002709 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002710 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002711
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002712 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002713 bool NeedsEnvironment() const OVERRIDE { return true; }
2714 // It may throw when called on:
2715 // - interfaces
2716 // - abstract/innaccessible/unknown classes
2717 // TODO: optimize when possible.
2718 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002719
Calin Juravle10e244f2015-01-26 18:54:32 +00002720 bool CanBeNull() const OVERRIDE { return false; }
2721
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002722 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2723
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002724 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002725
2726 private:
2727 const uint32_t dex_pc_;
2728 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002729 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002730 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002731
2732 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2733};
2734
Roland Levillain88cb1752014-10-20 16:36:47 +01002735class HNeg : public HUnaryOperation {
2736 public:
2737 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2738 : HUnaryOperation(result_type, input) {}
2739
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002740 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2741 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002742
Roland Levillain88cb1752014-10-20 16:36:47 +01002743 DECLARE_INSTRUCTION(Neg);
2744
2745 private:
2746 DISALLOW_COPY_AND_ASSIGN(HNeg);
2747};
2748
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002749class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002750 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002751 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002752 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002753 uint32_t dex_pc,
2754 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002755 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002756 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002757 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2758 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002759 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002760 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002761 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002762 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002763 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002764 }
2765
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002766 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002767 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002768 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002769
2770 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002771 bool NeedsEnvironment() const OVERRIDE { return true; }
2772
Mingyao Yang0c365e62015-03-31 15:09:29 -07002773 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2774 bool CanThrow() const OVERRIDE { return true; }
2775
Calin Juravle10e244f2015-01-26 18:54:32 +00002776 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002777
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002778 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2779
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002780 DECLARE_INSTRUCTION(NewArray);
2781
2782 private:
2783 const uint32_t dex_pc_;
2784 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002785 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002786 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002787
2788 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2789};
2790
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002791class HAdd : public HBinaryOperation {
2792 public:
2793 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2794 : HBinaryOperation(result_type, left, right) {}
2795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002796 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002797
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002798 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002799 return x + y;
2800 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002801 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002802 return x + y;
2803 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002804
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002805 DECLARE_INSTRUCTION(Add);
2806
2807 private:
2808 DISALLOW_COPY_AND_ASSIGN(HAdd);
2809};
2810
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002811class HSub : public HBinaryOperation {
2812 public:
2813 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2814 : HBinaryOperation(result_type, left, right) {}
2815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002816 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002817 return x - y;
2818 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002819 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002820 return x - y;
2821 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002822
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002823 DECLARE_INSTRUCTION(Sub);
2824
2825 private:
2826 DISALLOW_COPY_AND_ASSIGN(HSub);
2827};
2828
Calin Juravle34bacdf2014-10-07 20:23:36 +01002829class HMul : public HBinaryOperation {
2830 public:
2831 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2832 : HBinaryOperation(result_type, left, right) {}
2833
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002834 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002835
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002836 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2837 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002838
2839 DECLARE_INSTRUCTION(Mul);
2840
2841 private:
2842 DISALLOW_COPY_AND_ASSIGN(HMul);
2843};
2844
Calin Juravle7c4954d2014-10-28 16:57:40 +00002845class HDiv : public HBinaryOperation {
2846 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002847 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2848 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002849
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002850 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002851 // Our graph structure ensures we never have 0 for `y` during constant folding.
2852 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002853 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002854 return (y == -1) ? -x : x / y;
2855 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002857 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002858 DCHECK_NE(y, 0);
2859 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2860 return (y == -1) ? -x : x / y;
2861 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002862
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002863 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002864
Calin Juravle7c4954d2014-10-28 16:57:40 +00002865 DECLARE_INSTRUCTION(Div);
2866
2867 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002868 const uint32_t dex_pc_;
2869
Calin Juravle7c4954d2014-10-28 16:57:40 +00002870 DISALLOW_COPY_AND_ASSIGN(HDiv);
2871};
2872
Calin Juravlebacfec32014-11-14 15:54:36 +00002873class HRem : public HBinaryOperation {
2874 public:
2875 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2876 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2877
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002878 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002879 DCHECK_NE(y, 0);
2880 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2881 return (y == -1) ? 0 : x % y;
2882 }
2883
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002884 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002885 DCHECK_NE(y, 0);
2886 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2887 return (y == -1) ? 0 : x % y;
2888 }
2889
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002890 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002891
2892 DECLARE_INSTRUCTION(Rem);
2893
2894 private:
2895 const uint32_t dex_pc_;
2896
2897 DISALLOW_COPY_AND_ASSIGN(HRem);
2898};
2899
Calin Juravled0d48522014-11-04 16:40:20 +00002900class HDivZeroCheck : public HExpression<1> {
2901 public:
2902 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2903 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2904 SetRawInputAt(0, value);
2905 }
2906
2907 bool CanBeMoved() const OVERRIDE { return true; }
2908
2909 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2910 UNUSED(other);
2911 return true;
2912 }
2913
2914 bool NeedsEnvironment() const OVERRIDE { return true; }
2915 bool CanThrow() const OVERRIDE { return true; }
2916
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002917 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002918
2919 DECLARE_INSTRUCTION(DivZeroCheck);
2920
2921 private:
2922 const uint32_t dex_pc_;
2923
2924 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2925};
2926
Calin Juravle9aec02f2014-11-18 23:06:35 +00002927class HShl : public HBinaryOperation {
2928 public:
2929 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2930 : HBinaryOperation(result_type, left, right) {}
2931
2932 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2933 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2934
2935 DECLARE_INSTRUCTION(Shl);
2936
2937 private:
2938 DISALLOW_COPY_AND_ASSIGN(HShl);
2939};
2940
2941class HShr : public HBinaryOperation {
2942 public:
2943 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2944 : HBinaryOperation(result_type, left, right) {}
2945
2946 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2947 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2948
2949 DECLARE_INSTRUCTION(Shr);
2950
2951 private:
2952 DISALLOW_COPY_AND_ASSIGN(HShr);
2953};
2954
2955class HUShr : public HBinaryOperation {
2956 public:
2957 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2958 : HBinaryOperation(result_type, left, right) {}
2959
2960 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2961 uint32_t ux = static_cast<uint32_t>(x);
2962 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2963 return static_cast<int32_t>(ux >> uy);
2964 }
2965
2966 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2967 uint64_t ux = static_cast<uint64_t>(x);
2968 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2969 return static_cast<int64_t>(ux >> uy);
2970 }
2971
2972 DECLARE_INSTRUCTION(UShr);
2973
2974 private:
2975 DISALLOW_COPY_AND_ASSIGN(HUShr);
2976};
2977
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002978class HAnd : public HBinaryOperation {
2979 public:
2980 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2981 : HBinaryOperation(result_type, left, right) {}
2982
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002983 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002984
2985 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2986 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2987
2988 DECLARE_INSTRUCTION(And);
2989
2990 private:
2991 DISALLOW_COPY_AND_ASSIGN(HAnd);
2992};
2993
2994class HOr : public HBinaryOperation {
2995 public:
2996 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2997 : HBinaryOperation(result_type, left, right) {}
2998
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002999 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003000
3001 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3002 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3003
3004 DECLARE_INSTRUCTION(Or);
3005
3006 private:
3007 DISALLOW_COPY_AND_ASSIGN(HOr);
3008};
3009
3010class HXor : public HBinaryOperation {
3011 public:
3012 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3013 : HBinaryOperation(result_type, left, right) {}
3014
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003015 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003016
3017 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3018 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3019
3020 DECLARE_INSTRUCTION(Xor);
3021
3022 private:
3023 DISALLOW_COPY_AND_ASSIGN(HXor);
3024};
3025
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003026// The value of a parameter in this method. Its location depends on
3027// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003028class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003029 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003030 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3031 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003032
3033 uint8_t GetIndex() const { return index_; }
3034
Calin Juravle10e244f2015-01-26 18:54:32 +00003035 bool CanBeNull() const OVERRIDE { return !is_this_; }
3036
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003037 bool IsThis() const { return is_this_; }
3038
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003039 DECLARE_INSTRUCTION(ParameterValue);
3040
3041 private:
3042 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003043 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003044 const uint8_t index_;
3045
Calin Juravle10e244f2015-01-26 18:54:32 +00003046 // Whether or not the parameter value corresponds to 'this' argument.
3047 const bool is_this_;
3048
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003049 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3050};
3051
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003052class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003053 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003054 explicit HNot(Primitive::Type result_type, HInstruction* input)
3055 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003056
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003057 bool CanBeMoved() const OVERRIDE { return true; }
3058 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003059 UNUSED(other);
3060 return true;
3061 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003062
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003063 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3064 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003065
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003066 DECLARE_INSTRUCTION(Not);
3067
3068 private:
3069 DISALLOW_COPY_AND_ASSIGN(HNot);
3070};
3071
David Brazdil66d126e2015-04-03 16:02:44 +01003072class HBooleanNot : public HUnaryOperation {
3073 public:
3074 explicit HBooleanNot(HInstruction* input)
3075 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3076
3077 bool CanBeMoved() const OVERRIDE { return true; }
3078 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3079 UNUSED(other);
3080 return true;
3081 }
3082
3083 int32_t Evaluate(int32_t x) const OVERRIDE {
3084 DCHECK(IsUint<1>(x));
3085 return !x;
3086 }
3087
3088 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3089 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3090 UNREACHABLE();
3091 }
3092
3093 DECLARE_INSTRUCTION(BooleanNot);
3094
3095 private:
3096 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3097};
3098
Roland Levillaindff1f282014-11-05 14:15:05 +00003099class HTypeConversion : public HExpression<1> {
3100 public:
3101 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003102 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3103 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003104 SetRawInputAt(0, input);
3105 DCHECK_NE(input->GetType(), result_type);
3106 }
3107
3108 HInstruction* GetInput() const { return InputAt(0); }
3109 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3110 Primitive::Type GetResultType() const { return GetType(); }
3111
Roland Levillain624279f2014-12-04 11:54:28 +00003112 // Required by the x86 and ARM code generators when producing calls
3113 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003114 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003115
Roland Levillaindff1f282014-11-05 14:15:05 +00003116 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003117 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003118
Mark Mendelle82549b2015-05-06 10:55:34 -04003119 // Try to statically evaluate the conversion and return a HConstant
3120 // containing the result. If the input cannot be converted, return nullptr.
3121 HConstant* TryStaticEvaluation() const;
3122
Roland Levillaindff1f282014-11-05 14:15:05 +00003123 DECLARE_INSTRUCTION(TypeConversion);
3124
3125 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003126 const uint32_t dex_pc_;
3127
Roland Levillaindff1f282014-11-05 14:15:05 +00003128 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3129};
3130
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003131static constexpr uint32_t kNoRegNumber = -1;
3132
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003133class HPhi : public HInstruction {
3134 public:
3135 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003136 : HInstruction(SideEffects::None()),
3137 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003138 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003139 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003140 is_live_(false),
3141 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003142 inputs_.SetSize(number_of_inputs);
3143 }
3144
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003145 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3146 static Primitive::Type ToPhiType(Primitive::Type type) {
3147 switch (type) {
3148 case Primitive::kPrimBoolean:
3149 case Primitive::kPrimByte:
3150 case Primitive::kPrimShort:
3151 case Primitive::kPrimChar:
3152 return Primitive::kPrimInt;
3153 default:
3154 return type;
3155 }
3156 }
3157
Calin Juravle10e244f2015-01-26 18:54:32 +00003158 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003159
3160 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003161 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003162
Calin Juravle10e244f2015-01-26 18:54:32 +00003163 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003164 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003165
Calin Juravle10e244f2015-01-26 18:54:32 +00003166 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3167 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3168
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003169 uint32_t GetRegNumber() const { return reg_number_; }
3170
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003171 void SetDead() { is_live_ = false; }
3172 void SetLive() { is_live_ = true; }
3173 bool IsDead() const { return !is_live_; }
3174 bool IsLive() const { return is_live_; }
3175
Calin Juravlea4f88312015-04-16 12:57:19 +01003176 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3177 // An equivalent phi is a phi having the same dex register and type.
3178 // It assumes that phis with the same dex register are adjacent.
3179 HPhi* GetNextEquivalentPhiWithSameType() {
3180 HInstruction* next = GetNext();
3181 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3182 if (next->GetType() == GetType()) {
3183 return next->AsPhi();
3184 }
3185 next = next->GetNext();
3186 }
3187 return nullptr;
3188 }
3189
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003190 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003191
David Brazdil1abb4192015-02-17 18:33:36 +00003192 protected:
3193 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3194
3195 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3196 inputs_.Put(index, input);
3197 }
3198
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003199 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003200 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003201 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003202 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003203 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003204 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003205
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003206 DISALLOW_COPY_AND_ASSIGN(HPhi);
3207};
3208
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003209class HNullCheck : public HExpression<1> {
3210 public:
3211 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003212 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003213 SetRawInputAt(0, value);
3214 }
3215
Calin Juravle10e244f2015-01-26 18:54:32 +00003216 bool CanBeMoved() const OVERRIDE { return true; }
3217 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003218 UNUSED(other);
3219 return true;
3220 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003221
Calin Juravle10e244f2015-01-26 18:54:32 +00003222 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003223
Calin Juravle10e244f2015-01-26 18:54:32 +00003224 bool CanThrow() const OVERRIDE { return true; }
3225
3226 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003227
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003228 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003229
3230 DECLARE_INSTRUCTION(NullCheck);
3231
3232 private:
3233 const uint32_t dex_pc_;
3234
3235 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3236};
3237
3238class FieldInfo : public ValueObject {
3239 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003240 FieldInfo(MemberOffset field_offset,
3241 Primitive::Type field_type,
3242 bool is_volatile,
3243 uint32_t index,
3244 const DexFile& dex_file)
3245 : field_offset_(field_offset),
3246 field_type_(field_type),
3247 is_volatile_(is_volatile),
3248 index_(index),
3249 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003250
3251 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003252 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003253 uint32_t GetFieldIndex() const { return index_; }
3254 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003255 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003256
3257 private:
3258 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003259 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003260 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003261 uint32_t index_;
3262 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003263};
3264
3265class HInstanceFieldGet : public HExpression<1> {
3266 public:
3267 HInstanceFieldGet(HInstruction* value,
3268 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003269 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003270 bool is_volatile,
3271 uint32_t field_idx,
3272 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003273 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003274 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003275 SetRawInputAt(0, value);
3276 }
3277
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003278 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003279
3280 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3281 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3282 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003283 }
3284
Calin Juravle641547a2015-04-21 22:08:51 +01003285 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3286 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003287 }
3288
3289 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003290 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3291 }
3292
Calin Juravle52c48962014-12-16 17:02:57 +00003293 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003294 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003295 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003296 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003297
3298 DECLARE_INSTRUCTION(InstanceFieldGet);
3299
3300 private:
3301 const FieldInfo field_info_;
3302
3303 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3304};
3305
3306class HInstanceFieldSet : public HTemplateInstruction<2> {
3307 public:
3308 HInstanceFieldSet(HInstruction* object,
3309 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003310 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003311 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003312 bool is_volatile,
3313 uint32_t field_idx,
3314 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003315 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003316 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003317 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003318 SetRawInputAt(0, object);
3319 SetRawInputAt(1, value);
3320 }
3321
Calin Juravle641547a2015-04-21 22:08:51 +01003322 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3323 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003324 }
3325
Calin Juravle52c48962014-12-16 17:02:57 +00003326 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003327 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003328 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003329 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003330 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003331 bool GetValueCanBeNull() const { return value_can_be_null_; }
3332 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003333
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003334 DECLARE_INSTRUCTION(InstanceFieldSet);
3335
3336 private:
3337 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003338 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003339
3340 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3341};
3342
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003343class HArrayGet : public HExpression<2> {
3344 public:
3345 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003346 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003347 SetRawInputAt(0, array);
3348 SetRawInputAt(1, index);
3349 }
3350
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003351 bool CanBeMoved() const OVERRIDE { return true; }
3352 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003353 UNUSED(other);
3354 return true;
3355 }
Calin Juravle641547a2015-04-21 22:08:51 +01003356 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3357 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003358 // TODO: We can be smarter here.
3359 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3360 // which generates the implicit null check. There are cases when these can be removed
3361 // to produce better code. If we ever add optimizations to do so we should allow an
3362 // implicit check here (as long as the address falls in the first page).
3363 return false;
3364 }
3365
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003366 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003367
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003368 HInstruction* GetArray() const { return InputAt(0); }
3369 HInstruction* GetIndex() const { return InputAt(1); }
3370
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003371 DECLARE_INSTRUCTION(ArrayGet);
3372
3373 private:
3374 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3375};
3376
3377class HArraySet : public HTemplateInstruction<3> {
3378 public:
3379 HArraySet(HInstruction* array,
3380 HInstruction* index,
3381 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003382 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003383 uint32_t dex_pc)
3384 : HTemplateInstruction(SideEffects::ChangesSomething()),
3385 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003386 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003387 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3388 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003389 SetRawInputAt(0, array);
3390 SetRawInputAt(1, index);
3391 SetRawInputAt(2, value);
3392 }
3393
Calin Juravle77520bc2015-01-12 18:45:46 +00003394 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003395 // We currently always call a runtime method to catch array store
3396 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003397 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003398 }
3399
Mingyao Yang81014cb2015-06-02 03:16:27 -07003400 // Can throw ArrayStoreException.
3401 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3402
Calin Juravle641547a2015-04-21 22:08:51 +01003403 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3404 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003405 // TODO: Same as for ArrayGet.
3406 return false;
3407 }
3408
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003409 void ClearNeedsTypeCheck() {
3410 needs_type_check_ = false;
3411 }
3412
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003413 void ClearValueCanBeNull() {
3414 value_can_be_null_ = false;
3415 }
3416
3417 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003418 bool NeedsTypeCheck() const { return needs_type_check_; }
3419
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003420 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003421
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003422 HInstruction* GetArray() const { return InputAt(0); }
3423 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003424 HInstruction* GetValue() const { return InputAt(2); }
3425
3426 Primitive::Type GetComponentType() const {
3427 // The Dex format does not type floating point index operations. Since the
3428 // `expected_component_type_` is set during building and can therefore not
3429 // be correct, we also check what is the value type. If it is a floating
3430 // point type, we must use that type.
3431 Primitive::Type value_type = GetValue()->GetType();
3432 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3433 ? value_type
3434 : expected_component_type_;
3435 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003436
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003437 DECLARE_INSTRUCTION(ArraySet);
3438
3439 private:
3440 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003441 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003442 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003443 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003444
3445 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3446};
3447
3448class HArrayLength : public HExpression<1> {
3449 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003450 explicit HArrayLength(HInstruction* array)
3451 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3452 // Note that arrays do not change length, so the instruction does not
3453 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003454 SetRawInputAt(0, array);
3455 }
3456
Calin Juravle77520bc2015-01-12 18:45:46 +00003457 bool CanBeMoved() const OVERRIDE { return true; }
3458 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003459 UNUSED(other);
3460 return true;
3461 }
Calin Juravle641547a2015-04-21 22:08:51 +01003462 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3463 return obj == InputAt(0);
3464 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003465
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003466 DECLARE_INSTRUCTION(ArrayLength);
3467
3468 private:
3469 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3470};
3471
3472class HBoundsCheck : public HExpression<2> {
3473 public:
3474 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003475 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003476 DCHECK(index->GetType() == Primitive::kPrimInt);
3477 SetRawInputAt(0, index);
3478 SetRawInputAt(1, length);
3479 }
3480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003481 bool CanBeMoved() const OVERRIDE { return true; }
3482 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003483 UNUSED(other);
3484 return true;
3485 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003486
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003487 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003489 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003490
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003491 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003492
3493 DECLARE_INSTRUCTION(BoundsCheck);
3494
3495 private:
3496 const uint32_t dex_pc_;
3497
3498 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3499};
3500
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003501/**
3502 * Some DEX instructions are folded into multiple HInstructions that need
3503 * to stay live until the last HInstruction. This class
3504 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003505 * HInstruction stays live. `index` represents the stack location index of the
3506 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003507 */
3508class HTemporary : public HTemplateInstruction<0> {
3509 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003510 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003511
3512 size_t GetIndex() const { return index_; }
3513
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003514 Primitive::Type GetType() const OVERRIDE {
3515 // The previous instruction is the one that will be stored in the temporary location.
3516 DCHECK(GetPrevious() != nullptr);
3517 return GetPrevious()->GetType();
3518 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003519
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003520 DECLARE_INSTRUCTION(Temporary);
3521
3522 private:
3523 const size_t index_;
3524
3525 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3526};
3527
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003528class HSuspendCheck : public HTemplateInstruction<0> {
3529 public:
3530 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003531 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003532
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003533 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003534 return true;
3535 }
3536
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003537 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003538 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3539 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003540
3541 DECLARE_INSTRUCTION(SuspendCheck);
3542
3543 private:
3544 const uint32_t dex_pc_;
3545
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003546 // Only used for code generation, in order to share the same slow path between back edges
3547 // of a same loop.
3548 SlowPathCode* slow_path_;
3549
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003550 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3551};
3552
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003553/**
3554 * Instruction to load a Class object.
3555 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003556class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003557 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003558 HLoadClass(HCurrentMethod* current_method,
3559 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003560 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003561 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003562 uint32_t dex_pc)
3563 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3564 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003565 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003566 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003567 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003568 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003569 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3570 SetRawInputAt(0, current_method);
3571 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003572
3573 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003574
3575 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3576 return other->AsLoadClass()->type_index_ == type_index_;
3577 }
3578
3579 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3580
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003581 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003582 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003583 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003584
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003585 bool NeedsEnvironment() const OVERRIDE {
3586 // Will call runtime and load the class if the class is not loaded yet.
3587 // TODO: finer grain decision.
3588 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003589 }
3590
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003591 bool MustGenerateClinitCheck() const {
3592 return generate_clinit_check_;
3593 }
3594
Calin Juravle0ba218d2015-05-19 18:46:01 +01003595 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3596 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003597 }
3598
3599 bool CanCallRuntime() const {
3600 return MustGenerateClinitCheck() || !is_referrers_class_;
3601 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003602
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003603 bool CanThrow() const OVERRIDE {
3604 // May call runtime and and therefore can throw.
3605 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003606 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003607 }
3608
Calin Juravleacf735c2015-02-12 15:25:22 +00003609 ReferenceTypeInfo GetLoadedClassRTI() {
3610 return loaded_class_rti_;
3611 }
3612
3613 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3614 // Make sure we only set exact types (the loaded class should never be merged).
3615 DCHECK(rti.IsExact());
3616 loaded_class_rti_ = rti;
3617 }
3618
3619 bool IsResolved() {
3620 return loaded_class_rti_.IsExact();
3621 }
3622
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003623 const DexFile& GetDexFile() { return dex_file_; }
3624
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003625 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3626
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003627 DECLARE_INSTRUCTION(LoadClass);
3628
3629 private:
3630 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003631 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003632 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003633 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003634 // Whether this instruction must generate the initialization check.
3635 // Used for code generation.
3636 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003637
Calin Juravleacf735c2015-02-12 15:25:22 +00003638 ReferenceTypeInfo loaded_class_rti_;
3639
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003640 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3641};
3642
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003643class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003644 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003645 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003646 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3647 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003648 dex_pc_(dex_pc) {
3649 SetRawInputAt(0, current_method);
3650 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003651
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003652 bool CanBeMoved() const OVERRIDE { return true; }
3653
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003654 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3655 return other->AsLoadString()->string_index_ == string_index_;
3656 }
3657
3658 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3659
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003660 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003661 uint32_t GetStringIndex() const { return string_index_; }
3662
3663 // TODO: Can we deopt or debug when we resolve a string?
3664 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003665 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003666
3667 DECLARE_INSTRUCTION(LoadString);
3668
3669 private:
3670 const uint32_t string_index_;
3671 const uint32_t dex_pc_;
3672
3673 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3674};
3675
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003676/**
3677 * Performs an initialization check on its Class object input.
3678 */
3679class HClinitCheck : public HExpression<1> {
3680 public:
3681 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003682 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003683 dex_pc_(dex_pc) {
3684 SetRawInputAt(0, constant);
3685 }
3686
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003687 bool CanBeMoved() const OVERRIDE { return true; }
3688 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3689 UNUSED(other);
3690 return true;
3691 }
3692
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003693 bool NeedsEnvironment() const OVERRIDE {
3694 // May call runtime to initialize the class.
3695 return true;
3696 }
3697
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003698 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003699
3700 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3701
3702 DECLARE_INSTRUCTION(ClinitCheck);
3703
3704 private:
3705 const uint32_t dex_pc_;
3706
3707 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3708};
3709
3710class HStaticFieldGet : public HExpression<1> {
3711 public:
3712 HStaticFieldGet(HInstruction* cls,
3713 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003714 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003715 bool is_volatile,
3716 uint32_t field_idx,
3717 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003718 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003719 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003720 SetRawInputAt(0, cls);
3721 }
3722
Calin Juravle52c48962014-12-16 17:02:57 +00003723
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003724 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003725
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003726 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003727 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3728 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003729 }
3730
3731 size_t ComputeHashCode() const OVERRIDE {
3732 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3733 }
3734
Calin Juravle52c48962014-12-16 17:02:57 +00003735 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003736 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3737 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003738 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003739
3740 DECLARE_INSTRUCTION(StaticFieldGet);
3741
3742 private:
3743 const FieldInfo field_info_;
3744
3745 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3746};
3747
3748class HStaticFieldSet : public HTemplateInstruction<2> {
3749 public:
3750 HStaticFieldSet(HInstruction* cls,
3751 HInstruction* value,
3752 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003753 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003754 bool is_volatile,
3755 uint32_t field_idx,
3756 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003757 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003758 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003759 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003760 SetRawInputAt(0, cls);
3761 SetRawInputAt(1, value);
3762 }
3763
Calin Juravle52c48962014-12-16 17:02:57 +00003764 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003765 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3766 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003767 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003768
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003769 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003770 bool GetValueCanBeNull() const { return value_can_be_null_; }
3771 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003772
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003773 DECLARE_INSTRUCTION(StaticFieldSet);
3774
3775 private:
3776 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003777 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003778
3779 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3780};
3781
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003782// Implement the move-exception DEX instruction.
3783class HLoadException : public HExpression<0> {
3784 public:
3785 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3786
3787 DECLARE_INSTRUCTION(LoadException);
3788
3789 private:
3790 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3791};
3792
3793class HThrow : public HTemplateInstruction<1> {
3794 public:
3795 HThrow(HInstruction* exception, uint32_t dex_pc)
3796 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3797 SetRawInputAt(0, exception);
3798 }
3799
3800 bool IsControlFlow() const OVERRIDE { return true; }
3801
3802 bool NeedsEnvironment() const OVERRIDE { return true; }
3803
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003804 bool CanThrow() const OVERRIDE { return true; }
3805
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003806 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003807
3808 DECLARE_INSTRUCTION(Throw);
3809
3810 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003811 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003812
3813 DISALLOW_COPY_AND_ASSIGN(HThrow);
3814};
3815
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003816class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003817 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003818 HInstanceOf(HInstruction* object,
3819 HLoadClass* constant,
3820 bool class_is_final,
3821 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003822 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3823 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003824 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003825 dex_pc_(dex_pc) {
3826 SetRawInputAt(0, object);
3827 SetRawInputAt(1, constant);
3828 }
3829
3830 bool CanBeMoved() const OVERRIDE { return true; }
3831
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003832 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003833 return true;
3834 }
3835
3836 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003837 return false;
3838 }
3839
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003840 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003841
3842 bool IsClassFinal() const { return class_is_final_; }
3843
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003844 // Used only in code generation.
3845 bool MustDoNullCheck() const { return must_do_null_check_; }
3846 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3847
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003848 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003849
3850 private:
3851 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003852 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003853 const uint32_t dex_pc_;
3854
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003855 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3856};
3857
Calin Juravleb1498f62015-02-16 13:13:29 +00003858class HBoundType : public HExpression<1> {
3859 public:
3860 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3861 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3862 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003863 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003864 SetRawInputAt(0, input);
3865 }
3866
3867 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3868
3869 bool CanBeNull() const OVERRIDE {
3870 // `null instanceof ClassX` always return false so we can't be null.
3871 return false;
3872 }
3873
3874 DECLARE_INSTRUCTION(BoundType);
3875
3876 private:
3877 // Encodes the most upper class that this instruction can have. In other words
3878 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3879 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3880 const ReferenceTypeInfo bound_type_;
3881
3882 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3883};
3884
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003885class HCheckCast : public HTemplateInstruction<2> {
3886 public:
3887 HCheckCast(HInstruction* object,
3888 HLoadClass* constant,
3889 bool class_is_final,
3890 uint32_t dex_pc)
3891 : HTemplateInstruction(SideEffects::None()),
3892 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003893 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003894 dex_pc_(dex_pc) {
3895 SetRawInputAt(0, object);
3896 SetRawInputAt(1, constant);
3897 }
3898
3899 bool CanBeMoved() const OVERRIDE { return true; }
3900
3901 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3902 return true;
3903 }
3904
3905 bool NeedsEnvironment() const OVERRIDE {
3906 // Instruction may throw a CheckCastError.
3907 return true;
3908 }
3909
3910 bool CanThrow() const OVERRIDE { return true; }
3911
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003912 bool MustDoNullCheck() const { return must_do_null_check_; }
3913 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3914
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003915 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003916
3917 bool IsClassFinal() const { return class_is_final_; }
3918
3919 DECLARE_INSTRUCTION(CheckCast);
3920
3921 private:
3922 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003923 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003924 const uint32_t dex_pc_;
3925
3926 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003927};
3928
Calin Juravle27df7582015-04-17 19:12:31 +01003929class HMemoryBarrier : public HTemplateInstruction<0> {
3930 public:
3931 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3932 : HTemplateInstruction(SideEffects::None()),
3933 barrier_kind_(barrier_kind) {}
3934
3935 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3936
3937 DECLARE_INSTRUCTION(MemoryBarrier);
3938
3939 private:
3940 const MemBarrierKind barrier_kind_;
3941
3942 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3943};
3944
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003945class HMonitorOperation : public HTemplateInstruction<1> {
3946 public:
3947 enum OperationKind {
3948 kEnter,
3949 kExit,
3950 };
3951
3952 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3953 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3954 SetRawInputAt(0, object);
3955 }
3956
3957 // Instruction may throw a Java exception, so we need an environment.
3958 bool NeedsEnvironment() const OVERRIDE { return true; }
3959 bool CanThrow() const OVERRIDE { return true; }
3960
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003961 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003962
3963 bool IsEnter() const { return kind_ == kEnter; }
3964
3965 DECLARE_INSTRUCTION(MonitorOperation);
3966
Calin Juravle52c48962014-12-16 17:02:57 +00003967 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003968 const OperationKind kind_;
3969 const uint32_t dex_pc_;
3970
3971 private:
3972 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3973};
3974
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003975class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003976 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003977 MoveOperands(Location source,
3978 Location destination,
3979 Primitive::Type type,
3980 HInstruction* instruction)
3981 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003982
3983 Location GetSource() const { return source_; }
3984 Location GetDestination() const { return destination_; }
3985
3986 void SetSource(Location value) { source_ = value; }
3987 void SetDestination(Location value) { destination_ = value; }
3988
3989 // The parallel move resolver marks moves as "in-progress" by clearing the
3990 // destination (but not the source).
3991 Location MarkPending() {
3992 DCHECK(!IsPending());
3993 Location dest = destination_;
3994 destination_ = Location::NoLocation();
3995 return dest;
3996 }
3997
3998 void ClearPending(Location dest) {
3999 DCHECK(IsPending());
4000 destination_ = dest;
4001 }
4002
4003 bool IsPending() const {
4004 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4005 return destination_.IsInvalid() && !source_.IsInvalid();
4006 }
4007
4008 // True if this blocks a move from the given location.
4009 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004010 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004011 }
4012
4013 // A move is redundant if it's been eliminated, if its source and
4014 // destination are the same, or if its destination is unneeded.
4015 bool IsRedundant() const {
4016 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4017 }
4018
4019 // We clear both operands to indicate move that's been eliminated.
4020 void Eliminate() {
4021 source_ = destination_ = Location::NoLocation();
4022 }
4023
4024 bool IsEliminated() const {
4025 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4026 return source_.IsInvalid();
4027 }
4028
Nicolas Geoffray90218252015-04-15 11:56:51 +01004029 bool Is64BitMove() const {
4030 return Primitive::Is64BitType(type_);
4031 }
4032
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004033 HInstruction* GetInstruction() const { return instruction_; }
4034
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004035 private:
4036 Location source_;
4037 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004038 // The type this move is for.
4039 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004040 // The instruction this move is assocatied with. Null when this move is
4041 // for moving an input in the expected locations of user (including a phi user).
4042 // This is only used in debug mode, to ensure we do not connect interval siblings
4043 // in the same parallel move.
4044 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004045};
4046
4047static constexpr size_t kDefaultNumberOfMoves = 4;
4048
4049class HParallelMove : public HTemplateInstruction<0> {
4050 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004051 explicit HParallelMove(ArenaAllocator* arena)
4052 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004053
Nicolas Geoffray90218252015-04-15 11:56:51 +01004054 void AddMove(Location source,
4055 Location destination,
4056 Primitive::Type type,
4057 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004058 DCHECK(source.IsValid());
4059 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004060 if (kIsDebugBuild) {
4061 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004062 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004063 if (moves_.Get(i).GetInstruction() == instruction) {
4064 // Special case the situation where the move is for the spill slot
4065 // of the instruction.
4066 if ((GetPrevious() == instruction)
4067 || ((GetPrevious() == nullptr)
4068 && instruction->IsPhi()
4069 && instruction->GetBlock() == GetBlock())) {
4070 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4071 << "Doing parallel moves for the same instruction.";
4072 } else {
4073 DCHECK(false) << "Doing parallel moves for the same instruction.";
4074 }
4075 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004076 }
4077 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004078 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004079 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004080 << "Overlapped destination for two moves in a parallel move: "
4081 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4082 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004083 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004084 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004085 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004086 }
4087
4088 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004089 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004090 }
4091
4092 size_t NumMoves() const { return moves_.Size(); }
4093
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004094 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004095
4096 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004097 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004098
4099 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4100};
4101
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004102class HGraphVisitor : public ValueObject {
4103 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004104 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4105 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004106
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004107 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004108 virtual void VisitBasicBlock(HBasicBlock* block);
4109
Roland Levillain633021e2014-10-01 14:12:25 +01004110 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004111 void VisitInsertionOrder();
4112
Roland Levillain633021e2014-10-01 14:12:25 +01004113 // Visit the graph following dominator tree reverse post-order.
4114 void VisitReversePostOrder();
4115
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004116 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004117
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004118 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004119#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004120 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4121
4122 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4123
4124#undef DECLARE_VISIT_INSTRUCTION
4125
4126 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004127 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004128
4129 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4130};
4131
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004132class HGraphDelegateVisitor : public HGraphVisitor {
4133 public:
4134 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4135 virtual ~HGraphDelegateVisitor() {}
4136
4137 // Visit functions that delegate to to super class.
4138#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004139 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004140
4141 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4142
4143#undef DECLARE_VISIT_INSTRUCTION
4144
4145 private:
4146 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4147};
4148
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004149class HInsertionOrderIterator : public ValueObject {
4150 public:
4151 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4152
4153 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4154 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4155 void Advance() { ++index_; }
4156
4157 private:
4158 const HGraph& graph_;
4159 size_t index_;
4160
4161 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4162};
4163
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004164class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004165 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004166 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4167 // Check that reverse post order of the graph has been built.
4168 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4169 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004170
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004171 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4172 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004173 void Advance() { ++index_; }
4174
4175 private:
4176 const HGraph& graph_;
4177 size_t index_;
4178
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004179 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004180};
4181
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004182class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004183 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004184 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004185 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4186 // Check that reverse post order of the graph has been built.
4187 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4188 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004189
4190 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004191 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004192 void Advance() { --index_; }
4193
4194 private:
4195 const HGraph& graph_;
4196 size_t index_;
4197
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004198 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004199};
4200
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004201class HLinearPostOrderIterator : public ValueObject {
4202 public:
4203 explicit HLinearPostOrderIterator(const HGraph& graph)
4204 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4205
4206 bool Done() const { return index_ == 0; }
4207
4208 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4209
4210 void Advance() {
4211 --index_;
4212 DCHECK_GE(index_, 0U);
4213 }
4214
4215 private:
4216 const GrowableArray<HBasicBlock*>& order_;
4217 size_t index_;
4218
4219 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4220};
4221
4222class HLinearOrderIterator : public ValueObject {
4223 public:
4224 explicit HLinearOrderIterator(const HGraph& graph)
4225 : order_(graph.GetLinearOrder()), index_(0) {}
4226
4227 bool Done() const { return index_ == order_.Size(); }
4228 HBasicBlock* Current() const { return order_.Get(index_); }
4229 void Advance() { ++index_; }
4230
4231 private:
4232 const GrowableArray<HBasicBlock*>& order_;
4233 size_t index_;
4234
4235 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4236};
4237
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004238// Iterator over the blocks that art part of the loop. Includes blocks part
4239// of an inner loop. The order in which the blocks are iterated is on their
4240// block id.
4241class HBlocksInLoopIterator : public ValueObject {
4242 public:
4243 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4244 : blocks_in_loop_(info.GetBlocks()),
4245 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4246 index_(0) {
4247 if (!blocks_in_loop_.IsBitSet(index_)) {
4248 Advance();
4249 }
4250 }
4251
4252 bool Done() const { return index_ == blocks_.Size(); }
4253 HBasicBlock* Current() const { return blocks_.Get(index_); }
4254 void Advance() {
4255 ++index_;
4256 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4257 if (blocks_in_loop_.IsBitSet(index_)) {
4258 break;
4259 }
4260 }
4261 }
4262
4263 private:
4264 const BitVector& blocks_in_loop_;
4265 const GrowableArray<HBasicBlock*>& blocks_;
4266 size_t index_;
4267
4268 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4269};
4270
Mingyao Yang3584bce2015-05-19 16:01:59 -07004271// Iterator over the blocks that art part of the loop. Includes blocks part
4272// of an inner loop. The order in which the blocks are iterated is reverse
4273// post order.
4274class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4275 public:
4276 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4277 : blocks_in_loop_(info.GetBlocks()),
4278 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4279 index_(0) {
4280 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4281 Advance();
4282 }
4283 }
4284
4285 bool Done() const { return index_ == blocks_.Size(); }
4286 HBasicBlock* Current() const { return blocks_.Get(index_); }
4287 void Advance() {
4288 ++index_;
4289 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4290 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4291 break;
4292 }
4293 }
4294 }
4295
4296 private:
4297 const BitVector& blocks_in_loop_;
4298 const GrowableArray<HBasicBlock*>& blocks_;
4299 size_t index_;
4300
4301 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4302};
4303
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004304inline int64_t Int64FromConstant(HConstant* constant) {
4305 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4306 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4307 : constant->AsLongConstant()->GetValue();
4308}
4309
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004310} // namespace art
4311
4312#endif // ART_COMPILER_OPTIMIZING_NODES_H_