blob: 8546a1066fa0d8fccd7bfe120eb838558f00dc7e [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
1356 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1357 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1358 if (type_handle->IsObjectClass()) {
1359 // Override the type handle to be consistent with the case when we get to
1360 // Top but don't have the Object class available. It avoids having to guess
1361 // what value the type_handle has when it's Top.
1362 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1363 } else {
1364 return ReferenceTypeInfo(type_handle, is_exact, false);
1365 }
1366 }
1367
1368 static ReferenceTypeInfo CreateTop(bool is_exact) {
1369 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001370 }
1371
1372 bool IsExact() const { return is_exact_; }
1373 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001374 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1375 return !IsTop() && GetTypeHandle()->IsInterface();
1376 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001377
1378 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1379
Calin Juravleb1498f62015-02-16 13:13:29 +00001380 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001381 if (IsTop()) {
1382 // Top (equivalent for java.lang.Object) is supertype of anything.
1383 return true;
1384 }
1385 if (rti.IsTop()) {
1386 // If we get here `this` is not Top() so it can't be a supertype.
1387 return false;
1388 }
1389 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1390 }
1391
1392 // Returns true if the type information provide the same amount of details.
1393 // Note that it does not mean that the instructions have the same actual type
1394 // (e.g. tops are equal but they can be the result of a merge).
1395 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1396 if (IsExact() != rti.IsExact()) {
1397 return false;
1398 }
1399 if (IsTop() && rti.IsTop()) {
1400 // `Top` means java.lang.Object, so the types are equivalent.
1401 return true;
1402 }
1403 if (IsTop() || rti.IsTop()) {
1404 // If only one is top or object than they are not equivalent.
1405 // NB: We need this extra check because the type_handle of `Top` is invalid
1406 // and we cannot inspect its reference.
1407 return false;
1408 }
1409
1410 // Finally check the types.
1411 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1412 }
1413
1414 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001415 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1416 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1417 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1418
Calin Juravleacf735c2015-02-12 15:25:22 +00001419 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001420 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001421 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001422 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001423 bool is_exact_;
1424 // A true value here means that the object type should be java.lang.Object.
1425 // We don't have access to the corresponding mirror object every time so this
1426 // flag acts as a substitute. When true, the TypeHandle refers to a null
1427 // pointer and should not be used.
1428 bool is_top_;
1429};
1430
1431std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1432
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001433class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001434 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001435 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001436 : previous_(nullptr),
1437 next_(nullptr),
1438 block_(nullptr),
1439 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001440 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001441 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001442 locations_(nullptr),
1443 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001444 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001445 side_effects_(side_effects),
1446 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001447
Dave Allison20dfc792014-06-16 20:44:29 -07001448 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001449
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001450#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001451 enum InstructionKind {
1452 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1453 };
1454#undef DECLARE_KIND
1455
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001456 HInstruction* GetNext() const { return next_; }
1457 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001458
Calin Juravle77520bc2015-01-12 18:45:46 +00001459 HInstruction* GetNextDisregardingMoves() const;
1460 HInstruction* GetPreviousDisregardingMoves() const;
1461
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001462 HBasicBlock* GetBlock() const { return block_; }
1463 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001464 bool IsInBlock() const { return block_ != nullptr; }
1465 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001466 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001467
Roland Levillain6b879dd2014-09-22 17:13:44 +01001468 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001469 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001470
1471 virtual void Accept(HGraphVisitor* visitor) = 0;
1472 virtual const char* DebugName() const = 0;
1473
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001474 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001475 void SetRawInputAt(size_t index, HInstruction* input) {
1476 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1477 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001478
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001479 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001480 virtual uint32_t GetDexPc() const {
1481 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1482 " does not need an environment";
1483 UNREACHABLE();
1484 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001485 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001486 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001487 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001488
Calin Juravle10e244f2015-01-26 18:54:32 +00001489 // Does not apply for all instructions, but having this at top level greatly
1490 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001491 virtual bool CanBeNull() const {
1492 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1493 return true;
1494 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001495
Calin Juravle641547a2015-04-21 22:08:51 +01001496 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1497 UNUSED(obj);
1498 return false;
1499 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001500
Calin Juravleacf735c2015-02-12 15:25:22 +00001501 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001502 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001503 reference_type_info_ = reference_type_info;
1504 }
1505
Calin Juravle61d544b2015-02-23 16:46:57 +00001506 ReferenceTypeInfo GetReferenceTypeInfo() const {
1507 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1508 return reference_type_info_;
1509 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001510
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001511 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001512 DCHECK(user != nullptr);
1513 HUseListNode<HInstruction*>* use =
1514 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1515 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001516 }
1517
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001518 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001519 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001520 HUseListNode<HEnvironment*>* env_use =
1521 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1522 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001523 }
1524
David Brazdil1abb4192015-02-17 18:33:36 +00001525 void RemoveAsUserOfInput(size_t input) {
1526 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1527 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1528 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001529
David Brazdil1abb4192015-02-17 18:33:36 +00001530 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1531 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001532
David Brazdiled596192015-01-23 10:39:45 +00001533 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1534 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001535 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001536 bool HasOnlyOneNonEnvironmentUse() const {
1537 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1538 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001539
Roland Levillain6c82d402014-10-13 16:10:27 +01001540 // Does this instruction strictly dominate `other_instruction`?
1541 // Returns false if this instruction and `other_instruction` are the same.
1542 // Aborts if this instruction and `other_instruction` are both phis.
1543 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001544
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001545 int GetId() const { return id_; }
1546 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001547
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001548 int GetSsaIndex() const { return ssa_index_; }
1549 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1550 bool HasSsaIndex() const { return ssa_index_ != -1; }
1551
1552 bool HasEnvironment() const { return environment_ != nullptr; }
1553 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001554 // Set the `environment_` field. Raw because this method does not
1555 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001556 void SetRawEnvironment(HEnvironment* environment) {
1557 DCHECK(environment_ == nullptr);
1558 DCHECK_EQ(environment->GetHolder(), this);
1559 environment_ = environment;
1560 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001561
1562 // Set the environment of this instruction, copying it from `environment`. While
1563 // copying, the uses lists are being updated.
1564 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001565 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001566 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001567 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001568 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001569 if (environment->GetParent() != nullptr) {
1570 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1571 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001572 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001573
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001574 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1575 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001576 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001577 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001578 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001579 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001580 if (environment->GetParent() != nullptr) {
1581 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1582 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001583 }
1584
Nicolas Geoffray39468442014-09-02 15:17:15 +01001585 // Returns the number of entries in the environment. Typically, that is the
1586 // number of dex registers in a method. It could be more in case of inlining.
1587 size_t EnvironmentSize() const;
1588
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001589 LocationSummary* GetLocations() const { return locations_; }
1590 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001592 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001593 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001594
Alexandre Rames188d4312015-04-09 18:30:21 +01001595 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1596 // uses of this instruction by `other` are *not* updated.
1597 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1598 ReplaceWith(other);
1599 other->ReplaceInput(this, use_index);
1600 }
1601
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001602 // Move `this` instruction before `cursor`.
1603 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001604
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001605#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001606 bool Is##type() const { return (As##type() != nullptr); } \
1607 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001608 virtual H##type* As##type() { return nullptr; }
1609
1610 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1611#undef INSTRUCTION_TYPE_CHECK
1612
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001613 // Returns whether the instruction can be moved within the graph.
1614 virtual bool CanBeMoved() const { return false; }
1615
1616 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001617 virtual bool InstructionTypeEquals(HInstruction* other) const {
1618 UNUSED(other);
1619 return false;
1620 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001621
1622 // Returns whether any data encoded in the two instructions is equal.
1623 // This method does not look at the inputs. Both instructions must be
1624 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001625 virtual bool InstructionDataEquals(HInstruction* other) const {
1626 UNUSED(other);
1627 return false;
1628 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001629
1630 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001631 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001632 // 2) Their inputs are identical.
1633 bool Equals(HInstruction* other) const;
1634
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001635 virtual InstructionKind GetKind() const = 0;
1636
1637 virtual size_t ComputeHashCode() const {
1638 size_t result = GetKind();
1639 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1640 result = (result * 31) + InputAt(i)->GetId();
1641 }
1642 return result;
1643 }
1644
1645 SideEffects GetSideEffects() const { return side_effects_; }
1646
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001647 size_t GetLifetimePosition() const { return lifetime_position_; }
1648 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1649 LiveInterval* GetLiveInterval() const { return live_interval_; }
1650 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1651 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1652
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001653 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1654
1655 // Returns whether the code generation of the instruction will require to have access
1656 // to the current method. Such instructions are:
1657 // (1): Instructions that require an environment, as calling the runtime requires
1658 // to walk the stack and have the current method stored at a specific stack address.
1659 // (2): Object literals like classes and strings, that are loaded from the dex cache
1660 // fields of the current method.
1661 bool NeedsCurrentMethod() const {
1662 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1663 }
1664
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001665 virtual bool NeedsDexCache() const { return false; }
1666
Mark Mendellc4701932015-04-10 13:18:51 -04001667 // Does this instruction have any use in an environment before
1668 // control flow hits 'other'?
1669 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1670
1671 // Remove all references to environment uses of this instruction.
1672 // The caller must ensure that this is safe to do.
1673 void RemoveEnvironmentUsers();
1674
David Brazdil1abb4192015-02-17 18:33:36 +00001675 protected:
1676 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1677 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1678
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001679 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001680 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1681
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001682 HInstruction* previous_;
1683 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001684 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001685
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001686 // An instruction gets an id when it is added to the graph.
1687 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001688 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001689 int id_;
1690
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001691 // When doing liveness analysis, instructions that have uses get an SSA index.
1692 int ssa_index_;
1693
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001694 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001695 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001696
1697 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001698 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001699
Nicolas Geoffray39468442014-09-02 15:17:15 +01001700 // The environment associated with this instruction. Not null if the instruction
1701 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001702 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001703
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001704 // Set by the code generator.
1705 LocationSummary* locations_;
1706
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001707 // Set by the liveness analysis.
1708 LiveInterval* live_interval_;
1709
1710 // Set by the liveness analysis, this is the position in a linear
1711 // order of blocks where this instruction's live interval start.
1712 size_t lifetime_position_;
1713
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001714 const SideEffects side_effects_;
1715
Calin Juravleacf735c2015-02-12 15:25:22 +00001716 // TODO: for primitive types this should be marked as invalid.
1717 ReferenceTypeInfo reference_type_info_;
1718
David Brazdil1abb4192015-02-17 18:33:36 +00001719 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001720 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001721 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001722 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001723 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001724
1725 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1726};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001727std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001728
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001729class HInputIterator : public ValueObject {
1730 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001731 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001732
1733 bool Done() const { return index_ == instruction_->InputCount(); }
1734 HInstruction* Current() const { return instruction_->InputAt(index_); }
1735 void Advance() { index_++; }
1736
1737 private:
1738 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001739 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001740
1741 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1742};
1743
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001744class HInstructionIterator : public ValueObject {
1745 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001746 explicit HInstructionIterator(const HInstructionList& instructions)
1747 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001748 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001749 }
1750
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001751 bool Done() const { return instruction_ == nullptr; }
1752 HInstruction* Current() const { return instruction_; }
1753 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001754 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001755 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001756 }
1757
1758 private:
1759 HInstruction* instruction_;
1760 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001761
1762 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001763};
1764
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001765class HBackwardInstructionIterator : public ValueObject {
1766 public:
1767 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1768 : instruction_(instructions.last_instruction_) {
1769 next_ = Done() ? nullptr : instruction_->GetPrevious();
1770 }
1771
1772 bool Done() const { return instruction_ == nullptr; }
1773 HInstruction* Current() const { return instruction_; }
1774 void Advance() {
1775 instruction_ = next_;
1776 next_ = Done() ? nullptr : instruction_->GetPrevious();
1777 }
1778
1779 private:
1780 HInstruction* instruction_;
1781 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001782
1783 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001784};
1785
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001786// An embedded container with N elements of type T. Used (with partial
1787// specialization for N=0) because embedded arrays cannot have size 0.
1788template<typename T, intptr_t N>
1789class EmbeddedArray {
1790 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001791 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001792
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001793 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001794
1795 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001796 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001797 return elements_[i];
1798 }
1799
1800 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001801 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001802 return elements_[i];
1803 }
1804
1805 const T& At(intptr_t i) const {
1806 return (*this)[i];
1807 }
1808
1809 void SetAt(intptr_t i, const T& val) {
1810 (*this)[i] = val;
1811 }
1812
1813 private:
1814 T elements_[N];
1815};
1816
1817template<typename T>
1818class EmbeddedArray<T, 0> {
1819 public:
1820 intptr_t length() const { return 0; }
1821 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001822 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001823 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001824 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001825 }
1826 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001827 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001828 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001829 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001830 }
1831};
1832
1833template<intptr_t N>
1834class HTemplateInstruction: public HInstruction {
1835 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001836 HTemplateInstruction<N>(SideEffects side_effects)
1837 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001838 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001840 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001841
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001842 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001843 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1844
1845 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1846 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001847 }
1848
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001849 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001850 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001851
1852 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001853};
1854
Dave Allison20dfc792014-06-16 20:44:29 -07001855template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001856class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001857 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001858 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1859 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001860 virtual ~HExpression() {}
1861
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001862 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001863
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001864 protected:
1865 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001866};
1867
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001868// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1869// instruction that branches to the exit block.
1870class HReturnVoid : public HTemplateInstruction<0> {
1871 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001872 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001874 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001875
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001876 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001877
1878 private:
1879 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1880};
1881
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001882// Represents dex's RETURN opcodes. A HReturn is a control flow
1883// instruction that branches to the exit block.
1884class HReturn : public HTemplateInstruction<1> {
1885 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001886 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001887 SetRawInputAt(0, value);
1888 }
1889
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001890 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001891
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001892 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001893
1894 private:
1895 DISALLOW_COPY_AND_ASSIGN(HReturn);
1896};
1897
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001898// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001899// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001900// exit block.
1901class HExit : public HTemplateInstruction<0> {
1902 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001903 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001904
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001905 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001907 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001908
1909 private:
1910 DISALLOW_COPY_AND_ASSIGN(HExit);
1911};
1912
1913// Jumps from one block to another.
1914class HGoto : public HTemplateInstruction<0> {
1915 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001916 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1917
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001918 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001919
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001920 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001921 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001922 }
1923
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001924 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001925
1926 private:
1927 DISALLOW_COPY_AND_ASSIGN(HGoto);
1928};
1929
Dave Allison20dfc792014-06-16 20:44:29 -07001930
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001931// Conditional branch. A block ending with an HIf instruction must have
1932// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001933class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001934 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001935 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001936 SetRawInputAt(0, input);
1937 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001938
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001939 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001940
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001941 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001942 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001943 }
1944
1945 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001946 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001947 }
1948
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001949 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001950
1951 private:
1952 DISALLOW_COPY_AND_ASSIGN(HIf);
1953};
1954
David Brazdilfc6a86a2015-06-26 10:33:45 +00001955
1956// Abstract instruction which marks the beginning and/or end of a try block and
1957// links it to the respective exception handlers. Behaves the same as a Goto in
1958// non-exceptional control flow.
1959// Normal-flow successor is stored at index zero, exception handlers under
1960// higher indices in no particular order.
1961class HTryBoundary : public HTemplateInstruction<0> {
1962 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01001963 enum BoundaryKind {
1964 kEntry,
1965 kExit,
1966 };
1967
1968 explicit HTryBoundary(BoundaryKind kind)
1969 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00001970
1971 bool IsControlFlow() const OVERRIDE { return true; }
1972
1973 // Returns the block's non-exceptional successor (index zero).
1974 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
1975
1976 // Returns whether `handler` is among its exception handlers (non-zero index
1977 // successors).
1978 bool HasExceptionHandler(HBasicBlock* handler) const {
1979 DCHECK(handler->IsCatchBlock());
1980 return GetBlock()->GetSuccessors().Contains(handler, /* start_from */ 1);
1981 }
1982
1983 // Returns whether successor at index `idx` is an exception handler.
1984 bool IsExceptionalSuccessor(size_t idx) const {
1985 DCHECK_LT(idx, GetBlock()->GetSuccessors().Size());
1986 bool is_handler = (idx != 0);
1987 DCHECK(!is_handler || GetBlock()->GetSuccessors().Get(idx)->IsCatchBlock());
1988 return is_handler;
1989 }
1990
1991 // If not present already, adds `handler` to its block's list of exception
1992 // handlers.
1993 void AddExceptionHandler(HBasicBlock* handler) {
1994 if (!HasExceptionHandler(handler)) {
1995 GetBlock()->AddSuccessor(handler);
1996 }
1997 }
1998
David Brazdil56e1acc2015-06-30 15:41:36 +01001999 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002000
2001 DECLARE_INSTRUCTION(TryBoundary);
2002
2003 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002004 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002005
2006 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2007};
2008
2009
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002010// Deoptimize to interpreter, upon checking a condition.
2011class HDeoptimize : public HTemplateInstruction<1> {
2012 public:
2013 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2014 : HTemplateInstruction(SideEffects::None()),
2015 dex_pc_(dex_pc) {
2016 SetRawInputAt(0, cond);
2017 }
2018
2019 bool NeedsEnvironment() const OVERRIDE { return true; }
2020 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002021 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002022
2023 DECLARE_INSTRUCTION(Deoptimize);
2024
2025 private:
2026 uint32_t dex_pc_;
2027
2028 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2029};
2030
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002031// Represents the ArtMethod that was passed as a first argument to
2032// the method. It is used by instructions that depend on it, like
2033// instructions that work with the dex cache.
2034class HCurrentMethod : public HExpression<0> {
2035 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002036 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002037
2038 DECLARE_INSTRUCTION(CurrentMethod);
2039
2040 private:
2041 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2042};
2043
Roland Levillain88cb1752014-10-20 16:36:47 +01002044class HUnaryOperation : public HExpression<1> {
2045 public:
2046 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2047 : HExpression(result_type, SideEffects::None()) {
2048 SetRawInputAt(0, input);
2049 }
2050
2051 HInstruction* GetInput() const { return InputAt(0); }
2052 Primitive::Type GetResultType() const { return GetType(); }
2053
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002054 bool CanBeMoved() const OVERRIDE { return true; }
2055 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002056 UNUSED(other);
2057 return true;
2058 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002059
Roland Levillain9240d6a2014-10-20 16:47:04 +01002060 // Try to statically evaluate `operation` and return a HConstant
2061 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002062 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002063 HConstant* TryStaticEvaluation() const;
2064
2065 // Apply this operation to `x`.
2066 virtual int32_t Evaluate(int32_t x) const = 0;
2067 virtual int64_t Evaluate(int64_t x) const = 0;
2068
Roland Levillain88cb1752014-10-20 16:36:47 +01002069 DECLARE_INSTRUCTION(UnaryOperation);
2070
2071 private:
2072 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2073};
2074
Dave Allison20dfc792014-06-16 20:44:29 -07002075class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002076 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002077 HBinaryOperation(Primitive::Type result_type,
2078 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002079 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002080 SetRawInputAt(0, left);
2081 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002082 }
2083
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002084 HInstruction* GetLeft() const { return InputAt(0); }
2085 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002086 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002087
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002088 virtual bool IsCommutative() const { return false; }
2089
2090 // Put constant on the right.
2091 // Returns whether order is changed.
2092 bool OrderInputsWithConstantOnTheRight() {
2093 HInstruction* left = InputAt(0);
2094 HInstruction* right = InputAt(1);
2095 if (left->IsConstant() && !right->IsConstant()) {
2096 ReplaceInput(right, 0);
2097 ReplaceInput(left, 1);
2098 return true;
2099 }
2100 return false;
2101 }
2102
2103 // Order inputs by instruction id, but favor constant on the right side.
2104 // This helps GVN for commutative ops.
2105 void OrderInputs() {
2106 DCHECK(IsCommutative());
2107 HInstruction* left = InputAt(0);
2108 HInstruction* right = InputAt(1);
2109 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2110 return;
2111 }
2112 if (OrderInputsWithConstantOnTheRight()) {
2113 return;
2114 }
2115 // Order according to instruction id.
2116 if (left->GetId() > right->GetId()) {
2117 ReplaceInput(right, 0);
2118 ReplaceInput(left, 1);
2119 }
2120 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002121
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002122 bool CanBeMoved() const OVERRIDE { return true; }
2123 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002124 UNUSED(other);
2125 return true;
2126 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002127
Roland Levillain9240d6a2014-10-20 16:47:04 +01002128 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002129 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002130 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002131 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002132
2133 // Apply this operation to `x` and `y`.
2134 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2135 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2136
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002137 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002138 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002139 HConstant* GetConstantRight() const;
2140
2141 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002142 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002143 HInstruction* GetLeastConstantLeft() const;
2144
Roland Levillainccc07a92014-09-16 14:48:16 +01002145 DECLARE_INSTRUCTION(BinaryOperation);
2146
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002147 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002148 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2149};
2150
Mark Mendellc4701932015-04-10 13:18:51 -04002151// The comparison bias applies for floating point operations and indicates how NaN
2152// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002153enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002154 kNoBias, // bias is not applicable (i.e. for long operation)
2155 kGtBias, // return 1 for NaN comparisons
2156 kLtBias, // return -1 for NaN comparisons
2157};
2158
Dave Allison20dfc792014-06-16 20:44:29 -07002159class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002160 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002161 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002162 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002163 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002164 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002165
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002166 bool NeedsMaterialization() const { return needs_materialization_; }
2167 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002168
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002169 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002170 // `instruction`, and disregard moves in between.
2171 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002172
Dave Allison20dfc792014-06-16 20:44:29 -07002173 DECLARE_INSTRUCTION(Condition);
2174
2175 virtual IfCondition GetCondition() const = 0;
2176
Mark Mendellc4701932015-04-10 13:18:51 -04002177 virtual IfCondition GetOppositeCondition() const = 0;
2178
Roland Levillain4fa13f62015-07-06 18:11:54 +01002179 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002180
2181 void SetBias(ComparisonBias bias) { bias_ = bias; }
2182
2183 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2184 return bias_ == other->AsCondition()->bias_;
2185 }
2186
Roland Levillain4fa13f62015-07-06 18:11:54 +01002187 bool IsFPConditionTrueIfNaN() const {
2188 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2189 IfCondition if_cond = GetCondition();
2190 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2191 }
2192
2193 bool IsFPConditionFalseIfNaN() const {
2194 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2195 IfCondition if_cond = GetCondition();
2196 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2197 }
2198
Dave Allison20dfc792014-06-16 20:44:29 -07002199 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002200 // For register allocation purposes, returns whether this instruction needs to be
2201 // materialized (that is, not just be in the processor flags).
2202 bool needs_materialization_;
2203
Mark Mendellc4701932015-04-10 13:18:51 -04002204 // Needed if we merge a HCompare into a HCondition.
2205 ComparisonBias bias_;
2206
Dave Allison20dfc792014-06-16 20:44:29 -07002207 DISALLOW_COPY_AND_ASSIGN(HCondition);
2208};
2209
2210// Instruction to check if two inputs are equal to each other.
2211class HEqual : public HCondition {
2212 public:
2213 HEqual(HInstruction* first, HInstruction* second)
2214 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002215
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002216 bool IsCommutative() const OVERRIDE { return true; }
2217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002218 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002219 return x == y ? 1 : 0;
2220 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002221 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002222 return x == y ? 1 : 0;
2223 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002224
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002225 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002227 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002228 return kCondEQ;
2229 }
2230
Mark Mendellc4701932015-04-10 13:18:51 -04002231 IfCondition GetOppositeCondition() const OVERRIDE {
2232 return kCondNE;
2233 }
2234
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002235 private:
2236 DISALLOW_COPY_AND_ASSIGN(HEqual);
2237};
2238
Dave Allison20dfc792014-06-16 20:44:29 -07002239class HNotEqual : public HCondition {
2240 public:
2241 HNotEqual(HInstruction* first, HInstruction* second)
2242 : HCondition(first, second) {}
2243
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002244 bool IsCommutative() const OVERRIDE { return true; }
2245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002246 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002247 return x != y ? 1 : 0;
2248 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002249 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002250 return x != y ? 1 : 0;
2251 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002252
Dave Allison20dfc792014-06-16 20:44:29 -07002253 DECLARE_INSTRUCTION(NotEqual);
2254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002255 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002256 return kCondNE;
2257 }
2258
Mark Mendellc4701932015-04-10 13:18:51 -04002259 IfCondition GetOppositeCondition() const OVERRIDE {
2260 return kCondEQ;
2261 }
2262
Dave Allison20dfc792014-06-16 20:44:29 -07002263 private:
2264 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2265};
2266
2267class HLessThan : public HCondition {
2268 public:
2269 HLessThan(HInstruction* first, HInstruction* second)
2270 : HCondition(first, second) {}
2271
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002272 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002273 return x < y ? 1 : 0;
2274 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002275 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002276 return x < y ? 1 : 0;
2277 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002278
Dave Allison20dfc792014-06-16 20:44:29 -07002279 DECLARE_INSTRUCTION(LessThan);
2280
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002281 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002282 return kCondLT;
2283 }
2284
Mark Mendellc4701932015-04-10 13:18:51 -04002285 IfCondition GetOppositeCondition() const OVERRIDE {
2286 return kCondGE;
2287 }
2288
Dave Allison20dfc792014-06-16 20:44:29 -07002289 private:
2290 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2291};
2292
2293class HLessThanOrEqual : public HCondition {
2294 public:
2295 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2296 : HCondition(first, second) {}
2297
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002298 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002299 return x <= y ? 1 : 0;
2300 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002301 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002302 return x <= y ? 1 : 0;
2303 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002304
Dave Allison20dfc792014-06-16 20:44:29 -07002305 DECLARE_INSTRUCTION(LessThanOrEqual);
2306
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002307 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002308 return kCondLE;
2309 }
2310
Mark Mendellc4701932015-04-10 13:18:51 -04002311 IfCondition GetOppositeCondition() const OVERRIDE {
2312 return kCondGT;
2313 }
2314
Dave Allison20dfc792014-06-16 20:44:29 -07002315 private:
2316 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2317};
2318
2319class HGreaterThan : public HCondition {
2320 public:
2321 HGreaterThan(HInstruction* first, HInstruction* second)
2322 : HCondition(first, second) {}
2323
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002324 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002325 return x > y ? 1 : 0;
2326 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002327 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002328 return x > y ? 1 : 0;
2329 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002330
Dave Allison20dfc792014-06-16 20:44:29 -07002331 DECLARE_INSTRUCTION(GreaterThan);
2332
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002333 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002334 return kCondGT;
2335 }
2336
Mark Mendellc4701932015-04-10 13:18:51 -04002337 IfCondition GetOppositeCondition() const OVERRIDE {
2338 return kCondLE;
2339 }
2340
Dave Allison20dfc792014-06-16 20:44:29 -07002341 private:
2342 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2343};
2344
2345class HGreaterThanOrEqual : public HCondition {
2346 public:
2347 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2348 : HCondition(first, second) {}
2349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002350 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002351 return x >= y ? 1 : 0;
2352 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002353 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002354 return x >= y ? 1 : 0;
2355 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002356
Dave Allison20dfc792014-06-16 20:44:29 -07002357 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2358
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002359 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002360 return kCondGE;
2361 }
2362
Mark Mendellc4701932015-04-10 13:18:51 -04002363 IfCondition GetOppositeCondition() const OVERRIDE {
2364 return kCondLT;
2365 }
2366
Dave Allison20dfc792014-06-16 20:44:29 -07002367 private:
2368 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2369};
2370
2371
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002372// Instruction to check how two inputs compare to each other.
2373// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2374class HCompare : public HBinaryOperation {
2375 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002376 HCompare(Primitive::Type type,
2377 HInstruction* first,
2378 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002379 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002380 uint32_t dex_pc)
2381 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002382 DCHECK_EQ(type, first->GetType());
2383 DCHECK_EQ(type, second->GetType());
2384 }
2385
Calin Juravleddb7df22014-11-25 20:56:51 +00002386 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002387 return
2388 x == y ? 0 :
2389 x > y ? 1 :
2390 -1;
2391 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002392
2393 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002394 return
2395 x == y ? 0 :
2396 x > y ? 1 :
2397 -1;
2398 }
2399
Calin Juravleddb7df22014-11-25 20:56:51 +00002400 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2401 return bias_ == other->AsCompare()->bias_;
2402 }
2403
Mark Mendellc4701932015-04-10 13:18:51 -04002404 ComparisonBias GetBias() const { return bias_; }
2405
Roland Levillain4fa13f62015-07-06 18:11:54 +01002406 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002407
Alexey Frunze4dda3372015-06-01 18:31:49 -07002408 uint32_t GetDexPc() const { return dex_pc_; }
2409
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002410 DECLARE_INSTRUCTION(Compare);
2411
2412 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002413 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002414 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002415
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002416 DISALLOW_COPY_AND_ASSIGN(HCompare);
2417};
2418
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002419// A local in the graph. Corresponds to a Dex register.
2420class HLocal : public HTemplateInstruction<0> {
2421 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002422 explicit HLocal(uint16_t reg_number)
2423 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002424
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002425 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002426
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002427 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002428
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002429 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002430 // The Dex register number.
2431 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002432
2433 DISALLOW_COPY_AND_ASSIGN(HLocal);
2434};
2435
2436// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002437class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002438 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002439 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002440 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002441 SetRawInputAt(0, local);
2442 }
2443
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002444 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2445
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002446 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002447
2448 private:
2449 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2450};
2451
2452// Store a value in a given local. This instruction has two inputs: the value
2453// and the local.
2454class HStoreLocal : public HTemplateInstruction<2> {
2455 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002456 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002457 SetRawInputAt(0, local);
2458 SetRawInputAt(1, value);
2459 }
2460
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002461 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2462
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002463 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002464
2465 private:
2466 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2467};
2468
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002469class HConstant : public HExpression<0> {
2470 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002471 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2472
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002473 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002474
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002475 virtual bool IsMinusOne() const { return false; }
2476 virtual bool IsZero() const { return false; }
2477 virtual bool IsOne() const { return false; }
2478
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002479 DECLARE_INSTRUCTION(Constant);
2480
2481 private:
2482 DISALLOW_COPY_AND_ASSIGN(HConstant);
2483};
2484
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002485class HFloatConstant : public HConstant {
2486 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002487 float GetValue() const { return value_; }
2488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002489 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002490 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2491 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002492 }
2493
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002494 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002495
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002496 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002497 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002498 }
2499 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002500 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002501 }
2502 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002503 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2504 }
2505 bool IsNaN() const {
2506 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002507 }
2508
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002509 DECLARE_INSTRUCTION(FloatConstant);
2510
2511 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002512 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002513 explicit HFloatConstant(int32_t value)
2514 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002515
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002516 const float value_;
2517
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002518 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002519 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002520 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002521 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2522};
2523
2524class HDoubleConstant : public HConstant {
2525 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002526 double GetValue() const { return value_; }
2527
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002528 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002529 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2530 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002531 }
2532
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002533 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002534
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002535 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002536 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002537 }
2538 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002539 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002540 }
2541 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002542 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2543 }
2544 bool IsNaN() const {
2545 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002546 }
2547
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002548 DECLARE_INSTRUCTION(DoubleConstant);
2549
2550 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002551 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002552 explicit HDoubleConstant(int64_t value)
2553 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002554
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002555 const double value_;
2556
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002557 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002558 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002559 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002560 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2561};
2562
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002563class HNullConstant : public HConstant {
2564 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002565 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2566 return true;
2567 }
2568
2569 size_t ComputeHashCode() const OVERRIDE { return 0; }
2570
2571 DECLARE_INSTRUCTION(NullConstant);
2572
2573 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002574 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2575
2576 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002577 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2578};
2579
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002580// Constants of the type int. Those can be from Dex instructions, or
2581// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002582class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002583 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002584 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002585
Calin Juravle61d544b2015-02-23 16:46:57 +00002586 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002587 return other->AsIntConstant()->value_ == value_;
2588 }
2589
Calin Juravle61d544b2015-02-23 16:46:57 +00002590 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2591
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002592 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2593 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2594 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2595
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002596 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002597
2598 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002599 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2600
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002601 const int32_t value_;
2602
David Brazdil8d5b8b22015-03-24 10:51:52 +00002603 friend class HGraph;
2604 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002605 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002606 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2607};
2608
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002609class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002610 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002611 int64_t GetValue() const { return value_; }
2612
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002613 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002614 return other->AsLongConstant()->value_ == value_;
2615 }
2616
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002617 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002618
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002619 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2620 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2621 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2622
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002623 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002624
2625 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002626 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2627
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002628 const int64_t value_;
2629
David Brazdil8d5b8b22015-03-24 10:51:52 +00002630 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002631 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2632};
2633
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002634enum class Intrinsics {
2635#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2636#include "intrinsics_list.h"
2637 kNone,
2638 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2639#undef INTRINSICS_LIST
2640#undef OPTIMIZING_INTRINSICS
2641};
2642std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2643
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002644class HInvoke : public HInstruction {
2645 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002646 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002647
2648 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2649 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002650 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002651
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002652 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002653 SetRawInputAt(index, argument);
2654 }
2655
Roland Levillain3e3d7332015-04-28 11:00:54 +01002656 // Return the number of arguments. This number can be lower than
2657 // the number of inputs returned by InputCount(), as some invoke
2658 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2659 // inputs at the end of their list of inputs.
2660 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2661
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002662 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002663
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002664 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002665
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002666 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002667 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002668
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002669 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2670
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002671 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002672 return intrinsic_;
2673 }
2674
2675 void SetIntrinsic(Intrinsics intrinsic) {
2676 intrinsic_ = intrinsic;
2677 }
2678
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002679 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002680 return GetEnvironment()->GetParent() != nullptr;
2681 }
2682
2683 bool CanThrow() const OVERRIDE { return true; }
2684
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002685 DECLARE_INSTRUCTION(Invoke);
2686
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002687 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002688 HInvoke(ArenaAllocator* arena,
2689 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002690 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002691 Primitive::Type return_type,
2692 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002693 uint32_t dex_method_index,
2694 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002695 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002696 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002697 inputs_(arena, number_of_arguments),
2698 return_type_(return_type),
2699 dex_pc_(dex_pc),
2700 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002701 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002702 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002703 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2704 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002705 }
2706
David Brazdil1abb4192015-02-17 18:33:36 +00002707 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2708 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2709 inputs_.Put(index, input);
2710 }
2711
Roland Levillain3e3d7332015-04-28 11:00:54 +01002712 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002713 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002714 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002715 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002716 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002717 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002718 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002719
2720 private:
2721 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2722};
2723
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002724class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002725 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002726 // Requirements of this method call regarding the class
2727 // initialization (clinit) check of its declaring class.
2728 enum class ClinitCheckRequirement {
2729 kNone, // Class already initialized.
2730 kExplicit, // Static call having explicit clinit check as last input.
2731 kImplicit, // Static call implicitly requiring a clinit check.
2732 };
2733
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002734 HInvokeStaticOrDirect(ArenaAllocator* arena,
2735 uint32_t number_of_arguments,
2736 Primitive::Type return_type,
2737 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002738 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002739 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002740 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002741 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002742 InvokeType invoke_type,
2743 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002744 : HInvoke(arena,
2745 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002746 // There is one extra argument for the HCurrentMethod node, and
2747 // potentially one other if the clinit check is explicit, and one other
2748 // if the method is a string factory.
2749 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2750 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002751 return_type,
2752 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002753 dex_method_index,
2754 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002755 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002756 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002757 clinit_check_requirement_(clinit_check_requirement),
2758 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002759
Calin Juravle641547a2015-04-21 22:08:51 +01002760 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2761 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002762 // We access the method via the dex cache so we can't do an implicit null check.
2763 // TODO: for intrinsics we can generate implicit null checks.
2764 return false;
2765 }
2766
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002767 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002768 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002769 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002770 bool IsStringInit() const { return string_init_offset_ != 0; }
2771 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002772 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002773
Roland Levillain4c0eb422015-04-24 16:43:49 +01002774 // Is this instruction a call to a static method?
2775 bool IsStatic() const {
2776 return GetInvokeType() == kStatic;
2777 }
2778
Roland Levillain3e3d7332015-04-28 11:00:54 +01002779 // Remove the art::HLoadClass instruction set as last input by
2780 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2781 // the initial art::HClinitCheck instruction (only relevant for
2782 // static calls with explicit clinit check).
2783 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002784 DCHECK(IsStaticWithExplicitClinitCheck());
2785 size_t last_input_index = InputCount() - 1;
2786 HInstruction* last_input = InputAt(last_input_index);
2787 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002788 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002789 RemoveAsUserOfInput(last_input_index);
2790 inputs_.DeleteAt(last_input_index);
2791 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2792 DCHECK(IsStaticWithImplicitClinitCheck());
2793 }
2794
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002795 bool IsStringFactoryFor(HFakeString* str) const {
2796 if (!IsStringInit()) return false;
2797 // +1 for the current method.
2798 if (InputCount() == (number_of_arguments_ + 1)) return false;
2799 return InputAt(InputCount() - 1)->AsFakeString() == str;
2800 }
2801
2802 void RemoveFakeStringArgumentAsLastInput() {
2803 DCHECK(IsStringInit());
2804 size_t last_input_index = InputCount() - 1;
2805 HInstruction* last_input = InputAt(last_input_index);
2806 DCHECK(last_input != nullptr);
2807 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2808 RemoveAsUserOfInput(last_input_index);
2809 inputs_.DeleteAt(last_input_index);
2810 }
2811
Roland Levillain4c0eb422015-04-24 16:43:49 +01002812 // Is this a call to a static method whose declaring class has an
2813 // explicit intialization check in the graph?
2814 bool IsStaticWithExplicitClinitCheck() const {
2815 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2816 }
2817
2818 // Is this a call to a static method whose declaring class has an
2819 // implicit intialization check requirement?
2820 bool IsStaticWithImplicitClinitCheck() const {
2821 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2822 }
2823
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002824 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002825
Roland Levillain4c0eb422015-04-24 16:43:49 +01002826 protected:
2827 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2828 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2829 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2830 HInstruction* input = input_record.GetInstruction();
2831 // `input` is the last input of a static invoke marked as having
2832 // an explicit clinit check. It must either be:
2833 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2834 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2835 DCHECK(input != nullptr);
2836 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2837 }
2838 return input_record;
2839 }
2840
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002841 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002842 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002843 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002844 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002845 // Thread entrypoint offset for string init method if this is a string init invoke.
2846 // Note that there are multiple string init methods, each having its own offset.
2847 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002848
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002849 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002850};
2851
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002852class HInvokeVirtual : public HInvoke {
2853 public:
2854 HInvokeVirtual(ArenaAllocator* arena,
2855 uint32_t number_of_arguments,
2856 Primitive::Type return_type,
2857 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002858 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002859 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002860 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002861 vtable_index_(vtable_index) {}
2862
Calin Juravle641547a2015-04-21 22:08:51 +01002863 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002864 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002865 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002866 }
2867
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002868 uint32_t GetVTableIndex() const { return vtable_index_; }
2869
2870 DECLARE_INSTRUCTION(InvokeVirtual);
2871
2872 private:
2873 const uint32_t vtable_index_;
2874
2875 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2876};
2877
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002878class HInvokeInterface : public HInvoke {
2879 public:
2880 HInvokeInterface(ArenaAllocator* arena,
2881 uint32_t number_of_arguments,
2882 Primitive::Type return_type,
2883 uint32_t dex_pc,
2884 uint32_t dex_method_index,
2885 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002886 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002887 imt_index_(imt_index) {}
2888
Calin Juravle641547a2015-04-21 22:08:51 +01002889 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002890 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002891 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002892 }
2893
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002894 uint32_t GetImtIndex() const { return imt_index_; }
2895 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2896
2897 DECLARE_INSTRUCTION(InvokeInterface);
2898
2899 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002900 const uint32_t imt_index_;
2901
2902 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2903};
2904
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002905class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002906 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002907 HNewInstance(HCurrentMethod* current_method,
2908 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002909 uint16_t type_index,
2910 const DexFile& dex_file,
2911 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002912 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2913 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002914 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002915 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002916 entrypoint_(entrypoint) {
2917 SetRawInputAt(0, current_method);
2918 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002919
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002920 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002921 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002922 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002923
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002924 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002925 bool NeedsEnvironment() const OVERRIDE { return true; }
2926 // It may throw when called on:
2927 // - interfaces
2928 // - abstract/innaccessible/unknown classes
2929 // TODO: optimize when possible.
2930 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002931
Calin Juravle10e244f2015-01-26 18:54:32 +00002932 bool CanBeNull() const OVERRIDE { return false; }
2933
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002934 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2935
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002936 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002937
2938 private:
2939 const uint32_t dex_pc_;
2940 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002941 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002942 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002943
2944 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2945};
2946
Roland Levillain88cb1752014-10-20 16:36:47 +01002947class HNeg : public HUnaryOperation {
2948 public:
2949 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2950 : HUnaryOperation(result_type, input) {}
2951
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002952 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2953 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002954
Roland Levillain88cb1752014-10-20 16:36:47 +01002955 DECLARE_INSTRUCTION(Neg);
2956
2957 private:
2958 DISALLOW_COPY_AND_ASSIGN(HNeg);
2959};
2960
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002961class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002962 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002963 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002964 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002965 uint32_t dex_pc,
2966 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002967 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002968 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002969 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2970 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002971 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002972 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002973 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002974 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002975 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002976 }
2977
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002978 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002979 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002980 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002981
2982 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002983 bool NeedsEnvironment() const OVERRIDE { return true; }
2984
Mingyao Yang0c365e62015-03-31 15:09:29 -07002985 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2986 bool CanThrow() const OVERRIDE { return true; }
2987
Calin Juravle10e244f2015-01-26 18:54:32 +00002988 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002989
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002990 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2991
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002992 DECLARE_INSTRUCTION(NewArray);
2993
2994 private:
2995 const uint32_t dex_pc_;
2996 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002997 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002998 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002999
3000 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3001};
3002
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003003class HAdd : public HBinaryOperation {
3004 public:
3005 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3006 : HBinaryOperation(result_type, left, right) {}
3007
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003008 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003009
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 Geoffrayd8ee7372014-03-28 15:43:40 +00003017 DECLARE_INSTRUCTION(Add);
3018
3019 private:
3020 DISALLOW_COPY_AND_ASSIGN(HAdd);
3021};
3022
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003023class HSub : public HBinaryOperation {
3024 public:
3025 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3026 : HBinaryOperation(result_type, left, right) {}
3027
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003028 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003029 return x - y;
3030 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003031 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003032 return x - y;
3033 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003034
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003035 DECLARE_INSTRUCTION(Sub);
3036
3037 private:
3038 DISALLOW_COPY_AND_ASSIGN(HSub);
3039};
3040
Calin Juravle34bacdf2014-10-07 20:23:36 +01003041class HMul : public HBinaryOperation {
3042 public:
3043 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3044 : HBinaryOperation(result_type, left, right) {}
3045
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003046 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003047
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003048 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3049 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003050
3051 DECLARE_INSTRUCTION(Mul);
3052
3053 private:
3054 DISALLOW_COPY_AND_ASSIGN(HMul);
3055};
3056
Calin Juravle7c4954d2014-10-28 16:57:40 +00003057class HDiv : public HBinaryOperation {
3058 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003059 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3060 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003061
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003062 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003063 // Our graph structure ensures we never have 0 for `y` during constant folding.
3064 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003065 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003066 return (y == -1) ? -x : x / y;
3067 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003068
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003069 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003070 DCHECK_NE(y, 0);
3071 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3072 return (y == -1) ? -x : x / y;
3073 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003074
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003075 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003076
Calin Juravle7c4954d2014-10-28 16:57:40 +00003077 DECLARE_INSTRUCTION(Div);
3078
3079 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003080 const uint32_t dex_pc_;
3081
Calin Juravle7c4954d2014-10-28 16:57:40 +00003082 DISALLOW_COPY_AND_ASSIGN(HDiv);
3083};
3084
Calin Juravlebacfec32014-11-14 15:54:36 +00003085class HRem : public HBinaryOperation {
3086 public:
3087 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3088 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3089
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003090 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003091 DCHECK_NE(y, 0);
3092 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3093 return (y == -1) ? 0 : x % y;
3094 }
3095
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003096 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003097 DCHECK_NE(y, 0);
3098 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3099 return (y == -1) ? 0 : x % y;
3100 }
3101
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003102 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003103
3104 DECLARE_INSTRUCTION(Rem);
3105
3106 private:
3107 const uint32_t dex_pc_;
3108
3109 DISALLOW_COPY_AND_ASSIGN(HRem);
3110};
3111
Calin Juravled0d48522014-11-04 16:40:20 +00003112class HDivZeroCheck : public HExpression<1> {
3113 public:
3114 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3115 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3116 SetRawInputAt(0, value);
3117 }
3118
3119 bool CanBeMoved() const OVERRIDE { return true; }
3120
3121 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3122 UNUSED(other);
3123 return true;
3124 }
3125
3126 bool NeedsEnvironment() const OVERRIDE { return true; }
3127 bool CanThrow() const OVERRIDE { return true; }
3128
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003129 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003130
3131 DECLARE_INSTRUCTION(DivZeroCheck);
3132
3133 private:
3134 const uint32_t dex_pc_;
3135
3136 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3137};
3138
Calin Juravle9aec02f2014-11-18 23:06:35 +00003139class HShl : public HBinaryOperation {
3140 public:
3141 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3142 : HBinaryOperation(result_type, left, right) {}
3143
3144 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3145 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3146
3147 DECLARE_INSTRUCTION(Shl);
3148
3149 private:
3150 DISALLOW_COPY_AND_ASSIGN(HShl);
3151};
3152
3153class HShr : public HBinaryOperation {
3154 public:
3155 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3156 : HBinaryOperation(result_type, left, right) {}
3157
3158 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3159 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3160
3161 DECLARE_INSTRUCTION(Shr);
3162
3163 private:
3164 DISALLOW_COPY_AND_ASSIGN(HShr);
3165};
3166
3167class HUShr : public HBinaryOperation {
3168 public:
3169 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3170 : HBinaryOperation(result_type, left, right) {}
3171
3172 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3173 uint32_t ux = static_cast<uint32_t>(x);
3174 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3175 return static_cast<int32_t>(ux >> uy);
3176 }
3177
3178 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3179 uint64_t ux = static_cast<uint64_t>(x);
3180 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3181 return static_cast<int64_t>(ux >> uy);
3182 }
3183
3184 DECLARE_INSTRUCTION(UShr);
3185
3186 private:
3187 DISALLOW_COPY_AND_ASSIGN(HUShr);
3188};
3189
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003190class HAnd : public HBinaryOperation {
3191 public:
3192 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3193 : HBinaryOperation(result_type, left, right) {}
3194
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003195 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003196
3197 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3198 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3199
3200 DECLARE_INSTRUCTION(And);
3201
3202 private:
3203 DISALLOW_COPY_AND_ASSIGN(HAnd);
3204};
3205
3206class HOr : public HBinaryOperation {
3207 public:
3208 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3209 : HBinaryOperation(result_type, left, right) {}
3210
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003211 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003212
3213 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3214 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3215
3216 DECLARE_INSTRUCTION(Or);
3217
3218 private:
3219 DISALLOW_COPY_AND_ASSIGN(HOr);
3220};
3221
3222class HXor : public HBinaryOperation {
3223 public:
3224 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3225 : HBinaryOperation(result_type, left, right) {}
3226
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003227 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003228
3229 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3230 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3231
3232 DECLARE_INSTRUCTION(Xor);
3233
3234 private:
3235 DISALLOW_COPY_AND_ASSIGN(HXor);
3236};
3237
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003238// The value of a parameter in this method. Its location depends on
3239// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003240class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003241 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003242 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3243 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003244
3245 uint8_t GetIndex() const { return index_; }
3246
Calin Juravle10e244f2015-01-26 18:54:32 +00003247 bool CanBeNull() const OVERRIDE { return !is_this_; }
3248
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003249 bool IsThis() const { return is_this_; }
3250
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003251 DECLARE_INSTRUCTION(ParameterValue);
3252
3253 private:
3254 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003255 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003256 const uint8_t index_;
3257
Calin Juravle10e244f2015-01-26 18:54:32 +00003258 // Whether or not the parameter value corresponds to 'this' argument.
3259 const bool is_this_;
3260
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003261 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3262};
3263
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003264class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003265 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003266 explicit HNot(Primitive::Type result_type, HInstruction* input)
3267 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003268
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003269 bool CanBeMoved() const OVERRIDE { return true; }
3270 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003271 UNUSED(other);
3272 return true;
3273 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003274
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003275 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3276 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003277
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003278 DECLARE_INSTRUCTION(Not);
3279
3280 private:
3281 DISALLOW_COPY_AND_ASSIGN(HNot);
3282};
3283
David Brazdil66d126e2015-04-03 16:02:44 +01003284class HBooleanNot : public HUnaryOperation {
3285 public:
3286 explicit HBooleanNot(HInstruction* input)
3287 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3288
3289 bool CanBeMoved() const OVERRIDE { return true; }
3290 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3291 UNUSED(other);
3292 return true;
3293 }
3294
3295 int32_t Evaluate(int32_t x) const OVERRIDE {
3296 DCHECK(IsUint<1>(x));
3297 return !x;
3298 }
3299
3300 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3301 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3302 UNREACHABLE();
3303 }
3304
3305 DECLARE_INSTRUCTION(BooleanNot);
3306
3307 private:
3308 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3309};
3310
Roland Levillaindff1f282014-11-05 14:15:05 +00003311class HTypeConversion : public HExpression<1> {
3312 public:
3313 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003314 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3315 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003316 SetRawInputAt(0, input);
3317 DCHECK_NE(input->GetType(), result_type);
3318 }
3319
3320 HInstruction* GetInput() const { return InputAt(0); }
3321 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3322 Primitive::Type GetResultType() const { return GetType(); }
3323
Roland Levillain624279f2014-12-04 11:54:28 +00003324 // Required by the x86 and ARM code generators when producing calls
3325 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003326 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003327
Roland Levillaindff1f282014-11-05 14:15:05 +00003328 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003329 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003330
Mark Mendelle82549b2015-05-06 10:55:34 -04003331 // Try to statically evaluate the conversion and return a HConstant
3332 // containing the result. If the input cannot be converted, return nullptr.
3333 HConstant* TryStaticEvaluation() const;
3334
Roland Levillaindff1f282014-11-05 14:15:05 +00003335 DECLARE_INSTRUCTION(TypeConversion);
3336
3337 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003338 const uint32_t dex_pc_;
3339
Roland Levillaindff1f282014-11-05 14:15:05 +00003340 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3341};
3342
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003343static constexpr uint32_t kNoRegNumber = -1;
3344
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003345class HPhi : public HInstruction {
3346 public:
3347 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003348 : HInstruction(SideEffects::None()),
3349 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003350 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003351 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003352 is_live_(false),
3353 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003354 inputs_.SetSize(number_of_inputs);
3355 }
3356
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003357 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3358 static Primitive::Type ToPhiType(Primitive::Type type) {
3359 switch (type) {
3360 case Primitive::kPrimBoolean:
3361 case Primitive::kPrimByte:
3362 case Primitive::kPrimShort:
3363 case Primitive::kPrimChar:
3364 return Primitive::kPrimInt;
3365 default:
3366 return type;
3367 }
3368 }
3369
Calin Juravle10e244f2015-01-26 18:54:32 +00003370 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003371
3372 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003373 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003374
Calin Juravle10e244f2015-01-26 18:54:32 +00003375 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003376 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003377
Calin Juravle10e244f2015-01-26 18:54:32 +00003378 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3379 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3380
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003381 uint32_t GetRegNumber() const { return reg_number_; }
3382
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003383 void SetDead() { is_live_ = false; }
3384 void SetLive() { is_live_ = true; }
3385 bool IsDead() const { return !is_live_; }
3386 bool IsLive() const { return is_live_; }
3387
Calin Juravlea4f88312015-04-16 12:57:19 +01003388 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3389 // An equivalent phi is a phi having the same dex register and type.
3390 // It assumes that phis with the same dex register are adjacent.
3391 HPhi* GetNextEquivalentPhiWithSameType() {
3392 HInstruction* next = GetNext();
3393 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3394 if (next->GetType() == GetType()) {
3395 return next->AsPhi();
3396 }
3397 next = next->GetNext();
3398 }
3399 return nullptr;
3400 }
3401
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003402 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003403
David Brazdil1abb4192015-02-17 18:33:36 +00003404 protected:
3405 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3406
3407 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3408 inputs_.Put(index, input);
3409 }
3410
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003411 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003412 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003413 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003414 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003415 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003416 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003417
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003418 DISALLOW_COPY_AND_ASSIGN(HPhi);
3419};
3420
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003421class HNullCheck : public HExpression<1> {
3422 public:
3423 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003424 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003425 SetRawInputAt(0, value);
3426 }
3427
Calin Juravle10e244f2015-01-26 18:54:32 +00003428 bool CanBeMoved() const OVERRIDE { return true; }
3429 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003430 UNUSED(other);
3431 return true;
3432 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003433
Calin Juravle10e244f2015-01-26 18:54:32 +00003434 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003435
Calin Juravle10e244f2015-01-26 18:54:32 +00003436 bool CanThrow() const OVERRIDE { return true; }
3437
3438 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003439
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003440 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003441
3442 DECLARE_INSTRUCTION(NullCheck);
3443
3444 private:
3445 const uint32_t dex_pc_;
3446
3447 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3448};
3449
3450class FieldInfo : public ValueObject {
3451 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003452 FieldInfo(MemberOffset field_offset,
3453 Primitive::Type field_type,
3454 bool is_volatile,
3455 uint32_t index,
3456 const DexFile& dex_file)
3457 : field_offset_(field_offset),
3458 field_type_(field_type),
3459 is_volatile_(is_volatile),
3460 index_(index),
3461 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003462
3463 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003464 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003465 uint32_t GetFieldIndex() const { return index_; }
3466 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003467 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003468
3469 private:
3470 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003471 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003472 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003473 uint32_t index_;
3474 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003475};
3476
3477class HInstanceFieldGet : public HExpression<1> {
3478 public:
3479 HInstanceFieldGet(HInstruction* value,
3480 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003481 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003482 bool is_volatile,
3483 uint32_t field_idx,
3484 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003485 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003486 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003487 SetRawInputAt(0, value);
3488 }
3489
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003490 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003491
3492 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3493 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3494 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003495 }
3496
Calin Juravle641547a2015-04-21 22:08:51 +01003497 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3498 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003499 }
3500
3501 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003502 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3503 }
3504
Calin Juravle52c48962014-12-16 17:02:57 +00003505 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003506 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003507 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003508 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003509
3510 DECLARE_INSTRUCTION(InstanceFieldGet);
3511
3512 private:
3513 const FieldInfo field_info_;
3514
3515 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3516};
3517
3518class HInstanceFieldSet : public HTemplateInstruction<2> {
3519 public:
3520 HInstanceFieldSet(HInstruction* object,
3521 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003522 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003523 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003524 bool is_volatile,
3525 uint32_t field_idx,
3526 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003527 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003528 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003529 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003530 SetRawInputAt(0, object);
3531 SetRawInputAt(1, value);
3532 }
3533
Calin Juravle641547a2015-04-21 22:08:51 +01003534 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3535 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003536 }
3537
Calin Juravle52c48962014-12-16 17:02:57 +00003538 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003539 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003540 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003541 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003542 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003543 bool GetValueCanBeNull() const { return value_can_be_null_; }
3544 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003545
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003546 DECLARE_INSTRUCTION(InstanceFieldSet);
3547
3548 private:
3549 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003550 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003551
3552 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3553};
3554
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003555class HArrayGet : public HExpression<2> {
3556 public:
3557 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003558 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003559 SetRawInputAt(0, array);
3560 SetRawInputAt(1, index);
3561 }
3562
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003563 bool CanBeMoved() const OVERRIDE { return true; }
3564 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003565 UNUSED(other);
3566 return true;
3567 }
Calin Juravle641547a2015-04-21 22:08:51 +01003568 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3569 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003570 // TODO: We can be smarter here.
3571 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3572 // which generates the implicit null check. There are cases when these can be removed
3573 // to produce better code. If we ever add optimizations to do so we should allow an
3574 // implicit check here (as long as the address falls in the first page).
3575 return false;
3576 }
3577
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003578 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003579
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003580 HInstruction* GetArray() const { return InputAt(0); }
3581 HInstruction* GetIndex() const { return InputAt(1); }
3582
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003583 DECLARE_INSTRUCTION(ArrayGet);
3584
3585 private:
3586 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3587};
3588
3589class HArraySet : public HTemplateInstruction<3> {
3590 public:
3591 HArraySet(HInstruction* array,
3592 HInstruction* index,
3593 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003594 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003595 uint32_t dex_pc)
3596 : HTemplateInstruction(SideEffects::ChangesSomething()),
3597 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003598 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003599 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3600 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003601 SetRawInputAt(0, array);
3602 SetRawInputAt(1, index);
3603 SetRawInputAt(2, value);
3604 }
3605
Calin Juravle77520bc2015-01-12 18:45:46 +00003606 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003607 // We currently always call a runtime method to catch array store
3608 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003609 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003610 }
3611
Mingyao Yang81014cb2015-06-02 03:16:27 -07003612 // Can throw ArrayStoreException.
3613 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3614
Calin Juravle641547a2015-04-21 22:08:51 +01003615 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3616 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003617 // TODO: Same as for ArrayGet.
3618 return false;
3619 }
3620
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003621 void ClearNeedsTypeCheck() {
3622 needs_type_check_ = false;
3623 }
3624
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003625 void ClearValueCanBeNull() {
3626 value_can_be_null_ = false;
3627 }
3628
3629 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003630 bool NeedsTypeCheck() const { return needs_type_check_; }
3631
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003632 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003633
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003634 HInstruction* GetArray() const { return InputAt(0); }
3635 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003636 HInstruction* GetValue() const { return InputAt(2); }
3637
3638 Primitive::Type GetComponentType() const {
3639 // The Dex format does not type floating point index operations. Since the
3640 // `expected_component_type_` is set during building and can therefore not
3641 // be correct, we also check what is the value type. If it is a floating
3642 // point type, we must use that type.
3643 Primitive::Type value_type = GetValue()->GetType();
3644 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3645 ? value_type
3646 : expected_component_type_;
3647 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003648
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003649 DECLARE_INSTRUCTION(ArraySet);
3650
3651 private:
3652 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003653 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003654 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003655 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003656
3657 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3658};
3659
3660class HArrayLength : public HExpression<1> {
3661 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003662 explicit HArrayLength(HInstruction* array)
3663 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3664 // Note that arrays do not change length, so the instruction does not
3665 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003666 SetRawInputAt(0, array);
3667 }
3668
Calin Juravle77520bc2015-01-12 18:45:46 +00003669 bool CanBeMoved() const OVERRIDE { return true; }
3670 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003671 UNUSED(other);
3672 return true;
3673 }
Calin Juravle641547a2015-04-21 22:08:51 +01003674 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3675 return obj == InputAt(0);
3676 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003677
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003678 DECLARE_INSTRUCTION(ArrayLength);
3679
3680 private:
3681 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3682};
3683
3684class HBoundsCheck : public HExpression<2> {
3685 public:
3686 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003687 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003688 DCHECK(index->GetType() == Primitive::kPrimInt);
3689 SetRawInputAt(0, index);
3690 SetRawInputAt(1, length);
3691 }
3692
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003693 bool CanBeMoved() const OVERRIDE { return true; }
3694 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003695 UNUSED(other);
3696 return true;
3697 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003698
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003699 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003700
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003701 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003702
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003703 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003704
3705 DECLARE_INSTRUCTION(BoundsCheck);
3706
3707 private:
3708 const uint32_t dex_pc_;
3709
3710 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3711};
3712
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003713/**
3714 * Some DEX instructions are folded into multiple HInstructions that need
3715 * to stay live until the last HInstruction. This class
3716 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003717 * HInstruction stays live. `index` represents the stack location index of the
3718 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003719 */
3720class HTemporary : public HTemplateInstruction<0> {
3721 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003722 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003723
3724 size_t GetIndex() const { return index_; }
3725
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003726 Primitive::Type GetType() const OVERRIDE {
3727 // The previous instruction is the one that will be stored in the temporary location.
3728 DCHECK(GetPrevious() != nullptr);
3729 return GetPrevious()->GetType();
3730 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003731
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003732 DECLARE_INSTRUCTION(Temporary);
3733
3734 private:
3735 const size_t index_;
3736
3737 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3738};
3739
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003740class HSuspendCheck : public HTemplateInstruction<0> {
3741 public:
3742 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003743 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003744
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003745 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003746 return true;
3747 }
3748
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003749 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003750 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3751 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003752
3753 DECLARE_INSTRUCTION(SuspendCheck);
3754
3755 private:
3756 const uint32_t dex_pc_;
3757
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003758 // Only used for code generation, in order to share the same slow path between back edges
3759 // of a same loop.
3760 SlowPathCode* slow_path_;
3761
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003762 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3763};
3764
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003765/**
3766 * Instruction to load a Class object.
3767 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003768class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003769 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003770 HLoadClass(HCurrentMethod* current_method,
3771 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003772 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003773 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003774 uint32_t dex_pc)
3775 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3776 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003777 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003778 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003779 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003780 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003781 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3782 SetRawInputAt(0, current_method);
3783 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003784
3785 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003786
3787 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3788 return other->AsLoadClass()->type_index_ == type_index_;
3789 }
3790
3791 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3792
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003793 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003794 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003795 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003796 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003797
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003798 bool NeedsEnvironment() const OVERRIDE {
3799 // Will call runtime and load the class if the class is not loaded yet.
3800 // TODO: finer grain decision.
3801 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003802 }
3803
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003804 bool MustGenerateClinitCheck() const {
3805 return generate_clinit_check_;
3806 }
3807
Calin Juravle0ba218d2015-05-19 18:46:01 +01003808 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3809 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003810 }
3811
3812 bool CanCallRuntime() const {
3813 return MustGenerateClinitCheck() || !is_referrers_class_;
3814 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003815
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003816 bool CanThrow() const OVERRIDE {
3817 // May call runtime and and therefore can throw.
3818 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003819 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003820 }
3821
Calin Juravleacf735c2015-02-12 15:25:22 +00003822 ReferenceTypeInfo GetLoadedClassRTI() {
3823 return loaded_class_rti_;
3824 }
3825
3826 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3827 // Make sure we only set exact types (the loaded class should never be merged).
3828 DCHECK(rti.IsExact());
3829 loaded_class_rti_ = rti;
3830 }
3831
3832 bool IsResolved() {
3833 return loaded_class_rti_.IsExact();
3834 }
3835
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003836 const DexFile& GetDexFile() { return dex_file_; }
3837
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003838 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3839
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003840 DECLARE_INSTRUCTION(LoadClass);
3841
3842 private:
3843 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003844 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003845 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003846 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003847 // Whether this instruction must generate the initialization check.
3848 // Used for code generation.
3849 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003850
Calin Juravleacf735c2015-02-12 15:25:22 +00003851 ReferenceTypeInfo loaded_class_rti_;
3852
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003853 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3854};
3855
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003856class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003857 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003858 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003859 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3860 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003861 dex_pc_(dex_pc) {
3862 SetRawInputAt(0, current_method);
3863 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003864
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003865 bool CanBeMoved() const OVERRIDE { return true; }
3866
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003867 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3868 return other->AsLoadString()->string_index_ == string_index_;
3869 }
3870
3871 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3872
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003873 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003874 uint32_t GetStringIndex() const { return string_index_; }
3875
3876 // TODO: Can we deopt or debug when we resolve a string?
3877 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003878 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003879
3880 DECLARE_INSTRUCTION(LoadString);
3881
3882 private:
3883 const uint32_t string_index_;
3884 const uint32_t dex_pc_;
3885
3886 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3887};
3888
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003889/**
3890 * Performs an initialization check on its Class object input.
3891 */
3892class HClinitCheck : public HExpression<1> {
3893 public:
3894 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003895 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003896 dex_pc_(dex_pc) {
3897 SetRawInputAt(0, constant);
3898 }
3899
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003900 bool CanBeMoved() const OVERRIDE { return true; }
3901 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3902 UNUSED(other);
3903 return true;
3904 }
3905
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003906 bool NeedsEnvironment() const OVERRIDE {
3907 // May call runtime to initialize the class.
3908 return true;
3909 }
3910
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003911 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003912
3913 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3914
3915 DECLARE_INSTRUCTION(ClinitCheck);
3916
3917 private:
3918 const uint32_t dex_pc_;
3919
3920 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3921};
3922
3923class HStaticFieldGet : public HExpression<1> {
3924 public:
3925 HStaticFieldGet(HInstruction* cls,
3926 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003927 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003928 bool is_volatile,
3929 uint32_t field_idx,
3930 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003931 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003932 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003933 SetRawInputAt(0, cls);
3934 }
3935
Calin Juravle52c48962014-12-16 17:02:57 +00003936
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003937 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003938
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003939 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003940 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3941 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003942 }
3943
3944 size_t ComputeHashCode() const OVERRIDE {
3945 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3946 }
3947
Calin Juravle52c48962014-12-16 17:02:57 +00003948 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003949 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3950 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003951 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003952
3953 DECLARE_INSTRUCTION(StaticFieldGet);
3954
3955 private:
3956 const FieldInfo field_info_;
3957
3958 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3959};
3960
3961class HStaticFieldSet : public HTemplateInstruction<2> {
3962 public:
3963 HStaticFieldSet(HInstruction* cls,
3964 HInstruction* value,
3965 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003966 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003967 bool is_volatile,
3968 uint32_t field_idx,
3969 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003970 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003971 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003972 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003973 SetRawInputAt(0, cls);
3974 SetRawInputAt(1, value);
3975 }
3976
Calin Juravle52c48962014-12-16 17:02:57 +00003977 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003978 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3979 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003980 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003981
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003982 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003983 bool GetValueCanBeNull() const { return value_can_be_null_; }
3984 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003985
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003986 DECLARE_INSTRUCTION(StaticFieldSet);
3987
3988 private:
3989 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003990 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003991
3992 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3993};
3994
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003995// Implement the move-exception DEX instruction.
3996class HLoadException : public HExpression<0> {
3997 public:
3998 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3999
4000 DECLARE_INSTRUCTION(LoadException);
4001
4002 private:
4003 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4004};
4005
4006class HThrow : public HTemplateInstruction<1> {
4007 public:
4008 HThrow(HInstruction* exception, uint32_t dex_pc)
4009 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4010 SetRawInputAt(0, exception);
4011 }
4012
4013 bool IsControlFlow() const OVERRIDE { return true; }
4014
4015 bool NeedsEnvironment() const OVERRIDE { return true; }
4016
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004017 bool CanThrow() const OVERRIDE { return true; }
4018
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004019 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004020
4021 DECLARE_INSTRUCTION(Throw);
4022
4023 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004024 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004025
4026 DISALLOW_COPY_AND_ASSIGN(HThrow);
4027};
4028
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004029class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004030 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004031 HInstanceOf(HInstruction* object,
4032 HLoadClass* constant,
4033 bool class_is_final,
4034 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004035 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4036 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004037 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004038 dex_pc_(dex_pc) {
4039 SetRawInputAt(0, object);
4040 SetRawInputAt(1, constant);
4041 }
4042
4043 bool CanBeMoved() const OVERRIDE { return true; }
4044
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004045 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004046 return true;
4047 }
4048
4049 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004050 return false;
4051 }
4052
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004053 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004054
4055 bool IsClassFinal() const { return class_is_final_; }
4056
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004057 // Used only in code generation.
4058 bool MustDoNullCheck() const { return must_do_null_check_; }
4059 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4060
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004061 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004062
4063 private:
4064 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004065 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004066 const uint32_t dex_pc_;
4067
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004068 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4069};
4070
Calin Juravleb1498f62015-02-16 13:13:29 +00004071class HBoundType : public HExpression<1> {
4072 public:
Calin Juravle9b0096b2015-07-22 17:14:32 +00004073 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
Calin Juravleb1498f62015-02-16 13:13:29 +00004074 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravle9b0096b2015-07-22 17:14:32 +00004075 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004076 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004077 SetRawInputAt(0, input);
4078 }
4079
Calin Juravle9b0096b2015-07-22 17:14:32 +00004080 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004081
Calin Juravle9b0096b2015-07-22 17:14:32 +00004082 bool CanBeNull() const OVERRIDE {
4083 // `null instanceof ClassX` always return false so we can't be null.
4084 return false;
Calin Juravleb1498f62015-02-16 13:13:29 +00004085 }
4086
4087 DECLARE_INSTRUCTION(BoundType);
4088
4089 private:
4090 // Encodes the most upper class that this instruction can have. In other words
Calin Juravle9b0096b2015-07-22 17:14:32 +00004091 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
4092 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
4093 const ReferenceTypeInfo bound_type_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004094
4095 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4096};
4097
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004098class HCheckCast : public HTemplateInstruction<2> {
4099 public:
4100 HCheckCast(HInstruction* object,
4101 HLoadClass* constant,
4102 bool class_is_final,
4103 uint32_t dex_pc)
4104 : HTemplateInstruction(SideEffects::None()),
4105 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004106 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004107 dex_pc_(dex_pc) {
4108 SetRawInputAt(0, object);
4109 SetRawInputAt(1, constant);
4110 }
4111
4112 bool CanBeMoved() const OVERRIDE { return true; }
4113
4114 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4115 return true;
4116 }
4117
4118 bool NeedsEnvironment() const OVERRIDE {
4119 // Instruction may throw a CheckCastError.
4120 return true;
4121 }
4122
4123 bool CanThrow() const OVERRIDE { return true; }
4124
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004125 bool MustDoNullCheck() const { return must_do_null_check_; }
4126 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4127
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004128 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004129
4130 bool IsClassFinal() const { return class_is_final_; }
4131
4132 DECLARE_INSTRUCTION(CheckCast);
4133
4134 private:
4135 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004136 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004137 const uint32_t dex_pc_;
4138
4139 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004140};
4141
Calin Juravle27df7582015-04-17 19:12:31 +01004142class HMemoryBarrier : public HTemplateInstruction<0> {
4143 public:
4144 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
4145 : HTemplateInstruction(SideEffects::None()),
4146 barrier_kind_(barrier_kind) {}
4147
4148 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4149
4150 DECLARE_INSTRUCTION(MemoryBarrier);
4151
4152 private:
4153 const MemBarrierKind barrier_kind_;
4154
4155 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4156};
4157
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004158class HMonitorOperation : public HTemplateInstruction<1> {
4159 public:
4160 enum OperationKind {
4161 kEnter,
4162 kExit,
4163 };
4164
4165 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
David Brazdilbff75032015-07-08 17:26:51 +00004166 : HTemplateInstruction(SideEffects::ChangesSomething()), kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004167 SetRawInputAt(0, object);
4168 }
4169
4170 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004171 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4172
4173 bool CanThrow() const OVERRIDE {
4174 // Verifier guarantees that monitor-exit cannot throw.
4175 // This is important because it allows the HGraphBuilder to remove
4176 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4177 return IsEnter();
4178 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004179
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004180 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004181
4182 bool IsEnter() const { return kind_ == kEnter; }
4183
4184 DECLARE_INSTRUCTION(MonitorOperation);
4185
Calin Juravle52c48962014-12-16 17:02:57 +00004186 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004187 const OperationKind kind_;
4188 const uint32_t dex_pc_;
4189
4190 private:
4191 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4192};
4193
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004194/**
4195 * A HInstruction used as a marker for the replacement of new + <init>
4196 * of a String to a call to a StringFactory. Only baseline will see
4197 * the node at code generation, where it will be be treated as null.
4198 * When compiling non-baseline, `HFakeString` instructions are being removed
4199 * in the instruction simplifier.
4200 */
4201class HFakeString : public HTemplateInstruction<0> {
4202 public:
4203 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4204
4205 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4206
4207 DECLARE_INSTRUCTION(FakeString);
4208
4209 private:
4210 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4211};
4212
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004213class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004214 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004215 MoveOperands(Location source,
4216 Location destination,
4217 Primitive::Type type,
4218 HInstruction* instruction)
4219 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004220
4221 Location GetSource() const { return source_; }
4222 Location GetDestination() const { return destination_; }
4223
4224 void SetSource(Location value) { source_ = value; }
4225 void SetDestination(Location value) { destination_ = value; }
4226
4227 // The parallel move resolver marks moves as "in-progress" by clearing the
4228 // destination (but not the source).
4229 Location MarkPending() {
4230 DCHECK(!IsPending());
4231 Location dest = destination_;
4232 destination_ = Location::NoLocation();
4233 return dest;
4234 }
4235
4236 void ClearPending(Location dest) {
4237 DCHECK(IsPending());
4238 destination_ = dest;
4239 }
4240
4241 bool IsPending() const {
4242 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4243 return destination_.IsInvalid() && !source_.IsInvalid();
4244 }
4245
4246 // True if this blocks a move from the given location.
4247 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004248 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004249 }
4250
4251 // A move is redundant if it's been eliminated, if its source and
4252 // destination are the same, or if its destination is unneeded.
4253 bool IsRedundant() const {
4254 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4255 }
4256
4257 // We clear both operands to indicate move that's been eliminated.
4258 void Eliminate() {
4259 source_ = destination_ = Location::NoLocation();
4260 }
4261
4262 bool IsEliminated() const {
4263 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4264 return source_.IsInvalid();
4265 }
4266
Alexey Frunze4dda3372015-06-01 18:31:49 -07004267 Primitive::Type GetType() const { return type_; }
4268
Nicolas Geoffray90218252015-04-15 11:56:51 +01004269 bool Is64BitMove() const {
4270 return Primitive::Is64BitType(type_);
4271 }
4272
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004273 HInstruction* GetInstruction() const { return instruction_; }
4274
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004275 private:
4276 Location source_;
4277 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004278 // The type this move is for.
4279 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004280 // The instruction this move is assocatied with. Null when this move is
4281 // for moving an input in the expected locations of user (including a phi user).
4282 // This is only used in debug mode, to ensure we do not connect interval siblings
4283 // in the same parallel move.
4284 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004285};
4286
4287static constexpr size_t kDefaultNumberOfMoves = 4;
4288
4289class HParallelMove : public HTemplateInstruction<0> {
4290 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004291 explicit HParallelMove(ArenaAllocator* arena)
4292 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004293
Nicolas Geoffray90218252015-04-15 11:56:51 +01004294 void AddMove(Location source,
4295 Location destination,
4296 Primitive::Type type,
4297 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004298 DCHECK(source.IsValid());
4299 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004300 if (kIsDebugBuild) {
4301 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004302 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004303 if (moves_.Get(i).GetInstruction() == instruction) {
4304 // Special case the situation where the move is for the spill slot
4305 // of the instruction.
4306 if ((GetPrevious() == instruction)
4307 || ((GetPrevious() == nullptr)
4308 && instruction->IsPhi()
4309 && instruction->GetBlock() == GetBlock())) {
4310 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4311 << "Doing parallel moves for the same instruction.";
4312 } else {
4313 DCHECK(false) << "Doing parallel moves for the same instruction.";
4314 }
4315 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004316 }
4317 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004318 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004319 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004320 << "Overlapped destination for two moves in a parallel move: "
4321 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4322 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004323 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004324 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004325 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004326 }
4327
4328 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004329 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004330 }
4331
4332 size_t NumMoves() const { return moves_.Size(); }
4333
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004334 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004335
4336 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004337 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004338
4339 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4340};
4341
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004342class HGraphVisitor : public ValueObject {
4343 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004344 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4345 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004346
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004347 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004348 virtual void VisitBasicBlock(HBasicBlock* block);
4349
Roland Levillain633021e2014-10-01 14:12:25 +01004350 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004351 void VisitInsertionOrder();
4352
Roland Levillain633021e2014-10-01 14:12:25 +01004353 // Visit the graph following dominator tree reverse post-order.
4354 void VisitReversePostOrder();
4355
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004356 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004357
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004358 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004359#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004360 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4361
4362 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4363
4364#undef DECLARE_VISIT_INSTRUCTION
4365
4366 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004367 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004368
4369 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4370};
4371
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004372class HGraphDelegateVisitor : public HGraphVisitor {
4373 public:
4374 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4375 virtual ~HGraphDelegateVisitor() {}
4376
4377 // Visit functions that delegate to to super class.
4378#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004379 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004380
4381 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4382
4383#undef DECLARE_VISIT_INSTRUCTION
4384
4385 private:
4386 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4387};
4388
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004389class HInsertionOrderIterator : public ValueObject {
4390 public:
4391 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4392
4393 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4394 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4395 void Advance() { ++index_; }
4396
4397 private:
4398 const HGraph& graph_;
4399 size_t index_;
4400
4401 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4402};
4403
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004404class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004405 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004406 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4407 // Check that reverse post order of the graph has been built.
4408 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4409 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004410
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004411 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4412 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004413 void Advance() { ++index_; }
4414
4415 private:
4416 const HGraph& graph_;
4417 size_t index_;
4418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004419 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004420};
4421
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004422class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004423 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004424 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004425 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4426 // Check that reverse post order of the graph has been built.
4427 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4428 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004429
4430 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004431 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004432 void Advance() { --index_; }
4433
4434 private:
4435 const HGraph& graph_;
4436 size_t index_;
4437
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004438 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004439};
4440
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004441class HLinearPostOrderIterator : public ValueObject {
4442 public:
4443 explicit HLinearPostOrderIterator(const HGraph& graph)
4444 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4445
4446 bool Done() const { return index_ == 0; }
4447
4448 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4449
4450 void Advance() {
4451 --index_;
4452 DCHECK_GE(index_, 0U);
4453 }
4454
4455 private:
4456 const GrowableArray<HBasicBlock*>& order_;
4457 size_t index_;
4458
4459 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4460};
4461
4462class HLinearOrderIterator : public ValueObject {
4463 public:
4464 explicit HLinearOrderIterator(const HGraph& graph)
4465 : order_(graph.GetLinearOrder()), index_(0) {}
4466
4467 bool Done() const { return index_ == order_.Size(); }
4468 HBasicBlock* Current() const { return order_.Get(index_); }
4469 void Advance() { ++index_; }
4470
4471 private:
4472 const GrowableArray<HBasicBlock*>& order_;
4473 size_t index_;
4474
4475 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4476};
4477
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004478// Iterator over the blocks that art part of the loop. Includes blocks part
4479// of an inner loop. The order in which the blocks are iterated is on their
4480// block id.
4481class HBlocksInLoopIterator : public ValueObject {
4482 public:
4483 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4484 : blocks_in_loop_(info.GetBlocks()),
4485 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4486 index_(0) {
4487 if (!blocks_in_loop_.IsBitSet(index_)) {
4488 Advance();
4489 }
4490 }
4491
4492 bool Done() const { return index_ == blocks_.Size(); }
4493 HBasicBlock* Current() const { return blocks_.Get(index_); }
4494 void Advance() {
4495 ++index_;
4496 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4497 if (blocks_in_loop_.IsBitSet(index_)) {
4498 break;
4499 }
4500 }
4501 }
4502
4503 private:
4504 const BitVector& blocks_in_loop_;
4505 const GrowableArray<HBasicBlock*>& blocks_;
4506 size_t index_;
4507
4508 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4509};
4510
Mingyao Yang3584bce2015-05-19 16:01:59 -07004511// Iterator over the blocks that art part of the loop. Includes blocks part
4512// of an inner loop. The order in which the blocks are iterated is reverse
4513// post order.
4514class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4515 public:
4516 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4517 : blocks_in_loop_(info.GetBlocks()),
4518 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4519 index_(0) {
4520 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4521 Advance();
4522 }
4523 }
4524
4525 bool Done() const { return index_ == blocks_.Size(); }
4526 HBasicBlock* Current() const { return blocks_.Get(index_); }
4527 void Advance() {
4528 ++index_;
4529 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4530 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4531 break;
4532 }
4533 }
4534 }
4535
4536 private:
4537 const BitVector& blocks_in_loop_;
4538 const GrowableArray<HBasicBlock*>& blocks_;
4539 size_t index_;
4540
4541 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4542};
4543
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004544inline int64_t Int64FromConstant(HConstant* constant) {
4545 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4546 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4547 : constant->AsLongConstant()->GetValue();
4548}
4549
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004550} // namespace art
4551
4552#endif // ART_COMPILER_OPTIMIZING_NODES_H_