blob: 2c7bac55d391c9e72c8b6e38201b5a53e506381e [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;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010041class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000042class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000043class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000046class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000047class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000049class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010050class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010051class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010052class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000053class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010054class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000056
57static const int kDefaultNumberOfBlocks = 8;
58static const int kDefaultNumberOfSuccessors = 2;
59static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010060static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000061static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Calin Juravle9aec02f2014-11-18 23:06:35 +000063static constexpr uint32_t kMaxIntShiftValue = 0x1f;
64static constexpr uint64_t kMaxLongShiftValue = 0x3f;
65
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010066static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
67
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010068static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
69
Dave Allison20dfc792014-06-16 20:44:29 -070070enum IfCondition {
71 kCondEQ,
72 kCondNE,
73 kCondLT,
74 kCondLE,
75 kCondGT,
76 kCondGE,
77};
78
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010079class HInstructionList {
80 public:
81 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
82
83 void AddInstruction(HInstruction* instruction);
84 void RemoveInstruction(HInstruction* instruction);
85
David Brazdilc3d743f2015-04-22 13:40:50 +010086 // Insert `instruction` before/after an existing instruction `cursor`.
87 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
88 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
89
Roland Levillain6b469232014-09-25 10:10:38 +010090 // Return true if this list contains `instruction`.
91 bool Contains(HInstruction* instruction) const;
92
Roland Levillainccc07a92014-09-16 14:48:16 +010093 // Return true if `instruction1` is found before `instruction2` in
94 // this instruction list and false otherwise. Abort if none
95 // of these instructions is found.
96 bool FoundBefore(const HInstruction* instruction1,
97 const HInstruction* instruction2) const;
98
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000099 bool IsEmpty() const { return first_instruction_ == nullptr; }
100 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
101
102 // Update the block of all instructions to be `block`.
103 void SetBlockOfInstructions(HBasicBlock* block) const;
104
105 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
106 void Add(const HInstructionList& instruction_list);
107
David Brazdil2d7352b2015-04-20 14:52:42 +0100108 // Return the number of instructions in the list. This is an expensive operation.
109 size_t CountSize() const;
110
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100111 private:
112 HInstruction* first_instruction_;
113 HInstruction* last_instruction_;
114
115 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000116 friend class HGraph;
117 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100118 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100119 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100120
121 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
122};
123
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000124// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700125class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000126 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100127 HGraph(ArenaAllocator* arena,
128 const DexFile& dex_file,
129 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100130 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100132 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100133 bool debuggable = false,
134 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000135 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000136 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100137 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100138 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700139 entry_block_(nullptr),
140 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100141 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100142 number_of_vregs_(0),
143 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000144 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400145 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000146 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000147 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100148 dex_file_(dex_file),
149 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100150 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100151 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100152 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000154 cached_null_constant_(nullptr),
155 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000156 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
157 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100158 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
159 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000160
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000161 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100162 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100163 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164
David Brazdil69ba7b72015-06-23 18:27:30 +0100165 bool IsInSsaForm() const { return in_ssa_form_; }
166
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000167 HBasicBlock* GetEntryBlock() const { return entry_block_; }
168 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100169 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000170
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000171 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
172 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000173
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000174 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100175
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 // Try building the SSA form of this graph, with dominance computation and loop
177 // recognition. Returns whether it was successful in doing all these steps.
178 bool TryBuildingSsa() {
179 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000180 // The SSA builder requires loops to all be natural. Specifically, the dead phi
181 // elimination phase checks the consistency of the graph when doing a post-order
182 // visit for eliminating dead phis: a dead phi can only have loop header phi
183 // users remaining when being visited.
184 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100186 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000187 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000188 }
189
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100190 void ComputeDominanceInformation();
191 void ClearDominanceInformation();
192
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000193 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000194 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100195 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000196
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000197 // Analyze all natural loops in this graph. Returns false if one
198 // loop is not natural, that is the header does not dominate the
199 // back edge.
200 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100201
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000202 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
203 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
204
Mingyao Yang3584bce2015-05-19 16:01:59 -0700205 // Need to add a couple of blocks to test if the loop body is entered and
206 // put deoptimization instructions, etc.
207 void TransformLoopHeaderForBCE(HBasicBlock* header);
208
David Brazdil2d7352b2015-04-20 14:52:42 +0100209 // Removes `block` from the graph.
210 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000211
David Brazdilfc6a86a2015-06-26 10:33:45 +0000212 // Splits the edge between `block` and `successor` while preserving the
213 // indices in the predecessor/successor lists. If there are multiple edges
214 // between the blocks, the lowest indices are used.
215 // Returns the new block which is empty and has the same dex pc as `successor`.
216 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
217
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100218 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
219 void SimplifyLoop(HBasicBlock* header);
220
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 int32_t GetNextInstructionId() {
222 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000223 return current_instruction_id_++;
224 }
225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 int32_t GetCurrentInstructionId() const {
227 return current_instruction_id_;
228 }
229
230 void SetCurrentInstructionId(int32_t id) {
231 current_instruction_id_ = id;
232 }
233
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100234 uint16_t GetMaximumNumberOfOutVRegs() const {
235 return maximum_number_of_out_vregs_;
236 }
237
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
239 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100240 }
241
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100242 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
243 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
244 }
245
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000246 void UpdateTemporariesVRegSlots(size_t slots) {
247 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100248 }
249
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000250 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100251 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000252 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100253 }
254
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100255 void SetNumberOfVRegs(uint16_t number_of_vregs) {
256 number_of_vregs_ = number_of_vregs;
257 }
258
259 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100260 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100261 return number_of_vregs_;
262 }
263
264 void SetNumberOfInVRegs(uint16_t value) {
265 number_of_in_vregs_ = value;
266 }
267
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100268 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100269 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100270 return number_of_vregs_ - number_of_in_vregs_;
271 }
272
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100273 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
274 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100275 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100276
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100277 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
278 return linear_order_;
279 }
280
Mark Mendell1152c922015-04-24 17:06:35 -0400281 bool HasBoundsChecks() const {
282 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800283 }
284
Mark Mendell1152c922015-04-24 17:06:35 -0400285 void SetHasBoundsChecks(bool value) {
286 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800287 }
288
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100289 bool ShouldGenerateConstructorBarrier() const {
290 return should_generate_constructor_barrier_;
291 }
292
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000293 bool IsDebuggable() const { return debuggable_; }
294
David Brazdil8d5b8b22015-03-24 10:51:52 +0000295 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000296 // already, it is created and inserted into the graph. This method is only for
297 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000298 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000299 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000300 HIntConstant* GetIntConstant(int32_t value) {
301 return CreateConstant(value, &cached_int_constants_);
302 }
303 HLongConstant* GetLongConstant(int64_t value) {
304 return CreateConstant(value, &cached_long_constants_);
305 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000306 HFloatConstant* GetFloatConstant(float value) {
307 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
308 }
309 HDoubleConstant* GetDoubleConstant(double value) {
310 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
311 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000312
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100313 HCurrentMethod* GetCurrentMethod();
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100316
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 const DexFile& GetDexFile() const {
318 return dex_file_;
319 }
320
321 uint32_t GetMethodIdx() const {
322 return method_idx_;
323 }
324
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100325 InvokeType GetInvokeType() const {
326 return invoke_type_;
327 }
328
Mark Mendellc4701932015-04-10 13:18:51 -0400329 InstructionSet GetInstructionSet() const {
330 return instruction_set_;
331 }
332
David Brazdil2d7352b2015-04-20 14:52:42 +0100333 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000334 void VisitBlockForDominatorTree(HBasicBlock* block,
335 HBasicBlock* predecessor,
336 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100337 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000338 void VisitBlockForBackEdges(HBasicBlock* block,
339 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100340 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000341 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100342 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000343
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000344 template <class InstructionType, typename ValueType>
345 InstructionType* CreateConstant(ValueType value,
346 ArenaSafeMap<ValueType, InstructionType*>* cache) {
347 // Try to find an existing constant of the given value.
348 InstructionType* constant = nullptr;
349 auto cached_constant = cache->find(value);
350 if (cached_constant != cache->end()) {
351 constant = cached_constant->second;
352 }
353
354 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100355 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000356 if (constant == nullptr || constant->GetBlock() == nullptr) {
357 constant = new (arena_) InstructionType(value);
358 cache->Overwrite(value, constant);
359 InsertConstant(constant);
360 }
361 return constant;
362 }
363
David Brazdil8d5b8b22015-03-24 10:51:52 +0000364 void InsertConstant(HConstant* instruction);
365
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000366 // Cache a float constant into the graph. This method should only be
367 // called by the SsaBuilder when creating "equivalent" instructions.
368 void CacheFloatConstant(HFloatConstant* constant);
369
370 // See CacheFloatConstant comment.
371 void CacheDoubleConstant(HDoubleConstant* constant);
372
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000374
375 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376 GrowableArray<HBasicBlock*> blocks_;
377
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378 // List of blocks to perform a reverse post order tree traversal.
379 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000380
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100381 // List of blocks to perform a linear order tree traversal.
382 GrowableArray<HBasicBlock*> linear_order_;
383
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000384 HBasicBlock* entry_block_;
385 HBasicBlock* exit_block_;
386
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100387 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100388 uint16_t maximum_number_of_out_vregs_;
389
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100390 // The number of virtual registers in this method. Contains the parameters.
391 uint16_t number_of_vregs_;
392
393 // The number of virtual registers used by parameters of this method.
394 uint16_t number_of_in_vregs_;
395
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000396 // Number of vreg size slots that the temporaries use (used in baseline compiler).
397 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100398
Mark Mendell1152c922015-04-24 17:06:35 -0400399 // Has bounds checks. We can totally skip BCE if it's false.
400 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800401
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000402 // Indicates whether the graph should be compiled in a way that
403 // ensures full debuggability. If false, we can apply more
404 // aggressive optimizations that may limit the level of debugging.
405 const bool debuggable_;
406
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000407 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000408 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000409
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100410 // The dex file from which the method is from.
411 const DexFile& dex_file_;
412
413 // The method index in the dex file.
414 const uint32_t method_idx_;
415
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100416 // If inlined, this encodes how the callee is being invoked.
417 const InvokeType invoke_type_;
418
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100419 // Whether the graph has been transformed to SSA form. Only used
420 // in debug mode to ensure we are not using properties only valid
421 // for non-SSA form (like the number of temporaries).
422 bool in_ssa_form_;
423
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100424 const bool should_generate_constructor_barrier_;
425
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426 const InstructionSet instruction_set_;
427
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000428 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000429 HNullConstant* cached_null_constant_;
430 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000431 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000432 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000433 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000434
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100435 HCurrentMethod* cached_current_method_;
436
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000437 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100438 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000440 DISALLOW_COPY_AND_ASSIGN(HGraph);
441};
442
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700443class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000444 public:
445 HLoopInformation(HBasicBlock* header, HGraph* graph)
446 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100447 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100448 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100449 // Make bit vector growable, as the number of blocks may change.
450 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100451
452 HBasicBlock* GetHeader() const {
453 return header_;
454 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000455
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000456 void SetHeader(HBasicBlock* block) {
457 header_ = block;
458 }
459
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100460 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
461 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
462 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
463
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000464 void AddBackEdge(HBasicBlock* back_edge) {
465 back_edges_.Add(back_edge);
466 }
467
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468 void RemoveBackEdge(HBasicBlock* back_edge) {
469 back_edges_.Delete(back_edge);
470 }
471
David Brazdil46e2a392015-03-16 17:31:52 +0000472 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100473 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000474 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100475 }
476 return false;
477 }
478
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000479 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000480 return back_edges_.Size();
481 }
482
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100483 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100484
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
486 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100487 }
488
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100489 // Returns the lifetime position of the back edge that has the
490 // greatest lifetime position.
491 size_t GetLifetimeEnd() const;
492
493 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
494 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
495 if (back_edges_.Get(i) == existing) {
496 back_edges_.Put(i, new_back_edge);
497 return;
498 }
499 }
500 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100501 }
502
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100503 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100504 // that is the header dominates the back edge.
505 bool Populate();
506
David Brazdila4b8c212015-05-07 09:59:30 +0100507 // Reanalyzes the loop by removing loop info from its blocks and re-running
508 // Populate(). If there are no back edges left, the loop info is completely
509 // removed as well as its SuspendCheck instruction. It must be run on nested
510 // inner loops first.
511 void Update();
512
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100513 // Returns whether this loop information contains `block`.
514 // Note that this loop information *must* be populated before entering this function.
515 bool Contains(const HBasicBlock& block) const;
516
517 // Returns whether this loop information is an inner loop of `other`.
518 // Note that `other` *must* be populated before entering this function.
519 bool IsIn(const HLoopInformation& other) const;
520
521 const ArenaBitVector& GetBlocks() const { return blocks_; }
522
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000523 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000524 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000525
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000526 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100527 // Internal recursive implementation of `Populate`.
528 void PopulateRecursive(HBasicBlock* block);
529
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000530 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100531 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000532 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100533 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000534
535 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
536};
537
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100538static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100539static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100540
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000541// A block in a method. Contains the list of instructions represented
542// as a double linked list. Each block knows its predecessors and
543// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100544
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700545class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000546 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100547 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000548 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000549 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
550 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551 loop_information_(nullptr),
552 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100553 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100554 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100555 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100556 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000557 lifetime_end_(kNoLifetime),
558 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
561 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000562 }
563
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100564 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
565 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000566 }
567
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100568 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
569 return dominated_blocks_;
570 }
571
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100572 bool IsEntryBlock() const {
573 return graph_->GetEntryBlock() == this;
574 }
575
576 bool IsExitBlock() const {
577 return graph_->GetExitBlock() == this;
578 }
579
David Brazdil46e2a392015-03-16 17:31:52 +0000580 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000581 bool IsSingleTryBoundary() const;
582
583 // Returns true if this block emits nothing but a jump.
584 bool IsSingleJump() const {
585 HLoopInformation* loop_info = GetLoopInformation();
586 return (IsSingleGoto() || IsSingleTryBoundary())
587 // Back edges generate a suspend check.
588 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
589 }
David Brazdil46e2a392015-03-16 17:31:52 +0000590
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591 void AddBackEdge(HBasicBlock* back_edge) {
592 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000593 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000594 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100595 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000596 loop_information_->AddBackEdge(back_edge);
597 }
598
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000599 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000600 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000601
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000602 int GetBlockId() const { return block_id_; }
603 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000604
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000605 HBasicBlock* GetDominator() const { return dominator_; }
606 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100607 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100608 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000609 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
610 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
611 if (dominated_blocks_.Get(i) == existing) {
612 dominated_blocks_.Put(i, new_block);
613 return;
614 }
615 }
616 LOG(FATAL) << "Unreachable";
617 UNREACHABLE();
618 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100619 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620
621 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100622 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000623 }
624
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100625 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
626 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100627 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100628 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100629 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
630 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631
632 void AddSuccessor(HBasicBlock* block) {
633 successors_.Add(block);
634 block->predecessors_.Add(this);
635 }
636
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100637 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
638 size_t successor_index = GetSuccessorIndexOf(existing);
639 DCHECK_NE(successor_index, static_cast<size_t>(-1));
640 existing->RemovePredecessor(this);
641 new_block->predecessors_.Add(this);
642 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 }
644
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000645 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
646 size_t predecessor_index = GetPredecessorIndexOf(existing);
647 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
648 existing->RemoveSuccessor(this);
649 new_block->successors_.Add(this);
650 predecessors_.Put(predecessor_index, new_block);
651 }
652
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100653 // Insert `this` between `predecessor` and `successor. This method
654 // preserves the indicies, and will update the first edge found between
655 // `predecessor` and `successor`.
656 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
657 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
658 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
659 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
660 DCHECK_NE(successor_index, static_cast<size_t>(-1));
661 successor->predecessors_.Put(predecessor_index, this);
662 predecessor->successors_.Put(successor_index, this);
663 successors_.Add(successor);
664 predecessors_.Add(predecessor);
665 }
666
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100667 void RemovePredecessor(HBasicBlock* block) {
668 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100669 }
670
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000671 void RemoveSuccessor(HBasicBlock* block) {
672 successors_.Delete(block);
673 }
674
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100675 void ClearAllPredecessors() {
676 predecessors_.Reset();
677 }
678
679 void AddPredecessor(HBasicBlock* block) {
680 predecessors_.Add(block);
681 block->successors_.Add(this);
682 }
683
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100684 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100685 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100686 HBasicBlock* temp = predecessors_.Get(0);
687 predecessors_.Put(0, predecessors_.Get(1));
688 predecessors_.Put(1, temp);
689 }
690
David Brazdil769c9e52015-04-27 13:54:09 +0100691 void SwapSuccessors() {
692 DCHECK_EQ(successors_.Size(), 2u);
693 HBasicBlock* temp = successors_.Get(0);
694 successors_.Put(0, successors_.Get(1));
695 successors_.Put(1, temp);
696 }
697
David Brazdilfc6a86a2015-06-26 10:33:45 +0000698 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100699 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
700 if (predecessors_.Get(i) == predecessor) {
701 return i;
702 }
703 }
704 return -1;
705 }
706
David Brazdilfc6a86a2015-06-26 10:33:45 +0000707 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100708 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
709 if (successors_.Get(i) == successor) {
710 return i;
711 }
712 }
713 return -1;
714 }
715
David Brazdilfc6a86a2015-06-26 10:33:45 +0000716 HBasicBlock* GetSinglePredecessor() const {
717 DCHECK_EQ(GetPredecessors().Size(), 1u);
718 return GetPredecessors().Get(0);
719 }
720
721 HBasicBlock* GetSingleSuccessor() const {
722 DCHECK_EQ(GetSuccessors().Size(), 1u);
723 return GetSuccessors().Get(0);
724 }
725
726 // Returns whether the first occurrence of `predecessor` in the list of
727 // predecessors is at index `idx`.
728 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
729 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
730 return GetPredecessorIndexOf(predecessor) == idx;
731 }
732
733 // Returns whether successor at index `idx` is an exception handler.
734 bool IsExceptionalSuccessor(size_t idx) const;
735
736 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100737 // created, latter block. Note that this method will add the block to the
738 // graph, create a Goto at the end of the former block and will create an edge
739 // between the blocks. It will not, however, update the reverse post order or
740 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000741 HBasicBlock* SplitBefore(HInstruction* cursor);
742
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000743 // Split the block into two blocks just after `cursor`. Returns the newly
744 // created block. Note that this method just updates raw block information,
745 // like predecessors, successors, dominators, and instruction list. It does not
746 // update the graph, reverse post order, loop information, nor make sure the
747 // blocks are consistent (for example ending with a control flow instruction).
748 HBasicBlock* SplitAfter(HInstruction* cursor);
749
750 // Merge `other` at the end of `this`. Successors and dominated blocks of
751 // `other` are changed to be successors and dominated blocks of `this`. Note
752 // that this method does not update the graph, reverse post order, loop
753 // information, nor make sure the blocks are consistent (for example ending
754 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100755 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756
757 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
758 // of `this` are moved to `other`.
759 // Note that this method does not update the graph, reverse post order, loop
760 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000761 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000762 void ReplaceWith(HBasicBlock* other);
763
David Brazdil2d7352b2015-04-20 14:52:42 +0100764 // Merge `other` at the end of `this`. This method updates loops, reverse post
765 // order, links to predecessors, successors, dominators and deletes the block
766 // from the graph. The two blocks must be successive, i.e. `this` the only
767 // predecessor of `other` and vice versa.
768 void MergeWith(HBasicBlock* other);
769
770 // Disconnects `this` from all its predecessors, successors and dominator,
771 // removes it from all loops it is included in and eventually from the graph.
772 // The block must not dominate any other block. Predecessors and successors
773 // are safely updated.
774 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000775
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000776 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100777 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100778 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100779 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100780 // Replace instruction `initial` with `replacement` within this block.
781 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
782 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100783 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100784 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000785 // RemoveInstruction and RemovePhi delete a given instruction from the respective
786 // instruction list. With 'ensure_safety' set to true, it verifies that the
787 // instruction is not in use and removes it from the use lists of its inputs.
788 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
789 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100790 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100791
792 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100793 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100794 }
795
Roland Levillain6b879dd2014-09-22 17:13:44 +0100796 bool IsLoopPreHeaderFirstPredecessor() const {
797 DCHECK(IsLoopHeader());
798 DCHECK(!GetPredecessors().IsEmpty());
799 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
800 }
801
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100802 HLoopInformation* GetLoopInformation() const {
803 return loop_information_;
804 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000806 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000808 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100809 void SetInLoop(HLoopInformation* info) {
810 if (IsLoopHeader()) {
811 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100812 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100813 loop_information_ = info;
814 } else if (loop_information_->Contains(*info->GetHeader())) {
815 // Block is currently part of an outer loop. Make it part of this inner loop.
816 // Note that a non loop header having a loop information means this loop information
817 // has already been populated
818 loop_information_ = info;
819 } else {
820 // Block is part of an inner loop. Do not update the loop information.
821 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
822 // at this point, because this method is being called while populating `info`.
823 }
824 }
825
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000826 // Raw update of the loop information.
827 void SetLoopInformation(HLoopInformation* info) {
828 loop_information_ = info;
829 }
830
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100831 bool IsInLoop() const { return loop_information_ != nullptr; }
832
David Brazdila4b8c212015-05-07 09:59:30 +0100833 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100834 bool Dominates(HBasicBlock* block) const;
835
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100836 size_t GetLifetimeStart() const { return lifetime_start_; }
837 size_t GetLifetimeEnd() const { return lifetime_end_; }
838
839 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
840 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
841
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100842 uint32_t GetDexPc() const { return dex_pc_; }
843
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000844 bool IsCatchBlock() const { return is_catch_block_; }
845 void SetIsCatchBlock() { is_catch_block_ = true; }
846
David Brazdil8d5b8b22015-03-24 10:51:52 +0000847 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000848 bool EndsWithIf() const;
849 bool HasSinglePhi() const;
850
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000852 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000853 GrowableArray<HBasicBlock*> predecessors_;
854 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100855 HInstructionList instructions_;
856 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000857 HLoopInformation* loop_information_;
858 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100859 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100861 // The dex program counter of the first instruction of this block.
862 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100863 size_t lifetime_start_;
864 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000865 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000867 friend class HGraph;
868 friend class HInstruction;
869
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000870 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
871};
872
David Brazdilb2bd1c52015-03-25 11:17:37 +0000873// Iterates over the LoopInformation of all loops which contain 'block'
874// from the innermost to the outermost.
875class HLoopInformationOutwardIterator : public ValueObject {
876 public:
877 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
878 : current_(block.GetLoopInformation()) {}
879
880 bool Done() const { return current_ == nullptr; }
881
882 void Advance() {
883 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100884 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000885 }
886
887 HLoopInformation* Current() const {
888 DCHECK(!Done());
889 return current_;
890 }
891
892 private:
893 HLoopInformation* current_;
894
895 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
896};
897
Alexandre Ramesef20f712015-06-09 10:29:30 +0100898#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100899 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000900 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000901 M(ArrayGet, Instruction) \
902 M(ArrayLength, Instruction) \
903 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100904 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000905 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000906 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000907 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100908 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000909 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100910 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100911 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700912 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000913 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000914 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000915 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100916 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000917 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100918 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000919 M(FloatConstant, Constant) \
920 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100921 M(GreaterThan, Condition) \
922 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100923 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000924 M(InstanceFieldGet, Instruction) \
925 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000926 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100927 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000928 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000929 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100930 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000931 M(LessThan, Condition) \
932 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000933 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000934 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100935 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000936 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100937 M(Local, Instruction) \
938 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100939 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000940 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000941 M(Mul, BinaryOperation) \
942 M(Neg, UnaryOperation) \
943 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100944 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100945 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000946 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000947 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000948 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000949 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100950 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000951 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100952 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000953 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100954 M(Return, Instruction) \
955 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000956 M(Shl, BinaryOperation) \
957 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100958 M(StaticFieldGet, Instruction) \
959 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100960 M(StoreLocal, Instruction) \
961 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100962 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000963 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000964 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +0000965 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000966 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000967 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000968 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969
Alexandre Ramesef20f712015-06-09 10:29:30 +0100970#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
971
972#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
973
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100974#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
975
Alexandre Ramesef20f712015-06-09 10:29:30 +0100976#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
977
978#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
979
980#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
981 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
982 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
983 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100984 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +0100985 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
986 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
987
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100988#define FOR_EACH_INSTRUCTION(M) \
989 FOR_EACH_CONCRETE_INSTRUCTION(M) \
990 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100991 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100992 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100993 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700994
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100995#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000996FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
997#undef FORWARD_DECLARATION
998
Roland Levillainccc07a92014-09-16 14:48:16 +0100999#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001000 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1001 const char* DebugName() const OVERRIDE { return #type; } \
1002 const H##type* As##type() const OVERRIDE { return this; } \
1003 H##type* As##type() OVERRIDE { return this; } \
1004 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001005 return other->Is##type(); \
1006 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001007 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001008
David Brazdiled596192015-01-23 10:39:45 +00001009template <typename T> class HUseList;
1010
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001011template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001012class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001013 public:
David Brazdiled596192015-01-23 10:39:45 +00001014 HUseListNode* GetPrevious() const { return prev_; }
1015 HUseListNode* GetNext() const { return next_; }
1016 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001017 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001018 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001019
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001020 private:
David Brazdiled596192015-01-23 10:39:45 +00001021 HUseListNode(T user, size_t index)
1022 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1023
1024 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001025 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001026 HUseListNode<T>* prev_;
1027 HUseListNode<T>* next_;
1028
1029 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001030
1031 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1032};
1033
David Brazdiled596192015-01-23 10:39:45 +00001034template <typename T>
1035class HUseList : public ValueObject {
1036 public:
1037 HUseList() : first_(nullptr) {}
1038
1039 void Clear() {
1040 first_ = nullptr;
1041 }
1042
1043 // Adds a new entry at the beginning of the use list and returns
1044 // the newly created node.
1045 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001046 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001047 if (IsEmpty()) {
1048 first_ = new_node;
1049 } else {
1050 first_->prev_ = new_node;
1051 new_node->next_ = first_;
1052 first_ = new_node;
1053 }
1054 return new_node;
1055 }
1056
1057 HUseListNode<T>* GetFirst() const {
1058 return first_;
1059 }
1060
1061 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001062 DCHECK(node != nullptr);
1063 DCHECK(Contains(node));
1064
David Brazdiled596192015-01-23 10:39:45 +00001065 if (node->prev_ != nullptr) {
1066 node->prev_->next_ = node->next_;
1067 }
1068 if (node->next_ != nullptr) {
1069 node->next_->prev_ = node->prev_;
1070 }
1071 if (node == first_) {
1072 first_ = node->next_;
1073 }
1074 }
1075
David Brazdil1abb4192015-02-17 18:33:36 +00001076 bool Contains(const HUseListNode<T>* node) const {
1077 if (node == nullptr) {
1078 return false;
1079 }
1080 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1081 if (current == node) {
1082 return true;
1083 }
1084 }
1085 return false;
1086 }
1087
David Brazdiled596192015-01-23 10:39:45 +00001088 bool IsEmpty() const {
1089 return first_ == nullptr;
1090 }
1091
1092 bool HasOnlyOneUse() const {
1093 return first_ != nullptr && first_->next_ == nullptr;
1094 }
1095
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001096 size_t SizeSlow() const {
1097 size_t count = 0;
1098 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1099 ++count;
1100 }
1101 return count;
1102 }
1103
David Brazdiled596192015-01-23 10:39:45 +00001104 private:
1105 HUseListNode<T>* first_;
1106};
1107
1108template<typename T>
1109class HUseIterator : public ValueObject {
1110 public:
1111 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1112
1113 bool Done() const { return current_ == nullptr; }
1114
1115 void Advance() {
1116 DCHECK(!Done());
1117 current_ = current_->GetNext();
1118 }
1119
1120 HUseListNode<T>* Current() const {
1121 DCHECK(!Done());
1122 return current_;
1123 }
1124
1125 private:
1126 HUseListNode<T>* current_;
1127
1128 friend class HValue;
1129};
1130
David Brazdil1abb4192015-02-17 18:33:36 +00001131// This class is used by HEnvironment and HInstruction classes to record the
1132// instructions they use and pointers to the corresponding HUseListNodes kept
1133// by the used instructions.
1134template <typename T>
1135class HUserRecord : public ValueObject {
1136 public:
1137 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1138 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1139
1140 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1141 : instruction_(old_record.instruction_), use_node_(use_node) {
1142 DCHECK(instruction_ != nullptr);
1143 DCHECK(use_node_ != nullptr);
1144 DCHECK(old_record.use_node_ == nullptr);
1145 }
1146
1147 HInstruction* GetInstruction() const { return instruction_; }
1148 HUseListNode<T>* GetUseNode() const { return use_node_; }
1149
1150 private:
1151 // Instruction used by the user.
1152 HInstruction* instruction_;
1153
1154 // Corresponding entry in the use list kept by 'instruction_'.
1155 HUseListNode<T>* use_node_;
1156};
1157
Calin Juravle27df7582015-04-17 19:12:31 +01001158// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1159// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1160// flag is consider.
1161// - DependsOn suggests that there is a real dependency between side effects but it only
1162// checks DependendsOnSomething flag.
1163//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001164// Represents the side effects an instruction may have.
1165class SideEffects : public ValueObject {
1166 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001167 SideEffects() : flags_(0) {}
1168
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001169 static SideEffects None() {
1170 return SideEffects(0);
1171 }
1172
1173 static SideEffects All() {
1174 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1175 }
1176
1177 static SideEffects ChangesSomething() {
1178 return SideEffects((1 << kFlagChangesCount) - 1);
1179 }
1180
1181 static SideEffects DependsOnSomething() {
1182 int count = kFlagDependsOnCount - kFlagChangesCount;
1183 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1184 }
1185
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001186 SideEffects Union(SideEffects other) const {
1187 return SideEffects(flags_ | other.flags_);
1188 }
1189
Roland Levillain72bceff2014-09-15 18:29:00 +01001190 bool HasSideEffects() const {
1191 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1192 return (flags_ & all_bits_set) != 0;
1193 }
1194
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001195 bool HasAllSideEffects() const {
1196 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1197 return all_bits_set == (flags_ & all_bits_set);
1198 }
1199
1200 bool DependsOn(SideEffects other) const {
1201 size_t depends_flags = other.ComputeDependsFlags();
1202 return (flags_ & depends_flags) != 0;
1203 }
1204
1205 bool HasDependencies() const {
1206 int count = kFlagDependsOnCount - kFlagChangesCount;
1207 size_t all_bits_set = (1 << count) - 1;
1208 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1209 }
1210
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001211 private:
1212 static constexpr int kFlagChangesSomething = 0;
1213 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1214
1215 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1216 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1217
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001218 explicit SideEffects(size_t flags) : flags_(flags) {}
1219
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001220 size_t ComputeDependsFlags() const {
1221 return flags_ << kFlagChangesCount;
1222 }
1223
1224 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001225};
1226
David Brazdiled596192015-01-23 10:39:45 +00001227// A HEnvironment object contains the values of virtual registers at a given location.
1228class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1229 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001230 HEnvironment(ArenaAllocator* arena,
1231 size_t number_of_vregs,
1232 const DexFile& dex_file,
1233 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001234 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001235 InvokeType invoke_type,
1236 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001237 : vregs_(arena, number_of_vregs),
1238 locations_(arena, number_of_vregs),
1239 parent_(nullptr),
1240 dex_file_(dex_file),
1241 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001242 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001243 invoke_type_(invoke_type),
1244 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001245 vregs_.SetSize(number_of_vregs);
1246 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001247 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001248 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001249
1250 locations_.SetSize(number_of_vregs);
1251 for (size_t i = 0; i < number_of_vregs; ++i) {
1252 locations_.Put(i, Location());
1253 }
1254 }
1255
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001256 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001257 : HEnvironment(arena,
1258 to_copy.Size(),
1259 to_copy.GetDexFile(),
1260 to_copy.GetMethodIdx(),
1261 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001262 to_copy.GetInvokeType(),
1263 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001264
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001265 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001266 if (parent_ != nullptr) {
1267 parent_->SetAndCopyParentChain(allocator, parent);
1268 } else {
1269 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1270 parent_->CopyFrom(parent);
1271 if (parent->GetParent() != nullptr) {
1272 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1273 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001274 }
David Brazdiled596192015-01-23 10:39:45 +00001275 }
1276
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001277 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1278 void CopyFrom(HEnvironment* environment);
1279
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001280 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1281 // input to the loop phi instead. This is for inserting instructions that
1282 // require an environment (like HDeoptimization) in the loop pre-header.
1283 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001284
1285 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001286 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001287 }
1288
1289 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001290 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001291 }
1292
David Brazdil1abb4192015-02-17 18:33:36 +00001293 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001294
1295 size_t Size() const { return vregs_.Size(); }
1296
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001297 HEnvironment* GetParent() const { return parent_; }
1298
1299 void SetLocationAt(size_t index, Location location) {
1300 locations_.Put(index, location);
1301 }
1302
1303 Location GetLocationAt(size_t index) const {
1304 return locations_.Get(index);
1305 }
1306
1307 uint32_t GetDexPc() const {
1308 return dex_pc_;
1309 }
1310
1311 uint32_t GetMethodIdx() const {
1312 return method_idx_;
1313 }
1314
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001315 InvokeType GetInvokeType() const {
1316 return invoke_type_;
1317 }
1318
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001319 const DexFile& GetDexFile() const {
1320 return dex_file_;
1321 }
1322
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001323 HInstruction* GetHolder() const {
1324 return holder_;
1325 }
1326
David Brazdiled596192015-01-23 10:39:45 +00001327 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001328 // Record instructions' use entries of this environment for constant-time removal.
1329 // It should only be called by HInstruction when a new environment use is added.
1330 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1331 DCHECK(env_use->GetUser() == this);
1332 size_t index = env_use->GetIndex();
1333 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1334 }
David Brazdiled596192015-01-23 10:39:45 +00001335
David Brazdil1abb4192015-02-17 18:33:36 +00001336 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001337 GrowableArray<Location> locations_;
1338 HEnvironment* parent_;
1339 const DexFile& dex_file_;
1340 const uint32_t method_idx_;
1341 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001342 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001343
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001344 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001345 HInstruction* const holder_;
1346
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001347 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001348
1349 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1350};
1351
Calin Juravleacf735c2015-02-12 15:25:22 +00001352class ReferenceTypeInfo : ValueObject {
1353 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001354 typedef Handle<mirror::Class> TypeHandle;
1355
Calin Juravle3fabec72015-07-16 16:51:30 +01001356 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1357 // The constructor will check that the type_handle is valid.
1358 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001359 }
1360
Calin Juravle3fabec72015-07-16 16:51:30 +01001361 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1362
1363 static bool IsValidHandle(TypeHandle handle) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1364 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001365 }
1366
Calin Juravle3fabec72015-07-16 16:51:30 +01001367 bool IsValid() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1368 return IsValidHandle(type_handle_);
1369 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001370 bool IsExact() const { return is_exact_; }
Calin Juravle3fabec72015-07-16 16:51:30 +01001371 bool IsObjectClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1372 DCHECK(IsValid());
1373 return GetTypeHandle()->IsObjectClass();
1374 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001375 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravle3fabec72015-07-16 16:51:30 +01001376 DCHECK(IsValid());
1377 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001378 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001379
1380 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1381
Calin Juravleb1498f62015-02-16 13:13:29 +00001382 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravle3fabec72015-07-16 16:51:30 +01001383 DCHECK(IsValid());
1384 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001385 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1386 }
1387
1388 // Returns true if the type information provide the same amount of details.
1389 // Note that it does not mean that the instructions have the same actual type
Calin Juravle3fabec72015-07-16 16:51:30 +01001390 // (because the type can be the result of a merge).
Calin Juravleacf735c2015-02-12 15:25:22 +00001391 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravle3fabec72015-07-16 16:51:30 +01001392 if (!IsValid() && !rti.IsValid()) {
1393 // Invalid types are equal.
Calin Juravleacf735c2015-02-12 15:25:22 +00001394 return true;
1395 }
Calin Juravle3fabec72015-07-16 16:51:30 +01001396 if (!IsValid() || !rti.IsValid()) {
1397 // One is valid, the other not.
Calin Juravleacf735c2015-02-12 15:25:22 +00001398 return false;
1399 }
Calin Juravle3fabec72015-07-16 16:51:30 +01001400 return IsExact() == rti.IsExact()
1401 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001402 }
1403
1404 private:
Calin Juravle3fabec72015-07-16 16:51:30 +01001405 ReferenceTypeInfo();
1406 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001407
Calin Juravleacf735c2015-02-12 15:25:22 +00001408 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001409 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001410 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001411 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001412 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001413};
1414
1415std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1416
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001417class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001418 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001419 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001420 : previous_(nullptr),
1421 next_(nullptr),
1422 block_(nullptr),
1423 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001424 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001425 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001426 locations_(nullptr),
1427 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001428 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001429 side_effects_(side_effects),
Calin Juravle3fabec72015-07-16 16:51:30 +01001430 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001431
Dave Allison20dfc792014-06-16 20:44:29 -07001432 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001433
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001434#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001435 enum InstructionKind {
1436 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1437 };
1438#undef DECLARE_KIND
1439
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001440 HInstruction* GetNext() const { return next_; }
1441 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001442
Calin Juravle77520bc2015-01-12 18:45:46 +00001443 HInstruction* GetNextDisregardingMoves() const;
1444 HInstruction* GetPreviousDisregardingMoves() const;
1445
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001446 HBasicBlock* GetBlock() const { return block_; }
1447 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001448 bool IsInBlock() const { return block_ != nullptr; }
1449 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001450 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001451
Roland Levillain6b879dd2014-09-22 17:13:44 +01001452 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001453 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001454
1455 virtual void Accept(HGraphVisitor* visitor) = 0;
1456 virtual const char* DebugName() const = 0;
1457
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001458 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001459 void SetRawInputAt(size_t index, HInstruction* input) {
1460 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1461 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001462
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001463 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001464 virtual uint32_t GetDexPc() const {
1465 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1466 " does not need an environment";
1467 UNREACHABLE();
1468 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001469 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001470 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001471 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001472
Calin Juravle10e244f2015-01-26 18:54:32 +00001473 // Does not apply for all instructions, but having this at top level greatly
1474 // simplifies the null check elimination.
Calin Juravleb0d5fc02015-07-15 14:41:29 +01001475 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001476 virtual bool CanBeNull() const {
1477 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1478 return true;
1479 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001480
Calin Juravle641547a2015-04-21 22:08:51 +01001481 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1482 UNUSED(obj);
1483 return false;
1484 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001485
Calin Juravle3fabec72015-07-16 16:51:30 +01001486 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001487
Calin Juravle61d544b2015-02-23 16:46:57 +00001488 ReferenceTypeInfo GetReferenceTypeInfo() const {
1489 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1490 return reference_type_info_;
1491 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001492
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001493 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001494 DCHECK(user != nullptr);
1495 HUseListNode<HInstruction*>* use =
1496 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1497 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001498 }
1499
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001500 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001501 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001502 HUseListNode<HEnvironment*>* env_use =
1503 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1504 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001505 }
1506
David Brazdil1abb4192015-02-17 18:33:36 +00001507 void RemoveAsUserOfInput(size_t input) {
1508 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1509 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1510 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001511
David Brazdil1abb4192015-02-17 18:33:36 +00001512 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1513 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001514
David Brazdiled596192015-01-23 10:39:45 +00001515 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1516 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001517 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001518 bool HasOnlyOneNonEnvironmentUse() const {
1519 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1520 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001521
Roland Levillain6c82d402014-10-13 16:10:27 +01001522 // Does this instruction strictly dominate `other_instruction`?
1523 // Returns false if this instruction and `other_instruction` are the same.
1524 // Aborts if this instruction and `other_instruction` are both phis.
1525 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001526
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001527 int GetId() const { return id_; }
1528 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001529
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001530 int GetSsaIndex() const { return ssa_index_; }
1531 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1532 bool HasSsaIndex() const { return ssa_index_ != -1; }
1533
1534 bool HasEnvironment() const { return environment_ != nullptr; }
1535 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001536 // Set the `environment_` field. Raw because this method does not
1537 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001538 void SetRawEnvironment(HEnvironment* environment) {
1539 DCHECK(environment_ == nullptr);
1540 DCHECK_EQ(environment->GetHolder(), this);
1541 environment_ = environment;
1542 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001543
1544 // Set the environment of this instruction, copying it from `environment`. While
1545 // copying, the uses lists are being updated.
1546 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001547 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001548 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001549 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001550 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001551 if (environment->GetParent() != nullptr) {
1552 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1553 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001554 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001555
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001556 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1557 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001558 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001559 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001560 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001561 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001562 if (environment->GetParent() != nullptr) {
1563 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1564 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001565 }
1566
Nicolas Geoffray39468442014-09-02 15:17:15 +01001567 // Returns the number of entries in the environment. Typically, that is the
1568 // number of dex registers in a method. It could be more in case of inlining.
1569 size_t EnvironmentSize() const;
1570
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001571 LocationSummary* GetLocations() const { return locations_; }
1572 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001573
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001574 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001575 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001576
Alexandre Rames188d4312015-04-09 18:30:21 +01001577 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1578 // uses of this instruction by `other` are *not* updated.
1579 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1580 ReplaceWith(other);
1581 other->ReplaceInput(this, use_index);
1582 }
1583
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001584 // Move `this` instruction before `cursor`.
1585 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001586
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001587#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001588 bool Is##type() const { return (As##type() != nullptr); } \
1589 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001590 virtual H##type* As##type() { return nullptr; }
1591
1592 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1593#undef INSTRUCTION_TYPE_CHECK
1594
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001595 // Returns whether the instruction can be moved within the graph.
1596 virtual bool CanBeMoved() const { return false; }
1597
1598 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001599 virtual bool InstructionTypeEquals(HInstruction* other) const {
1600 UNUSED(other);
1601 return false;
1602 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001603
1604 // Returns whether any data encoded in the two instructions is equal.
1605 // This method does not look at the inputs. Both instructions must be
1606 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001607 virtual bool InstructionDataEquals(HInstruction* other) const {
1608 UNUSED(other);
1609 return false;
1610 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001611
1612 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001613 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001614 // 2) Their inputs are identical.
1615 bool Equals(HInstruction* other) const;
1616
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001617 virtual InstructionKind GetKind() const = 0;
1618
1619 virtual size_t ComputeHashCode() const {
1620 size_t result = GetKind();
1621 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1622 result = (result * 31) + InputAt(i)->GetId();
1623 }
1624 return result;
1625 }
1626
1627 SideEffects GetSideEffects() const { return side_effects_; }
1628
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001629 size_t GetLifetimePosition() const { return lifetime_position_; }
1630 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1631 LiveInterval* GetLiveInterval() const { return live_interval_; }
1632 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1633 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1634
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001635 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1636
1637 // Returns whether the code generation of the instruction will require to have access
1638 // to the current method. Such instructions are:
1639 // (1): Instructions that require an environment, as calling the runtime requires
1640 // to walk the stack and have the current method stored at a specific stack address.
1641 // (2): Object literals like classes and strings, that are loaded from the dex cache
1642 // fields of the current method.
1643 bool NeedsCurrentMethod() const {
1644 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1645 }
1646
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001647 virtual bool NeedsDexCache() const { return false; }
1648
Mark Mendellc4701932015-04-10 13:18:51 -04001649 // Does this instruction have any use in an environment before
1650 // control flow hits 'other'?
1651 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1652
1653 // Remove all references to environment uses of this instruction.
1654 // The caller must ensure that this is safe to do.
1655 void RemoveEnvironmentUsers();
1656
David Brazdil1abb4192015-02-17 18:33:36 +00001657 protected:
1658 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1659 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1660
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001661 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001662 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1663
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001664 HInstruction* previous_;
1665 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001666 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001667
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001668 // An instruction gets an id when it is added to the graph.
1669 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001670 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001671 int id_;
1672
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001673 // When doing liveness analysis, instructions that have uses get an SSA index.
1674 int ssa_index_;
1675
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001676 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001677 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001678
1679 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001680 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001681
Nicolas Geoffray39468442014-09-02 15:17:15 +01001682 // The environment associated with this instruction. Not null if the instruction
1683 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001684 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001685
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001686 // Set by the code generator.
1687 LocationSummary* locations_;
1688
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001689 // Set by the liveness analysis.
1690 LiveInterval* live_interval_;
1691
1692 // Set by the liveness analysis, this is the position in a linear
1693 // order of blocks where this instruction's live interval start.
1694 size_t lifetime_position_;
1695
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001696 const SideEffects side_effects_;
1697
Calin Juravleacf735c2015-02-12 15:25:22 +00001698 // TODO: for primitive types this should be marked as invalid.
1699 ReferenceTypeInfo reference_type_info_;
1700
David Brazdil1abb4192015-02-17 18:33:36 +00001701 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001702 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001703 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001704 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001705 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001706
1707 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1708};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001709std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001710
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001711class HInputIterator : public ValueObject {
1712 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001713 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001714
1715 bool Done() const { return index_ == instruction_->InputCount(); }
1716 HInstruction* Current() const { return instruction_->InputAt(index_); }
1717 void Advance() { index_++; }
1718
1719 private:
1720 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001721 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001722
1723 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1724};
1725
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001726class HInstructionIterator : public ValueObject {
1727 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001728 explicit HInstructionIterator(const HInstructionList& instructions)
1729 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001730 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001731 }
1732
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001733 bool Done() const { return instruction_ == nullptr; }
1734 HInstruction* Current() const { return instruction_; }
1735 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001736 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001737 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001738 }
1739
1740 private:
1741 HInstruction* instruction_;
1742 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001743
1744 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001745};
1746
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001747class HBackwardInstructionIterator : public ValueObject {
1748 public:
1749 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1750 : instruction_(instructions.last_instruction_) {
1751 next_ = Done() ? nullptr : instruction_->GetPrevious();
1752 }
1753
1754 bool Done() const { return instruction_ == nullptr; }
1755 HInstruction* Current() const { return instruction_; }
1756 void Advance() {
1757 instruction_ = next_;
1758 next_ = Done() ? nullptr : instruction_->GetPrevious();
1759 }
1760
1761 private:
1762 HInstruction* instruction_;
1763 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001764
1765 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001766};
1767
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001768// An embedded container with N elements of type T. Used (with partial
1769// specialization for N=0) because embedded arrays cannot have size 0.
1770template<typename T, intptr_t N>
1771class EmbeddedArray {
1772 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001773 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001774
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001775 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001776
1777 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001778 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001779 return elements_[i];
1780 }
1781
1782 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001784 return elements_[i];
1785 }
1786
1787 const T& At(intptr_t i) const {
1788 return (*this)[i];
1789 }
1790
1791 void SetAt(intptr_t i, const T& val) {
1792 (*this)[i] = val;
1793 }
1794
1795 private:
1796 T elements_[N];
1797};
1798
1799template<typename T>
1800class EmbeddedArray<T, 0> {
1801 public:
1802 intptr_t length() const { return 0; }
1803 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001804 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001805 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001806 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001807 }
1808 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001809 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001811 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001812 }
1813};
1814
1815template<intptr_t N>
1816class HTemplateInstruction: public HInstruction {
1817 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001818 HTemplateInstruction<N>(SideEffects side_effects)
1819 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001820 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001822 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001823
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001824 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001825 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1826
1827 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1828 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001829 }
1830
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001831 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001832 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001833
1834 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001835};
1836
Dave Allison20dfc792014-06-16 20:44:29 -07001837template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001838class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001839 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001840 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1841 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001842 virtual ~HExpression() {}
1843
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001844 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001845
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001846 protected:
1847 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001848};
1849
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001850// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1851// instruction that branches to the exit block.
1852class HReturnVoid : public HTemplateInstruction<0> {
1853 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001854 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001856 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001857
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001858 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001859
1860 private:
1861 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1862};
1863
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001864// Represents dex's RETURN opcodes. A HReturn is a control flow
1865// instruction that branches to the exit block.
1866class HReturn : public HTemplateInstruction<1> {
1867 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001868 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001869 SetRawInputAt(0, value);
1870 }
1871
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001872 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001873
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001874 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001875
1876 private:
1877 DISALLOW_COPY_AND_ASSIGN(HReturn);
1878};
1879
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001880// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001881// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001882// exit block.
1883class HExit : public HTemplateInstruction<0> {
1884 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001885 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001886
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001887 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001888
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001889 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001890
1891 private:
1892 DISALLOW_COPY_AND_ASSIGN(HExit);
1893};
1894
1895// Jumps from one block to another.
1896class HGoto : public HTemplateInstruction<0> {
1897 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001898 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1899
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001900 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001901
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001902 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001903 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001904 }
1905
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001906 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001907
1908 private:
1909 DISALLOW_COPY_AND_ASSIGN(HGoto);
1910};
1911
Dave Allison20dfc792014-06-16 20:44:29 -07001912
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001913// Conditional branch. A block ending with an HIf instruction must have
1914// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001915class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001916 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001917 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001918 SetRawInputAt(0, input);
1919 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001920
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001921 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001922
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001923 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001924 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001925 }
1926
1927 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001928 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001929 }
1930
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001931 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001932
1933 private:
1934 DISALLOW_COPY_AND_ASSIGN(HIf);
1935};
1936
David Brazdilfc6a86a2015-06-26 10:33:45 +00001937
1938// Abstract instruction which marks the beginning and/or end of a try block and
1939// links it to the respective exception handlers. Behaves the same as a Goto in
1940// non-exceptional control flow.
1941// Normal-flow successor is stored at index zero, exception handlers under
1942// higher indices in no particular order.
1943class HTryBoundary : public HTemplateInstruction<0> {
1944 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01001945 enum BoundaryKind {
1946 kEntry,
1947 kExit,
1948 };
1949
1950 explicit HTryBoundary(BoundaryKind kind)
1951 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00001952
1953 bool IsControlFlow() const OVERRIDE { return true; }
1954
1955 // Returns the block's non-exceptional successor (index zero).
1956 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
1957
1958 // Returns whether `handler` is among its exception handlers (non-zero index
1959 // successors).
1960 bool HasExceptionHandler(HBasicBlock* handler) const {
1961 DCHECK(handler->IsCatchBlock());
1962 return GetBlock()->GetSuccessors().Contains(handler, /* start_from */ 1);
1963 }
1964
1965 // Returns whether successor at index `idx` is an exception handler.
1966 bool IsExceptionalSuccessor(size_t idx) const {
1967 DCHECK_LT(idx, GetBlock()->GetSuccessors().Size());
1968 bool is_handler = (idx != 0);
1969 DCHECK(!is_handler || GetBlock()->GetSuccessors().Get(idx)->IsCatchBlock());
1970 return is_handler;
1971 }
1972
1973 // If not present already, adds `handler` to its block's list of exception
1974 // handlers.
1975 void AddExceptionHandler(HBasicBlock* handler) {
1976 if (!HasExceptionHandler(handler)) {
1977 GetBlock()->AddSuccessor(handler);
1978 }
1979 }
1980
David Brazdil56e1acc2015-06-30 15:41:36 +01001981 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001982
1983 DECLARE_INSTRUCTION(TryBoundary);
1984
1985 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01001986 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001987
1988 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
1989};
1990
1991
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001992// Deoptimize to interpreter, upon checking a condition.
1993class HDeoptimize : public HTemplateInstruction<1> {
1994 public:
1995 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1996 : HTemplateInstruction(SideEffects::None()),
1997 dex_pc_(dex_pc) {
1998 SetRawInputAt(0, cond);
1999 }
2000
2001 bool NeedsEnvironment() const OVERRIDE { return true; }
2002 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002003 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002004
2005 DECLARE_INSTRUCTION(Deoptimize);
2006
2007 private:
2008 uint32_t dex_pc_;
2009
2010 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2011};
2012
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002013// Represents the ArtMethod that was passed as a first argument to
2014// the method. It is used by instructions that depend on it, like
2015// instructions that work with the dex cache.
2016class HCurrentMethod : public HExpression<0> {
2017 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002018 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002019
2020 DECLARE_INSTRUCTION(CurrentMethod);
2021
2022 private:
2023 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2024};
2025
Roland Levillain88cb1752014-10-20 16:36:47 +01002026class HUnaryOperation : public HExpression<1> {
2027 public:
2028 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2029 : HExpression(result_type, SideEffects::None()) {
2030 SetRawInputAt(0, input);
2031 }
2032
2033 HInstruction* GetInput() const { return InputAt(0); }
2034 Primitive::Type GetResultType() const { return GetType(); }
2035
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002036 bool CanBeMoved() const OVERRIDE { return true; }
2037 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002038 UNUSED(other);
2039 return true;
2040 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002041
Roland Levillain9240d6a2014-10-20 16:47:04 +01002042 // Try to statically evaluate `operation` and return a HConstant
2043 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002044 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002045 HConstant* TryStaticEvaluation() const;
2046
2047 // Apply this operation to `x`.
2048 virtual int32_t Evaluate(int32_t x) const = 0;
2049 virtual int64_t Evaluate(int64_t x) const = 0;
2050
Roland Levillain88cb1752014-10-20 16:36:47 +01002051 DECLARE_INSTRUCTION(UnaryOperation);
2052
2053 private:
2054 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2055};
2056
Dave Allison20dfc792014-06-16 20:44:29 -07002057class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002058 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002059 HBinaryOperation(Primitive::Type result_type,
2060 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002061 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002062 SetRawInputAt(0, left);
2063 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002064 }
2065
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002066 HInstruction* GetLeft() const { return InputAt(0); }
2067 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002068 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002069
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002070 virtual bool IsCommutative() const { return false; }
2071
2072 // Put constant on the right.
2073 // Returns whether order is changed.
2074 bool OrderInputsWithConstantOnTheRight() {
2075 HInstruction* left = InputAt(0);
2076 HInstruction* right = InputAt(1);
2077 if (left->IsConstant() && !right->IsConstant()) {
2078 ReplaceInput(right, 0);
2079 ReplaceInput(left, 1);
2080 return true;
2081 }
2082 return false;
2083 }
2084
2085 // Order inputs by instruction id, but favor constant on the right side.
2086 // This helps GVN for commutative ops.
2087 void OrderInputs() {
2088 DCHECK(IsCommutative());
2089 HInstruction* left = InputAt(0);
2090 HInstruction* right = InputAt(1);
2091 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2092 return;
2093 }
2094 if (OrderInputsWithConstantOnTheRight()) {
2095 return;
2096 }
2097 // Order according to instruction id.
2098 if (left->GetId() > right->GetId()) {
2099 ReplaceInput(right, 0);
2100 ReplaceInput(left, 1);
2101 }
2102 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002103
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002104 bool CanBeMoved() const OVERRIDE { return true; }
2105 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002106 UNUSED(other);
2107 return true;
2108 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002109
Roland Levillain9240d6a2014-10-20 16:47:04 +01002110 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002111 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002112 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002113 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002114
2115 // Apply this operation to `x` and `y`.
2116 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2117 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2118
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002119 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002120 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002121 HConstant* GetConstantRight() const;
2122
2123 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002124 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002125 HInstruction* GetLeastConstantLeft() const;
2126
Roland Levillainccc07a92014-09-16 14:48:16 +01002127 DECLARE_INSTRUCTION(BinaryOperation);
2128
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002129 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002130 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2131};
2132
Mark Mendellc4701932015-04-10 13:18:51 -04002133// The comparison bias applies for floating point operations and indicates how NaN
2134// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002135enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002136 kNoBias, // bias is not applicable (i.e. for long operation)
2137 kGtBias, // return 1 for NaN comparisons
2138 kLtBias, // return -1 for NaN comparisons
2139};
2140
Dave Allison20dfc792014-06-16 20:44:29 -07002141class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002142 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002143 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002144 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002145 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002146 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002147
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002148 bool NeedsMaterialization() const { return needs_materialization_; }
2149 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002150
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002151 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002152 // `instruction`, and disregard moves in between.
2153 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002154
Dave Allison20dfc792014-06-16 20:44:29 -07002155 DECLARE_INSTRUCTION(Condition);
2156
2157 virtual IfCondition GetCondition() const = 0;
2158
Mark Mendellc4701932015-04-10 13:18:51 -04002159 virtual IfCondition GetOppositeCondition() const = 0;
2160
Roland Levillain4fa13f62015-07-06 18:11:54 +01002161 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002162
2163 void SetBias(ComparisonBias bias) { bias_ = bias; }
2164
2165 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2166 return bias_ == other->AsCondition()->bias_;
2167 }
2168
Roland Levillain4fa13f62015-07-06 18:11:54 +01002169 bool IsFPConditionTrueIfNaN() const {
2170 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2171 IfCondition if_cond = GetCondition();
2172 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2173 }
2174
2175 bool IsFPConditionFalseIfNaN() const {
2176 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2177 IfCondition if_cond = GetCondition();
2178 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2179 }
2180
Dave Allison20dfc792014-06-16 20:44:29 -07002181 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002182 // For register allocation purposes, returns whether this instruction needs to be
2183 // materialized (that is, not just be in the processor flags).
2184 bool needs_materialization_;
2185
Mark Mendellc4701932015-04-10 13:18:51 -04002186 // Needed if we merge a HCompare into a HCondition.
2187 ComparisonBias bias_;
2188
Dave Allison20dfc792014-06-16 20:44:29 -07002189 DISALLOW_COPY_AND_ASSIGN(HCondition);
2190};
2191
2192// Instruction to check if two inputs are equal to each other.
2193class HEqual : public HCondition {
2194 public:
2195 HEqual(HInstruction* first, HInstruction* second)
2196 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002197
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002198 bool IsCommutative() const OVERRIDE { return true; }
2199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002200 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002201 return x == y ? 1 : 0;
2202 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002203 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002204 return x == y ? 1 : 0;
2205 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002206
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002207 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002209 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002210 return kCondEQ;
2211 }
2212
Mark Mendellc4701932015-04-10 13:18:51 -04002213 IfCondition GetOppositeCondition() const OVERRIDE {
2214 return kCondNE;
2215 }
2216
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002217 private:
2218 DISALLOW_COPY_AND_ASSIGN(HEqual);
2219};
2220
Dave Allison20dfc792014-06-16 20:44:29 -07002221class HNotEqual : public HCondition {
2222 public:
2223 HNotEqual(HInstruction* first, HInstruction* second)
2224 : HCondition(first, second) {}
2225
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002226 bool IsCommutative() const OVERRIDE { return true; }
2227
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002228 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002229 return x != y ? 1 : 0;
2230 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002231 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002232 return x != y ? 1 : 0;
2233 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002234
Dave Allison20dfc792014-06-16 20:44:29 -07002235 DECLARE_INSTRUCTION(NotEqual);
2236
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002237 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002238 return kCondNE;
2239 }
2240
Mark Mendellc4701932015-04-10 13:18:51 -04002241 IfCondition GetOppositeCondition() const OVERRIDE {
2242 return kCondEQ;
2243 }
2244
Dave Allison20dfc792014-06-16 20:44:29 -07002245 private:
2246 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2247};
2248
2249class HLessThan : public HCondition {
2250 public:
2251 HLessThan(HInstruction* first, HInstruction* second)
2252 : HCondition(first, second) {}
2253
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002254 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002255 return x < y ? 1 : 0;
2256 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002257 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002258 return x < y ? 1 : 0;
2259 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002260
Dave Allison20dfc792014-06-16 20:44:29 -07002261 DECLARE_INSTRUCTION(LessThan);
2262
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002263 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002264 return kCondLT;
2265 }
2266
Mark Mendellc4701932015-04-10 13:18:51 -04002267 IfCondition GetOppositeCondition() const OVERRIDE {
2268 return kCondGE;
2269 }
2270
Dave Allison20dfc792014-06-16 20:44:29 -07002271 private:
2272 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2273};
2274
2275class HLessThanOrEqual : public HCondition {
2276 public:
2277 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2278 : HCondition(first, second) {}
2279
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002280 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002281 return x <= y ? 1 : 0;
2282 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002283 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002284 return x <= y ? 1 : 0;
2285 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002286
Dave Allison20dfc792014-06-16 20:44:29 -07002287 DECLARE_INSTRUCTION(LessThanOrEqual);
2288
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002289 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002290 return kCondLE;
2291 }
2292
Mark Mendellc4701932015-04-10 13:18:51 -04002293 IfCondition GetOppositeCondition() const OVERRIDE {
2294 return kCondGT;
2295 }
2296
Dave Allison20dfc792014-06-16 20:44:29 -07002297 private:
2298 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2299};
2300
2301class HGreaterThan : public HCondition {
2302 public:
2303 HGreaterThan(HInstruction* first, HInstruction* second)
2304 : HCondition(first, second) {}
2305
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002306 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002307 return x > y ? 1 : 0;
2308 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002309 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002310 return x > y ? 1 : 0;
2311 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002312
Dave Allison20dfc792014-06-16 20:44:29 -07002313 DECLARE_INSTRUCTION(GreaterThan);
2314
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002315 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002316 return kCondGT;
2317 }
2318
Mark Mendellc4701932015-04-10 13:18:51 -04002319 IfCondition GetOppositeCondition() const OVERRIDE {
2320 return kCondLE;
2321 }
2322
Dave Allison20dfc792014-06-16 20:44:29 -07002323 private:
2324 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2325};
2326
2327class HGreaterThanOrEqual : public HCondition {
2328 public:
2329 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2330 : HCondition(first, second) {}
2331
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002332 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002333 return x >= y ? 1 : 0;
2334 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002335 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002336 return x >= y ? 1 : 0;
2337 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002338
Dave Allison20dfc792014-06-16 20:44:29 -07002339 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2340
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002341 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002342 return kCondGE;
2343 }
2344
Mark Mendellc4701932015-04-10 13:18:51 -04002345 IfCondition GetOppositeCondition() const OVERRIDE {
2346 return kCondLT;
2347 }
2348
Dave Allison20dfc792014-06-16 20:44:29 -07002349 private:
2350 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2351};
2352
2353
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002354// Instruction to check how two inputs compare to each other.
2355// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2356class HCompare : public HBinaryOperation {
2357 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002358 HCompare(Primitive::Type type,
2359 HInstruction* first,
2360 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002361 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002362 uint32_t dex_pc)
2363 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002364 DCHECK_EQ(type, first->GetType());
2365 DCHECK_EQ(type, second->GetType());
2366 }
2367
Calin Juravleddb7df22014-11-25 20:56:51 +00002368 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002369 return
2370 x == y ? 0 :
2371 x > y ? 1 :
2372 -1;
2373 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002374
2375 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002376 return
2377 x == y ? 0 :
2378 x > y ? 1 :
2379 -1;
2380 }
2381
Calin Juravleddb7df22014-11-25 20:56:51 +00002382 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2383 return bias_ == other->AsCompare()->bias_;
2384 }
2385
Mark Mendellc4701932015-04-10 13:18:51 -04002386 ComparisonBias GetBias() const { return bias_; }
2387
Roland Levillain4fa13f62015-07-06 18:11:54 +01002388 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002389
Alexey Frunze4dda3372015-06-01 18:31:49 -07002390 uint32_t GetDexPc() const { return dex_pc_; }
2391
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002392 DECLARE_INSTRUCTION(Compare);
2393
2394 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002395 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002396 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002397
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002398 DISALLOW_COPY_AND_ASSIGN(HCompare);
2399};
2400
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002401// A local in the graph. Corresponds to a Dex register.
2402class HLocal : public HTemplateInstruction<0> {
2403 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002404 explicit HLocal(uint16_t reg_number)
2405 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002406
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002407 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002408
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002409 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002410
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002411 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002412 // The Dex register number.
2413 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002414
2415 DISALLOW_COPY_AND_ASSIGN(HLocal);
2416};
2417
2418// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002419class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002420 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002421 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002422 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002423 SetRawInputAt(0, local);
2424 }
2425
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002426 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2427
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002428 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002429
2430 private:
2431 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2432};
2433
2434// Store a value in a given local. This instruction has two inputs: the value
2435// and the local.
2436class HStoreLocal : public HTemplateInstruction<2> {
2437 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002438 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002439 SetRawInputAt(0, local);
2440 SetRawInputAt(1, value);
2441 }
2442
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002443 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2444
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002445 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002446
2447 private:
2448 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2449};
2450
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002451class HConstant : public HExpression<0> {
2452 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002453 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2454
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002455 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002456
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002457 virtual bool IsMinusOne() const { return false; }
2458 virtual bool IsZero() const { return false; }
2459 virtual bool IsOne() const { return false; }
2460
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002461 DECLARE_INSTRUCTION(Constant);
2462
2463 private:
2464 DISALLOW_COPY_AND_ASSIGN(HConstant);
2465};
2466
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002467class HFloatConstant : public HConstant {
2468 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002469 float GetValue() const { return value_; }
2470
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002471 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002472 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2473 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002474 }
2475
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002476 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002477
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002478 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002479 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002480 }
2481 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002482 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002483 }
2484 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002485 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2486 }
2487 bool IsNaN() const {
2488 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002489 }
2490
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002491 DECLARE_INSTRUCTION(FloatConstant);
2492
2493 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002494 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002495 explicit HFloatConstant(int32_t value)
2496 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002497
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002498 const float value_;
2499
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002500 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002501 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002502 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002503 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2504};
2505
2506class HDoubleConstant : public HConstant {
2507 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002508 double GetValue() const { return value_; }
2509
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002510 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002511 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2512 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002513 }
2514
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002515 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002516
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002517 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002518 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002519 }
2520 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002521 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002522 }
2523 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002524 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2525 }
2526 bool IsNaN() const {
2527 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002528 }
2529
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002530 DECLARE_INSTRUCTION(DoubleConstant);
2531
2532 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002533 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002534 explicit HDoubleConstant(int64_t value)
2535 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002536
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002537 const double value_;
2538
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002539 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002540 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002541 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002542 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2543};
2544
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002545class HNullConstant : public HConstant {
2546 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002547 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2548 return true;
2549 }
2550
2551 size_t ComputeHashCode() const OVERRIDE { return 0; }
2552
2553 DECLARE_INSTRUCTION(NullConstant);
2554
2555 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002556 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2557
2558 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002559 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2560};
2561
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002562// Constants of the type int. Those can be from Dex instructions, or
2563// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002564class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002565 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002566 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002567
Calin Juravle61d544b2015-02-23 16:46:57 +00002568 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002569 return other->AsIntConstant()->value_ == value_;
2570 }
2571
Calin Juravle61d544b2015-02-23 16:46:57 +00002572 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2573
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002574 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2575 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2576 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2577
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002578 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002579
2580 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002581 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2582
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002583 const int32_t value_;
2584
David Brazdil8d5b8b22015-03-24 10:51:52 +00002585 friend class HGraph;
2586 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002587 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002588 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2589};
2590
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002591class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002592 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002593 int64_t GetValue() const { return value_; }
2594
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002595 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002596 return other->AsLongConstant()->value_ == value_;
2597 }
2598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002599 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002600
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002601 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2602 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2603 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2604
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002605 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002606
2607 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002608 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2609
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002610 const int64_t value_;
2611
David Brazdil8d5b8b22015-03-24 10:51:52 +00002612 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002613 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2614};
2615
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002616enum class Intrinsics {
2617#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2618#include "intrinsics_list.h"
2619 kNone,
2620 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2621#undef INTRINSICS_LIST
2622#undef OPTIMIZING_INTRINSICS
2623};
2624std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2625
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002626class HInvoke : public HInstruction {
2627 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002628 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002629
2630 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2631 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002632 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002633
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002634 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002635 SetRawInputAt(index, argument);
2636 }
2637
Roland Levillain3e3d7332015-04-28 11:00:54 +01002638 // Return the number of arguments. This number can be lower than
2639 // the number of inputs returned by InputCount(), as some invoke
2640 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2641 // inputs at the end of their list of inputs.
2642 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2643
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002644 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002645
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002646 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002647
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002648 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002649 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002650
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002651 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2652
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002653 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002654 return intrinsic_;
2655 }
2656
2657 void SetIntrinsic(Intrinsics intrinsic) {
2658 intrinsic_ = intrinsic;
2659 }
2660
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002661 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002662 return GetEnvironment()->GetParent() != nullptr;
2663 }
2664
2665 bool CanThrow() const OVERRIDE { return true; }
2666
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002667 DECLARE_INSTRUCTION(Invoke);
2668
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002669 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002670 HInvoke(ArenaAllocator* arena,
2671 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002672 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002673 Primitive::Type return_type,
2674 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002675 uint32_t dex_method_index,
2676 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002677 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002678 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002679 inputs_(arena, number_of_arguments),
2680 return_type_(return_type),
2681 dex_pc_(dex_pc),
2682 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002683 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002684 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002685 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2686 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002687 }
2688
David Brazdil1abb4192015-02-17 18:33:36 +00002689 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2690 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2691 inputs_.Put(index, input);
2692 }
2693
Roland Levillain3e3d7332015-04-28 11:00:54 +01002694 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002695 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002696 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002697 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002698 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002699 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002700 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002701
2702 private:
2703 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2704};
2705
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002706class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002707 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002708 // Requirements of this method call regarding the class
2709 // initialization (clinit) check of its declaring class.
2710 enum class ClinitCheckRequirement {
2711 kNone, // Class already initialized.
2712 kExplicit, // Static call having explicit clinit check as last input.
2713 kImplicit, // Static call implicitly requiring a clinit check.
2714 };
2715
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002716 HInvokeStaticOrDirect(ArenaAllocator* arena,
2717 uint32_t number_of_arguments,
2718 Primitive::Type return_type,
2719 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002720 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002721 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002722 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002723 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002724 InvokeType invoke_type,
2725 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002726 : HInvoke(arena,
2727 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002728 // There is one extra argument for the HCurrentMethod node, and
2729 // potentially one other if the clinit check is explicit, and one other
2730 // if the method is a string factory.
2731 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2732 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002733 return_type,
2734 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002735 dex_method_index,
2736 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002737 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002738 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002739 clinit_check_requirement_(clinit_check_requirement),
2740 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002741
Calin Juravle641547a2015-04-21 22:08:51 +01002742 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2743 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002744 // We access the method via the dex cache so we can't do an implicit null check.
2745 // TODO: for intrinsics we can generate implicit null checks.
2746 return false;
2747 }
2748
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002749 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002750 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002751 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002752 bool IsStringInit() const { return string_init_offset_ != 0; }
2753 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002754 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002755
Roland Levillain4c0eb422015-04-24 16:43:49 +01002756 // Is this instruction a call to a static method?
2757 bool IsStatic() const {
2758 return GetInvokeType() == kStatic;
2759 }
2760
Roland Levillain3e3d7332015-04-28 11:00:54 +01002761 // Remove the art::HLoadClass instruction set as last input by
2762 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2763 // the initial art::HClinitCheck instruction (only relevant for
2764 // static calls with explicit clinit check).
2765 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002766 DCHECK(IsStaticWithExplicitClinitCheck());
2767 size_t last_input_index = InputCount() - 1;
2768 HInstruction* last_input = InputAt(last_input_index);
2769 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002770 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002771 RemoveAsUserOfInput(last_input_index);
2772 inputs_.DeleteAt(last_input_index);
2773 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2774 DCHECK(IsStaticWithImplicitClinitCheck());
2775 }
2776
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002777 bool IsStringFactoryFor(HFakeString* str) const {
2778 if (!IsStringInit()) return false;
2779 // +1 for the current method.
2780 if (InputCount() == (number_of_arguments_ + 1)) return false;
2781 return InputAt(InputCount() - 1)->AsFakeString() == str;
2782 }
2783
2784 void RemoveFakeStringArgumentAsLastInput() {
2785 DCHECK(IsStringInit());
2786 size_t last_input_index = InputCount() - 1;
2787 HInstruction* last_input = InputAt(last_input_index);
2788 DCHECK(last_input != nullptr);
2789 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2790 RemoveAsUserOfInput(last_input_index);
2791 inputs_.DeleteAt(last_input_index);
2792 }
2793
Roland Levillain4c0eb422015-04-24 16:43:49 +01002794 // Is this a call to a static method whose declaring class has an
2795 // explicit intialization check in the graph?
2796 bool IsStaticWithExplicitClinitCheck() const {
2797 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2798 }
2799
2800 // Is this a call to a static method whose declaring class has an
2801 // implicit intialization check requirement?
2802 bool IsStaticWithImplicitClinitCheck() const {
2803 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2804 }
2805
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002806 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002807
Roland Levillain4c0eb422015-04-24 16:43:49 +01002808 protected:
2809 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2810 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2811 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2812 HInstruction* input = input_record.GetInstruction();
2813 // `input` is the last input of a static invoke marked as having
2814 // an explicit clinit check. It must either be:
2815 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2816 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2817 DCHECK(input != nullptr);
2818 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2819 }
2820 return input_record;
2821 }
2822
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002823 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002824 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002825 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002826 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002827 // Thread entrypoint offset for string init method if this is a string init invoke.
2828 // Note that there are multiple string init methods, each having its own offset.
2829 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002830
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002831 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002832};
2833
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002834class HInvokeVirtual : public HInvoke {
2835 public:
2836 HInvokeVirtual(ArenaAllocator* arena,
2837 uint32_t number_of_arguments,
2838 Primitive::Type return_type,
2839 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002840 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002841 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002842 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002843 vtable_index_(vtable_index) {}
2844
Calin Juravle641547a2015-04-21 22:08:51 +01002845 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002846 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002847 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002848 }
2849
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002850 uint32_t GetVTableIndex() const { return vtable_index_; }
2851
2852 DECLARE_INSTRUCTION(InvokeVirtual);
2853
2854 private:
2855 const uint32_t vtable_index_;
2856
2857 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2858};
2859
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002860class HInvokeInterface : public HInvoke {
2861 public:
2862 HInvokeInterface(ArenaAllocator* arena,
2863 uint32_t number_of_arguments,
2864 Primitive::Type return_type,
2865 uint32_t dex_pc,
2866 uint32_t dex_method_index,
2867 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002868 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002869 imt_index_(imt_index) {}
2870
Calin Juravle641547a2015-04-21 22:08:51 +01002871 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002872 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002873 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002874 }
2875
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002876 uint32_t GetImtIndex() const { return imt_index_; }
2877 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2878
2879 DECLARE_INSTRUCTION(InvokeInterface);
2880
2881 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002882 const uint32_t imt_index_;
2883
2884 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2885};
2886
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002887class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002888 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002889 HNewInstance(HCurrentMethod* current_method,
2890 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002891 uint16_t type_index,
2892 const DexFile& dex_file,
2893 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002894 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2895 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002896 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002897 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002898 entrypoint_(entrypoint) {
2899 SetRawInputAt(0, current_method);
2900 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002901
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002902 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002903 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002904 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002905
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002906 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002907 bool NeedsEnvironment() const OVERRIDE { return true; }
2908 // It may throw when called on:
2909 // - interfaces
2910 // - abstract/innaccessible/unknown classes
2911 // TODO: optimize when possible.
2912 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002913
Calin Juravle10e244f2015-01-26 18:54:32 +00002914 bool CanBeNull() const OVERRIDE { return false; }
2915
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002916 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2917
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002918 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002919
2920 private:
2921 const uint32_t dex_pc_;
2922 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002923 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002924 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002925
2926 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2927};
2928
Roland Levillain88cb1752014-10-20 16:36:47 +01002929class HNeg : public HUnaryOperation {
2930 public:
2931 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2932 : HUnaryOperation(result_type, input) {}
2933
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002934 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2935 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002936
Roland Levillain88cb1752014-10-20 16:36:47 +01002937 DECLARE_INSTRUCTION(Neg);
2938
2939 private:
2940 DISALLOW_COPY_AND_ASSIGN(HNeg);
2941};
2942
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002943class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002944 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002945 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002946 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002947 uint32_t dex_pc,
2948 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002949 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002950 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002951 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2952 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002953 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002954 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002955 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002956 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002957 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002958 }
2959
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002960 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002961 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002962 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002963
2964 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002965 bool NeedsEnvironment() const OVERRIDE { return true; }
2966
Mingyao Yang0c365e62015-03-31 15:09:29 -07002967 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2968 bool CanThrow() const OVERRIDE { return true; }
2969
Calin Juravle10e244f2015-01-26 18:54:32 +00002970 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002971
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002972 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2973
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002974 DECLARE_INSTRUCTION(NewArray);
2975
2976 private:
2977 const uint32_t dex_pc_;
2978 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002979 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002980 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002981
2982 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2983};
2984
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002985class HAdd : public HBinaryOperation {
2986 public:
2987 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2988 : HBinaryOperation(result_type, left, right) {}
2989
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002990 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002991
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002992 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002993 return x + y;
2994 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002995 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002996 return x + y;
2997 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002998
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002999 DECLARE_INSTRUCTION(Add);
3000
3001 private:
3002 DISALLOW_COPY_AND_ASSIGN(HAdd);
3003};
3004
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003005class HSub : public HBinaryOperation {
3006 public:
3007 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3008 : HBinaryOperation(result_type, left, right) {}
3009
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003010 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003011 return x - y;
3012 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003013 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003014 return x - y;
3015 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003016
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003017 DECLARE_INSTRUCTION(Sub);
3018
3019 private:
3020 DISALLOW_COPY_AND_ASSIGN(HSub);
3021};
3022
Calin Juravle34bacdf2014-10-07 20:23:36 +01003023class HMul : public HBinaryOperation {
3024 public:
3025 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3026 : HBinaryOperation(result_type, left, right) {}
3027
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003028 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003029
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003030 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3031 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003032
3033 DECLARE_INSTRUCTION(Mul);
3034
3035 private:
3036 DISALLOW_COPY_AND_ASSIGN(HMul);
3037};
3038
Calin Juravle7c4954d2014-10-28 16:57:40 +00003039class HDiv : public HBinaryOperation {
3040 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003041 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3042 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003043
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003044 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003045 // Our graph structure ensures we never have 0 for `y` during constant folding.
3046 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003047 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003048 return (y == -1) ? -x : x / y;
3049 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003050
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003051 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003052 DCHECK_NE(y, 0);
3053 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3054 return (y == -1) ? -x : x / y;
3055 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003056
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003057 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003058
Calin Juravle7c4954d2014-10-28 16:57:40 +00003059 DECLARE_INSTRUCTION(Div);
3060
3061 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003062 const uint32_t dex_pc_;
3063
Calin Juravle7c4954d2014-10-28 16:57:40 +00003064 DISALLOW_COPY_AND_ASSIGN(HDiv);
3065};
3066
Calin Juravlebacfec32014-11-14 15:54:36 +00003067class HRem : public HBinaryOperation {
3068 public:
3069 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3070 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3071
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003072 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003073 DCHECK_NE(y, 0);
3074 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3075 return (y == -1) ? 0 : x % y;
3076 }
3077
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003078 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003079 DCHECK_NE(y, 0);
3080 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3081 return (y == -1) ? 0 : x % y;
3082 }
3083
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003084 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003085
3086 DECLARE_INSTRUCTION(Rem);
3087
3088 private:
3089 const uint32_t dex_pc_;
3090
3091 DISALLOW_COPY_AND_ASSIGN(HRem);
3092};
3093
Calin Juravled0d48522014-11-04 16:40:20 +00003094class HDivZeroCheck : public HExpression<1> {
3095 public:
3096 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3097 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3098 SetRawInputAt(0, value);
3099 }
3100
3101 bool CanBeMoved() const OVERRIDE { return true; }
3102
3103 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3104 UNUSED(other);
3105 return true;
3106 }
3107
3108 bool NeedsEnvironment() const OVERRIDE { return true; }
3109 bool CanThrow() const OVERRIDE { return true; }
3110
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003111 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003112
3113 DECLARE_INSTRUCTION(DivZeroCheck);
3114
3115 private:
3116 const uint32_t dex_pc_;
3117
3118 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3119};
3120
Calin Juravle9aec02f2014-11-18 23:06:35 +00003121class HShl : public HBinaryOperation {
3122 public:
3123 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3124 : HBinaryOperation(result_type, left, right) {}
3125
3126 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3127 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3128
3129 DECLARE_INSTRUCTION(Shl);
3130
3131 private:
3132 DISALLOW_COPY_AND_ASSIGN(HShl);
3133};
3134
3135class HShr : public HBinaryOperation {
3136 public:
3137 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3138 : HBinaryOperation(result_type, left, right) {}
3139
3140 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3141 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3142
3143 DECLARE_INSTRUCTION(Shr);
3144
3145 private:
3146 DISALLOW_COPY_AND_ASSIGN(HShr);
3147};
3148
3149class HUShr : public HBinaryOperation {
3150 public:
3151 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3152 : HBinaryOperation(result_type, left, right) {}
3153
3154 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3155 uint32_t ux = static_cast<uint32_t>(x);
3156 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3157 return static_cast<int32_t>(ux >> uy);
3158 }
3159
3160 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3161 uint64_t ux = static_cast<uint64_t>(x);
3162 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3163 return static_cast<int64_t>(ux >> uy);
3164 }
3165
3166 DECLARE_INSTRUCTION(UShr);
3167
3168 private:
3169 DISALLOW_COPY_AND_ASSIGN(HUShr);
3170};
3171
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003172class HAnd : public HBinaryOperation {
3173 public:
3174 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3175 : HBinaryOperation(result_type, left, right) {}
3176
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003177 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003178
3179 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3180 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3181
3182 DECLARE_INSTRUCTION(And);
3183
3184 private:
3185 DISALLOW_COPY_AND_ASSIGN(HAnd);
3186};
3187
3188class HOr : public HBinaryOperation {
3189 public:
3190 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3191 : HBinaryOperation(result_type, left, right) {}
3192
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003193 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003194
3195 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3196 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3197
3198 DECLARE_INSTRUCTION(Or);
3199
3200 private:
3201 DISALLOW_COPY_AND_ASSIGN(HOr);
3202};
3203
3204class HXor : public HBinaryOperation {
3205 public:
3206 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3207 : HBinaryOperation(result_type, left, right) {}
3208
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003209 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003210
3211 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3212 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3213
3214 DECLARE_INSTRUCTION(Xor);
3215
3216 private:
3217 DISALLOW_COPY_AND_ASSIGN(HXor);
3218};
3219
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003220// The value of a parameter in this method. Its location depends on
3221// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003222class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003223 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003224 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3225 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003226
3227 uint8_t GetIndex() const { return index_; }
3228
Calin Juravle10e244f2015-01-26 18:54:32 +00003229 bool CanBeNull() const OVERRIDE { return !is_this_; }
3230
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003231 bool IsThis() const { return is_this_; }
3232
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003233 DECLARE_INSTRUCTION(ParameterValue);
3234
3235 private:
3236 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003237 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003238 const uint8_t index_;
3239
Calin Juravle10e244f2015-01-26 18:54:32 +00003240 // Whether or not the parameter value corresponds to 'this' argument.
3241 const bool is_this_;
3242
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003243 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3244};
3245
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003246class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003247 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003248 explicit HNot(Primitive::Type result_type, HInstruction* input)
3249 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003250
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003251 bool CanBeMoved() const OVERRIDE { return true; }
3252 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003253 UNUSED(other);
3254 return true;
3255 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003256
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003257 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3258 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003259
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003260 DECLARE_INSTRUCTION(Not);
3261
3262 private:
3263 DISALLOW_COPY_AND_ASSIGN(HNot);
3264};
3265
David Brazdil66d126e2015-04-03 16:02:44 +01003266class HBooleanNot : public HUnaryOperation {
3267 public:
3268 explicit HBooleanNot(HInstruction* input)
3269 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3270
3271 bool CanBeMoved() const OVERRIDE { return true; }
3272 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3273 UNUSED(other);
3274 return true;
3275 }
3276
3277 int32_t Evaluate(int32_t x) const OVERRIDE {
3278 DCHECK(IsUint<1>(x));
3279 return !x;
3280 }
3281
3282 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3283 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3284 UNREACHABLE();
3285 }
3286
3287 DECLARE_INSTRUCTION(BooleanNot);
3288
3289 private:
3290 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3291};
3292
Roland Levillaindff1f282014-11-05 14:15:05 +00003293class HTypeConversion : public HExpression<1> {
3294 public:
3295 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003296 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3297 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003298 SetRawInputAt(0, input);
3299 DCHECK_NE(input->GetType(), result_type);
3300 }
3301
3302 HInstruction* GetInput() const { return InputAt(0); }
3303 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3304 Primitive::Type GetResultType() const { return GetType(); }
3305
Roland Levillain624279f2014-12-04 11:54:28 +00003306 // Required by the x86 and ARM code generators when producing calls
3307 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003308 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003309
Roland Levillaindff1f282014-11-05 14:15:05 +00003310 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003311 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003312
Mark Mendelle82549b2015-05-06 10:55:34 -04003313 // Try to statically evaluate the conversion and return a HConstant
3314 // containing the result. If the input cannot be converted, return nullptr.
3315 HConstant* TryStaticEvaluation() const;
3316
Roland Levillaindff1f282014-11-05 14:15:05 +00003317 DECLARE_INSTRUCTION(TypeConversion);
3318
3319 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003320 const uint32_t dex_pc_;
3321
Roland Levillaindff1f282014-11-05 14:15:05 +00003322 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3323};
3324
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003325static constexpr uint32_t kNoRegNumber = -1;
3326
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003327class HPhi : public HInstruction {
3328 public:
3329 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003330 : HInstruction(SideEffects::None()),
3331 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003332 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003333 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003334 is_live_(false),
3335 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003336 inputs_.SetSize(number_of_inputs);
3337 }
3338
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003339 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3340 static Primitive::Type ToPhiType(Primitive::Type type) {
3341 switch (type) {
3342 case Primitive::kPrimBoolean:
3343 case Primitive::kPrimByte:
3344 case Primitive::kPrimShort:
3345 case Primitive::kPrimChar:
3346 return Primitive::kPrimInt;
3347 default:
3348 return type;
3349 }
3350 }
3351
Calin Juravle10e244f2015-01-26 18:54:32 +00003352 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003353
3354 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003355 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003356
Calin Juravle10e244f2015-01-26 18:54:32 +00003357 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003358 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003359
Calin Juravle10e244f2015-01-26 18:54:32 +00003360 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3361 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3362
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003363 uint32_t GetRegNumber() const { return reg_number_; }
3364
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003365 void SetDead() { is_live_ = false; }
3366 void SetLive() { is_live_ = true; }
3367 bool IsDead() const { return !is_live_; }
3368 bool IsLive() const { return is_live_; }
3369
Calin Juravlea4f88312015-04-16 12:57:19 +01003370 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3371 // An equivalent phi is a phi having the same dex register and type.
3372 // It assumes that phis with the same dex register are adjacent.
3373 HPhi* GetNextEquivalentPhiWithSameType() {
3374 HInstruction* next = GetNext();
3375 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3376 if (next->GetType() == GetType()) {
3377 return next->AsPhi();
3378 }
3379 next = next->GetNext();
3380 }
3381 return nullptr;
3382 }
3383
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003384 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003385
David Brazdil1abb4192015-02-17 18:33:36 +00003386 protected:
3387 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3388
3389 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3390 inputs_.Put(index, input);
3391 }
3392
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003393 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003394 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003395 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003396 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003397 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003398 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003399
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003400 DISALLOW_COPY_AND_ASSIGN(HPhi);
3401};
3402
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003403class HNullCheck : public HExpression<1> {
3404 public:
3405 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003406 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003407 SetRawInputAt(0, value);
3408 }
3409
Calin Juravle10e244f2015-01-26 18:54:32 +00003410 bool CanBeMoved() const OVERRIDE { return true; }
3411 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003412 UNUSED(other);
3413 return true;
3414 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003415
Calin Juravle10e244f2015-01-26 18:54:32 +00003416 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003417
Calin Juravle10e244f2015-01-26 18:54:32 +00003418 bool CanThrow() const OVERRIDE { return true; }
3419
3420 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003421
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003422 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003423
3424 DECLARE_INSTRUCTION(NullCheck);
3425
3426 private:
3427 const uint32_t dex_pc_;
3428
3429 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3430};
3431
3432class FieldInfo : public ValueObject {
3433 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003434 FieldInfo(MemberOffset field_offset,
3435 Primitive::Type field_type,
3436 bool is_volatile,
3437 uint32_t index,
3438 const DexFile& dex_file)
3439 : field_offset_(field_offset),
3440 field_type_(field_type),
3441 is_volatile_(is_volatile),
3442 index_(index),
3443 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003444
3445 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003446 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003447 uint32_t GetFieldIndex() const { return index_; }
3448 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003449 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003450
3451 private:
3452 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003453 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003454 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003455 uint32_t index_;
3456 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003457};
3458
3459class HInstanceFieldGet : public HExpression<1> {
3460 public:
3461 HInstanceFieldGet(HInstruction* value,
3462 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003463 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003464 bool is_volatile,
3465 uint32_t field_idx,
3466 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003467 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003468 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003469 SetRawInputAt(0, value);
3470 }
3471
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003472 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003473
3474 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3475 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3476 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003477 }
3478
Calin Juravle641547a2015-04-21 22:08:51 +01003479 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3480 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003481 }
3482
3483 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003484 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3485 }
3486
Calin Juravle52c48962014-12-16 17:02:57 +00003487 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003488 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003489 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003490 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003491
3492 DECLARE_INSTRUCTION(InstanceFieldGet);
3493
3494 private:
3495 const FieldInfo field_info_;
3496
3497 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3498};
3499
3500class HInstanceFieldSet : public HTemplateInstruction<2> {
3501 public:
3502 HInstanceFieldSet(HInstruction* object,
3503 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003504 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003505 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003506 bool is_volatile,
3507 uint32_t field_idx,
3508 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003509 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003510 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003511 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003512 SetRawInputAt(0, object);
3513 SetRawInputAt(1, value);
3514 }
3515
Calin Juravle641547a2015-04-21 22:08:51 +01003516 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3517 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003518 }
3519
Calin Juravle52c48962014-12-16 17:02:57 +00003520 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003521 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003522 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003523 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003524 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003525 bool GetValueCanBeNull() const { return value_can_be_null_; }
3526 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003527
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003528 DECLARE_INSTRUCTION(InstanceFieldSet);
3529
3530 private:
3531 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003532 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003533
3534 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3535};
3536
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003537class HArrayGet : public HExpression<2> {
3538 public:
3539 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003540 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003541 SetRawInputAt(0, array);
3542 SetRawInputAt(1, index);
3543 }
3544
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003545 bool CanBeMoved() const OVERRIDE { return true; }
3546 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003547 UNUSED(other);
3548 return true;
3549 }
Calin Juravle641547a2015-04-21 22:08:51 +01003550 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3551 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003552 // TODO: We can be smarter here.
3553 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3554 // which generates the implicit null check. There are cases when these can be removed
3555 // to produce better code. If we ever add optimizations to do so we should allow an
3556 // implicit check here (as long as the address falls in the first page).
3557 return false;
3558 }
3559
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003560 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003561
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003562 HInstruction* GetArray() const { return InputAt(0); }
3563 HInstruction* GetIndex() const { return InputAt(1); }
3564
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003565 DECLARE_INSTRUCTION(ArrayGet);
3566
3567 private:
3568 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3569};
3570
3571class HArraySet : public HTemplateInstruction<3> {
3572 public:
3573 HArraySet(HInstruction* array,
3574 HInstruction* index,
3575 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003576 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003577 uint32_t dex_pc)
3578 : HTemplateInstruction(SideEffects::ChangesSomething()),
3579 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003580 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003581 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3582 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003583 SetRawInputAt(0, array);
3584 SetRawInputAt(1, index);
3585 SetRawInputAt(2, value);
3586 }
3587
Calin Juravle77520bc2015-01-12 18:45:46 +00003588 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003589 // We currently always call a runtime method to catch array store
3590 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003591 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003592 }
3593
Mingyao Yang81014cb2015-06-02 03:16:27 -07003594 // Can throw ArrayStoreException.
3595 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3596
Calin Juravle641547a2015-04-21 22:08:51 +01003597 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3598 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003599 // TODO: Same as for ArrayGet.
3600 return false;
3601 }
3602
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003603 void ClearNeedsTypeCheck() {
3604 needs_type_check_ = false;
3605 }
3606
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003607 void ClearValueCanBeNull() {
3608 value_can_be_null_ = false;
3609 }
3610
3611 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003612 bool NeedsTypeCheck() const { return needs_type_check_; }
3613
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003614 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003615
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003616 HInstruction* GetArray() const { return InputAt(0); }
3617 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003618 HInstruction* GetValue() const { return InputAt(2); }
3619
3620 Primitive::Type GetComponentType() const {
3621 // The Dex format does not type floating point index operations. Since the
3622 // `expected_component_type_` is set during building and can therefore not
3623 // be correct, we also check what is the value type. If it is a floating
3624 // point type, we must use that type.
3625 Primitive::Type value_type = GetValue()->GetType();
3626 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3627 ? value_type
3628 : expected_component_type_;
3629 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003630
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003631 DECLARE_INSTRUCTION(ArraySet);
3632
3633 private:
3634 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003635 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003636 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003637 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003638
3639 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3640};
3641
3642class HArrayLength : public HExpression<1> {
3643 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003644 explicit HArrayLength(HInstruction* array)
3645 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3646 // Note that arrays do not change length, so the instruction does not
3647 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003648 SetRawInputAt(0, array);
3649 }
3650
Calin Juravle77520bc2015-01-12 18:45:46 +00003651 bool CanBeMoved() const OVERRIDE { return true; }
3652 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003653 UNUSED(other);
3654 return true;
3655 }
Calin Juravle641547a2015-04-21 22:08:51 +01003656 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3657 return obj == InputAt(0);
3658 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003659
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003660 DECLARE_INSTRUCTION(ArrayLength);
3661
3662 private:
3663 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3664};
3665
3666class HBoundsCheck : public HExpression<2> {
3667 public:
3668 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003669 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003670 DCHECK(index->GetType() == Primitive::kPrimInt);
3671 SetRawInputAt(0, index);
3672 SetRawInputAt(1, length);
3673 }
3674
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003675 bool CanBeMoved() const OVERRIDE { return true; }
3676 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003677 UNUSED(other);
3678 return true;
3679 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003680
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003681 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003682
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003683 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003684
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003685 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003686
3687 DECLARE_INSTRUCTION(BoundsCheck);
3688
3689 private:
3690 const uint32_t dex_pc_;
3691
3692 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3693};
3694
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003695/**
3696 * Some DEX instructions are folded into multiple HInstructions that need
3697 * to stay live until the last HInstruction. This class
3698 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003699 * HInstruction stays live. `index` represents the stack location index of the
3700 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003701 */
3702class HTemporary : public HTemplateInstruction<0> {
3703 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003704 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003705
3706 size_t GetIndex() const { return index_; }
3707
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003708 Primitive::Type GetType() const OVERRIDE {
3709 // The previous instruction is the one that will be stored in the temporary location.
3710 DCHECK(GetPrevious() != nullptr);
3711 return GetPrevious()->GetType();
3712 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003713
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003714 DECLARE_INSTRUCTION(Temporary);
3715
3716 private:
3717 const size_t index_;
3718
3719 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3720};
3721
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003722class HSuspendCheck : public HTemplateInstruction<0> {
3723 public:
3724 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003725 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003726
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003727 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003728 return true;
3729 }
3730
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003731 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003732 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3733 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003734
3735 DECLARE_INSTRUCTION(SuspendCheck);
3736
3737 private:
3738 const uint32_t dex_pc_;
3739
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003740 // Only used for code generation, in order to share the same slow path between back edges
3741 // of a same loop.
3742 SlowPathCode* slow_path_;
3743
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003744 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3745};
3746
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003747/**
3748 * Instruction to load a Class object.
3749 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003750class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003751 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003752 HLoadClass(HCurrentMethod* current_method,
3753 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003754 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003755 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003756 uint32_t dex_pc)
3757 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3758 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003759 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003760 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003761 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003762 generate_clinit_check_(false),
Calin Juravle3fabec72015-07-16 16:51:30 +01003763 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003764 SetRawInputAt(0, current_method);
3765 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003766
3767 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003768
3769 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3770 return other->AsLoadClass()->type_index_ == type_index_;
3771 }
3772
3773 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3774
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003775 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003776 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003777 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003778 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003779
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003780 bool NeedsEnvironment() const OVERRIDE {
3781 // Will call runtime and load the class if the class is not loaded yet.
3782 // TODO: finer grain decision.
3783 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003784 }
3785
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003786 bool MustGenerateClinitCheck() const {
3787 return generate_clinit_check_;
3788 }
3789
Calin Juravle0ba218d2015-05-19 18:46:01 +01003790 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3791 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003792 }
3793
3794 bool CanCallRuntime() const {
3795 return MustGenerateClinitCheck() || !is_referrers_class_;
3796 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003797
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003798 bool CanThrow() const OVERRIDE {
3799 // May call runtime and and therefore can throw.
3800 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003801 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003802 }
3803
Calin Juravleacf735c2015-02-12 15:25:22 +00003804 ReferenceTypeInfo GetLoadedClassRTI() {
3805 return loaded_class_rti_;
3806 }
3807
3808 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3809 // Make sure we only set exact types (the loaded class should never be merged).
3810 DCHECK(rti.IsExact());
3811 loaded_class_rti_ = rti;
3812 }
3813
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003814 const DexFile& GetDexFile() { return dex_file_; }
3815
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003816 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3817
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003818 DECLARE_INSTRUCTION(LoadClass);
3819
3820 private:
3821 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003822 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003823 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003824 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003825 // Whether this instruction must generate the initialization check.
3826 // Used for code generation.
3827 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003828
Calin Juravleacf735c2015-02-12 15:25:22 +00003829 ReferenceTypeInfo loaded_class_rti_;
3830
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003831 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3832};
3833
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003834class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003835 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003836 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003837 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3838 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003839 dex_pc_(dex_pc) {
3840 SetRawInputAt(0, current_method);
3841 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003842
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003843 bool CanBeMoved() const OVERRIDE { return true; }
3844
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003845 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3846 return other->AsLoadString()->string_index_ == string_index_;
3847 }
3848
3849 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3850
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003851 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003852 uint32_t GetStringIndex() const { return string_index_; }
3853
3854 // TODO: Can we deopt or debug when we resolve a string?
3855 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003856 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003857
3858 DECLARE_INSTRUCTION(LoadString);
3859
3860 private:
3861 const uint32_t string_index_;
3862 const uint32_t dex_pc_;
3863
3864 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3865};
3866
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003867/**
3868 * Performs an initialization check on its Class object input.
3869 */
3870class HClinitCheck : public HExpression<1> {
3871 public:
3872 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003873 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003874 dex_pc_(dex_pc) {
3875 SetRawInputAt(0, constant);
3876 }
3877
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003878 bool CanBeMoved() const OVERRIDE { return true; }
3879 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3880 UNUSED(other);
3881 return true;
3882 }
3883
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003884 bool NeedsEnvironment() const OVERRIDE {
3885 // May call runtime to initialize the class.
3886 return true;
3887 }
3888
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003889 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003890
3891 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3892
3893 DECLARE_INSTRUCTION(ClinitCheck);
3894
3895 private:
3896 const uint32_t dex_pc_;
3897
3898 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3899};
3900
3901class HStaticFieldGet : public HExpression<1> {
3902 public:
3903 HStaticFieldGet(HInstruction* cls,
3904 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003905 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003906 bool is_volatile,
3907 uint32_t field_idx,
3908 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003909 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003910 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003911 SetRawInputAt(0, cls);
3912 }
3913
Calin Juravle52c48962014-12-16 17:02:57 +00003914
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003915 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003916
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003917 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003918 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3919 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003920 }
3921
3922 size_t ComputeHashCode() const OVERRIDE {
3923 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3924 }
3925
Calin Juravle52c48962014-12-16 17:02:57 +00003926 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003927 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3928 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003929 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003930
3931 DECLARE_INSTRUCTION(StaticFieldGet);
3932
3933 private:
3934 const FieldInfo field_info_;
3935
3936 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3937};
3938
3939class HStaticFieldSet : public HTemplateInstruction<2> {
3940 public:
3941 HStaticFieldSet(HInstruction* cls,
3942 HInstruction* value,
3943 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003944 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003945 bool is_volatile,
3946 uint32_t field_idx,
3947 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003948 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003949 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003950 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003951 SetRawInputAt(0, cls);
3952 SetRawInputAt(1, value);
3953 }
3954
Calin Juravle52c48962014-12-16 17:02:57 +00003955 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003956 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3957 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003958 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003959
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003960 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003961 bool GetValueCanBeNull() const { return value_can_be_null_; }
3962 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003963
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003964 DECLARE_INSTRUCTION(StaticFieldSet);
3965
3966 private:
3967 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003968 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003969
3970 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3971};
3972
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003973// Implement the move-exception DEX instruction.
3974class HLoadException : public HExpression<0> {
3975 public:
3976 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3977
3978 DECLARE_INSTRUCTION(LoadException);
3979
3980 private:
3981 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3982};
3983
3984class HThrow : public HTemplateInstruction<1> {
3985 public:
3986 HThrow(HInstruction* exception, uint32_t dex_pc)
3987 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3988 SetRawInputAt(0, exception);
3989 }
3990
3991 bool IsControlFlow() const OVERRIDE { return true; }
3992
3993 bool NeedsEnvironment() const OVERRIDE { return true; }
3994
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003995 bool CanThrow() const OVERRIDE { return true; }
3996
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003997 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003998
3999 DECLARE_INSTRUCTION(Throw);
4000
4001 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004002 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004003
4004 DISALLOW_COPY_AND_ASSIGN(HThrow);
4005};
4006
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004007class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004008 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004009 HInstanceOf(HInstruction* object,
4010 HLoadClass* constant,
4011 bool class_is_final,
4012 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004013 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4014 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004015 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004016 dex_pc_(dex_pc) {
4017 SetRawInputAt(0, object);
4018 SetRawInputAt(1, constant);
4019 }
4020
4021 bool CanBeMoved() const OVERRIDE { return true; }
4022
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004023 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004024 return true;
4025 }
4026
4027 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004028 return false;
4029 }
4030
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004031 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004032
4033 bool IsClassFinal() const { return class_is_final_; }
4034
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004035 // Used only in code generation.
4036 bool MustDoNullCheck() const { return must_do_null_check_; }
4037 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4038
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004039 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004040
4041 private:
4042 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004043 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004044 const uint32_t dex_pc_;
4045
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004046 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4047};
4048
Calin Juravleb1498f62015-02-16 13:13:29 +00004049class HBoundType : public HExpression<1> {
4050 public:
Calin Juravle3fabec72015-07-16 16:51:30 +01004051 // Constructs an HBoundType with the given upper_bound.
4052 // Ensures that the upper_bound is valid.
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004053 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004054 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004055 upper_bound_(upper_bound),
4056 upper_can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004057 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004058 SetRawInputAt(0, input);
Calin Juravle3fabec72015-07-16 16:51:30 +01004059 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004060 }
4061
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004062 // GetUpper* should only be used in reference type propagation.
4063 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4064 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004065
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004066 void SetCanBeNull(bool can_be_null) {
4067 DCHECK(upper_can_be_null_ || !can_be_null);
4068 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004069 }
4070
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004071 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4072
Calin Juravleb1498f62015-02-16 13:13:29 +00004073 DECLARE_INSTRUCTION(BoundType);
4074
4075 private:
4076 // Encodes the most upper class that this instruction can have. In other words
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004077 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4078 // It is used to bound the type in cases like:
4079 // if (x instanceof ClassX) {
4080 // // uper_bound_ will be ClassX
4081 // }
4082 const ReferenceTypeInfo upper_bound_;
4083 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4084 // is false then can_be_null_ cannot be true).
4085 const bool upper_can_be_null_;
4086 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004087
4088 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4089};
4090
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004091class HCheckCast : public HTemplateInstruction<2> {
4092 public:
4093 HCheckCast(HInstruction* object,
4094 HLoadClass* constant,
4095 bool class_is_final,
4096 uint32_t dex_pc)
4097 : HTemplateInstruction(SideEffects::None()),
4098 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004099 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004100 dex_pc_(dex_pc) {
4101 SetRawInputAt(0, object);
4102 SetRawInputAt(1, constant);
4103 }
4104
4105 bool CanBeMoved() const OVERRIDE { return true; }
4106
4107 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4108 return true;
4109 }
4110
4111 bool NeedsEnvironment() const OVERRIDE {
4112 // Instruction may throw a CheckCastError.
4113 return true;
4114 }
4115
4116 bool CanThrow() const OVERRIDE { return true; }
4117
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004118 bool MustDoNullCheck() const { return must_do_null_check_; }
4119 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4120
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004121 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004122
4123 bool IsClassFinal() const { return class_is_final_; }
4124
4125 DECLARE_INSTRUCTION(CheckCast);
4126
4127 private:
4128 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004129 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004130 const uint32_t dex_pc_;
4131
4132 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004133};
4134
Calin Juravle27df7582015-04-17 19:12:31 +01004135class HMemoryBarrier : public HTemplateInstruction<0> {
4136 public:
4137 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
4138 : HTemplateInstruction(SideEffects::None()),
4139 barrier_kind_(barrier_kind) {}
4140
4141 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4142
4143 DECLARE_INSTRUCTION(MemoryBarrier);
4144
4145 private:
4146 const MemBarrierKind barrier_kind_;
4147
4148 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4149};
4150
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004151class HMonitorOperation : public HTemplateInstruction<1> {
4152 public:
4153 enum OperationKind {
4154 kEnter,
4155 kExit,
4156 };
4157
4158 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
David Brazdilbff75032015-07-08 17:26:51 +00004159 : HTemplateInstruction(SideEffects::ChangesSomething()), kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004160 SetRawInputAt(0, object);
4161 }
4162
4163 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004164 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4165
4166 bool CanThrow() const OVERRIDE {
4167 // Verifier guarantees that monitor-exit cannot throw.
4168 // This is important because it allows the HGraphBuilder to remove
4169 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4170 return IsEnter();
4171 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004172
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004173 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004174
4175 bool IsEnter() const { return kind_ == kEnter; }
4176
4177 DECLARE_INSTRUCTION(MonitorOperation);
4178
Calin Juravle52c48962014-12-16 17:02:57 +00004179 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004180 const OperationKind kind_;
4181 const uint32_t dex_pc_;
4182
4183 private:
4184 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4185};
4186
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004187/**
4188 * A HInstruction used as a marker for the replacement of new + <init>
4189 * of a String to a call to a StringFactory. Only baseline will see
4190 * the node at code generation, where it will be be treated as null.
4191 * When compiling non-baseline, `HFakeString` instructions are being removed
4192 * in the instruction simplifier.
4193 */
4194class HFakeString : public HTemplateInstruction<0> {
4195 public:
4196 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4197
4198 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4199
4200 DECLARE_INSTRUCTION(FakeString);
4201
4202 private:
4203 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4204};
4205
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004206class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004207 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004208 MoveOperands(Location source,
4209 Location destination,
4210 Primitive::Type type,
4211 HInstruction* instruction)
4212 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004213
4214 Location GetSource() const { return source_; }
4215 Location GetDestination() const { return destination_; }
4216
4217 void SetSource(Location value) { source_ = value; }
4218 void SetDestination(Location value) { destination_ = value; }
4219
4220 // The parallel move resolver marks moves as "in-progress" by clearing the
4221 // destination (but not the source).
4222 Location MarkPending() {
4223 DCHECK(!IsPending());
4224 Location dest = destination_;
4225 destination_ = Location::NoLocation();
4226 return dest;
4227 }
4228
4229 void ClearPending(Location dest) {
4230 DCHECK(IsPending());
4231 destination_ = dest;
4232 }
4233
4234 bool IsPending() const {
4235 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4236 return destination_.IsInvalid() && !source_.IsInvalid();
4237 }
4238
4239 // True if this blocks a move from the given location.
4240 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004241 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004242 }
4243
4244 // A move is redundant if it's been eliminated, if its source and
4245 // destination are the same, or if its destination is unneeded.
4246 bool IsRedundant() const {
4247 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4248 }
4249
4250 // We clear both operands to indicate move that's been eliminated.
4251 void Eliminate() {
4252 source_ = destination_ = Location::NoLocation();
4253 }
4254
4255 bool IsEliminated() const {
4256 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4257 return source_.IsInvalid();
4258 }
4259
Alexey Frunze4dda3372015-06-01 18:31:49 -07004260 Primitive::Type GetType() const { return type_; }
4261
Nicolas Geoffray90218252015-04-15 11:56:51 +01004262 bool Is64BitMove() const {
4263 return Primitive::Is64BitType(type_);
4264 }
4265
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004266 HInstruction* GetInstruction() const { return instruction_; }
4267
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004268 private:
4269 Location source_;
4270 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004271 // The type this move is for.
4272 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004273 // The instruction this move is assocatied with. Null when this move is
4274 // for moving an input in the expected locations of user (including a phi user).
4275 // This is only used in debug mode, to ensure we do not connect interval siblings
4276 // in the same parallel move.
4277 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004278};
4279
4280static constexpr size_t kDefaultNumberOfMoves = 4;
4281
4282class HParallelMove : public HTemplateInstruction<0> {
4283 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004284 explicit HParallelMove(ArenaAllocator* arena)
4285 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004286
Nicolas Geoffray90218252015-04-15 11:56:51 +01004287 void AddMove(Location source,
4288 Location destination,
4289 Primitive::Type type,
4290 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004291 DCHECK(source.IsValid());
4292 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004293 if (kIsDebugBuild) {
4294 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004295 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004296 if (moves_.Get(i).GetInstruction() == instruction) {
4297 // Special case the situation where the move is for the spill slot
4298 // of the instruction.
4299 if ((GetPrevious() == instruction)
4300 || ((GetPrevious() == nullptr)
4301 && instruction->IsPhi()
4302 && instruction->GetBlock() == GetBlock())) {
4303 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4304 << "Doing parallel moves for the same instruction.";
4305 } else {
4306 DCHECK(false) << "Doing parallel moves for the same instruction.";
4307 }
4308 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004309 }
4310 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004311 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004312 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004313 << "Overlapped destination for two moves in a parallel move: "
4314 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4315 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004316 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004317 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004318 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004319 }
4320
4321 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004322 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004323 }
4324
4325 size_t NumMoves() const { return moves_.Size(); }
4326
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004327 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004328
4329 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004330 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004331
4332 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4333};
4334
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004335class HGraphVisitor : public ValueObject {
4336 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004337 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4338 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004339
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004340 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004341 virtual void VisitBasicBlock(HBasicBlock* block);
4342
Roland Levillain633021e2014-10-01 14:12:25 +01004343 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004344 void VisitInsertionOrder();
4345
Roland Levillain633021e2014-10-01 14:12:25 +01004346 // Visit the graph following dominator tree reverse post-order.
4347 void VisitReversePostOrder();
4348
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004349 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004350
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004351 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004352#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004353 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4354
4355 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4356
4357#undef DECLARE_VISIT_INSTRUCTION
4358
4359 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004360 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004361
4362 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4363};
4364
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004365class HGraphDelegateVisitor : public HGraphVisitor {
4366 public:
4367 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4368 virtual ~HGraphDelegateVisitor() {}
4369
4370 // Visit functions that delegate to to super class.
4371#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004372 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004373
4374 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4375
4376#undef DECLARE_VISIT_INSTRUCTION
4377
4378 private:
4379 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4380};
4381
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004382class HInsertionOrderIterator : public ValueObject {
4383 public:
4384 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4385
4386 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4387 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4388 void Advance() { ++index_; }
4389
4390 private:
4391 const HGraph& graph_;
4392 size_t index_;
4393
4394 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4395};
4396
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004397class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004398 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004399 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4400 // Check that reverse post order of the graph has been built.
4401 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4402 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004403
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004404 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4405 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004406 void Advance() { ++index_; }
4407
4408 private:
4409 const HGraph& graph_;
4410 size_t index_;
4411
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004412 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004413};
4414
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004415class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004416 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004417 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004418 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4419 // Check that reverse post order of the graph has been built.
4420 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4421 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004422
4423 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004424 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004425 void Advance() { --index_; }
4426
4427 private:
4428 const HGraph& graph_;
4429 size_t index_;
4430
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004431 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004432};
4433
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004434class HLinearPostOrderIterator : public ValueObject {
4435 public:
4436 explicit HLinearPostOrderIterator(const HGraph& graph)
4437 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4438
4439 bool Done() const { return index_ == 0; }
4440
4441 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4442
4443 void Advance() {
4444 --index_;
4445 DCHECK_GE(index_, 0U);
4446 }
4447
4448 private:
4449 const GrowableArray<HBasicBlock*>& order_;
4450 size_t index_;
4451
4452 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4453};
4454
4455class HLinearOrderIterator : public ValueObject {
4456 public:
4457 explicit HLinearOrderIterator(const HGraph& graph)
4458 : order_(graph.GetLinearOrder()), index_(0) {}
4459
4460 bool Done() const { return index_ == order_.Size(); }
4461 HBasicBlock* Current() const { return order_.Get(index_); }
4462 void Advance() { ++index_; }
4463
4464 private:
4465 const GrowableArray<HBasicBlock*>& order_;
4466 size_t index_;
4467
4468 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4469};
4470
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004471// Iterator over the blocks that art part of the loop. Includes blocks part
4472// of an inner loop. The order in which the blocks are iterated is on their
4473// block id.
4474class HBlocksInLoopIterator : public ValueObject {
4475 public:
4476 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4477 : blocks_in_loop_(info.GetBlocks()),
4478 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4479 index_(0) {
4480 if (!blocks_in_loop_.IsBitSet(index_)) {
4481 Advance();
4482 }
4483 }
4484
4485 bool Done() const { return index_ == blocks_.Size(); }
4486 HBasicBlock* Current() const { return blocks_.Get(index_); }
4487 void Advance() {
4488 ++index_;
4489 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4490 if (blocks_in_loop_.IsBitSet(index_)) {
4491 break;
4492 }
4493 }
4494 }
4495
4496 private:
4497 const BitVector& blocks_in_loop_;
4498 const GrowableArray<HBasicBlock*>& blocks_;
4499 size_t index_;
4500
4501 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4502};
4503
Mingyao Yang3584bce2015-05-19 16:01:59 -07004504// Iterator over the blocks that art part of the loop. Includes blocks part
4505// of an inner loop. The order in which the blocks are iterated is reverse
4506// post order.
4507class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4508 public:
4509 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4510 : blocks_in_loop_(info.GetBlocks()),
4511 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4512 index_(0) {
4513 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4514 Advance();
4515 }
4516 }
4517
4518 bool Done() const { return index_ == blocks_.Size(); }
4519 HBasicBlock* Current() const { return blocks_.Get(index_); }
4520 void Advance() {
4521 ++index_;
4522 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4523 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4524 break;
4525 }
4526 }
4527 }
4528
4529 private:
4530 const BitVector& blocks_in_loop_;
4531 const GrowableArray<HBasicBlock*>& blocks_;
4532 size_t index_;
4533
4534 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4535};
4536
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004537inline int64_t Int64FromConstant(HConstant* constant) {
4538 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4539 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4540 : constant->AsLongConstant()->GetValue();
4541}
4542
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004543} // namespace art
4544
4545#endif // ART_COMPILER_OPTIMIZING_NODES_H_